//----------------------------------------------------------------------- // // Copyright (c) 2009 Craig Sutherland. All rights reserved. // //----------------------------------------------------------------------- /* * Copyright (c) 2009 Craig Sutherland * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ namespace FastForward.WinCore { using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using FastForward.WinCore.Configuration; /// /// The base class for all plug-ins to inherit from. /// public abstract class Plugin { #region Constructors /// /// Initializes a new instance of the class. /// /// The configuration for the plugin. protected Plugin(PluginConfig configuration) { this.Configuration = configuration; this.CloseAction = HostCloseAction.Close; } #endregion #region Public properties #region Configuration /// /// Gets the configuration for this plugin. /// public PluginConfig Configuration { get; private set; } #endregion #region Host /// /// Gets the host for the plugin. /// public PluginHost Host { get; private set; } #endregion #region CloseAction /// /// Gets or sets the close action to perform when the user tries to close the host. /// public HostCloseAction CloseAction { get; protected set; } #endregion #endregion #region Protected properties #region Servers /// /// Gets the associated servers. /// protected ServerList Servers { get; private set; } #endregion #endregion #region Public methods #region RetrieveName() /// /// Retrieve the name of a plugin. /// /// The type to get the name from. /// The name of the plugin. public static string RetrieveName(Type pluginType) { var name = pluginType.Name; var nameAttribute = Attribute.GetCustomAttribute(pluginType, typeof(DisplayNameAttribute)) as DisplayNameAttribute; if (nameAttribute != null) { name = nameAttribute.DisplayName; } return name; } #endregion #region RetrieveDescription() /// /// Retrieve the description of a plugin. /// /// The type to get the description from. /// The description of the plugin. public static string RetrieveDescription(Type pluginType) { var description = string.Empty; var descAttribute = Attribute.GetCustomAttribute(pluginType, typeof(DescriptionAttribute)) as DescriptionAttribute; if (descAttribute != null) { description = descAttribute.Description; } return description; } #endregion #region RetrieveType() /// /// Retrieve the type for the plugin. /// /// The configuration to get the type from. /// The type of the plugin. public static Type RetrieveType(PluginConfig configuration) { var type = Type.GetType(configuration.Type); if ((type == null) && !string.IsNullOrEmpty(configuration.Assembly)) { var pluginAssembly = Assembly.LoadFrom(configuration.Assembly); type = pluginAssembly.GetType(configuration.Type); } return type; } #endregion #region StartNew() /// /// Start a new instance of a plugin. /// /// The configuration to start the plugin from. /// The new plugin instance/ public static Plugin StartNew(PluginConfig configuration) { var type = RetrieveType(configuration); var plugin = Activator.CreateInstance(type, configuration) as Plugin; return plugin; } #endregion #region Initialise() /// /// Initialises the plugin. /// /// The host that will contain the plugin. /// The loaded servers. public void Initialise(PluginHost host, ServerList servers) { this.Host = host; this.Servers = servers; this.OnInitialise(); } #endregion #region Terminate() /// /// Terminate the plugin. /// public abstract void Terminate(); #endregion #region Configure() /// /// Configure the plugin. /// public void Configure() { var newConfig = this.OnConfigure(); if (newConfig != null) { this.Configuration = newConfig; } } #endregion #region UpdateConfiguration() /// /// Update the configuration for the plugin. /// /// The new configuration. public void UpdateConfiguration(PluginConfig configuration) { this.Configuration = configuration; this.OnConfigurationUpdated(); } #endregion #region ListTabs() /// /// Lists the available tabs for this plug-in. /// /// public virtual IEnumerable ListTabs() { return new List(); } #endregion #endregion #region Protected methods #region OnInitialise() /// /// Initialise the actual plugin instance. /// protected abstract void OnInitialise(); #endregion #region OnConfigure() /// /// Configure the actual plugin instance. /// /// Returns the updated configuration. protected abstract PluginConfig OnConfigure(); #endregion #region OnConfigurationUpdated() /// /// The configuration has been updated. /// protected abstract void OnConfigurationUpdated(); #endregion #endregion } }