using System; using System.Collections.Specialized; using System.Xml.Serialization; using System.Windows.Forms; using System.IO; using System.Text; namespace Trigger { /// /// A that enables the user to choose which s shall be enabled and disabled /// public partial class TaskOptions : Form { #region Properties /// Our internal Main Main; /// A to display the in the internal BindingSource pluginBinding; /// A list with all available plugins and whether they are enabled internal ObservableDictionary plugins = new ObservableDictionary(); #endregion #region Constructor /// /// Create a new instance of /// /// public TaskOptions(Main Main) { InitializeComponent(); this.Main = Main; // get plugins from Settings plugins = GetPluginsAll(Main); // show them in the DataGridView pluginBinding = new BindingSource(); pluginBinding.DataSource = plugins; this.dgvPlugins.DataSource = pluginBinding; } #endregion #region Event handler /// /// CellClick-Event of /// Update the dictionary and refresh the /// /// /// private void dgvPlugins_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex != 1 || e.RowIndex < 0) // Only first column return; plugins[e.RowIndex] = !plugins[e.RowIndex]; // Toggle enabled value of plugin. Why does the BindingSource not do this? this.pluginBinding.DataSource = null; // TODO: BindingSource does not register for OnCollectionChanged!? this.pluginBinding.DataSource = plugins; } /// /// FormClosing-Event of /// Save the plugin settings and notify the /// /// /// private void TaskOptions_FormClosing(object sender, FormClosingEventArgs e) { PutPluginsSetting(plugins); this.Hide(); this.Main.TaskMgr.Refresh(plugins); } #endregion #region Methods /// /// Fetches the settings and convert them to a /// /// public static ObservableDictionary GetPluginsSetting() { ObservableDictionary setting; if (String.IsNullOrEmpty(Properties.Settings.Default.PluginsEnabled)) setting = new ObservableDictionary(); else { XmlSerializer xmlSer = new XmlSerializer(typeof(ObservableDictionary)); setting = (ObservableDictionary)xmlSer.Deserialize(new StringReader(Properties.Settings.Default.PluginsEnabled)); } return setting; } /// /// Saves the in the settings /// /// public static void PutPluginsSetting(ObservableDictionary plugins) { StringBuilder str = new StringBuilder(); XmlSerializer xmlSer = new XmlSerializer(typeof(ObservableDictionary)); xmlSer.Serialize(new StringWriter(str), plugins); Properties.Settings.Default.PluginsEnabled = str.ToString(); Properties.Settings.Default.Save(); } /// /// Creates a that contains all available s and loads their values from the settings /// /// /// public static ObservableDictionary GetPluginsAll(Main Main) { ObservableDictionary setting = GetPluginsSetting(); ObservableDictionary plugins = new ObservableDictionary(); foreach (Type task in Main.TaskMgr.TaskPluginsAvailable) { string name = task.Name; bool enabled = true; if (setting.ContainsKey(name)) enabled = setting[name]; plugins.Add(name, enabled); } return plugins; } #endregion } }