| 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 | Application.Run(form); |
|---|
| 85 | |
|---|
| 86 | //Save the task list |
|---|
| 87 | using (MemoryStream stream = new MemoryStream()) |
|---|
| 88 | { |
|---|
| 89 | eraserClient.SaveTaskList(stream); |
|---|
| 90 | key.SetValue("TaskList", stream.ToArray(), RegistryValueKind.Binary); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | public static DirectExecutor eraserClient; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | public class Settings : Manager.Settings |
|---|
| 99 | { |
|---|
| 100 | public Settings() |
|---|
| 101 | { |
|---|
| 102 | RegistryKey key = Application.UserAppDataRegistry; |
|---|
| 103 | ActivePRNG = new Guid((string) |
|---|
| 104 | key.GetValue("PRNG", Guid.Empty.ToString())); |
|---|
| 105 | EraseLockedFilesOnRestart = |
|---|
| 106 | (int)key.GetValue("EraseOnRestart", (object)1) != 0; |
|---|
| 107 | ConfirmEraseOnRestart = |
|---|
| 108 | (int)key.GetValue("ConfirmEraseOnRestart", (object)0) != 0; |
|---|
| 109 | DefaultFileErasureMethod = new Guid((string) |
|---|
| 110 | key.GetValue("DefaultFileErasureMethod", Guid.Empty.ToString())); |
|---|
| 111 | DefaultUnusedSpaceErasureMethod = new Guid((string) |
|---|
| 112 | key.GetValue("DefaultUnusedSpaceErasureMethod", Guid.Empty.ToString())); |
|---|
| 113 | ExecuteMissedTasksImmediately = |
|---|
| 114 | (int)key.GetValue("ExecuteMissedTasksImmediately", (object)1) != 0; |
|---|
| 115 | PlausibleDeniability = |
|---|
| 116 | (int)key.GetValue("PlausibleDeniability", (object)0) != 0; |
|---|
| 117 | UILanguage = (string)key.GetValue("UILanguage", CultureInfo.CurrentUICulture.Name); |
|---|
| 118 | S.Language = new CultureInfo(UILanguage); |
|---|
| 119 | |
|---|
| 120 | //Load the plausible deniability files |
|---|
| 121 | byte[] plausibleDeniabilityFiles = (byte[]) |
|---|
| 122 | key.GetValue("PlausibleDeniabilityFiles", new byte[0]); |
|---|
| 123 | if (plausibleDeniabilityFiles.Length != 0) |
|---|
| 124 | using (MemoryStream stream = new MemoryStream(plausibleDeniabilityFiles)) |
|---|
| 125 | { |
|---|
| 126 | try |
|---|
| 127 | { |
|---|
| 128 | this.PlausibleDeniabilityFiles = (List<string>) |
|---|
| 129 | new BinaryFormatter().Deserialize(stream); |
|---|
| 130 | } |
|---|
| 131 | catch (Exception) |
|---|
| 132 | { |
|---|
| 133 | key.DeleteValue("PlausibleDeniabilityFiles"); |
|---|
| 134 | MessageBox.Show(S._("Could not load list of files used for plausible " + |
|---|
| 135 | "deniability.\n\nThe list of files have been lost."), |
|---|
| 136 | S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | //Load the plugin settings. |
|---|
| 141 | byte[] pluginSettings = (byte[])key.GetValue("PluginSettings", new byte[0]); |
|---|
| 142 | if (pluginSettings.Length != 0) |
|---|
| 143 | using (MemoryStream stream = new MemoryStream(pluginSettings)) |
|---|
| 144 | { |
|---|
| 145 | try |
|---|
| 146 | { |
|---|
| 147 | this.pluginSettings = (Dictionary<Guid, Dictionary<string, object>>) |
|---|
| 148 | new BinaryFormatter().Deserialize(stream); |
|---|
| 149 | } |
|---|
| 150 | catch (Exception) |
|---|
| 151 | { |
|---|
| 152 | key.DeleteValue("PluginSettings"); |
|---|
| 153 | MessageBox.Show(S._("Could not load plugin settings. All settings " + |
|---|
| 154 | "have been lost."), S._("Eraser"), MessageBoxButtons.OK, |
|---|
| 155 | MessageBoxIcon.Error); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | protected override void Save() |
|---|
| 161 | { |
|---|
| 162 | RegistryKey key = Application.UserAppDataRegistry; |
|---|
| 163 | key.SetValue("PRNG", ActivePRNG); |
|---|
| 164 | key.SetValue("EraseOnRestart", EraseLockedFilesOnRestart, |
|---|
| 165 | RegistryValueKind.DWord); |
|---|
| 166 | key.SetValue("ConfirmEraseOnRestart", ConfirmEraseOnRestart, |
|---|
| 167 | RegistryValueKind.DWord); |
|---|
| 168 | key.SetValue("DefaultFileErasureMethod", DefaultFileErasureMethod); |
|---|
| 169 | key.SetValue("DefaultUnusedSpaceErasureMethod", |
|---|
| 170 | DefaultUnusedSpaceErasureMethod); |
|---|
| 171 | key.SetValue("ExecuteMissedTasksImmediately", |
|---|
| 172 | ExecuteMissedTasksImmediately, RegistryValueKind.DWord); |
|---|
| 173 | key.SetValue("PlausibleDeniability", PlausibleDeniability, |
|---|
| 174 | RegistryValueKind.DWord); |
|---|
| 175 | key.SetValue("UILanguage", UILanguage); |
|---|
| 176 | |
|---|
| 177 | using (MemoryStream stream = new MemoryStream()) |
|---|
| 178 | { |
|---|
| 179 | new BinaryFormatter().Serialize(stream, pluginSettings); |
|---|
| 180 | key.SetValue("PluginSettings", stream.ToArray(), RegistryValueKind.Binary); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | using (MemoryStream stream = new MemoryStream()) |
|---|
| 184 | { |
|---|
| 185 | new BinaryFormatter().Serialize(stream, PlausibleDeniabilityFiles); |
|---|
| 186 | key.SetValue("PlausibleDeniabilityFiles", stream.ToArray(), RegistryValueKind.Binary); |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | } |
|---|