jellyfin/MediaBrowser.Common/Plugins/BasePlugin.cs

248 lines
7.1 KiB
C#
Raw Normal View History

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