| 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 | //Load the task list |
|---|
| 51 | RegistryKey key = Application.UserAppDataRegistry; |
|---|
| 52 | byte[] savedTaskList = (byte[])key.GetValue("TaskList", new byte[0]); |
|---|
| 53 | using (MemoryStream stream = new MemoryStream(savedTaskList)) |
|---|
| 54 | { |
|---|
| 55 | try |
|---|
| 56 | { |
|---|
| 57 | if (savedTaskList.Length != 0) |
|---|
| 58 | eraserClient.LoadTaskList(stream); |
|---|
| 59 | } |
|---|
| 60 | catch (Exception) |
|---|
| 61 | { |
|---|
| 62 | key.DeleteValue("TaskList"); |
|---|
| 63 | MessageBox.Show(S._("Could not load task list. All task entries have " + |
|---|
| 64 | "been lost."), S._("Eraser"), MessageBoxButtons.OK, |
|---|
| 65 | MessageBoxIcon.Error); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | //Create the main form |
|---|
| 70 | MainForm form = new MainForm(); |
|---|
| 71 | |
|---|
| 72 | //Run tasks which are meant to be run on restart |
|---|
| 73 | int restartPos = Environment.CommandLine.ToLower().IndexOf("restart"); |
|---|
| 74 | if ((restartPos > 1 && |
|---|
| 75 | Environment.CommandLine[restartPos - 1] == '/') || |
|---|
| 76 | (restartPos > 2 && |
|---|
| 77 | Environment.CommandLine[restartPos - 1] == '-' && |
|---|
| 78 | Environment.CommandLine[restartPos - 2] == '-')) |
|---|
| 79 | { |
|---|
| 80 | eraserClient.QueueRestartTasks(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | //Run the program |
|---|
| 84 | eraserClient.Run(); |
|---|
| 85 | Application.Run(form); |
|---|
| 86 | |
|---|
| 87 | //Save the task list |
|---|
| 88 | using (MemoryStream stream = new MemoryStream()) |
|---|
| 89 | { |
|---|
| 90 | eraserClient.SaveTaskList(stream); |
|---|
| 91 | key.SetValue("TaskList", stream.ToArray(), RegistryValueKind.Binary); |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | public static DirectExecutor eraserClient; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public class Settings : Manager.SettingsManager |
|---|
| 100 | { |
|---|
| 101 | /// <summary> |
|---|
| 102 | /// Registry-based storage backing for the Settings class. |
|---|
| 103 | /// </summary> |
|---|
| 104 | private class RegistrySettings : Manager.Settings |
|---|
| 105 | { |
|---|
| 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; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | public override void Save() |
|---|
| 158 | { |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | protected override Manager.Settings GetSettings(Guid guid) |
|---|
| 162 | { |
|---|
| 163 | //Open the registry key containing the settings |
|---|
| 164 | const string eraserKeyPath = @"SOFTWARE\Eraser\Eraser 6"; |
|---|
| 165 | RegistryKey eraserKey = Registry.CurrentUser.OpenSubKey(eraserKeyPath, true); |
|---|
| 166 | if (eraserKey == null) |
|---|
| 167 | eraserKey = Registry.CurrentUser.CreateSubKey(eraserKeyPath); |
|---|
| 168 | |
|---|
| 169 | RegistryKey pluginsKey = eraserKey.OpenSubKey(guid.ToString(), true); |
|---|
| 170 | if (pluginsKey == null) |
|---|
| 171 | pluginsKey = eraserKey.CreateSubKey(guid.ToString()); |
|---|
| 172 | |
|---|
| 173 | //Return the Settings object. |
|---|
| 174 | return new RegistrySettings(guid, pluginsKey); |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | internal class EraserSettings |
|---|
| 179 | { |
|---|
| 180 | public EraserSettings() |
|---|
| 181 | { |
|---|
| 182 | settings = Manager.ManagerLibrary.Instance.SettingsManager.ModuleSettings; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | /// <summary> |
|---|
| 186 | /// Gets or sets the LCID of the language which the UI should be displayed in. |
|---|
| 187 | /// </summary> |
|---|
| 188 | public string Language |
|---|
| 189 | { |
|---|
| 190 | get |
|---|
| 191 | { |
|---|
| 192 | return settings["Language"] == null ? |
|---|
| 193 | GetCurrentCulture().Name : |
|---|
| 194 | (string)settings["Language"]; |
|---|
| 195 | } |
|---|
| 196 | set |
|---|
| 197 | { |
|---|
| 198 | settings["Language"] = value; |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /// <summary> |
|---|
| 203 | /// Gets or sets a value on whether the main frame should be minimised to the |
|---|
| 204 | /// system notification area. |
|---|
| 205 | /// </summary> |
|---|
| 206 | public bool HideWhenMinimised |
|---|
| 207 | { |
|---|
| 208 | get |
|---|
| 209 | { |
|---|
| 210 | return settings["HideWhenMinimised"] == null ? |
|---|
| 211 | true : (bool)settings["HideWhenMinimised"]; |
|---|
| 212 | } |
|---|
| 213 | set |
|---|
| 214 | { |
|---|
| 215 | settings["HideWhenMinimised"] = value; |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /// <summary> |
|---|
| 220 | /// Gets the current UI culture, correct to the top-level culture (i.e., English |
|---|
| 221 | /// instead of English (United Kingdom)) |
|---|
| 222 | /// </summary> |
|---|
| 223 | /// <returns>The CultureInfo of the current UI culture, correct to the top level.</returns> |
|---|
| 224 | private static CultureInfo GetCurrentCulture() |
|---|
| 225 | { |
|---|
| 226 | CultureInfo culture = CultureInfo.CurrentUICulture; |
|---|
| 227 | while (culture.Parent != CultureInfo.InvariantCulture) |
|---|
| 228 | culture = culture.Parent; |
|---|
| 229 | |
|---|
| 230 | return culture; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | private Manager.Settings settings; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|