| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008 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 | |
|---|
| 26 | using Eraser.Manager; |
|---|
| 27 | using Eraser.Util; |
|---|
| 28 | using Microsoft.Win32; |
|---|
| 29 | using System.IO; |
|---|
| 30 | using System.Runtime.Serialization.Formatters.Binary; |
|---|
| 31 | using System.Globalization; |
|---|
| 32 | |
|---|
| 33 | namespace Eraser |
|---|
| 34 | { |
|---|
| 35 | static class Program |
|---|
| 36 | { |
|---|
| 37 | /// <summary> |
|---|
| 38 | /// The main entry point for the application. |
|---|
| 39 | /// </summary> |
|---|
| 40 | [STAThread] |
|---|
| 41 | static void Main() |
|---|
| 42 | { |
|---|
| 43 | Application.EnableVisualStyles(); |
|---|
| 44 | Application.SetCompatibleTextRenderingDefault(false); |
|---|
| 45 | Application.SafeTopLevelCaptionFormat = S._("Eraser"); |
|---|
| 46 | |
|---|
| 47 | using (ManagerLibrary library = new ManagerLibrary(new Settings())) |
|---|
| 48 | using (eraserClient = new DirectExecutor()) |
|---|
| 49 | { |
|---|
| 50 | //Set our UI language |
|---|
| 51 | EraserSettings settings = new EraserSettings(); |
|---|
| 52 | System.Threading.Thread.CurrentThread.CurrentUICulture = |
|---|
| 53 | new CultureInfo(settings.Language); |
|---|
| 54 | |
|---|
| 55 | //Load the task list |
|---|
| 56 | if (settings.TaskList != null) |
|---|
| 57 | using (MemoryStream stream = new MemoryStream(settings.TaskList)) |
|---|
| 58 | try |
|---|
| 59 | { |
|---|
| 60 | eraserClient.LoadTaskList(stream); |
|---|
| 61 | } |
|---|
| 62 | catch (Exception) |
|---|
| 63 | { |
|---|
| 64 | settings.TaskList = null; |
|---|
| 65 | MessageBox.Show(S._("Could not load task list. All task entries have " + |
|---|
| 66 | "been lost."), S._("Eraser"), MessageBoxButtons.OK, |
|---|
| 67 | MessageBoxIcon.Error); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | //Create the main form |
|---|
| 71 | MainForm form = new MainForm(); |
|---|
| 72 | |
|---|
| 73 | //Run tasks which are meant to be run on restart |
|---|
| 74 | int restartPos = Environment.CommandLine.ToLower().IndexOf("restart"); |
|---|
| 75 | if ((restartPos > 1 && |
|---|
| 76 | Environment.CommandLine[restartPos - 1] == '/') || |
|---|
| 77 | (restartPos > 2 && |
|---|
| 78 | Environment.CommandLine[restartPos - 1] == '-' && |
|---|
| 79 | Environment.CommandLine[restartPos - 2] == '-')) |
|---|
| 80 | { |
|---|
| 81 | eraserClient.QueueRestartTasks(); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | //Run the program |
|---|
| 85 | eraserClient.Run(); |
|---|
| 86 | Application.Run(form); |
|---|
| 87 | |
|---|
| 88 | //Save the task list |
|---|
| 89 | using (MemoryStream stream = new MemoryStream()) |
|---|
| 90 | { |
|---|
| 91 | eraserClient.SaveTaskList(stream); |
|---|
| 92 | settings.TaskList = stream.ToArray(); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | public static DirectExecutor eraserClient; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | public class Settings : Manager.SettingsManager |
|---|
| 101 | { |
|---|
| 102 | /// <summary> |
|---|
| 103 | /// Registry-based storage backing for the Settings class. |
|---|
| 104 | /// </summary> |
|---|
| 105 | private class RegistrySettings : Manager.Settings |
|---|
| 106 | { |
|---|
| 107 | /// <summary> |
|---|
| 108 | /// Constructor. |
|---|
| 109 | /// </summary> |
|---|
| 110 | /// <param name="key">The registry key to look for the settings in.</param> |
|---|
| 111 | public RegistrySettings(Guid pluginID, RegistryKey key) |
|---|
| 112 | { |
|---|
| 113 | this.key = key; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | public override object this[string setting] |
|---|
| 117 | { |
|---|
| 118 | get |
|---|
| 119 | { |
|---|
| 120 | byte[] currentSetting = (byte[])key.GetValue(setting, null); |
|---|
| 121 | if (currentSetting != null && currentSetting.Length != 0) |
|---|
| 122 | using (MemoryStream stream = new MemoryStream(currentSetting)) |
|---|
| 123 | try |
|---|
| 124 | { |
|---|
| 125 | return new BinaryFormatter().Deserialize(stream); |
|---|
| 126 | } |
|---|
| 127 | catch (Exception) |
|---|
| 128 | { |
|---|
| 129 | key.DeleteValue(setting); |
|---|
| 130 | MessageBox.Show(S._("Could not load the setting {0} for plugin {1}. " + |
|---|
| 131 | "The setting has been lost.", key, pluginID.ToString()), |
|---|
| 132 | S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | return null; |
|---|
| 136 | } |
|---|
| 137 | set |
|---|
| 138 | { |
|---|
| 139 | if (value == null) |
|---|
| 140 | { |
|---|
| 141 | key.DeleteValue(setting); |
|---|
| 142 | } |
|---|
| 143 | else |
|---|
| 144 | { |
|---|
| 145 | using (MemoryStream stream = new MemoryStream()) |
|---|
| 146 | { |
|---|
| 147 | new BinaryFormatter().Serialize(stream, value); |
|---|
| 148 | key.SetValue(setting, stream.ToArray(), RegistryValueKind.Binary); |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /// <summary> |
|---|
| 155 | /// The GUID of the plugin whose settings this object is storing. |
|---|
| 156 | /// </summary> |
|---|
| 157 | private Guid pluginID; |
|---|
| 158 | |
|---|
| 159 | /// <summary> |
|---|
| 160 | /// The registry key where the data is stored. |
|---|
| 161 | /// </summary> |
|---|
| 162 | private RegistryKey key; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | public override void Save() |
|---|
| 166 | { |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | protected override Manager.Settings GetSettings(Guid guid) |
|---|
| 170 | { |
|---|
| 171 | //Open the registry key containing the settings |
|---|
| 172 | const string eraserKeyPath = @"SOFTWARE\Eraser\Eraser 6"; |
|---|
| 173 | RegistryKey eraserKey = Registry.CurrentUser.OpenSubKey(eraserKeyPath, true); |
|---|
| 174 | if (eraserKey == null) |
|---|
| 175 | eraserKey = Registry.CurrentUser.CreateSubKey(eraserKeyPath); |
|---|
| 176 | |
|---|
| 177 | RegistryKey pluginsKey = eraserKey.OpenSubKey(guid.ToString(), true); |
|---|
| 178 | if (pluginsKey == null) |
|---|
| 179 | pluginsKey = eraserKey.CreateSubKey(guid.ToString()); |
|---|
| 180 | |
|---|
| 181 | //Return the Settings object. |
|---|
| 182 | return new RegistrySettings(guid, pluginsKey); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | internal class EraserSettings |
|---|
| 187 | { |
|---|
| 188 | public EraserSettings() |
|---|
| 189 | { |
|---|
| 190 | settings = Manager.ManagerLibrary.Instance.SettingsManager.ModuleSettings; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | /// <summary> |
|---|
| 194 | /// Gets or sets the task list, serialised in binary form by the Manager assembly. |
|---|
| 195 | /// </summary> |
|---|
| 196 | public byte[] TaskList |
|---|
| 197 | { |
|---|
| 198 | get |
|---|
| 199 | { |
|---|
| 200 | return (byte[])settings["TaskList"]; |
|---|
| 201 | } |
|---|
| 202 | set |
|---|
| 203 | { |
|---|
| 204 | settings["TaskList"] = value; |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | /// <summary> |
|---|
| 209 | /// Gets or sets the LCID of the language which the UI should be displayed in. |
|---|
| 210 | /// </summary> |
|---|
| 211 | public string Language |
|---|
| 212 | { |
|---|
| 213 | get |
|---|
| 214 | { |
|---|
| 215 | return settings["Language"] == null ? |
|---|
| 216 | GetCurrentCulture().Name : |
|---|
| 217 | (string)settings["Language"]; |
|---|
| 218 | } |
|---|
| 219 | set |
|---|
| 220 | { |
|---|
| 221 | settings["Language"] = value; |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | /// <summary> |
|---|
| 226 | /// Gets or sets a value on whether the main frame should be minimised to the |
|---|
| 227 | /// system notification area. |
|---|
| 228 | /// </summary> |
|---|
| 229 | public bool HideWhenMinimised |
|---|
| 230 | { |
|---|
| 231 | get |
|---|
| 232 | { |
|---|
| 233 | return settings["HideWhenMinimised"] == null ? |
|---|
| 234 | true : (bool)settings["HideWhenMinimised"]; |
|---|
| 235 | } |
|---|
| 236 | set |
|---|
| 237 | { |
|---|
| 238 | settings["HideWhenMinimised"] = value; |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | /// <summary> |
|---|
| 243 | /// Gets the current UI culture, correct to the top-level culture (i.e., English |
|---|
| 244 | /// instead of English (United Kingdom)) |
|---|
| 245 | /// </summary> |
|---|
| 246 | /// <returns>The CultureInfo of the current UI culture, correct to the top level.</returns> |
|---|
| 247 | private static CultureInfo GetCurrentCulture() |
|---|
| 248 | { |
|---|
| 249 | CultureInfo culture = CultureInfo.CurrentUICulture; |
|---|
| 250 | while (culture.Parent != CultureInfo.InvariantCulture) |
|---|
| 251 | culture = culture.Parent; |
|---|
| 252 | |
|---|
| 253 | return culture; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | private Manager.Settings settings; |
|---|
| 257 | } |
|---|
| 258 | } |
|---|