2012-09-16 16:12:31 -07:00
|
|
|
|
using MediaBrowser.Common.Kernel;
|
2012-09-18 12:33:57 -07:00
|
|
|
|
using MediaBrowser.Common.Logging;
|
2012-07-31 09:29:07 -07:00
|
|
|
|
using MediaBrowser.Common.Serialization;
|
2012-07-30 20:38:00 -07:00
|
|
|
|
using MediaBrowser.Model.Plugins;
|
2012-09-16 16:12:31 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2012-07-11 23:55:27 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Plugins
|
|
|
|
|
{
|
2012-07-24 07:54:34 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides a common base class for all plugins
|
|
|
|
|
/// </summary>
|
2012-07-29 08:19:25 -07:00
|
|
|
|
public abstract class BasePlugin : IDisposable
|
2012-07-24 07:54:34 -07:00
|
|
|
|
{
|
2012-09-05 09:33:54 -07:00
|
|
|
|
private IKernel Kernel { get; set; }
|
2012-09-03 09:40:35 -07:00
|
|
|
|
|
2012-07-29 08:19:25 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the plugin's current context
|
|
|
|
|
/// </summary>
|
2012-09-05 09:33:54 -07:00
|
|
|
|
protected KernelContext Context { get { return Kernel.KernelContext; } }
|
2012-07-29 08:19:25 -07:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the name of the plugin
|
|
|
|
|
/// </summary>
|
2012-07-24 07:54:34 -07:00
|
|
|
|
public abstract string Name { get; }
|
2012-07-25 19:33:11 -07:00
|
|
|
|
|
2012-07-29 08:19:25 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the type of configuration this plugin uses
|
|
|
|
|
/// </summary>
|
2012-09-17 11:09:35 -07:00
|
|
|
|
public virtual Type ConfigurationType
|
|
|
|
|
{
|
|
|
|
|
get { return typeof (BasePluginConfiguration); }
|
|
|
|
|
}
|
2012-07-29 08:19:25 -07:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-09-03 09:40:35 -07:00
|
|
|
|
/// Gets the plugin version
|
2012-07-29 08:19:25 -07:00
|
|
|
|
/// </summary>
|
2012-09-03 09:40:35 -07:00
|
|
|
|
public Version Version
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return GetType().Assembly.GetName().Version;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-25 19:33:11 -07:00
|
|
|
|
|
2012-09-03 12:12:02 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the name the assembly file
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string AssemblyFileName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return GetType().Assembly.GetName().Name + ".dll";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 12:37:14 -07:00
|
|
|
|
private DateTime? _configurationDateLastModified;
|
2012-09-03 20:11:16 -07:00
|
|
|
|
public DateTime ConfigurationDateLastModified
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-09-11 12:37:14 -07:00
|
|
|
|
if (_configurationDateLastModified == null)
|
2012-09-03 20:11:16 -07:00
|
|
|
|
{
|
|
|
|
|
if (File.Exists(ConfigurationFilePath))
|
|
|
|
|
{
|
2012-09-11 12:37:14 -07:00
|
|
|
|
_configurationDateLastModified = File.GetLastWriteTimeUtc(ConfigurationFilePath);
|
2012-09-03 20:11:16 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 12:37:14 -07:00
|
|
|
|
return _configurationDateLastModified ?? DateTime.MinValue;
|
2012-09-03 20:11:16 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-03 14:56:30 -07:00
|
|
|
|
|
2012-09-03 12:12:02 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the path to the assembly file
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string AssemblyFilePath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-09-05 09:33:54 -07:00
|
|
|
|
return Path.Combine(Kernel.ApplicationPaths.PluginsPath, AssemblyFileName);
|
2012-09-03 12:12:02 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-29 08:19:25 -07:00
|
|
|
|
/// <summary>
|
2012-09-03 09:40:35 -07:00
|
|
|
|
/// Gets or sets the current plugin configuration
|
2012-07-29 08:19:25 -07:00
|
|
|
|
/// </summary>
|
2012-09-03 09:40:35 -07:00
|
|
|
|
public BasePluginConfiguration Configuration { get; protected set; }
|
2012-07-11 23:55:27 -07:00
|
|
|
|
|
2012-07-29 08:19:25 -07:00
|
|
|
|
/// <summary>
|
2012-09-03 09:40:35 -07:00
|
|
|
|
/// Gets the name of the configuration file. Subclasses should override
|
2012-07-29 08:19:25 -07:00
|
|
|
|
/// </summary>
|
2012-09-16 16:12:31 -07:00
|
|
|
|
public virtual string ConfigurationFileName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Name.Replace(" ", string.Empty) + ".xml";
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-24 07:54:34 -07:00
|
|
|
|
|
2012-09-03 09:40:35 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the full path to the configuration file
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string ConfigurationFilePath
|
2012-07-11 23:55:27 -07:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-09-05 09:33:54 -07:00
|
|
|
|
return Path.Combine(Kernel.ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
|
2012-07-11 23:55:27 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 12:37:14 -07:00
|
|
|
|
private string _dataFolderPath;
|
2012-09-03 09:40:35 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string DataFolderPath
|
2012-07-24 07:54:34 -07:00
|
|
|
|
{
|
|
|
|
|
get
|
2012-07-11 23:55:27 -07:00
|
|
|
|
{
|
2012-09-11 12:37:14 -07:00
|
|
|
|
if (_dataFolderPath == null)
|
2012-09-03 09:40:35 -07:00
|
|
|
|
{
|
|
|
|
|
// Give the folder name the same name as the config file name
|
|
|
|
|
// We can always make this configurable if/when needed
|
2012-09-11 12:37:14 -07:00
|
|
|
|
_dataFolderPath = Path.Combine(Kernel.ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(ConfigurationFileName));
|
2012-09-03 09:40:35 -07:00
|
|
|
|
|
2012-09-11 12:37:14 -07:00
|
|
|
|
if (!Directory.Exists(_dataFolderPath))
|
2012-09-03 09:40:35 -07:00
|
|
|
|
{
|
2012-09-11 12:37:14 -07:00
|
|
|
|
Directory.CreateDirectory(_dataFolderPath);
|
2012-09-03 09:40:35 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 12:37:14 -07:00
|
|
|
|
return _dataFolderPath;
|
2012-07-11 23:55:27 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-03 09:40:35 -07:00
|
|
|
|
public bool Enabled
|
2012-07-24 07:54:34 -07:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-09-03 09:40:35 -07:00
|
|
|
|
return Configuration.Enabled;
|
2012-07-24 07:54:34 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-11 23:55:27 -07:00
|
|
|
|
|
2012-07-24 07:54:34 -07:00
|
|
|
|
/// <summary>
|
2012-09-11 12:37:14 -07:00
|
|
|
|
/// Returns true or false indicating if the plugin should be downloaded and run within the Ui.
|
2012-07-24 07:54:34 -07:00
|
|
|
|
/// </summary>
|
2012-09-11 12:37:14 -07:00
|
|
|
|
public virtual bool DownloadToUi
|
2012-07-11 23:55:27 -07:00
|
|
|
|
{
|
2012-07-24 07:54:34 -07:00
|
|
|
|
get
|
2012-07-11 23:55:27 -07:00
|
|
|
|
{
|
2012-07-24 07:54:34 -07:00
|
|
|
|
return false;
|
2012-07-11 23:55:27 -07:00
|
|
|
|
}
|
2012-07-24 07:54:34 -07:00
|
|
|
|
}
|
2012-07-11 23:55:27 -07:00
|
|
|
|
|
2012-09-05 09:33:54 -07:00
|
|
|
|
public void Initialize(IKernel kernel)
|
|
|
|
|
{
|
|
|
|
|
Initialize(kernel, true);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-29 08:19:25 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the plugin.
|
|
|
|
|
/// </summary>
|
2012-09-05 09:33:54 -07:00
|
|
|
|
public void Initialize(IKernel kernel, bool loadFeatures)
|
2012-09-03 09:40:35 -07:00
|
|
|
|
{
|
2012-09-05 09:33:54 -07:00
|
|
|
|
Kernel = kernel;
|
2012-09-03 09:40:35 -07:00
|
|
|
|
|
2012-09-05 09:33:54 -07:00
|
|
|
|
if (loadFeatures)
|
2012-09-03 09:40:35 -07:00
|
|
|
|
{
|
2012-09-05 09:33:54 -07:00
|
|
|
|
ReloadConfiguration();
|
|
|
|
|
|
|
|
|
|
if (Enabled)
|
|
|
|
|
{
|
2012-09-06 07:18:54 -07:00
|
|
|
|
if (kernel.KernelContext == KernelContext.Server)
|
|
|
|
|
{
|
|
|
|
|
InitializeOnServer();
|
|
|
|
|
}
|
2012-09-11 12:37:14 -07:00
|
|
|
|
else if (kernel.KernelContext == KernelContext.Ui)
|
2012-09-06 07:18:54 -07:00
|
|
|
|
{
|
2012-09-11 12:37:14 -07:00
|
|
|
|
InitializeInUi();
|
2012-09-06 07:18:54 -07:00
|
|
|
|
}
|
2012-09-05 09:33:54 -07:00
|
|
|
|
}
|
2012-09-03 09:40:35 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-09-06 07:18:54 -07:00
|
|
|
|
/// Starts the plugin on the server
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void InitializeOnServer()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-09-11 12:37:14 -07:00
|
|
|
|
/// Starts the plugin in the Ui
|
2012-09-03 09:40:35 -07:00
|
|
|
|
/// </summary>
|
2012-09-11 12:37:14 -07:00
|
|
|
|
protected virtual void InitializeInUi()
|
2012-07-24 07:54:34 -07:00
|
|
|
|
{
|
|
|
|
|
}
|
2012-07-11 23:55:27 -07:00
|
|
|
|
|
2012-07-29 08:19:25 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the plugins. Undos all actions performed during Init.
|
|
|
|
|
/// </summary>
|
2012-09-06 07:18:54 -07:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2012-09-18 12:33:57 -07:00
|
|
|
|
Logger.LogInfo("Disposing {0} Plugin", Name);
|
|
|
|
|
|
2012-09-06 07:18:54 -07:00
|
|
|
|
if (Context == KernelContext.Server)
|
|
|
|
|
{
|
|
|
|
|
DisposeOnServer();
|
|
|
|
|
}
|
2012-09-11 12:37:14 -07:00
|
|
|
|
else if (Context == KernelContext.Ui)
|
2012-09-06 07:18:54 -07:00
|
|
|
|
{
|
2012-09-11 12:37:14 -07:00
|
|
|
|
InitializeInUi();
|
2012-09-06 07:18:54 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the plugin on the server
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void DisposeOnServer()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-09-11 12:37:14 -07:00
|
|
|
|
/// Disposes the plugin in the Ui
|
2012-09-06 07:18:54 -07:00
|
|
|
|
/// </summary>
|
2012-09-11 12:37:14 -07:00
|
|
|
|
protected virtual void DisposeInUi()
|
2012-07-29 08:19:25 -07:00
|
|
|
|
{
|
|
|
|
|
}
|
2012-07-30 20:38:00 -07:00
|
|
|
|
|
|
|
|
|
public void ReloadConfiguration()
|
|
|
|
|
{
|
2012-09-03 09:40:35 -07:00
|
|
|
|
if (!File.Exists(ConfigurationFilePath))
|
2012-07-30 20:38:00 -07:00
|
|
|
|
{
|
|
|
|
|
Configuration = Activator.CreateInstance(ConfigurationType) as BasePluginConfiguration;
|
2012-09-03 14:56:30 -07:00
|
|
|
|
XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
|
2012-07-30 20:38:00 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-09-03 09:54:20 -07:00
|
|
|
|
Configuration = XmlSerializer.DeserializeFromFile(ConfigurationType, ConfigurationFilePath) as BasePluginConfiguration;
|
2012-07-30 20:38:00 -07:00
|
|
|
|
}
|
2012-09-03 14:56:30 -07:00
|
|
|
|
|
2012-09-03 20:11:16 -07:00
|
|
|
|
// Reset this so it will be loaded again next time it's accessed
|
2012-09-11 12:37:14 -07:00
|
|
|
|
_configurationDateLastModified = null;
|
2012-07-30 20:38:00 -07:00
|
|
|
|
}
|
2012-07-11 23:55:27 -07:00
|
|
|
|
}
|
|
|
|
|
}
|