| 1 | using System; |
|---|
| 2 | using System.Collections.Generic; |
|---|
| 3 | using System.ComponentModel; |
|---|
| 4 | using System.Data; |
|---|
| 5 | using System.Drawing; |
|---|
| 6 | using System.Text; |
|---|
| 7 | using System.Windows.Forms; |
|---|
| 8 | |
|---|
| 9 | using Eraser.Manager; |
|---|
| 10 | using Eraser.Util; |
|---|
| 11 | |
|---|
| 12 | namespace Eraser |
|---|
| 13 | { |
|---|
| 14 | public partial class TaskDataSelectionForm : Form |
|---|
| 15 | { |
|---|
| 16 | private class DriveItem |
|---|
| 17 | { |
|---|
| 18 | public override string ToString() |
|---|
| 19 | { |
|---|
| 20 | return Label; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | public string Drive; |
|---|
| 24 | public string Label; |
|---|
| 25 | public Icon Icon; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | public TaskDataSelectionForm() |
|---|
| 29 | { |
|---|
| 30 | //Create the UI |
|---|
| 31 | InitializeComponent(); |
|---|
| 32 | file.Checked = true; |
|---|
| 33 | |
|---|
| 34 | //Populate the drives list |
|---|
| 35 | string[] drives = Environment.GetLogicalDrives(); |
|---|
| 36 | foreach (string drive in drives) |
|---|
| 37 | { |
|---|
| 38 | DriveTypes driveType = Drives.GetDriveType(drive); |
|---|
| 39 | if (driveType != DriveTypes.DRIVE_UNKNOWN && |
|---|
| 40 | driveType != DriveTypes.DRIVE_NO_ROOT_DIR && |
|---|
| 41 | driveType != DriveTypes.DRIVE_CDROM && |
|---|
| 42 | driveType != DriveTypes.DRIVE_REMOTE) |
|---|
| 43 | { |
|---|
| 44 | DriveItem item = new DriveItem(); |
|---|
| 45 | item.Drive = drive.Substring(0, drive.Length - 1); |
|---|
| 46 | item.Label = File.GetFileDescription(item.Drive); |
|---|
| 47 | item.Icon = File.GetFileIcon(item.Drive); |
|---|
| 48 | unusedDisk.Items.Add(item); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | if (unusedDisk.Items.Count != 0) |
|---|
| 53 | unusedDisk.SelectedIndex = 0; |
|---|
| 54 | |
|---|
| 55 | //And the methods list |
|---|
| 56 | Dictionary<Guid, ErasureMethod> methods = ErasureMethodManager.GetAll(); |
|---|
| 57 | this.method.Items.Add(ErasureMethodManager.Default); |
|---|
| 58 | foreach (ErasureMethod method in methods.Values) |
|---|
| 59 | this.method.Items.Add(method); |
|---|
| 60 | if (this.method.Items.Count != 0) |
|---|
| 61 | this.method.SelectedIndex = 0; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /// <summary> |
|---|
| 65 | /// Retrieves the settings on the property page as the Eraser Manager API equivalent. |
|---|
| 66 | /// </summary> |
|---|
| 67 | /// <returns>An Eraser.Manager.Task.Data or Eraser.Manager.Task.UnusedSpace object |
|---|
| 68 | /// or any of its inherited classes, depending on the task selected</returns> |
|---|
| 69 | public Task.ErasureTarget Target |
|---|
| 70 | { |
|---|
| 71 | get |
|---|
| 72 | { |
|---|
| 73 | Task.ErasureTarget result = null; |
|---|
| 74 | if (file.Checked) |
|---|
| 75 | { |
|---|
| 76 | Manager.Task.File fileTask = new Task.File(); |
|---|
| 77 | result = fileTask; |
|---|
| 78 | |
|---|
| 79 | fileTask.Path = filePath.Text; |
|---|
| 80 | } |
|---|
| 81 | else if (folder.Checked) |
|---|
| 82 | { |
|---|
| 83 | Manager.Task.Folder folderTask = new Task.Folder(); |
|---|
| 84 | result = folderTask; |
|---|
| 85 | |
|---|
| 86 | folderTask.Path = folderPath.Text; |
|---|
| 87 | folderTask.IncludeMask = folderInclude.Text; |
|---|
| 88 | folderTask.ExcludeMask = folderExclude.Text; |
|---|
| 89 | folderTask.DeleteIfEmpty = folderDelete.Checked; |
|---|
| 90 | } |
|---|
| 91 | else |
|---|
| 92 | { |
|---|
| 93 | Task.UnusedSpace unusedSpaceTask = new Task.UnusedSpace(); |
|---|
| 94 | result = unusedSpaceTask; |
|---|
| 95 | |
|---|
| 96 | unusedSpaceTask.Drive = ((DriveItem)unusedDisk.SelectedItem).Drive; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | result.Method = (ErasureMethod)this.method.SelectedItem; |
|---|
| 100 | return result; |
|---|
| 101 | } |
|---|
| 102 | set |
|---|
| 103 | { |
|---|
| 104 | //Set the erasure method. |
|---|
| 105 | foreach (object item in method.Items) |
|---|
| 106 | if (item == value.Method) |
|---|
| 107 | method.SelectedItem = item; |
|---|
| 108 | |
|---|
| 109 | //Then the data to be erased. |
|---|
| 110 | if (value is Task.File) |
|---|
| 111 | { |
|---|
| 112 | filePath.Text = ((Task.File)value).Path; |
|---|
| 113 | } |
|---|
| 114 | else if (value is Task.Folder) |
|---|
| 115 | { |
|---|
| 116 | Manager.Task.Folder folderTask = (Task.Folder)value; |
|---|
| 117 | |
|---|
| 118 | folderPath.Text = folderTask.Path; |
|---|
| 119 | folderInclude.Text = folderTask.IncludeMask; |
|---|
| 120 | folderExclude.Text = folderTask.ExcludeMask; |
|---|
| 121 | folderDelete.Checked = folderTask.DeleteIfEmpty; |
|---|
| 122 | } |
|---|
| 123 | else if (value is Task.UnusedSpace) |
|---|
| 124 | { |
|---|
| 125 | Task.UnusedSpace unusedSpaceTask = new Task.UnusedSpace(); |
|---|
| 126 | foreach (object item in unusedDisk.Items) |
|---|
| 127 | if (((DriveItem)item).Drive == ((Task.UnusedSpace)value).Drive) |
|---|
| 128 | unusedDisk.SelectedItem = item; |
|---|
| 129 | } |
|---|
| 130 | else |
|---|
| 131 | throw new NotImplementedException("Unknown erasure target."); |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | private void data_CheckedChanged(object sender, EventArgs e) |
|---|
| 136 | { |
|---|
| 137 | filePath.Enabled = fileBrowse.Enabled = file.Checked; |
|---|
| 138 | folderPath.Enabled = folderBrowse.Enabled = folderIncludeLbl.Enabled = |
|---|
| 139 | folderInclude.Enabled = folderExcludeLbl.Enabled = folderExclude.Enabled = |
|---|
| 140 | folderDelete.Enabled = folder.Checked; |
|---|
| 141 | unusedDisk.Enabled = unused.Checked; |
|---|
| 142 | errorProvider.Clear(); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | private void fileBrowse_Click(object sender, EventArgs e) |
|---|
| 146 | { |
|---|
| 147 | fileDialog.FileName = filePath.Text; |
|---|
| 148 | if (fileDialog.ShowDialog() == DialogResult.OK) |
|---|
| 149 | filePath.Text = fileDialog.FileName; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | private void folderBrowse_Click(object sender, EventArgs e) |
|---|
| 153 | { |
|---|
| 154 | folderDialog.SelectedPath = folderPath.Text; |
|---|
| 155 | if (folderDialog.ShowDialog() == DialogResult.OK) |
|---|
| 156 | folderPath.Text = folderDialog.SelectedPath; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | private void unusedDisk_DrawItem(object sender, DrawItemEventArgs e) |
|---|
| 160 | { |
|---|
| 161 | if (e.Index == -1) |
|---|
| 162 | return; |
|---|
| 163 | |
|---|
| 164 | Graphics g = e.Graphics; |
|---|
| 165 | DriveItem item = (DriveItem)unusedDisk.Items[e.Index]; |
|---|
| 166 | Color textColour = e.ForeColor; |
|---|
| 167 | PointF textPos = e.Bounds.Location; |
|---|
| 168 | textPos.X += item.Icon.Width + 4; |
|---|
| 169 | textPos.Y += 2; |
|---|
| 170 | |
|---|
| 171 | //Set the text colour and background colour if the control is disabled |
|---|
| 172 | if ((e.State & DrawItemState.Disabled) == 0) |
|---|
| 173 | e.DrawBackground(); |
|---|
| 174 | else |
|---|
| 175 | { |
|---|
| 176 | g.FillRectangle(new SolidBrush(SystemColors.ButtonFace), e.Bounds); |
|---|
| 177 | textColour = SystemColors.GrayText; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | g.DrawIcon(item.Icon, e.Bounds.X + 2, e.Bounds.Y); |
|---|
| 181 | g.DrawString(item.Label, e.Font, new SolidBrush(textColour), textPos); |
|---|
| 182 | if ((e.State & DrawItemState.Focus) != 0) |
|---|
| 183 | e.DrawFocusRectangle(); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | private void ok_Click(object sender, EventArgs e) |
|---|
| 187 | { |
|---|
| 188 | if (file.Checked && filePath.Text.Length == 0) |
|---|
| 189 | errorProvider.SetError(filePath, "Invalid file path"); |
|---|
| 190 | else if (folder.Checked && folderPath.Text.Length == 0) |
|---|
| 191 | errorProvider.SetError(folderPath, "Invalid folder path"); |
|---|
| 192 | else |
|---|
| 193 | { |
|---|
| 194 | errorProvider.Clear(); |
|---|
| 195 | DialogResult = DialogResult.OK; |
|---|
| 196 | Close(); |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | } |
|---|