2009-12-10 00:15:58 +00:00
using System ;
using System.Collections.Generic ;
2009-12-15 19:50:37 +00:00
using System.Linq ;
2009-12-11 19:49:53 +00:00
using System.Web ;
2009-12-15 19:50:37 +00:00
using System.Web.Routing ;
2009-12-21 08:24:39 +00:00
using JetBrains.Annotations ;
2009-12-10 22:21:57 +00:00
using Orchard.Extensions ;
2009-12-10 00:15:58 +00:00
using Orchard.Logging ;
2009-12-21 20:29:53 +00:00
using Orchard.ContentManagement ;
2009-12-10 00:15:58 +00:00
using Orchard.Settings ;
using Orchard.Themes ;
using Orchard.Core.Themes.Models ;
namespace Orchard.Core.Themes.Services {
public class ThemeService : IThemeService {
2009-12-10 22:21:57 +00:00
private readonly IExtensionManager _extensionManager ;
2009-12-15 19:50:37 +00:00
private readonly IEnumerable < IThemeSelector > _themeSelectors ;
2009-12-10 22:21:57 +00:00
2009-12-15 19:50:37 +00:00
public ThemeService (
IExtensionManager extensionManager ,
IEnumerable < IThemeSelector > themeSelectors ) {
2009-12-10 22:21:57 +00:00
_extensionManager = extensionManager ;
2009-12-15 19:50:37 +00:00
_themeSelectors = themeSelectors ;
2009-12-10 00:15:58 +00:00
Logger = NullLogger . Instance ;
}
public ILogger Logger { get ; set ; }
2009-12-21 08:24:39 +00:00
protected virtual ISite CurrentSite { get ; [ UsedImplicitly ] private set ; }
2009-12-10 00:15:58 +00:00
#region Implementation of IThemeService
2009-12-15 19:50:37 +00:00
public ITheme GetSiteTheme ( ) {
2009-12-10 00:15:58 +00:00
string currentThemeName = CurrentSite . As < ThemeSiteSettings > ( ) . Record . CurrentThemeName ;
if ( String . IsNullOrEmpty ( currentThemeName ) ) {
return null ;
}
return GetThemeByName ( currentThemeName ) ;
}
2009-12-15 19:50:37 +00:00
public void SetSiteTheme ( string themeName ) {
if ( GetThemeByName ( themeName ) ! = null ) {
CurrentSite . As < ThemeSiteSettings > ( ) . Record . CurrentThemeName = themeName ;
}
}
public ITheme GetRequestTheme ( RequestContext requestContext ) {
var requestTheme = _themeSelectors
. Select ( x = > x . GetTheme ( requestContext ) )
. Where ( x = > x ! = null )
2009-12-17 00:53:03 +00:00
. OrderByDescending ( x = > x . Priority ) ;
2009-12-15 19:50:37 +00:00
2009-12-17 00:53:03 +00:00
if ( requestTheme . Count ( ) < 1 )
2009-12-15 19:50:37 +00:00
return null ;
2009-12-17 00:53:03 +00:00
foreach ( var theme in requestTheme ) {
var t = GetThemeByName ( theme . ThemeName ) ;
if ( t ! = null )
return t ;
2009-12-15 19:50:37 +00:00
}
2009-12-17 00:53:03 +00:00
return null ;
2009-12-15 19:50:37 +00:00
}
2009-12-10 00:15:58 +00:00
public ITheme GetThemeByName ( string name ) {
2009-12-10 22:21:57 +00:00
foreach ( var descriptor in _extensionManager . AvailableExtensions ( ) ) {
if ( String . Equals ( descriptor . Name , name , StringComparison . OrdinalIgnoreCase ) ) {
return new Theme {
Author = descriptor . Author ? ? String . Empty ,
Description = descriptor . Description ? ? String . Empty ,
DisplayName = descriptor . DisplayName ? ? String . Empty ,
HomePage = descriptor . HomePage ? ? String . Empty ,
ThemeName = descriptor . Name ,
2009-12-11 00:01:32 +00:00
Version = descriptor . Version ? ? String . Empty ,
2009-12-10 22:21:57 +00:00
} ;
}
}
return null ;
2009-12-10 00:15:58 +00:00
}
public IEnumerable < ITheme > GetInstalledThemes ( ) {
2009-12-10 22:21:57 +00:00
List < ITheme > themes = new List < ITheme > ( ) ;
foreach ( var descriptor in _extensionManager . AvailableExtensions ( ) ) {
2010-01-11 20:05:40 +00:00
//todo: (heskew) filter out Admin themes in a different manner - eventually we'll need a way to select the admin theme so there should be a clear separation
if ( String . Equals ( descriptor . ExtensionType , "Theme" , StringComparison . OrdinalIgnoreCase )
& & ! ( descriptor . Name . EndsWith ( "Admin" , StringComparison . OrdinalIgnoreCase ) ) ) {
2009-12-10 22:21:57 +00:00
Theme theme = new Theme {
Author = descriptor . Author ? ? String . Empty ,
Description = descriptor . Description ? ? String . Empty ,
DisplayName = descriptor . DisplayName ? ? String . Empty ,
HomePage = descriptor . HomePage ? ? String . Empty ,
ThemeName = descriptor . Name ,
2009-12-11 00:01:32 +00:00
Version = descriptor . Version ? ? String . Empty ,
2009-12-10 22:21:57 +00:00
} ;
themes . Add ( theme ) ;
}
}
return themes ;
2009-12-10 00:15:58 +00:00
}
2009-12-11 00:01:32 +00:00
2009-12-11 19:49:53 +00:00
public void InstallTheme ( HttpPostedFileBase file ) {
_extensionManager . InstallExtension ( "Theme" , file ) ;
}
2009-12-11 23:10:54 +00:00
public void UninstallTheme ( string themeName ) {
_extensionManager . UninstallExtension ( "Theme" , themeName ) ;
}
2009-12-10 00:15:58 +00:00
#endregion
}
}