| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008-2010 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Windows.Forms; |
|---|
| 25 | using System.Text; |
|---|
| 26 | |
|---|
| 27 | using System.IO; |
|---|
| 28 | using System.Globalization; |
|---|
| 29 | using System.Runtime.Serialization; |
|---|
| 30 | using System.Runtime.Serialization.Formatters.Binary; |
|---|
| 31 | using Microsoft.Win32; |
|---|
| 32 | |
|---|
| 33 | using Eraser.Util; |
|---|
| 34 | |
|---|
| 35 | namespace Eraser |
|---|
| 36 | { |
|---|
| 37 | internal class Settings : Manager.SettingsManager |
|---|
| 38 | { |
|---|
| 39 | /// <summary> |
|---|
| 40 | /// Registry-based storage backing for the Settings class. |
|---|
| 41 | /// </summary> |
|---|
| 42 | private sealed class RegistrySettings : Manager.Settings, IDisposable |
|---|
| 43 | { |
|---|
| 44 | /// <summary> |
|---|
| 45 | /// Constructor. |
|---|
| 46 | /// </summary> |
|---|
| 47 | /// <param name="pluginId">The GUID of the plugin for which settings are stored.</param> |
|---|
| 48 | /// <param name="key">The registry key to look for the settings in.</param> |
|---|
| 49 | public RegistrySettings(Guid pluginId, RegistryKey key) |
|---|
| 50 | { |
|---|
| 51 | if (key == null) |
|---|
| 52 | throw new ArgumentNullException("key"); |
|---|
| 53 | |
|---|
| 54 | PluginID = pluginId; |
|---|
| 55 | Key = key; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | #region IDisposable Members |
|---|
| 59 | |
|---|
| 60 | ~RegistrySettings() |
|---|
| 61 | { |
|---|
| 62 | Dispose(false); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public void Dispose() |
|---|
| 66 | { |
|---|
| 67 | Dispose(true); |
|---|
| 68 | GC.SuppressFinalize(this); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | private void Dispose(bool disposing) |
|---|
| 72 | { |
|---|
| 73 | if (Key == null) |
|---|
| 74 | return; |
|---|
| 75 | |
|---|
| 76 | if (disposing) |
|---|
| 77 | Key.Close(); |
|---|
| 78 | Key = null; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | #endregion |
|---|
| 82 | |
|---|
| 83 | public override T GetValue<T>(string name, T defaultValue) |
|---|
| 84 | { |
|---|
| 85 | //Get the raw registry value |
|---|
| 86 | object rawResult = Key.GetValue(name, null); |
|---|
| 87 | |
|---|
| 88 | //Check if it is a serialised object |
|---|
| 89 | byte[] resultArray = rawResult as byte[]; |
|---|
| 90 | if (resultArray != null) |
|---|
| 91 | { |
|---|
| 92 | using (MemoryStream stream = new MemoryStream(resultArray)) |
|---|
| 93 | try |
|---|
| 94 | { |
|---|
| 95 | BinaryFormatter formatter = new BinaryFormatter(); |
|---|
| 96 | if (typeof(T) != typeof(object)) |
|---|
| 97 | formatter.Binder = new TypeNameSerializationBinder(typeof(T)); |
|---|
| 98 | return (T)formatter.Deserialize(stream); |
|---|
| 99 | } |
|---|
| 100 | catch (SerializationException) |
|---|
| 101 | { |
|---|
| 102 | Key.DeleteValue(name); |
|---|
| 103 | MessageBox.Show(S._("Could not load the setting {0}\\{1} for " + |
|---|
| 104 | "plugin {2}. The setting has been lost.", Key, name, |
|---|
| 105 | PluginID.ToString()), |
|---|
| 106 | S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error, |
|---|
| 107 | MessageBoxDefaultButton.Button1, |
|---|
| 108 | Localisation.IsRightToLeft(null) ? MessageBoxOptions.RtlReading : 0); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | else if (typeof(T) == typeof(Guid)) |
|---|
| 112 | { |
|---|
| 113 | return (T)(object)new Guid((string)rawResult); |
|---|
| 114 | } |
|---|
| 115 | else |
|---|
| 116 | { |
|---|
| 117 | return (T)rawResult; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | return defaultValue; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | public override void SetValue(string name, object value) |
|---|
| 124 | { |
|---|
| 125 | if (value == null) |
|---|
| 126 | { |
|---|
| 127 | Key.DeleteValue(name); |
|---|
| 128 | } |
|---|
| 129 | else |
|---|
| 130 | { |
|---|
| 131 | if (value is bool) |
|---|
| 132 | Key.SetValue(name, value, RegistryValueKind.DWord); |
|---|
| 133 | else if ((value is int) || (value is uint)) |
|---|
| 134 | Key.SetValue(name, value, RegistryValueKind.DWord); |
|---|
| 135 | else if ((value is long) || (value is ulong)) |
|---|
| 136 | Key.SetValue(name, value, RegistryValueKind.QWord); |
|---|
| 137 | else if ((value is string) || (value is Guid)) |
|---|
| 138 | Key.SetValue(name, value, RegistryValueKind.String); |
|---|
| 139 | else if (value is ICollection<string>) |
|---|
| 140 | { |
|---|
| 141 | ICollection<string> collection = (ICollection<string>)value; |
|---|
| 142 | string[] temp = new string[collection.Count]; |
|---|
| 143 | collection.CopyTo(temp, 0); |
|---|
| 144 | Key.SetValue(name, temp, RegistryValueKind.MultiString); |
|---|
| 145 | } |
|---|
| 146 | else |
|---|
| 147 | using (MemoryStream stream = new MemoryStream()) |
|---|
| 148 | { |
|---|
| 149 | new BinaryFormatter().Serialize(stream, value); |
|---|
| 150 | Key.SetValue(name, stream.ToArray(), RegistryValueKind.Binary); |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /// <summary> |
|---|
| 156 | /// The GUID of the plugin whose settings this object is storing. |
|---|
| 157 | /// </summary> |
|---|
| 158 | private Guid PluginID; |
|---|
| 159 | |
|---|
| 160 | /// <summary> |
|---|
| 161 | /// The registry key where the data is stored. |
|---|
| 162 | /// </summary> |
|---|
| 163 | private RegistryKey Key; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | public override void Save() |
|---|
| 167 | { |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | protected override Manager.Settings GetSettings(Guid guid) |
|---|
| 171 | { |
|---|
| 172 | RegistryKey eraserKey = null; |
|---|
| 173 | |
|---|
| 174 | try |
|---|
| 175 | { |
|---|
| 176 | //Open the registry key containing the settings |
|---|
| 177 | eraserKey = Registry.CurrentUser.OpenSubKey(Program.SettingsPath, true); |
|---|
| 178 | if (eraserKey == null) |
|---|
| 179 | eraserKey = Registry.CurrentUser.CreateSubKey(Program.SettingsPath); |
|---|
| 180 | |
|---|
| 181 | RegistryKey pluginsKey = eraserKey.OpenSubKey(guid.ToString(), true); |
|---|
| 182 | if (pluginsKey == null) |
|---|
| 183 | pluginsKey = eraserKey.CreateSubKey(guid.ToString()); |
|---|
| 184 | |
|---|
| 185 | //Return the Settings object. |
|---|
| 186 | return new RegistrySettings(guid, pluginsKey); |
|---|
| 187 | } |
|---|
| 188 | finally |
|---|
| 189 | { |
|---|
| 190 | if (eraserKey != null) |
|---|
| 191 | eraserKey.Close(); |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | internal class EraserSettings |
|---|
| 197 | { |
|---|
| 198 | /// <summary> |
|---|
| 199 | /// Constructor. |
|---|
| 200 | /// </summary> |
|---|
| 201 | private EraserSettings() |
|---|
| 202 | { |
|---|
| 203 | settings = Manager.ManagerLibrary.Instance.SettingsManager.ModuleSettings; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | /// <summary> |
|---|
| 207 | /// Gets the singleton instance of the Eraser UI Settings. |
|---|
| 208 | /// </summary> |
|---|
| 209 | /// <returns>The global instance of the Eraser UI settings.</returns> |
|---|
| 210 | public static EraserSettings Get() |
|---|
| 211 | { |
|---|
| 212 | if (instance == null) |
|---|
| 213 | instance = new EraserSettings(); |
|---|
| 214 | return instance; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | /// <summary> |
|---|
| 218 | /// Gets or sets the LCID of the language which the UI should be displayed in. |
|---|
| 219 | /// </summary> |
|---|
| 220 | public string Language |
|---|
| 221 | { |
|---|
| 222 | get |
|---|
| 223 | { |
|---|
| 224 | return settings.GetValue("Language", GetCurrentCulture().Name); |
|---|
| 225 | } |
|---|
| 226 | set |
|---|
| 227 | { |
|---|
| 228 | settings.SetValue("Language", value); |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | /// <summary> |
|---|
| 233 | /// Gets or sets whether the Shell Extension should be loaded into Explorer. |
|---|
| 234 | /// </summary> |
|---|
| 235 | public bool IntegrateWithShell |
|---|
| 236 | { |
|---|
| 237 | get |
|---|
| 238 | { |
|---|
| 239 | return settings.GetValue("IntegrateWithShell", true); |
|---|
| 240 | } |
|---|
| 241 | set |
|---|
| 242 | { |
|---|
| 243 | settings.SetValue("IntegrateWithShell", value); |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | /// <summary> |
|---|
| 248 | /// Gets or sets a value on whether the main frame should be minimised to the |
|---|
| 249 | /// system notification area. |
|---|
| 250 | /// </summary> |
|---|
| 251 | public bool HideWhenMinimised |
|---|
| 252 | { |
|---|
| 253 | get |
|---|
| 254 | { |
|---|
| 255 | return settings.GetValue("HideWhenMinimised", true); |
|---|
| 256 | } |
|---|
| 257 | set |
|---|
| 258 | { |
|---|
| 259 | settings.SetValue("HideWhenMinimised", value); |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | /// <summary> |
|---|
| 264 | /// Gets ot setts a value whether tasks which were completed successfully |
|---|
| 265 | /// should be removed by the Eraser client. |
|---|
| 266 | /// </summary> |
|---|
| 267 | public bool ClearCompletedTasks |
|---|
| 268 | { |
|---|
| 269 | get |
|---|
| 270 | { |
|---|
| 271 | return settings.GetValue("ClearCompletedTasks", true); |
|---|
| 272 | } |
|---|
| 273 | set |
|---|
| 274 | { |
|---|
| 275 | settings.SetValue("ClearCompletedTasks", value); |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | /// <summary> |
|---|
| 280 | /// Gets the most specific UI culture with a localisation available, defaulting to English |
|---|
| 281 | /// if none exist. |
|---|
| 282 | /// </summary> |
|---|
| 283 | /// <returns>The CultureInfo of the current UI culture, correct to the top level.</returns> |
|---|
| 284 | private static CultureInfo GetCurrentCulture() |
|---|
| 285 | { |
|---|
| 286 | System.Reflection.Assembly entryAssembly = System.Reflection.Assembly.GetEntryAssembly(); |
|---|
| 287 | CultureInfo culture = CultureInfo.CurrentUICulture; |
|---|
| 288 | while (culture.Parent != CultureInfo.InvariantCulture && |
|---|
| 289 | !Localisation.LocalisationExists(culture, entryAssembly)) |
|---|
| 290 | { |
|---|
| 291 | culture = culture.Parent; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | //Default to English if any of our cultures don't exist. |
|---|
| 295 | if (!Localisation.LocalisationExists(culture, entryAssembly)) |
|---|
| 296 | culture = new CultureInfo("en"); |
|---|
| 297 | |
|---|
| 298 | return culture; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | /// <summary> |
|---|
| 302 | /// The data store behind the object. |
|---|
| 303 | /// </summary> |
|---|
| 304 | private Manager.Settings settings; |
|---|
| 305 | |
|---|
| 306 | /// <summary> |
|---|
| 307 | /// The global instance of the settings class. |
|---|
| 308 | /// </summary> |
|---|
| 309 | private static EraserSettings instance; |
|---|
| 310 | } |
|---|
| 311 | } |
|---|