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