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