| 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.ComponentModel; |
|---|
| 25 | using System.Data; |
|---|
| 26 | using System.Drawing; |
|---|
| 27 | using System.Text; |
|---|
| 28 | using System.Windows.Forms; |
|---|
| 29 | using System.Globalization; |
|---|
| 30 | |
|---|
| 31 | using Eraser.Util; |
|---|
| 32 | using Eraser.Plugins; |
|---|
| 33 | using Eraser.Plugins.ExtensionPoints; |
|---|
| 34 | |
|---|
| 35 | namespace Eraser.DefaultPlugins |
|---|
| 36 | { |
|---|
| 37 | public partial class SettingsForm : Form |
|---|
| 38 | { |
|---|
| 39 | public SettingsForm() |
|---|
| 40 | { |
|---|
| 41 | InitializeComponent(); |
|---|
| 42 | Theming.ApplyTheme(this); |
|---|
| 43 | |
|---|
| 44 | //Populate the list of erasure passes, except the FL16KB. |
|---|
| 45 | foreach (ErasureMethod method in Host.Instance.ErasureMethods) |
|---|
| 46 | if (method.Guid != typeof(FirstLast16KB).GUID) |
|---|
| 47 | fl16MethodCmb.Items.Add(method); |
|---|
| 48 | |
|---|
| 49 | //Load the settings. |
|---|
| 50 | DefaultPluginSettings settings = DefaultPlugin.Settings; |
|---|
| 51 | if (settings.FL16Method != Guid.Empty) |
|---|
| 52 | foreach (object item in fl16MethodCmb.Items) |
|---|
| 53 | if (((ErasureMethod)item).Guid == settings.FL16Method) |
|---|
| 54 | { |
|---|
| 55 | fl16MethodCmb.SelectedItem = item; |
|---|
| 56 | break; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | if (fl16MethodCmb.SelectedIndex == -1) |
|---|
| 60 | { |
|---|
| 61 | Guid methodGuid = |
|---|
| 62 | DefaultPlugin.Settings.DefaultFileErasureMethod; |
|---|
| 63 | if (methodGuid == typeof(FirstLast16KB).GUID) |
|---|
| 64 | methodGuid = typeof(Gutmann).GUID; |
|---|
| 65 | |
|---|
| 66 | foreach (object item in fl16MethodCmb.Items) |
|---|
| 67 | if (((ErasureMethod)item).Guid == methodGuid) |
|---|
| 68 | { |
|---|
| 69 | fl16MethodCmb.SelectedItem = item; |
|---|
| 70 | break; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | if (DefaultPlugin.Settings.EraseCustom != null) |
|---|
| 75 | { |
|---|
| 76 | customMethods = new Dictionary<Guid,CustomErasureMethod>( |
|---|
| 77 | DefaultPlugin.Settings.EraseCustom); |
|---|
| 78 | |
|---|
| 79 | //Display the whole set on the list. |
|---|
| 80 | foreach (Guid guid in customMethods.Keys) |
|---|
| 81 | AddMethod(customMethods[guid]); |
|---|
| 82 | } |
|---|
| 83 | else |
|---|
| 84 | customMethods = new Dictionary<Guid, CustomErasureMethod>(); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | private void customMethod_ItemActivate(object sender, EventArgs e) |
|---|
| 88 | { |
|---|
| 89 | //Create the dialog |
|---|
| 90 | CustomMethodEditorForm editorForm = new CustomMethodEditorForm(); |
|---|
| 91 | ListViewItem item = customMethod.SelectedItems[0]; |
|---|
| 92 | editorForm.Method = (CustomErasureMethod)item.Tag; |
|---|
| 93 | |
|---|
| 94 | if (editorForm.ShowDialog() == DialogResult.OK) |
|---|
| 95 | { |
|---|
| 96 | //Remove the old definition of the erasure method |
|---|
| 97 | CustomErasureMethod method = editorForm.Method; |
|---|
| 98 | if (customMethods.ContainsKey(method.Guid) && |
|---|
| 99 | removeCustomMethods.IndexOf(method.Guid) == -1) |
|---|
| 100 | { |
|---|
| 101 | removeCustomMethods.Add(method.Guid); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | //Add the new definition |
|---|
| 105 | foreach (CustomErasureMethod addMethod in addCustomMethods) |
|---|
| 106 | { |
|---|
| 107 | if (addMethod.Guid == method.Guid) |
|---|
| 108 | { |
|---|
| 109 | addCustomMethods.Remove(addMethod); |
|---|
| 110 | break; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | addCustomMethods.Add(method); |
|---|
| 115 | item.Tag = method; |
|---|
| 116 | UpdateMethod(item); |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | private void customMethodAdd_Click(object sender, EventArgs e) |
|---|
| 121 | { |
|---|
| 122 | CustomMethodEditorForm form = new CustomMethodEditorForm(); |
|---|
| 123 | if (form.ShowDialog() == DialogResult.OK) |
|---|
| 124 | { |
|---|
| 125 | CustomErasureMethod method = form.Method; |
|---|
| 126 | addCustomMethods.Add(method); |
|---|
| 127 | AddMethod(method); |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | private void customMethodContextMenuStrip_Opening(object sender, CancelEventArgs e) |
|---|
| 132 | { |
|---|
| 133 | e.Cancel = customMethod.SelectedIndices.Count == 0; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | private void deleteMethodToolStripMenuItem_Click(object sender, EventArgs e) |
|---|
| 137 | { |
|---|
| 138 | foreach (ListViewItem item in customMethod.SelectedItems) |
|---|
| 139 | { |
|---|
| 140 | CustomErasureMethod method = (CustomErasureMethod)item.Tag; |
|---|
| 141 | if (addCustomMethods.IndexOf(method) >= 0) |
|---|
| 142 | addCustomMethods.Remove(method); |
|---|
| 143 | else |
|---|
| 144 | removeCustomMethods.Add(method.Guid); |
|---|
| 145 | customMethod.Items.Remove(item); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | private void okBtn_Click(object sender, EventArgs e) |
|---|
| 150 | { |
|---|
| 151 | //Save the settings to the settings dictionary |
|---|
| 152 | if (fl16MethodCmb.SelectedIndex == -1) |
|---|
| 153 | { |
|---|
| 154 | errorProvider.SetError(fl16MethodCmb, S._("An invalid erasure method was selected.")); |
|---|
| 155 | return; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | DefaultPlugin.Settings.FL16Method = ((ErasureMethod)fl16MethodCmb.SelectedItem).Guid; |
|---|
| 159 | |
|---|
| 160 | //Remove the old methods. |
|---|
| 161 | foreach (Guid guid in removeCustomMethods) |
|---|
| 162 | { |
|---|
| 163 | customMethods.Remove(guid); |
|---|
| 164 | ManagerLibrary.Instance.ErasureMethodRegistrar.Remove(guid); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | //Update the Erasure method manager on the methods |
|---|
| 168 | foreach (CustomErasureMethod method in addCustomMethods) |
|---|
| 169 | { |
|---|
| 170 | customMethods.Add(method.Guid, method); |
|---|
| 171 | ManagerLibrary.Instance.ErasureMethodRegistrar.Add(new EraseCustom(method)); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | //Save the list of custom erasure methods |
|---|
| 175 | DefaultPlugin.Settings.EraseCustom = customMethods; |
|---|
| 176 | |
|---|
| 177 | //Close the dialog |
|---|
| 178 | DialogResult = DialogResult.OK; |
|---|
| 179 | Close(); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /// <summary> |
|---|
| 183 | /// Adds the given method to the custom methods list. |
|---|
| 184 | /// </summary> |
|---|
| 185 | /// <param name="method">The method to add.</param> |
|---|
| 186 | private void AddMethod(CustomErasureMethod method) |
|---|
| 187 | { |
|---|
| 188 | ListViewItem item = customMethod.Items.Add(method.Name); |
|---|
| 189 | item.SubItems.Add(method.Passes.Length.ToString(CultureInfo.CurrentCulture)); |
|---|
| 190 | item.Tag = method; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | /// <summary> |
|---|
| 194 | /// Updates the UI which represents the given custom erasure method. |
|---|
| 195 | /// </summary> |
|---|
| 196 | /// <param name="item">The method to update.</param> |
|---|
| 197 | private static void UpdateMethod(ListViewItem item) |
|---|
| 198 | { |
|---|
| 199 | CustomErasureMethod method = (CustomErasureMethod)item.Tag; |
|---|
| 200 | item.Text = method.Name; |
|---|
| 201 | item.SubItems[1].Text = method.Passes.Length.ToString(CultureInfo.CurrentCulture); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | private Dictionary<Guid, CustomErasureMethod> customMethods; |
|---|
| 205 | private List<CustomErasureMethod> addCustomMethods = new List<CustomErasureMethod>(); |
|---|
| 206 | private List<Guid> removeCustomMethods = new List<Guid>(); |
|---|
| 207 | } |
|---|
| 208 | } |
|---|