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