Changeset 561 for branches/eraser6/Eraser/Program.cs
- Timestamp:
- 11/14/2008 8:20:48 AM (4 years ago)
- File:
-
- 1 edited
-
branches/eraser6/Eraser/Program.cs (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/eraser6/Eraser/Program.cs
r495 r561 97 97 } 98 98 99 public class Settings : Manager.Settings 99 public class Settings : Manager.SettingsManager 100 100 { 101 public Settings() 101 /// <summary> 102 /// Registry-based storage backing for the Settings class. 103 /// </summary> 104 private class RegistrySettings : Manager.Settings 102 105 { 103 Load(); 106 /// <summary> 107 /// Constructor. 108 /// </summary> 109 /// <param name="key">The registry key to look for the settings in.</param> 110 public RegistrySettings(Guid pluginID, RegistryKey key) 111 { 112 this.key = key; 113 } 114 115 public override object this[string setting] 116 { 117 get 118 { 119 byte[] currentSetting = (byte[])key.GetValue(setting, null); 120 if (currentSetting != null && currentSetting.Length != 0) 121 using (MemoryStream stream = new MemoryStream(currentSetting)) 122 try 123 { 124 return new BinaryFormatter().Deserialize(stream); 125 } 126 catch (Exception) 127 { 128 key.DeleteValue(setting); 129 MessageBox.Show(string.Format(S._("Could not load the setting {0} for plugin {1}." + 130 "The setting has been lost."), key, pluginID.ToString()), 131 S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error); 132 } 133 134 return null; 135 } 136 set 137 { 138 using (MemoryStream stream = new MemoryStream()) 139 { 140 new BinaryFormatter().Serialize(stream, value); 141 key.SetValue(setting, stream.ToArray(), RegistryValueKind.Binary); 142 } 143 } 144 } 145 146 /// <summary> 147 /// The GUID of the plugin whose settings this object is storing. 148 /// </summary> 149 private Guid pluginID; 150 151 /// <summary> 152 /// The registry key where the data is stored. 153 /// </summary> 154 private RegistryKey key; 104 155 } 105 156 106 p rotected override void Load()157 public override void Save() 107 158 { 108 RegistryKey key = Application.UserAppDataRegistry;109 ActivePRNG = new Guid((string)110 key.GetValue("PRNG", Guid.Empty.ToString()));111 EraseLockedFilesOnRestart =112 (int)key.GetValue("EraseOnRestart", (object)1) != 0;113 ConfirmEraseOnRestart =114 (int)key.GetValue("ConfirmEraseOnRestart", (object)0) != 0;115 DefaultFileErasureMethod = new Guid((string)116 key.GetValue("DefaultFileErasureMethod", Guid.Empty.ToString()));117 DefaultUnusedSpaceErasureMethod = new Guid((string)118 key.GetValue("DefaultUnusedSpaceErasureMethod", Guid.Empty.ToString()));119 ExecuteMissedTasksImmediately =120 (int)key.GetValue("ExecuteMissedTasksImmediately", (object)1) != 0;121 PlausibleDeniability =122 (int)key.GetValue("PlausibleDeniability", (object)0) != 0;123 124 UILanguage = (string)key.GetValue("UILanguage", CultureInfo.CurrentUICulture.Name);125 System.Threading.Thread.CurrentThread.CurrentUICulture =126 new CultureInfo(UILanguage);127 128 //Load the plausible deniability files129 byte[] plausibleDeniabilityFiles = (byte[])130 key.GetValue("PlausibleDeniabilityFiles", new byte[0]);131 if (plausibleDeniabilityFiles.Length != 0)132 using (MemoryStream stream = new MemoryStream(plausibleDeniabilityFiles))133 {134 try135 {136 this.PlausibleDeniabilityFiles = (List<string>)137 new BinaryFormatter().Deserialize(stream);138 }139 catch (Exception)140 {141 key.DeleteValue("PlausibleDeniabilityFiles");142 MessageBox.Show(S._("Could not load list of files used for plausible " +143 "deniability.\n\nThe list of files have been lost."),144 S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error);145 }146 }147 159 } 148 160 149 protected override void Save() 150 { 151 RegistryKey key = Application.UserAppDataRegistry; 152 key.SetValue("PRNG", ActivePRNG); 153 key.SetValue("EraseOnRestart", EraseLockedFilesOnRestart, 154 RegistryValueKind.DWord); 155 key.SetValue("ConfirmEraseOnRestart", ConfirmEraseOnRestart, 156 RegistryValueKind.DWord); 157 key.SetValue("DefaultFileErasureMethod", DefaultFileErasureMethod); 158 key.SetValue("DefaultUnusedSpaceErasureMethod", 159 DefaultUnusedSpaceErasureMethod); 160 key.SetValue("ExecuteMissedTasksImmediately", 161 ExecuteMissedTasksImmediately, RegistryValueKind.DWord); 162 key.SetValue("PlausibleDeniability", PlausibleDeniability, 163 RegistryValueKind.DWord); 164 key.SetValue("UILanguage", UILanguage); 165 166 using (MemoryStream stream = new MemoryStream()) 167 { 168 new BinaryFormatter().Serialize(stream, PlausibleDeniabilityFiles); 169 key.SetValue("PlausibleDeniabilityFiles", stream.ToArray(), RegistryValueKind.Binary); 170 } 171 } 172 173 protected override Dictionary<string, object> GetSettings(Guid guid) 161 protected override Manager.Settings GetSettings(Guid guid) 174 162 { 175 163 //Open the registry key containing the settings 176 164 RegistryKey pluginsKey = Application.UserAppDataRegistry.OpenSubKey( 177 165 "Plugins\\" + guid.ToString(), true); 178 Dictionary<string, object> result = new Dictionary<string, object>(); 166 if (pluginsKey == null) 167 pluginsKey = Application.UserAppDataRegistry.CreateSubKey( 168 "Plugins\\" + guid.ToString()); 179 169 180 //Load every key/value pair into the dictionary 181 if (pluginsKey != null) 182 foreach (string key in pluginsKey.GetValueNames()) 183 { 184 byte[] currentSetting = (byte[])pluginsKey.GetValue(key, null); 185 if (currentSetting.Length != 0) 186 using (MemoryStream stream = new MemoryStream(currentSetting)) 187 { 188 try 189 { 190 result[key] = new BinaryFormatter().Deserialize(stream); 191 } 192 catch (Exception) 193 { 194 pluginsKey.DeleteValue(key); 195 MessageBox.Show(string.Format(S._("Could not load the setting {0} for plugin {1}." + 196 "The setting has been lost."), key, guid.ToString()), 197 S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error); 198 } 199 } 200 else 201 result[key] = null; 202 } 203 204 //We're done! 205 return result; 206 } 207 208 protected override void SetSettings(Guid guid, Dictionary<string, object> settings) 209 { 210 //Open the registry key containing the settings 211 RegistryKey pluginKey = Application.UserAppDataRegistry.OpenSubKey( 212 "Plugins\\" + guid.ToString(), true); 213 if (pluginKey == null) 214 pluginKey = Application.UserAppDataRegistry.CreateSubKey("Plugins\\" + guid.ToString()); 215 216 foreach (string key in settings.Keys) 217 { 218 using (MemoryStream stream = new MemoryStream()) 219 { 220 new BinaryFormatter().Serialize(stream, settings[key]); 221 pluginKey.SetValue(key, stream.ToArray(), RegistryValueKind.Binary); 222 } 223 } 170 //Return the Settings object. 171 return new RegistrySettings(guid, pluginsKey); 224 172 } 225 173 }
Note: See TracChangeset
for help on using the changeset viewer.
