From 6c7175e33d258ff2e65735f68cb05f110a8d2306 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Mon, 23 Jul 2012 11:05:30 -0400 Subject: [PATCH] Added an api call to pull down user configuration --- .../HttpHandlers/UserConfigurationHandler.cs | 18 +++++++++ MediaBrowser.Api/MediaBrowser.Api.csproj | 1 + MediaBrowser.Api/Plugin.cs | 4 ++ .../Configuration/BaseConfiguration.cs | 2 +- .../Configuration/ConfigurationController.cs | 37 +++++++++++++++++++ .../Configuration/UserConfiguration.cs | 21 +++++++++++ MediaBrowser.Common/Kernel/BaseKernel.cs | 21 ++++------- .../MediaBrowser.Common.csproj | 2 + .../Configuration/ServerConfiguration.cs | 26 +++++++++++-- .../ServerConfigurationController.cs | 27 ++++++++++++++ MediaBrowser.Controller/Kernel.cs | 7 +++- .../MediaBrowser.Controller.csproj | 1 + 12 files changed, 147 insertions(+), 20 deletions(-) create mode 100644 MediaBrowser.Api/HttpHandlers/UserConfigurationHandler.cs create mode 100644 MediaBrowser.Common/Configuration/ConfigurationController.cs create mode 100644 MediaBrowser.Common/Configuration/UserConfiguration.cs create mode 100644 MediaBrowser.Controller/Configuration/ServerConfigurationController.cs diff --git a/MediaBrowser.Api/HttpHandlers/UserConfigurationHandler.cs b/MediaBrowser.Api/HttpHandlers/UserConfigurationHandler.cs new file mode 100644 index 000000000..faace9c31 --- /dev/null +++ b/MediaBrowser.Api/HttpHandlers/UserConfigurationHandler.cs @@ -0,0 +1,18 @@ +using System; +using MediaBrowser.Controller; + +namespace MediaBrowser.Api.HttpHandlers +{ + public class UserConfigurationHandler : JsonHandler + { + protected override object ObjectToSerialize + { + get + { + Guid userId = Guid.Parse(QueryString["userid"]); + + return Kernel.Instance.ConfigurationController.GetUserConfiguration(userId); + } + } + } +} diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csproj index c0942cbb9..a803cb73a 100644 --- a/MediaBrowser.Api/MediaBrowser.Api.csproj +++ b/MediaBrowser.Api/MediaBrowser.Api.csproj @@ -57,6 +57,7 @@ + diff --git a/MediaBrowser.Api/Plugin.cs b/MediaBrowser.Api/Plugin.cs index 9a4db1518..6c2e9df7e 100644 --- a/MediaBrowser.Api/Plugin.cs +++ b/MediaBrowser.Api/Plugin.cs @@ -71,6 +71,10 @@ namespace MediaBrowser.Api { handler = new InProgressItemsHandler(); } + else if (localPath.EndsWith("/api/userconfiguration", StringComparison.OrdinalIgnoreCase)) + { + handler = new UserConfigurationHandler(); + } if (handler != null) { diff --git a/MediaBrowser.Common/Configuration/BaseConfiguration.cs b/MediaBrowser.Common/Configuration/BaseConfiguration.cs index bf53686ab..fe6c1f278 100644 --- a/MediaBrowser.Common/Configuration/BaseConfiguration.cs +++ b/MediaBrowser.Common/Configuration/BaseConfiguration.cs @@ -3,7 +3,7 @@ namespace MediaBrowser.Common.Configuration { /// - /// Serves as a common base class for the Server and UI Configurations + /// Serves as a common base class for the Server and UI application Configurations /// public class BaseConfiguration { diff --git a/MediaBrowser.Common/Configuration/ConfigurationController.cs b/MediaBrowser.Common/Configuration/ConfigurationController.cs new file mode 100644 index 000000000..547d195f5 --- /dev/null +++ b/MediaBrowser.Common/Configuration/ConfigurationController.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using MediaBrowser.Common.Json; + +namespace MediaBrowser.Common.Configuration +{ + public class ConfigurationController + where TConfigurationType : BaseConfiguration, new () + { + /// + /// The path to the configuration file + /// + public string Path { get; set; } + + public TConfigurationType Configuration { get; set; } + + public void Reload() + { + if (!File.Exists(Path)) + { + Configuration = new TConfigurationType(); + } + else + { + Configuration = JsonSerializer.DeserializeFromFile(Path); + } + } + + public void Save() + { + } + } +} diff --git a/MediaBrowser.Common/Configuration/UserConfiguration.cs b/MediaBrowser.Common/Configuration/UserConfiguration.cs new file mode 100644 index 000000000..0cdc810a6 --- /dev/null +++ b/MediaBrowser.Common/Configuration/UserConfiguration.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Configuration +{ + /// + /// This holds settings that can be personalized on a per-user, per-device basis. + /// + public class UserConfiguration + { + public int RecentItemDays { get; set; } + + public UserConfiguration() + { + RecentItemDays = 14; + } + } +} diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs index d6c13ce38..47e5d2c94 100644 --- a/MediaBrowser.Common/Kernel/BaseKernel.cs +++ b/MediaBrowser.Common/Kernel/BaseKernel.cs @@ -12,7 +12,8 @@ namespace MediaBrowser.Common.Kernel /// /// Represents a shared base kernel for both the UI and server apps /// - public abstract class BaseKernel + public abstract class BaseKernel + where TConfigurationContorllerType : ConfigurationController, new() where TConfigurationType : BaseConfiguration, new() { /// @@ -23,7 +24,7 @@ namespace MediaBrowser.Common.Kernel /// /// Gets the current configuration /// - public TConfigurationType Configuration { get; private set; } + public TConfigurationContorllerType ConfigurationController { get; private set; } /// /// Both the UI and server will have a built-in HttpServer. @@ -38,19 +39,12 @@ namespace MediaBrowser.Common.Kernel /// protected KernelContext KernelContext { get { return KernelContext.Server; } } - protected virtual string HttpServerUrlPrefix - { - get - { - return "http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/"; - } - } - public BaseKernel() { ProgramDataPath = GetProgramDataPath(); PluginController = new PluginController() { PluginsPath = Path.Combine(ProgramDataPath, "Plugins") }; + ConfigurationController = new TConfigurationContorllerType() { Path = Path.Combine(ProgramDataPath, "config.js") }; Logger.LoggerInstance = new FileLogger(Path.Combine(ProgramDataPath, "Logs")); } @@ -67,7 +61,6 @@ namespace MediaBrowser.Common.Kernel /// /// Gets the path to the application's ProgramDataFolder /// - /// private string GetProgramDataPath() { string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"]; @@ -94,9 +87,9 @@ namespace MediaBrowser.Common.Kernel private void ReloadConfiguration() { // Deserialize config - Configuration = GetConfiguration(ProgramDataPath); + ConfigurationController.Reload(); - Logger.LoggerInstance.LogSeverity = Configuration.LogSeverity; + Logger.LoggerInstance.LogSeverity = ConfigurationController.Configuration.LogSeverity; } private void ReloadHttpServer() @@ -106,7 +99,7 @@ namespace MediaBrowser.Common.Kernel HttpServer.Dispose(); } - HttpServer = new HttpServer("http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/"); + HttpServer = new HttpServer("http://+:" + ConfigurationController.Configuration.HttpServerPortNumber + "/mediabrowser/"); } protected virtual void ReloadPlugins() diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index 1f68a3da7..01d89d0f5 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -48,6 +48,8 @@ + + diff --git a/MediaBrowser.Controller/Configuration/ServerConfiguration.cs b/MediaBrowser.Controller/Configuration/ServerConfiguration.cs index 38b1b9caf..73727b393 100644 --- a/MediaBrowser.Controller/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Controller/Configuration/ServerConfiguration.cs @@ -1,16 +1,36 @@ -using MediaBrowser.Common.Configuration; +using System.Collections.Generic; +using MediaBrowser.Common.Configuration; namespace MediaBrowser.Controller.Configuration { public class ServerConfiguration : BaseConfiguration { public string ImagesByNamePath { get; set; } - public int RecentItemDays { get; set; } + + /// + /// Gets or sets the default UI configuration + /// + public UserConfiguration DefaultUserConfiguration { get; set; } + + /// + /// Gets or sets a list of registered UI device names + /// + public List DeviceNames { get; set; } + + /// + /// Gets or sets all available UIConfigurations + /// The key contains device name and user id + /// + public Dictionary UserConfigurations { get; set; } public ServerConfiguration() : base() { - RecentItemDays = 14; + DefaultUserConfiguration = new UserConfiguration(); + + UserConfigurations = new Dictionary(); + + DeviceNames = new List(); } } } diff --git a/MediaBrowser.Controller/Configuration/ServerConfigurationController.cs b/MediaBrowser.Controller/Configuration/ServerConfigurationController.cs new file mode 100644 index 000000000..6f262a32e --- /dev/null +++ b/MediaBrowser.Controller/Configuration/ServerConfigurationController.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MediaBrowser.Common.Configuration; + +namespace MediaBrowser.Controller.Configuration +{ + /// + /// Extends BaseConfigurationController by adding methods to get and set UIConfiguration data + /// + public class ServerConfigurationController : ConfigurationController + { + private string GetDictionaryKey(Guid userId, string deviceName) + { + string guidString = userId == Guid.Empty ? string.Empty : userId.ToString(); + + return deviceName + "-" + guidString; + } + + public UserConfiguration GetUserConfiguration(Guid userId) + { + return Configuration.DefaultUserConfiguration; + } + } +} diff --git a/MediaBrowser.Controller/Kernel.cs b/MediaBrowser.Controller/Kernel.cs index 0a5034859..468e7ab2f 100644 --- a/MediaBrowser.Controller/Kernel.cs +++ b/MediaBrowser.Controller/Kernel.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; +using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Kernel; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Events; @@ -16,7 +17,7 @@ using MediaBrowser.Model.Users; namespace MediaBrowser.Controller { - public class Kernel : BaseKernel + public class Kernel : BaseKernel { public static Kernel Instance { get; private set; } @@ -249,7 +250,9 @@ namespace MediaBrowser.Controller { DateTime now = DateTime.Now; - return GetParentalAllowedRecursiveChildren(parent, userId).Where(i => !(i is Folder) && (now - i.DateCreated).TotalDays < Configuration.RecentItemDays); + UserConfiguration config = ConfigurationController.GetUserConfiguration(userId); + + return GetParentalAllowedRecursiveChildren(parent, userId).Where(i => !(i is Folder) && (now - i.DateCreated).TotalDays < config.RecentItemDays); } /// diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 92a7e8107..296c44d1a 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -43,6 +43,7 @@ +