| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008-2009 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.Drawing; |
|---|
| 27 | using System.Text; |
|---|
| 28 | using System.Windows.Forms; |
|---|
| 29 | |
|---|
| 30 | using Eraser.Manager; |
|---|
| 31 | using Eraser.Util; |
|---|
| 32 | using System.Globalization; |
|---|
| 33 | |
|---|
| 34 | namespace Eraser |
|---|
| 35 | { |
|---|
| 36 | public partial class ProgressForm : Form |
|---|
| 37 | { |
|---|
| 38 | private Task task; |
|---|
| 39 | private DateTime lastUpdate; |
|---|
| 40 | |
|---|
| 41 | public ProgressForm(Task task) |
|---|
| 42 | { |
|---|
| 43 | InitializeComponent(); |
|---|
| 44 | UXThemeApi.UpdateControlTheme(this); |
|---|
| 45 | this.task = task; |
|---|
| 46 | this.lastUpdate = DateTime.Now; |
|---|
| 47 | this.ActiveControl = hide; |
|---|
| 48 | |
|---|
| 49 | //Register the event handlers |
|---|
| 50 | jobTitle.Text = task.UIText; |
|---|
| 51 | task.ProgressChanged += task_ProgressChanged; |
|---|
| 52 | task.TaskFinished += task_TaskFinished; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | private void ProgressForm_FormClosed(object sender, FormClosedEventArgs e) |
|---|
| 56 | { |
|---|
| 57 | task.ProgressChanged -= task_ProgressChanged; |
|---|
| 58 | task.TaskFinished -= task_TaskFinished; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | private void task_ProgressChanged(object sender, TaskProgressEventArgs e) |
|---|
| 62 | { |
|---|
| 63 | if (InvokeRequired) |
|---|
| 64 | { |
|---|
| 65 | //Don't update too often - we can slow down the code. |
|---|
| 66 | if (DateTime.Now - lastUpdate < new TimeSpan(0, 0, 0, 0, 300)) |
|---|
| 67 | return; |
|---|
| 68 | |
|---|
| 69 | lastUpdate = DateTime.Now; |
|---|
| 70 | Invoke(new EventHandler<TaskProgressEventArgs>(task_ProgressChanged), sender, e); |
|---|
| 71 | return; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | status.Text = e.CurrentTargetStatus; |
|---|
| 75 | item.Text = WrapItemName(e.CurrentItemName); |
|---|
| 76 | pass.Text = e.CurrentTargetTotalPasses != 0 ? |
|---|
| 77 | S._("{0} out of {1}", e.CurrentItemPass, e.CurrentTargetTotalPasses) : |
|---|
| 78 | e.CurrentItemPass.ToString(CultureInfo.CurrentCulture); |
|---|
| 79 | |
|---|
| 80 | if (e.TimeLeft >= TimeSpan.Zero) |
|---|
| 81 | timeLeft.Text = S._("About {0:T} left", e.TimeLeft); |
|---|
| 82 | else |
|---|
| 83 | timeLeft.Text = S._("Unknown"); |
|---|
| 84 | |
|---|
| 85 | if (e.CurrentItemProgress >= 0.0f) |
|---|
| 86 | { |
|---|
| 87 | itemProgress.Style = ProgressBarStyle.Continuous; |
|---|
| 88 | itemProgress.Value = (int)(e.CurrentItemProgress * 1000); |
|---|
| 89 | itemProgressLbl.Text = e.CurrentItemProgress.ToString("#0%", |
|---|
| 90 | CultureInfo.CurrentCulture); |
|---|
| 91 | } |
|---|
| 92 | else |
|---|
| 93 | { |
|---|
| 94 | itemProgress.Style = ProgressBarStyle.Marquee; |
|---|
| 95 | itemProgressLbl.Text = string.Empty; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | overallProgress.Value = (int)(e.OverallProgress * 1000); |
|---|
| 99 | overallProgressLbl.Text = S._("Total: {0,2:#0.00%}", e.OverallProgress); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | private void task_TaskFinished(object sender, TaskEventArgs e) |
|---|
| 103 | { |
|---|
| 104 | if (InvokeRequired) |
|---|
| 105 | { |
|---|
| 106 | Invoke(new EventHandler<TaskEventArgs>(task_TaskFinished), sender, e); |
|---|
| 107 | return; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | //Update the UI. Set everything to 100% |
|---|
| 111 | timeLeft.Text = item.Text = pass.Text = string.Empty; |
|---|
| 112 | overallProgressLbl.Text = S._("Total: {0,2:#0.00%}", 1.0); |
|---|
| 113 | overallProgress.Value = overallProgress.Maximum; |
|---|
| 114 | itemProgressLbl.Text = "100%"; |
|---|
| 115 | itemProgress.Style = ProgressBarStyle.Continuous; |
|---|
| 116 | itemProgress.Value = itemProgress.Maximum; |
|---|
| 117 | |
|---|
| 118 | //Inform the user on the status of the task. |
|---|
| 119 | LogLevel highestLevel = LogLevel.Information; |
|---|
| 120 | LogEntryCollection entries = e.Task.Log.LastSessionEntries; |
|---|
| 121 | foreach (LogEntry log in entries) |
|---|
| 122 | if (log.Level > highestLevel) |
|---|
| 123 | highestLevel = log.Level; |
|---|
| 124 | |
|---|
| 125 | switch (highestLevel) |
|---|
| 126 | { |
|---|
| 127 | case LogLevel.Warning: |
|---|
| 128 | status.Text = S._("Completed with warnings"); |
|---|
| 129 | break; |
|---|
| 130 | case LogLevel.Error: |
|---|
| 131 | status.Text = S._("Completed with errors"); |
|---|
| 132 | break; |
|---|
| 133 | case LogLevel.Fatal: |
|---|
| 134 | status.Text = S._("Not completed"); |
|---|
| 135 | break; |
|---|
| 136 | default: |
|---|
| 137 | status.Text = S._("Completed"); |
|---|
| 138 | break; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | //Change the Stop button to be a Close button and the Hide button |
|---|
| 142 | //to be disabled |
|---|
| 143 | hide.Enabled = false; |
|---|
| 144 | stop.Text = S._("Close"); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | private void hide_Click(object sender, EventArgs e) |
|---|
| 148 | { |
|---|
| 149 | Close(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | private void stop_Click(object sender, EventArgs e) |
|---|
| 153 | { |
|---|
| 154 | if (task.Executing) |
|---|
| 155 | task.Cancel(); |
|---|
| 156 | Close(); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | private string WrapItemName(string itemName) |
|---|
| 160 | { |
|---|
| 161 | StringBuilder result = new StringBuilder(itemName.Length); |
|---|
| 162 | |
|---|
| 163 | try |
|---|
| 164 | { |
|---|
| 165 | using (Graphics g = item.CreateGraphics()) |
|---|
| 166 | { |
|---|
| 167 | //Split the long file name into lines which fit into the width of the label |
|---|
| 168 | while (itemName.Length > 0) |
|---|
| 169 | { |
|---|
| 170 | int chars = 0; |
|---|
| 171 | int lines = 0; |
|---|
| 172 | g.MeasureString(itemName, item.Font, new SizeF(item.Width - 2, 15), |
|---|
| 173 | StringFormat.GenericDefault, out chars, out lines); |
|---|
| 174 | |
|---|
| 175 | result.AppendLine(itemName.Substring(0, chars)); |
|---|
| 176 | itemName = itemName.Remove(0, chars); |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | catch (ObjectDisposedException) |
|---|
| 181 | { |
|---|
| 182 | //Called when the user closes the form and the delegate call to Invoke was queued. |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | return result.ToString(); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | } |
|---|