| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008-2009 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: Kasra Nassiri <cjax@users.sourceforge.net> @10-11-2008 04:18:04 |
|---|
| 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 | using Eraser.Manager; |
|---|
| 31 | using Eraser.Util; |
|---|
| 32 | |
|---|
| 33 | namespace Eraser.DefaultPlugins |
|---|
| 34 | { |
|---|
| 35 | public partial class CustomMethodEditorForm : Form |
|---|
| 36 | { |
|---|
| 37 | public CustomMethodEditorForm() |
|---|
| 38 | { |
|---|
| 39 | InitializeComponent(); |
|---|
| 40 | UXThemeApi.UpdateControlTheme(this); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /// <summary> |
|---|
| 44 | /// Sets or retrieves the CustomErasureMethod object with all the values |
|---|
| 45 | /// in the dialog. |
|---|
| 46 | /// </summary> |
|---|
| 47 | public CustomErasureMethod Method |
|---|
| 48 | { |
|---|
| 49 | get |
|---|
| 50 | { |
|---|
| 51 | if (method == null) |
|---|
| 52 | { |
|---|
| 53 | method = new CustomErasureMethod(); |
|---|
| 54 | method.Guid = Guid.NewGuid(); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | //The method name. |
|---|
| 58 | method.Name = nameTxt.Text; |
|---|
| 59 | |
|---|
| 60 | //Whether passes can be randomized when executing. |
|---|
| 61 | method.RandomizePasses = randomizeChk.Checked; |
|---|
| 62 | |
|---|
| 63 | //And all the passes. |
|---|
| 64 | ErasureMethodPass[] passes = new ErasureMethodPass[passesLv.Items.Count]; |
|---|
| 65 | for (int i = 0; i < passesLv.Items.Count; ++i) |
|---|
| 66 | passes[i] = (ErasureMethodPass)passesLv.Items[i].Tag; |
|---|
| 67 | method.Passes = passes; |
|---|
| 68 | |
|---|
| 69 | return method; |
|---|
| 70 | } |
|---|
| 71 | set |
|---|
| 72 | { |
|---|
| 73 | method = value; |
|---|
| 74 | |
|---|
| 75 | //Method name. |
|---|
| 76 | nameTxt.Text = method.Name; |
|---|
| 77 | |
|---|
| 78 | //Randomize passes |
|---|
| 79 | randomizeChk.Checked = method.RandomizePasses; |
|---|
| 80 | |
|---|
| 81 | //Every pass. |
|---|
| 82 | foreach (ErasureMethodPass pass in method.Passes) |
|---|
| 83 | AddPass(pass); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /// <summary> |
|---|
| 88 | /// Adds the given pass to the displayed list of passes. |
|---|
| 89 | /// </summary> |
|---|
| 90 | /// <param name="pass">The pass to add.</param> |
|---|
| 91 | /// <returns>The item added to the list view.</returns> |
|---|
| 92 | private ListViewItem AddPass(ErasureMethodPass pass) |
|---|
| 93 | { |
|---|
| 94 | ListViewItem item = new ListViewItem((passesLv.Items.Count + 1).ToString( |
|---|
| 95 | CultureInfo.CurrentCulture)); |
|---|
| 96 | item.Tag = pass; |
|---|
| 97 | if (pass.Function == ErasureMethod.WriteRandom) |
|---|
| 98 | item.SubItems.Add(S._("Random Data")); |
|---|
| 99 | else |
|---|
| 100 | item.SubItems.Add(S._("Constant ({0} bytes)", ((byte[])pass.OpaqueValue).Length)); |
|---|
| 101 | |
|---|
| 102 | passesLv.Items.Add(item); |
|---|
| 103 | return item; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | /// <summary> |
|---|
| 107 | /// Saves the currently edited pass details to memory. |
|---|
| 108 | /// </summary> |
|---|
| 109 | private void SavePass(ListViewItem item) |
|---|
| 110 | { |
|---|
| 111 | ErasureMethodPass pass = (ErasureMethodPass)item.Tag; |
|---|
| 112 | if (passEditor.PassType == CustomMethodPassEditorPassType.Random) |
|---|
| 113 | { |
|---|
| 114 | pass.Function = ErasureMethod.WriteRandom; |
|---|
| 115 | pass.OpaqueValue = null; |
|---|
| 116 | item.SubItems[1].Text = S._("Random Data"); |
|---|
| 117 | } |
|---|
| 118 | else |
|---|
| 119 | { |
|---|
| 120 | pass.Function = ErasureMethod.WriteConstant; |
|---|
| 121 | pass.OpaqueValue = passEditor.PassData; |
|---|
| 122 | item.SubItems[1].Text = S._("Constant ({0} bytes)", passEditor.PassData.Length); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /// <summary> |
|---|
| 127 | /// Displays the pass associated with <paramref name="item"/> in the editing controls. |
|---|
| 128 | /// </summary> |
|---|
| 129 | /// <param name="item">The <see cref="ListViewItem"/> containing the pass to edit.</param> |
|---|
| 130 | private void DisplayPass(ListViewItem item) |
|---|
| 131 | { |
|---|
| 132 | currentPass = item; |
|---|
| 133 | ErasureMethodPass pass = (ErasureMethodPass)item.Tag; |
|---|
| 134 | passEditor.PassData = (byte[])pass.OpaqueValue; |
|---|
| 135 | passEditor.PassType = pass.Function == ErasureMethod.WriteRandom ? |
|---|
| 136 | CustomMethodPassEditorPassType.Random : |
|---|
| 137 | CustomMethodPassEditorPassType.Text; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /// <summary> |
|---|
| 141 | /// Renumbers all pass entries' pass number to be in sync with its position. |
|---|
| 142 | /// </summary> |
|---|
| 143 | private void RenumberPasses() |
|---|
| 144 | { |
|---|
| 145 | foreach (ListViewItem item in passesLv.Items) |
|---|
| 146 | item.Text = (item.Index + 1).ToString(CultureInfo.CurrentCulture); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /// <summary> |
|---|
| 150 | /// Enables buttons relevant to the currently selected items. |
|---|
| 151 | /// </summary> |
|---|
| 152 | private void EnableButtons() |
|---|
| 153 | { |
|---|
| 154 | passesRemoveBtn.Enabled = passesDuplicateBtn.Enabled = passesMoveUpBtn.Enabled = |
|---|
| 155 | passesMoveDownBtn.Enabled = passesLv.SelectedItems.Count >= 1; |
|---|
| 156 | passGrp.Enabled = passEditor.Enabled = passesLv.SelectedItems.Count == 1; |
|---|
| 157 | |
|---|
| 158 | ListView.SelectedListViewItemCollection items = passesLv.SelectedItems; |
|---|
| 159 | if (items.Count > 0) |
|---|
| 160 | { |
|---|
| 161 | foreach (ListViewItem item in items) |
|---|
| 162 | { |
|---|
| 163 | int index = item.Index; |
|---|
| 164 | passesMoveUpBtn.Enabled = passesMoveUpBtn.Enabled && index > 0; |
|---|
| 165 | passesMoveDownBtn.Enabled = passesMoveDownBtn.Enabled && index < passesLv.Items.Count - 1; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | private void passesAddBtn_Click(object sender, EventArgs e) |
|---|
| 171 | { |
|---|
| 172 | ErasureMethodPass pass = new ErasureMethodPass(ErasureMethod.WriteRandom, null); |
|---|
| 173 | ListViewItem item = AddPass(pass); |
|---|
| 174 | |
|---|
| 175 | if (passesLv.SelectedIndices.Count > 0) |
|---|
| 176 | { |
|---|
| 177 | item.Remove(); |
|---|
| 178 | passesLv.Items.Insert(passesLv.SelectedIndices[passesLv.SelectedIndices.Count - 1], |
|---|
| 179 | item); |
|---|
| 180 | RenumberPasses(); |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | private void passesRemoveBtn_Click(object sender, EventArgs e) |
|---|
| 185 | { |
|---|
| 186 | foreach (ListViewItem item in passesLv.SelectedItems) |
|---|
| 187 | passesLv.Items.Remove(item); |
|---|
| 188 | |
|---|
| 189 | RenumberPasses(); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | private void passesDuplicateBtn_Click(object sender, EventArgs e) |
|---|
| 193 | { |
|---|
| 194 | foreach (ListViewItem item in passesLv.SelectedItems) |
|---|
| 195 | { |
|---|
| 196 | ErasureMethodPass oldPass = (ErasureMethodPass)item.Tag; |
|---|
| 197 | ErasureMethodPass pass = new ErasureMethodPass( |
|---|
| 198 | oldPass.Function, oldPass.OpaqueValue); |
|---|
| 199 | AddPass(pass); |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | private void passesMoveUpBtn_Click(object sender, EventArgs e) |
|---|
| 204 | { |
|---|
| 205 | //Save the current pass to prevent data loss |
|---|
| 206 | SavePass(currentPass); |
|---|
| 207 | |
|---|
| 208 | foreach (ListViewItem item in passesLv.SelectedItems) |
|---|
| 209 | { |
|---|
| 210 | //Insert the current item into the index before, only if the item has got |
|---|
| 211 | //space to move up! |
|---|
| 212 | int index = item.Index; |
|---|
| 213 | if (index >= 1) |
|---|
| 214 | { |
|---|
| 215 | passesLv.Items.RemoveAt(index); |
|---|
| 216 | passesLv.Items.Insert(index - 1, item); |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | RenumberPasses(); |
|---|
| 221 | EnableButtons(); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | private void passesMoveDownBtn_Click(object sender, EventArgs e) |
|---|
| 225 | { |
|---|
| 226 | //Save the current pass to prevent data loss |
|---|
| 227 | SavePass(currentPass); |
|---|
| 228 | |
|---|
| 229 | ListView.SelectedListViewItemCollection items = passesLv.SelectedItems; |
|---|
| 230 | for (int i = items.Count; i-- != 0; ) |
|---|
| 231 | { |
|---|
| 232 | //Insert the current item into the index after, only if the item has got |
|---|
| 233 | //space to move down. |
|---|
| 234 | ListViewItem item = items[i]; |
|---|
| 235 | int index = item.Index; |
|---|
| 236 | if (index < passesLv.Items.Count - 1) |
|---|
| 237 | { |
|---|
| 238 | passesLv.Items.RemoveAt(index); |
|---|
| 239 | passesLv.Items.Insert(index + 1, item); |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | RenumberPasses(); |
|---|
| 244 | EnableButtons(); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | private void passesLv_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) |
|---|
| 248 | { |
|---|
| 249 | EnableButtons(); |
|---|
| 250 | |
|---|
| 251 | //Determine if we should load or save the pass information |
|---|
| 252 | if (!e.Item.Selected) |
|---|
| 253 | { |
|---|
| 254 | if (e.Item == currentPass) |
|---|
| 255 | SavePass(e.Item); |
|---|
| 256 | } |
|---|
| 257 | else if (passesLv.SelectedIndices.Count == 1) |
|---|
| 258 | { |
|---|
| 259 | DisplayPass(passesLv.SelectedItems[0]); |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | private void okBtn_Click(object sender, EventArgs e) |
|---|
| 264 | { |
|---|
| 265 | //Clear the errorProvider status |
|---|
| 266 | errorProvider.Clear(); |
|---|
| 267 | bool hasError = false; |
|---|
| 268 | |
|---|
| 269 | //Save the currently edited pass. |
|---|
| 270 | if (passesLv.SelectedItems.Count == 1) |
|---|
| 271 | SavePass(passesLv.SelectedItems[0]); |
|---|
| 272 | |
|---|
| 273 | //Validate the information |
|---|
| 274 | if (nameTxt.Text.Length == 0) |
|---|
| 275 | { |
|---|
| 276 | errorProvider.SetError(nameTxt, S._("The name of the custom method cannot be empty.")); |
|---|
| 277 | errorProvider.SetIconPadding(nameTxt, -16); |
|---|
| 278 | hasError = true; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | //Validate all passes |
|---|
| 282 | if (passesLv.Items.Count == 0) |
|---|
| 283 | { |
|---|
| 284 | errorProvider.SetError(passesLv, S._("The method needs to have at least one pass " + |
|---|
| 285 | "defined.")); |
|---|
| 286 | errorProvider.SetIconPadding(passesLv, -16); |
|---|
| 287 | hasError = true; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | //If there are errors, don't close the dialog. |
|---|
| 291 | if (!hasError) |
|---|
| 292 | { |
|---|
| 293 | DialogResult = DialogResult.OK; |
|---|
| 294 | Close(); |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | /// <summary> |
|---|
| 299 | /// Holds the CustomErasureMethod object which client code may set to allow |
|---|
| 300 | /// method editing. |
|---|
| 301 | /// </summary> |
|---|
| 302 | private CustomErasureMethod method; |
|---|
| 303 | |
|---|
| 304 | /// <summary> |
|---|
| 305 | /// Holds the current Erasure pass that is being edited. |
|---|
| 306 | /// </summary> |
|---|
| 307 | private ListViewItem currentPass; |
|---|
| 308 | } |
|---|
| 309 | } |
|---|