| 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.Linq; |
|---|
| 27 | using System.Drawing; |
|---|
| 28 | using System.Text; |
|---|
| 29 | using System.Windows.Forms; |
|---|
| 30 | |
|---|
| 31 | using Eraser.Manager; |
|---|
| 32 | using Eraser.Util; |
|---|
| 33 | using Eraser.Util.ExtensionMethods; |
|---|
| 34 | using System.IO; |
|---|
| 35 | |
|---|
| 36 | namespace Eraser |
|---|
| 37 | { |
|---|
| 38 | public partial class TaskDataSelectionForm : Form |
|---|
| 39 | { |
|---|
| 40 | private class ErasureType |
|---|
| 41 | { |
|---|
| 42 | public ErasureType(ErasureTarget target) |
|---|
| 43 | { |
|---|
| 44 | Target = target; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public override string ToString() |
|---|
| 48 | { |
|---|
| 49 | return Target.Name; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | public ErasureTarget Target; |
|---|
| 53 | |
|---|
| 54 | /// <summary> |
|---|
| 55 | /// The configurer returned by the active erasure target type. |
|---|
| 56 | /// </summary> |
|---|
| 57 | public IErasureTargetConfigurer Configurer; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | public TaskDataSelectionForm() |
|---|
| 61 | { |
|---|
| 62 | //Create the UI |
|---|
| 63 | InitializeComponent(); |
|---|
| 64 | Theming.ApplyTheme(this); |
|---|
| 65 | |
|---|
| 66 | //Insert the types of erasure targets |
|---|
| 67 | foreach (ErasureTarget target in ManagerLibrary.Instance.ErasureTargetRegistrar) |
|---|
| 68 | typeCmb.Items.Add(new ErasureType(target)); |
|---|
| 69 | if (typeCmb.Items.Count != 0) |
|---|
| 70 | typeCmb.SelectedIndex = 0; |
|---|
| 71 | |
|---|
| 72 | //And the methods list |
|---|
| 73 | methodCmb.Items.Add(ErasureMethodRegistrar.Default); |
|---|
| 74 | foreach (ErasureMethod method in ManagerLibrary.Instance.ErasureMethodRegistrar) |
|---|
| 75 | methodCmb.Items.Add(method); |
|---|
| 76 | if (methodCmb.Items.Count != 0) |
|---|
| 77 | methodCmb.SelectedIndex = 0; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /// <summary> |
|---|
| 81 | /// Retrieves the settings on the property page as the Eraser Manager API equivalent. |
|---|
| 82 | /// </summary> |
|---|
| 83 | /// <returns>An Eraser.Manager.Task.Data or Eraser.Manager.Task.UnusedSpace object |
|---|
| 84 | /// or any of its inherited classes, depending on the task selected</returns> |
|---|
| 85 | public ErasureTarget Target |
|---|
| 86 | { |
|---|
| 87 | get |
|---|
| 88 | { |
|---|
| 89 | ErasureType type = (ErasureType)typeCmb.SelectedItem; |
|---|
| 90 | ErasureTarget result = type.Target; |
|---|
| 91 | if (type.Configurer != null) |
|---|
| 92 | type.Configurer.SaveTo(result); |
|---|
| 93 | result.Method = (ErasureMethod)methodCmb.SelectedItem; |
|---|
| 94 | |
|---|
| 95 | return result; |
|---|
| 96 | } |
|---|
| 97 | set |
|---|
| 98 | { |
|---|
| 99 | //Set the erasure method. |
|---|
| 100 | foreach (object item in methodCmb.Items) |
|---|
| 101 | if (((ErasureMethod)item).Guid == value.Method.Guid) |
|---|
| 102 | methodCmb.SelectedItem = item; |
|---|
| 103 | |
|---|
| 104 | //Set the active erasure type. |
|---|
| 105 | foreach (ErasureType type in typeCmb.Items) |
|---|
| 106 | { |
|---|
| 107 | if (type.Target.GetType() == value.GetType()) |
|---|
| 108 | { |
|---|
| 109 | type.Target = value; |
|---|
| 110 | type.Configurer = value.Configurer; |
|---|
| 111 | if (type.Configurer != null) |
|---|
| 112 | type.Configurer.LoadFrom(value); |
|---|
| 113 | |
|---|
| 114 | typeCmb.SelectedItem = type; |
|---|
| 115 | typeCmb_SelectedIndexChanged(typeCmb, EventArgs.Empty); |
|---|
| 116 | break; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | private void typeCmb_SelectedIndexChanged(object sender, EventArgs e) |
|---|
| 123 | { |
|---|
| 124 | //Remove the old controls |
|---|
| 125 | while (typeSettingsPnl.Controls.Count > 0) |
|---|
| 126 | typeSettingsPnl.Controls.RemoveAt(0); |
|---|
| 127 | |
|---|
| 128 | //Then add in the new configurer |
|---|
| 129 | ErasureType type = (ErasureType)typeCmb.SelectedItem; |
|---|
| 130 | if (type.Configurer == null) |
|---|
| 131 | type.Configurer = type.Target.Configurer; |
|---|
| 132 | |
|---|
| 133 | if (type.Configurer == null || !(type.Configurer is Control)) |
|---|
| 134 | { |
|---|
| 135 | Label label = new Label(); |
|---|
| 136 | label.Text = S._("(This erasure type does not have any settings to define.)"); |
|---|
| 137 | label.Dock = DockStyle.Fill; |
|---|
| 138 | typeSettingsPnl.Controls.Add(label); |
|---|
| 139 | return; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | Control control = type.Configurer as Control; |
|---|
| 143 | typeSettingsPnl.Controls.Add(control); |
|---|
| 144 | control.Dock = DockStyle.Fill; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | private void ok_Click(object sender, EventArgs e) |
|---|
| 148 | { |
|---|
| 149 | ErasureType type = (ErasureType)typeCmb.SelectedItem; |
|---|
| 150 | if (methodCmb.SelectedItem != ErasureMethodRegistrar.Default && |
|---|
| 151 | !type.Target.SupportsMethod((ErasureMethod)methodCmb.SelectedItem)) |
|---|
| 152 | { |
|---|
| 153 | errorProvider.SetError(methodCmb, S._("The erasure method selected does " + |
|---|
| 154 | "not support unused disk space erasures.")); |
|---|
| 155 | } |
|---|
| 156 | else if (type.Configurer == null || type.Configurer.SaveTo(type.Target)) |
|---|
| 157 | { |
|---|
| 158 | errorProvider.Clear(); |
|---|
| 159 | DialogResult = DialogResult.OK; |
|---|
| 160 | Close(); |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | } |
|---|