| [1359] | 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| [1675] | 3 | * Copyright 2008-2010 The Eraser Project |
|---|
| [1359] | 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; |
|---|
| [1802] | 24 | using System.Linq; |
|---|
| [1359] | 25 | using System.Drawing; |
|---|
| 26 | using System.Text; |
|---|
| 27 | using System.Windows.Forms; |
|---|
| 28 | |
|---|
| 29 | using Eraser.Manager; |
|---|
| 30 | using Eraser.Util; |
|---|
| 31 | using System.Globalization; |
|---|
| 32 | |
|---|
| 33 | namespace Eraser |
|---|
| 34 | { |
|---|
| 35 | public partial class ProgressForm : Form |
|---|
| 36 | { |
|---|
| 37 | private Task task; |
|---|
| 38 | private DateTime lastUpdate; |
|---|
| 39 | |
|---|
| 40 | public ProgressForm(Task task) |
|---|
| 41 | { |
|---|
| 42 | InitializeComponent(); |
|---|
| [1802] | 43 | Theming.ApplyTheme(this); |
|---|
| [1359] | 44 | this.task = task; |
|---|
| 45 | this.lastUpdate = DateTime.Now; |
|---|
| 46 | this.ActiveControl = hide; |
|---|
| 47 | |
|---|
| 48 | //Register the event handlers |
|---|
| 49 | jobTitle.Text = task.UIText; |
|---|
| 50 | task.ProgressChanged += task_ProgressChanged; |
|---|
| 51 | task.TaskFinished += task_TaskFinished; |
|---|
| [1515] | 52 | |
|---|
| 53 | //Set the current progress |
|---|
| 54 | if (task.Progress.CurrentStep != null) |
|---|
| [2190] | 55 | UpdateProgress(task.Progress.CurrentStep.Progress, |
|---|
| [1515] | 56 | new ProgressChangedEventArgs(task.Progress.CurrentStep.Progress, null)); |
|---|
| [1359] | 57 | } |
|---|
| 58 | |
|---|
| 59 | private void ProgressForm_FormClosed(object sender, FormClosedEventArgs e) |
|---|
| 60 | { |
|---|
| 61 | task.ProgressChanged -= task_ProgressChanged; |
|---|
| 62 | task.TaskFinished -= task_TaskFinished; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| [1515] | 65 | private void task_ProgressChanged(object sender, ProgressChangedEventArgs e) |
|---|
| [1359] | 66 | { |
|---|
| [1741] | 67 | if (IsDisposed || !IsHandleCreated) |
|---|
| [1660] | 68 | return; |
|---|
| [1359] | 69 | if (InvokeRequired) |
|---|
| 70 | { |
|---|
| 71 | //Don't update too often - we can slow down the code. |
|---|
| 72 | if (DateTime.Now - lastUpdate < new TimeSpan(0, 0, 0, 0, 300)) |
|---|
| 73 | return; |
|---|
| 74 | |
|---|
| 75 | lastUpdate = DateTime.Now; |
|---|
| [1515] | 76 | Invoke((EventHandler<ProgressChangedEventArgs>)task_ProgressChanged, sender, e); |
|---|
| [1359] | 77 | return; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| [1515] | 80 | ErasureTarget target = sender as ErasureTarget; |
|---|
| 81 | if (target == null) |
|---|
| 82 | return; |
|---|
| [1359] | 83 | |
|---|
| [2190] | 84 | UpdateProgress(target.Progress, e); |
|---|
| [1359] | 85 | } |
|---|
| 86 | |
|---|
| [1802] | 87 | private void task_TaskFinished(object sender, EventArgs e) |
|---|
| [1359] | 88 | { |
|---|
| [1741] | 89 | if (IsDisposed || !IsHandleCreated) |
|---|
| [1660] | 90 | return; |
|---|
| [1359] | 91 | if (InvokeRequired) |
|---|
| 92 | { |
|---|
| [1802] | 93 | Invoke((EventHandler)task_TaskFinished, sender, e); |
|---|
| [1359] | 94 | return; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | //Update the UI. Set everything to 100% |
|---|
| [1802] | 98 | Task task = (Task)sender; |
|---|
| [1359] | 99 | timeLeft.Text = item.Text = pass.Text = string.Empty; |
|---|
| 100 | overallProgressLbl.Text = S._("Total: {0,2:#0.00%}", 1.0); |
|---|
| 101 | overallProgress.Value = overallProgress.Maximum; |
|---|
| 102 | itemProgressLbl.Text = "100%"; |
|---|
| 103 | itemProgress.Style = ProgressBarStyle.Continuous; |
|---|
| 104 | itemProgress.Value = itemProgress.Maximum; |
|---|
| 105 | |
|---|
| 106 | //Inform the user on the status of the task. |
|---|
| [1802] | 107 | LogLevel highestLevel = task.Log.Last().Highest; |
|---|
| [1359] | 108 | switch (highestLevel) |
|---|
| 109 | { |
|---|
| 110 | case LogLevel.Warning: |
|---|
| 111 | status.Text = S._("Completed with warnings"); |
|---|
| 112 | break; |
|---|
| 113 | case LogLevel.Error: |
|---|
| 114 | status.Text = S._("Completed with errors"); |
|---|
| 115 | break; |
|---|
| 116 | case LogLevel.Fatal: |
|---|
| 117 | status.Text = S._("Not completed"); |
|---|
| 118 | break; |
|---|
| 119 | default: |
|---|
| 120 | status.Text = S._("Completed"); |
|---|
| 121 | break; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | //Change the Stop button to be a Close button and the Hide button |
|---|
| 125 | //to be disabled |
|---|
| 126 | hide.Enabled = false; |
|---|
| 127 | stop.Text = S._("Close"); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | private void hide_Click(object sender, EventArgs e) |
|---|
| 131 | { |
|---|
| 132 | Close(); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | private void stop_Click(object sender, EventArgs e) |
|---|
| 136 | { |
|---|
| 137 | if (task.Executing) |
|---|
| 138 | task.Cancel(); |
|---|
| 139 | Close(); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| [2190] | 142 | private void UpdateProgress(ProgressManagerBase targetProgress, ProgressChangedEventArgs e) |
|---|
| [1515] | 143 | { |
|---|
| 144 | TaskProgressChangedEventArgs e2 = (TaskProgressChangedEventArgs)e.UserState; |
|---|
| [2190] | 145 | { |
|---|
| 146 | SteppedProgressManager targetProgressStepped = |
|---|
| 147 | targetProgress as SteppedProgressManager; |
|---|
| 148 | if (targetProgressStepped != null && targetProgressStepped.CurrentStep != null) |
|---|
| 149 | status.Text = targetProgressStepped.CurrentStep.Name; |
|---|
| 150 | else |
|---|
| 151 | status.Text = S._("Erasing..."); |
|---|
| 152 | } |
|---|
| [1515] | 153 | |
|---|
| 154 | if (e2 != null) |
|---|
| 155 | { |
|---|
| 156 | item.Text = WrapItemName(e2.ItemName); |
|---|
| 157 | pass.Text = e2.ItemTotalPasses != 0 ? |
|---|
| 158 | S._("{0} out of {1}", e2.ItemPass, e2.ItemTotalPasses) : |
|---|
| 159 | e2.ItemPass.ToString(CultureInfo.CurrentCulture); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | if (targetProgress.TimeLeft >= TimeSpan.Zero) |
|---|
| 163 | timeLeft.Text = S._("About {0} left", RoundToSeconds(targetProgress.TimeLeft)); |
|---|
| 164 | else |
|---|
| 165 | timeLeft.Text = S._("Unknown"); |
|---|
| 166 | |
|---|
| [2203] | 167 | if (!targetProgress.ProgressIndeterminate) |
|---|
| [1515] | 168 | { |
|---|
| 169 | itemProgress.Style = ProgressBarStyle.Continuous; |
|---|
| 170 | itemProgress.Value = (int)(targetProgress.Progress * 1000); |
|---|
| 171 | itemProgressLbl.Text = targetProgress.Progress.ToString("#0%", |
|---|
| 172 | CultureInfo.CurrentCulture); |
|---|
| 173 | } |
|---|
| 174 | else |
|---|
| 175 | { |
|---|
| 176 | itemProgress.Style = ProgressBarStyle.Marquee; |
|---|
| 177 | itemProgressLbl.Text = string.Empty; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| [2203] | 180 | if (!task.Progress.ProgressIndeterminate) |
|---|
| 181 | { |
|---|
| 182 | overallProgress.Style = ProgressBarStyle.Continuous; |
|---|
| 183 | overallProgress.Value = (int)(task.Progress.Progress * 1000); |
|---|
| 184 | overallProgressLbl.Text = S._("Total: {0,2:#0.00%}", task.Progress.Progress); |
|---|
| 185 | } |
|---|
| 186 | else |
|---|
| 187 | { |
|---|
| 188 | overallProgress.Style = ProgressBarStyle.Marquee; |
|---|
| 189 | overallProgressLbl.Text = S._("Total: Unknown"); |
|---|
| 190 | } |
|---|
| [1515] | 191 | } |
|---|
| 192 | |
|---|
| [1359] | 193 | private string WrapItemName(string itemName) |
|---|
| 194 | { |
|---|
| 195 | StringBuilder result = new StringBuilder(itemName.Length); |
|---|
| [1660] | 196 | using (Graphics g = item.CreateGraphics()) |
|---|
| [1359] | 197 | { |
|---|
| [1660] | 198 | //Split the long file name into lines which fit into the width of the label |
|---|
| 199 | while (itemName.Length > 0) |
|---|
| [1359] | 200 | { |
|---|
| [1660] | 201 | int chars = 0; |
|---|
| 202 | int lines = 0; |
|---|
| 203 | g.MeasureString(itemName, item.Font, new SizeF(item.Width - 2, 15), |
|---|
| 204 | StringFormat.GenericDefault, out chars, out lines); |
|---|
| [1359] | 205 | |
|---|
| [1660] | 206 | result.AppendLine(itemName.Substring(0, chars)); |
|---|
| 207 | itemName = itemName.Remove(0, chars); |
|---|
| [1359] | 208 | } |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | return result.ToString(); |
|---|
| 212 | } |
|---|
| [1515] | 213 | |
|---|
| [1802] | 214 | private static TimeSpan RoundToSeconds(TimeSpan span) |
|---|
| [1515] | 215 | { |
|---|
| 216 | return new TimeSpan(span.Ticks - span.Ticks % TimeSpan.TicksPerSecond); |
|---|
| 217 | } |
|---|
| [1359] | 218 | } |
|---|
| 219 | } |
|---|