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