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