| 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 | using System.IO; |
|---|
| 30 | |
|---|
| 31 | using Eraser.Util; |
|---|
| 32 | |
|---|
| 33 | using ProgressChangedEventArgs = System.ComponentModel.ProgressChangedEventArgs; |
|---|
| 34 | using ProgressChangedEventHandler = System.ComponentModel.ProgressChangedEventHandler; |
|---|
| 35 | using EraserProgressChangedEventArgs = Eraser.Util.ProgressChangedEventArgs; |
|---|
| 36 | using EraserProgressChangedEventHandler = Eraser.Util.ProgressChangedEventHandler; |
|---|
| 37 | |
|---|
| 38 | namespace Eraser |
|---|
| 39 | { |
|---|
| 40 | public partial class BlackBoxUploadForm : Form |
|---|
| 41 | { |
|---|
| 42 | /// <summary> |
|---|
| 43 | /// Constructor. |
|---|
| 44 | /// </summary> |
|---|
| 45 | /// <param name="reports">The list of reports to upload.</param> |
|---|
| 46 | public BlackBoxUploadForm(IList<BlackBoxReport> reports) |
|---|
| 47 | { |
|---|
| 48 | InitializeComponent(); |
|---|
| 49 | Theming.ApplyTheme(this); |
|---|
| 50 | UploadWorker.RunWorkerAsync(reports); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | private void BlackBoxUploadForm_FormClosing(object sender, FormClosingEventArgs e) |
|---|
| 54 | { |
|---|
| 55 | if (UploadWorker.IsBusy) |
|---|
| 56 | { |
|---|
| 57 | UploadWorker.CancelAsync(); |
|---|
| 58 | e.Cancel = true; |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | private void UploadWorker_DoWork(object sender, DoWorkEventArgs e) |
|---|
| 63 | { |
|---|
| 64 | IList<BlackBoxReport> reports = (IList<BlackBoxReport>)e.Argument; |
|---|
| 65 | SteppedProgressManager progress = new SteppedProgressManager(); |
|---|
| 66 | |
|---|
| 67 | for (int i = 0; i < reports.Count; ++i) |
|---|
| 68 | { |
|---|
| 69 | //Create the progress object that will handle the progress for this report. |
|---|
| 70 | SteppedProgressManager reportProgress = new SteppedProgressManager(); |
|---|
| 71 | progress.Steps.Add(new SteppedProgressManagerStep(reportProgress, |
|---|
| 72 | 1.0f / reports.Count)); |
|---|
| 73 | |
|---|
| 74 | BlackBoxReportUploader uploader = new BlackBoxReportUploader(reports[i]); |
|---|
| 75 | |
|---|
| 76 | //Check that a similar report has not yet been uploaded. |
|---|
| 77 | UploadWorker.ReportProgress((int)(progress.Progress * 100), |
|---|
| 78 | S._("Checking for status of report {0}...", reports[i].Name)); |
|---|
| 79 | if (!uploader.ReportIsNew()) |
|---|
| 80 | continue; |
|---|
| 81 | |
|---|
| 82 | if (UploadWorker.CancellationPending) |
|---|
| 83 | throw new OperationCanceledException(); |
|---|
| 84 | |
|---|
| 85 | { |
|---|
| 86 | //No similar reports have been uploaded. Compress the report. |
|---|
| 87 | ProgressManager step = new ProgressManager(); |
|---|
| 88 | step.Total = 1000; |
|---|
| 89 | reportProgress.Steps.Add(new SteppedProgressManagerStep(step, 0.5f)); |
|---|
| 90 | UploadWorker.ReportProgress((int)(progress.Progress * 100), |
|---|
| 91 | S._("Compressing Report {0}: {1}%", reports[i].Name, 0)); |
|---|
| 92 | |
|---|
| 93 | uploader.Compress(delegate(object from, EraserProgressChangedEventArgs e2) |
|---|
| 94 | { |
|---|
| 95 | step.Completed = (int)(e2.Progress.Progress * step.Total); |
|---|
| 96 | UploadWorker.ReportProgress((int)(progress.Progress * 100), |
|---|
| 97 | S._("Compressing Report {0}: {1:#0.00%}", |
|---|
| 98 | reports[i].Name, e2.Progress.Progress)); |
|---|
| 99 | |
|---|
| 100 | if (UploadWorker.CancellationPending) |
|---|
| 101 | throw new OperationCanceledException(); |
|---|
| 102 | }); |
|---|
| 103 | } |
|---|
| 104 | { |
|---|
| 105 | //Upload the report. |
|---|
| 106 | ProgressManager step = new ProgressManager(); |
|---|
| 107 | step.Total = 1000; |
|---|
| 108 | reportProgress.Steps.Add(new SteppedProgressManagerStep(step, 0.5f)); |
|---|
| 109 | UploadWorker.ReportProgress((int)(progress.Progress * 100), |
|---|
| 110 | S._("Uploading Report {0}: {1}%", reports[i].Name, 0)); |
|---|
| 111 | |
|---|
| 112 | uploader.Upload(delegate(object from, EraserProgressChangedEventArgs e2) |
|---|
| 113 | { |
|---|
| 114 | step.Completed = (int)(e2.Progress.Progress * step.Total); |
|---|
| 115 | UploadWorker.ReportProgress((int)progress.Progress, |
|---|
| 116 | S._("Uploading Report {0}: {1:#0.00%}", |
|---|
| 117 | reports[i].Name, e2.Progress.Progress)); |
|---|
| 118 | |
|---|
| 119 | if (UploadWorker.CancellationPending) |
|---|
| 120 | throw new OperationCanceledException(); |
|---|
| 121 | }); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | private void UploadWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) |
|---|
| 127 | { |
|---|
| 128 | if (e.UserState != null) |
|---|
| 129 | ProgressLbl.Text = e.UserState as string; |
|---|
| 130 | ProgressPb.Value = e.ProgressPercentage; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | private void UploadWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
|---|
| 134 | { |
|---|
| 135 | if (e.Error == null) |
|---|
| 136 | { |
|---|
| 137 | ProgressLbl.Text = S._("Reports submitted successfully."); |
|---|
| 138 | ProgressPb.Value = ProgressPb.Maximum; |
|---|
| 139 | CancelBtn.Text = S._("Close"); |
|---|
| 140 | } |
|---|
| 141 | else if (e.Error is OperationCanceledException) |
|---|
| 142 | { |
|---|
| 143 | ProgressLbl.Text = S._("Submission was cancelled."); |
|---|
| 144 | ProgressPb.Value = ProgressPb.Maximum; |
|---|
| 145 | CancelBtn.Text = S._("Close"); |
|---|
| 146 | } |
|---|
| 147 | else |
|---|
| 148 | { |
|---|
| 149 | MessageBox.Show(this, e.Error.Message, |
|---|
| 150 | S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error, |
|---|
| 151 | MessageBoxDefaultButton.Button1, S.IsRightToLeft(this) ? |
|---|
| 152 | MessageBoxOptions.RtlReading : 0); |
|---|
| 153 | Close(); |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | private void CancelBtn_Click(object sender, EventArgs e) |
|---|
| 158 | { |
|---|
| 159 | if (UploadWorker.IsBusy) |
|---|
| 160 | UploadWorker.CancelAsync(); |
|---|
| 161 | else |
|---|
| 162 | Close(); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | } |
|---|