/*
* 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.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using FastForward.WinCore;
using FastForward.WinCore.Configuration;
using System.IO;
namespace FastForward.Monitor
{
///
/// Allow the user to select a new plugin.
///
public partial class AddPluginForm
: Form
{
#region Private constants
private const string importPluginsText = "[Import Plugins...]";
#endregion
#region Private fields
private Dictionary plugins = new Dictionary();
private List currentTypes;
#endregion
#region Constructors
///
/// Initialise a new .
///
///
public AddPluginForm(IEnumerable currentPlugins, string defaultLocation)
{
InitializeComponent();
FindPlugins(currentPlugins, defaultLocation);
}
#endregion
#region Public properties
#region SelectedPlugin
///
/// The type of the selected plugin.
///
public Type SelectedPlugin
{
get { return plugins[(string)pluginNames.SelectedItem]; }
}
#endregion
#endregion
#region Public events
#region OkClicked
///
/// The OK button has been clicked.
///
public event EventHandler OkClicked;
#endregion
#endregion
#region Private methods
#region FindPlugins()
///
/// Find all the available plugins.
///
/// The currently loaded plugins.
private void FindPlugins(IEnumerable currentPlugins, string defaultLocation)
{
currentTypes = (from plugin in currentPlugins
select plugin.Type).ToList();
// Add the default available plug-ins
var assembly = Assembly.GetExecutingAssembly();
SeachForPlugins(assembly);
// Add the plug-ins that have been added to the plug-ins folder
if (Directory.Exists(defaultLocation))
{
var pluginFiles = Directory.GetFiles(defaultLocation, "*.dll");
foreach (var pluginFile in pluginFiles)
{
try
{
var pluginAssembly = Assembly.LoadFrom(pluginFile);
SeachForPlugins(pluginAssembly);
}
catch
{
// Ignore any errors
}
}
}
pluginNames.Items.Add(importPluginsText);
}
#endregion
#region SeachForPlugins()
///
/// Searches for plug-ins in an assembly.
///
/// The assembly to search.
private int SeachForPlugins(Assembly assembly)
{
var types = assembly.GetExportedTypes();
var count = 0;
foreach (var type in types)
{
if (typeof(Plugin).IsAssignableFrom(type) &&
!currentTypes.Contains(type.FullName))
{
var name = Plugin.RetrieveName(type);
pluginNames.Items.Add(name);
plugins.Add(name, type);
count++;
}
}
return count;
}
#endregion
#endregion
#region Event handlers
#region pluginNames_SelectedIndexChanged()
///
/// Display the description on a type.
///
///
///
private void pluginNames_SelectedIndexChanged(object sender, EventArgs e)
{
if (pluginNames.SelectedIndex >= 0)
{
var selected = (string)pluginNames.SelectedItem;
if (selected == importPluginsText)
{
// Prompt the user for the files to import
var openFileDialog = new OpenFileDialog
{
AddExtension = true,
AutoUpgradeEnabled = true,
CheckFileExists = true,
DefaultExt = "dll",
DereferenceLinks = true,
Filter = "Libraries (*.dll)|*.dll|All files (*.*)|*.*",
FilterIndex = 1,
Multiselect = true,
ShowHelp = false,
ShowReadOnly = false,
Title = "Select Plug-in Libraries",
ValidateNames = true
};
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
//
foreach (var fileName in openFileDialog.FileNames)
{
try
{
var assembly = Assembly.LoadFrom(fileName);
if (SeachForPlugins(assembly) == 0)
{
MessageBox.Show(
this,
"No new plug-ins found in '" + fileName + "'",
"Unable to load plug-ins",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
catch (Exception error)
{
MessageBox.Show(
this,
"Error loading '" + fileName + "': " + error.Message,
"Unable to load plug-ins",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
}
}
pluginNames.SelectedIndex = -1;
}
else
{
descriptionLabel.Text = Plugin.RetrieveDescription(plugins[selected]);
}
}
}
#endregion
#region okButton_Click()
///
/// The ok button has been clicked.
///
///
///
private void okButton_Click(object sender, EventArgs e)
{
if (pluginNames.SelectedIndex >= 0)
{
if (OkClicked != null) OkClicked(this, EventArgs.Empty);
DialogResult = DialogResult.OK;
Close();
}
else
{
MessageBox.Show(this,
"You must select a plugin first",
"No Plugin Selected",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
#endregion
#endregion
}
}