| 1 | using System; |
|---|
| 2 | using System.Collections.Generic; |
|---|
| 3 | using System.Windows.Forms; |
|---|
| 4 | |
|---|
| 5 | using Eraser.Manager; |
|---|
| 6 | using Eraser.Util; |
|---|
| 7 | using Microsoft.Win32; |
|---|
| 8 | using System.IO; |
|---|
| 9 | using System.Runtime.Serialization.Formatters.Binary; |
|---|
| 10 | using System.Globalization; |
|---|
| 11 | |
|---|
| 12 | namespace Eraser |
|---|
| 13 | { |
|---|
| 14 | static class Program |
|---|
| 15 | { |
|---|
| 16 | /// <summary> |
|---|
| 17 | /// The main entry point for the application. |
|---|
| 18 | /// </summary> |
|---|
| 19 | [STAThread] |
|---|
| 20 | static void Main() |
|---|
| 21 | { |
|---|
| 22 | Application.EnableVisualStyles(); |
|---|
| 23 | Application.SetCompatibleTextRenderingDefault(false); |
|---|
| 24 | Application.SafeTopLevelCaptionFormat = S._("Eraser"); |
|---|
| 25 | |
|---|
| 26 | using (ManagerLibrary library = new ManagerLibrary()) |
|---|
| 27 | using (eraserClient = new DirectExecutor()) |
|---|
| 28 | { |
|---|
| 29 | //Set the defaults for the library |
|---|
| 30 | library.Settings = new Settings(); |
|---|
| 31 | |
|---|
| 32 | //Load the task list |
|---|
| 33 | RegistryKey key = Application.UserAppDataRegistry; |
|---|
| 34 | byte[] savedTaskList = (byte[])key.GetValue("TaskList", new byte[] { }); |
|---|
| 35 | using (MemoryStream stream = new MemoryStream(savedTaskList)) |
|---|
| 36 | { |
|---|
| 37 | try |
|---|
| 38 | { |
|---|
| 39 | if (savedTaskList.Length != 0) |
|---|
| 40 | eraserClient.LoadTaskList(stream); |
|---|
| 41 | } |
|---|
| 42 | catch (Exception) |
|---|
| 43 | { |
|---|
| 44 | key.DeleteValue("TaskList"); |
|---|
| 45 | MessageBox.Show(S._("Could not load task list. All task entries have " + |
|---|
| 46 | "been lost."), S._("Eraser"), MessageBoxButtons.OK, |
|---|
| 47 | MessageBoxIcon.Error); |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | //Create the main form |
|---|
| 52 | MainForm form = new MainForm(); |
|---|
| 53 | |
|---|
| 54 | //Run tasks which are meant to be run on restart |
|---|
| 55 | int restartPos = Environment.CommandLine.ToLower().IndexOf("restart"); |
|---|
| 56 | if ((restartPos > 1 && |
|---|
| 57 | Environment.CommandLine[restartPos - 1] == '/') || |
|---|
| 58 | (restartPos > 2 && |
|---|
| 59 | Environment.CommandLine[restartPos - 1] == '-' && |
|---|
| 60 | Environment.CommandLine[restartPos - 2] == '-')) |
|---|
| 61 | { |
|---|
| 62 | eraserClient.QueueRestartTasks(); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | //Run the program |
|---|
| 66 | Application.Run(form); |
|---|
| 67 | |
|---|
| 68 | //Save the task list |
|---|
| 69 | using (MemoryStream stream = new MemoryStream()) |
|---|
| 70 | { |
|---|
| 71 | eraserClient.SaveTaskList(stream); |
|---|
| 72 | key.SetValue("TaskList", stream.ToArray(), RegistryValueKind.Binary); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | public static DirectExecutor eraserClient; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public class Settings : Manager.Settings |
|---|
| 81 | { |
|---|
| 82 | public Settings() |
|---|
| 83 | { |
|---|
| 84 | RegistryKey key = Application.UserAppDataRegistry; |
|---|
| 85 | ActivePRNG = new Guid((string) |
|---|
| 86 | key.GetValue("PRNG", Guid.Empty.ToString())); |
|---|
| 87 | EraseLockedFilesOnRestart = |
|---|
| 88 | (int)key.GetValue("EraseOnRestart", (object)1) != 0; |
|---|
| 89 | ConfirmEraseOnRestart = |
|---|
| 90 | (int)key.GetValue("ConfirmEraseOnRestart", (object)0) != 0; |
|---|
| 91 | DefaultFileErasureMethod = new Guid((string) |
|---|
| 92 | key.GetValue("DefaultFileErasureMethod", Guid.Empty.ToString())); |
|---|
| 93 | DefaultUnusedSpaceErasureMethod = new Guid((string) |
|---|
| 94 | key.GetValue("DefaultUnusedSpaceErasureMethod", Guid.Empty.ToString())); |
|---|
| 95 | ExecuteMissedTasksImmediately = |
|---|
| 96 | (int)key.GetValue("ExecuteMissedTasksImmediately", (object)1) != 0; |
|---|
| 97 | PlausibleDeniability = |
|---|
| 98 | (int)key.GetValue("PlausibleDeniability", (object)1) != 0; |
|---|
| 99 | UILanguage = (string)key.GetValue("UILanguage", string.Empty); |
|---|
| 100 | S.Language = new CultureInfo(UILanguage); |
|---|
| 101 | |
|---|
| 102 | //Load the plugin settings. |
|---|
| 103 | byte[] pluginSettings = (byte[])key.GetValue("PluginSettings", new byte[] { }); |
|---|
| 104 | if (pluginSettings.Length != 0) |
|---|
| 105 | using (MemoryStream stream = new MemoryStream(pluginSettings)) |
|---|
| 106 | { |
|---|
| 107 | try |
|---|
| 108 | { |
|---|
| 109 | this.pluginSettings = (Dictionary<Guid, Dictionary<string, object>>) |
|---|
| 110 | new BinaryFormatter().Deserialize(stream); |
|---|
| 111 | } |
|---|
| 112 | catch (Exception) |
|---|
| 113 | { |
|---|
| 114 | key.DeleteValue("PluginSettings"); |
|---|
| 115 | MessageBox.Show(S._("Could not load plugin settings. All settings " + |
|---|
| 116 | "have been lost."), S._("Eraser"), MessageBoxButtons.OK, |
|---|
| 117 | MessageBoxIcon.Error); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | ~Settings() |
|---|
| 123 | { |
|---|
| 124 | RegistryKey key = Application.UserAppDataRegistry; |
|---|
| 125 | key.SetValue("PRNG", ActivePRNG); |
|---|
| 126 | key.SetValue("EraseOnRestart", EraseLockedFilesOnRestart, |
|---|
| 127 | RegistryValueKind.DWord); |
|---|
| 128 | key.SetValue("ConfirmEraseOnRestart", ConfirmEraseOnRestart, |
|---|
| 129 | RegistryValueKind.DWord); |
|---|
| 130 | key.SetValue("DefaultFileErasureMethod", DefaultFileErasureMethod); |
|---|
| 131 | key.SetValue("DefaultUnusedSpaceErasureMethod", |
|---|
| 132 | DefaultUnusedSpaceErasureMethod); |
|---|
| 133 | key.SetValue("ExecuteMissedTasksImmediately", |
|---|
| 134 | ExecuteMissedTasksImmediately, RegistryValueKind.DWord); |
|---|
| 135 | key.SetValue("PlausibleDeniability", PlausibleDeniability, |
|---|
| 136 | RegistryValueKind.DWord); |
|---|
| 137 | key.SetValue("UILanguage", UILanguage); |
|---|
| 138 | |
|---|
| 139 | using (MemoryStream stream = new MemoryStream()) |
|---|
| 140 | { |
|---|
| 141 | new BinaryFormatter().Serialize(stream, pluginSettings); |
|---|
| 142 | key.SetValue("PluginSettings", stream.ToArray(), RegistryValueKind.Binary); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | } |
|---|