Changeset 412


Ignore:
Timestamp:
9/27/2008 9:00:44 AM (6 years ago)
Author:
lowjoel
Message:

Redesigned the Manager.Settings class.
-The plugin settings are no longer kept in a single dictionary; instead, they are distributed to be dealt with by the implementer of the Settings class. The GetSettings? and SetSettings? functions now are overloaded internally such that these values along with the plugins' GUID are sent together to the implementer class. The implementer class then stores it directly. This prevents unnecessary resource utilisation for large plugins and for plugins which are disabled
-This new design also allows for the serialisation and unserialisation of plugin settings which are non-primitive types, since the settings are loaded only on the initialisation of the plugin, not the initialisation of the settings object
-Created a SettingsChanged? event for classes to register callback functions to. This is useful when classes need to know when a setting is changed. Currently unimplemented though.
-Load is now a protected internal function which implementer classes must implement, this allows for the refreshing of settings whenever the program is running.

Location:
branches/eraser6
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/Eraser/Program.cs

    r367 r412  
    9999    { 
    100100        public Settings() 
     101        { 
     102            Load(); 
     103        } 
     104 
     105        protected override void Load() 
    101106        { 
    102107            RegistryKey key = Application.UserAppDataRegistry; 
     
    140145                } 
    141146 
    142             //Load the plugin settings. 
    143             byte[] pluginSettings = (byte[])key.GetValue("PluginSettings", new byte[0]); 
    144             if (pluginSettings.Length != 0) 
    145                 using (MemoryStream stream = new MemoryStream(pluginSettings)) 
    146                 { 
    147                     try 
    148                     { 
    149                         this.pluginSettings = (Dictionary<Guid, Dictionary<string, object>>) 
    150                             new BinaryFormatter().Deserialize(stream); 
    151                     } 
    152                     catch (Exception) 
    153                     { 
    154                         key.DeleteValue("PluginSettings"); 
    155                         MessageBox.Show(S._("Could not load plugin settings. All settings " + 
    156                             "have been lost."), S._("Eraser"), MessageBoxButtons.OK, 
    157                             MessageBoxIcon.Error); 
    158                     } 
    159                 } 
     147            base.Load(); 
    160148        } 
    161149 
     
    179167            using (MemoryStream stream = new MemoryStream()) 
    180168            { 
    181                 new BinaryFormatter().Serialize(stream, pluginSettings); 
    182                 key.SetValue("PluginSettings", stream.ToArray(), RegistryValueKind.Binary); 
    183             } 
    184  
    185             using (MemoryStream stream = new MemoryStream()) 
    186             { 
    187169                new BinaryFormatter().Serialize(stream, PlausibleDeniabilityFiles); 
    188170                key.SetValue("PlausibleDeniabilityFiles", stream.ToArray(), RegistryValueKind.Binary); 
    189171            } 
    190172        } 
     173 
     174        protected override Dictionary<string, object> GetSettings(Guid guid) 
     175        { 
     176            //Open the registry key containing the settings 
     177            RegistryKey pluginsKey = Application.UserAppDataRegistry.OpenSubKey( 
     178                "Plugins\\" + guid.ToString(), true); 
     179            Dictionary<string, object> result = new Dictionary<string, object>(); 
     180 
     181            //Load every key/value pair into the dictionary 
     182            if (pluginsKey != null) 
     183                foreach (string key in pluginsKey.GetValueNames()) 
     184                { 
     185                    byte[] currentSetting = (byte[])pluginsKey.GetValue(key, null); 
     186                    if (currentSetting.Length != 0) 
     187                        using (MemoryStream stream = new MemoryStream(currentSetting)) 
     188                        { 
     189                            try 
     190                            { 
     191                                result[key] = new BinaryFormatter().Deserialize(stream); 
     192                            } 
     193                            catch (Exception) 
     194                            { 
     195                                pluginsKey.DeleteValue(key); 
     196                                MessageBox.Show(string.Format(S._("Could not load the setting {0} for plugin {1}." + 
     197                                    "The setting has been lost."), key, guid.ToString()), 
     198                                    S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error); 
     199                            } 
     200                        } 
     201                    else 
     202                        result[key] = null; 
     203                } 
     204 
     205            //We're done! 
     206            return result; 
     207        } 
     208 
     209        protected override void SetSettings(Guid guid, Dictionary<string, object> settings) 
     210        { 
     211            //Open the registry key containing the settings 
     212            RegistryKey pluginKey = Application.UserAppDataRegistry.OpenSubKey( 
     213                "Plugins\\" + guid.ToString(), true); 
     214            if (pluginKey == null) 
     215                pluginKey = Application.UserAppDataRegistry.CreateSubKey("Plugins\\" + guid.ToString()); 
     216 
     217            foreach (string key in settings.Keys) 
     218            { 
     219                using (MemoryStream stream = new MemoryStream()) 
     220                { 
     221                    new BinaryFormatter().Serialize(stream, settings[key]); 
     222                    pluginKey.SetValue(key, stream.ToArray(), RegistryValueKind.Binary); 
     223                } 
     224            } 
     225        } 
    191226    } 
    192227} 
  • branches/eraser6/Eraser/Strings.NL.resx

    r373 r412  
    136136    <value>Kan geen taak lijst vinden. Alle taken zijn verdwenen.\n</value> 
    137137  </data> 
    138   <data name="Could not load plugin settings. All settings have been lost." xml:space="preserve"> 
    139     <value>Kan geen plugin lijst vinden. Alle plugins zijn verdwenen.</value> 
     138  <data name="Could not load the setting {0} for plugin {1}.The setting has been lost." xml:space="preserve"> 
     139    <value>(Untranslated)</value> 
    140140  </data> 
    141141  <data name="{0} out of {1}" xml:space="preserve"> 
  • branches/eraser6/Eraser/Strings.en.resx

    r372 r412  
    136136    <value>Could not load list of files used for plausible deniability.\n\nThe list of files have been lost.</value> 
    137137  </data> 
    138   <data name="Could not load plugin settings. All settings have been lost." xml:space="preserve"> 
    139     <value>Could not load plugin settings. All settings have been lost.</value> 
     138  <data name="Could not load the setting {0} for plugin {1}.The setting has been lost." xml:space="preserve"> 
     139    <value>(Untranslated)</value> 
    140140  </data> 
    141141  <data name="{0} out of {1}" xml:space="preserve"> 
  • branches/eraser6/Eraser/Strings.resx

    r370 r412  
    136136    <value>Could not load list of files used for plausible deniability.\n\nThe list of files have been lost.</value> 
    137137  </data> 
    138   <data name="Could not load plugin settings. All settings have been lost." xml:space="preserve"> 
    139     <value>Could not load plugin settings. All settings have been lost.</value> 
     138  <data name="Could not load the setting {0} for plugin {1}.The setting has been lost." xml:space="preserve"> 
     139    <value>Could not load the setting {0} for plugin {1}.The setting has been lost.</value> 
    140140  </data> 
    141141  <data name="{0} out of {1}" xml:space="preserve"> 
  • branches/eraser6/Manager/Method.cs

    r348 r412  
    217217 
    218218            /// <summary> 
    219             /// THe default pass function which writes a constant repeatedly to the 
     219            /// The default pass function which writes a constant repeatedly to the 
    220220            /// stream. The Pass' OpaqueValue must be set. 
    221221            /// </summary> 
  • branches/eraser6/Manager/Settings.cs

    r348 r412  
    3939 
    4040        /// <summary> 
     41        /// Loads all settings from storage. 
     42        /// </summary> 
     43        /// <remarks>Notes to inheritors: Always call the base class Load.</remarks> 
     44        protected internal virtual void Load() 
     45        { 
     46            if (SettingsChanged != null) 
     47                SettingsChanged(); 
     48        } 
     49 
     50        /// <summary> 
    4151        /// The language which all user interface elements should be presented in. 
    4252        /// This is a GUID since languages are supplied through plugins. 
     
    199209                lock (this) 
    200210                    plausibleDeniabilityFiles = value; 
    201             } 
    202         } 
    203  
    204         /// <summary> 
    205         /// Retrieves the dictionary holding settings for the given plugin. 
    206         /// </summary> 
    207         /// <param name="plugin">The GUID of the plugin querying for settings</param> 
    208         /// <returns>A dictionary holding settings for the plugin. This dictionary 
    209         /// will be automatically saved when the program exits holding the settings 
    210         /// permanenently. An empty dictionary will be returned if no settings 
    211         /// currently exist.</returns> 
    212         public Dictionary<string, object> GetSettings(Guid plugin) 
    213         { 
    214             lock (pluginSettings) 
    215             { 
    216                 if (pluginSettings.ContainsKey(plugin)) 
    217                     return pluginSettings[plugin]; 
    218  
    219                 Dictionary<string, object> result = new Dictionary<string, object>(); 
    220                 pluginSettings.Add(plugin, result); 
    221                 return result; 
    222211            } 
    223212        } 
     
    237226 
    238227        /// <summary> 
     228        /// Gets the settings from the data source. 
     229        /// </summary> 
     230        /// <param name="guid">The GUID of the calling plugin</param> 
     231        /// <returns>The dictionary containing settings for the plugin</returns> 
     232        protected abstract Dictionary<string, object> GetSettings(Guid guid); 
     233 
     234        /// <summary> 
    239235        /// Sets the settings for the calling plugin. 
    240236        /// </summary> 
     
    242238        public void SetSettings(Dictionary<string, object> settings) 
    243239        { 
    244             pluginSettings[new Guid(((GuidAttribute)Assembly.GetCallingAssembly(). 
    245                 GetCustomAttributes(typeof(GuidAttribute), false)[0]).Value)] 
    246                 = settings; 
    247         } 
     240            SetSettings(new Guid(((GuidAttribute)Assembly.GetCallingAssembly(). 
     241                GetCustomAttributes(typeof(GuidAttribute), false)[0]).Value), settings); 
     242            SettingsChanged(); 
     243        } 
     244 
     245        /// <summary> 
     246        /// Saves the settings from the plugin into the data source. 
     247        /// </summary> 
     248        /// <param name="guid"></param> 
     249        /// <param name="settings"></param> 
     250        protected abstract void SetSettings(Guid guid, Dictionary<string, object> settings); 
     251 
     252        /// <summary> 
     253        /// The prototype of functions which will handle a SettingsChanged Event. 
     254        /// </summary> 
     255        public delegate void OnSettingsChangedEvent(); 
     256 
     257        /// <summary> 
     258        /// The event handler handling a change in settings. 
     259        /// </summary> 
     260        public event OnSettingsChangedEvent SettingsChanged; 
    248261 
    249262        private string uiLanguage; 
     
    256269        private bool plausibleDeniability = true; 
    257270        private List<string> plausibleDeniabilityFiles = new List<string>(); 
    258  
    259         protected Dictionary<Guid, Dictionary<string, object>> pluginSettings = 
    260             new Dictionary<Guid, Dictionary<string, object>>(); 
    261271    } 
    262272} 
Note: See TracChangeset for help on using the changeset viewer.