source: branches/eraser6/CodeReview/Eraser/BlackBoxUploadForm.cs @ 1622

Revision 1622, 4.6 KB checked in by lowjoel, 5 years ago (diff)

Combine the progress and upload steps to the Upload function since compression of the report is strictly the task of the Report Uploader. Addresses #307: Eraser Updater restructuring

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29using System.IO;
30
31using Eraser.Util;
32
33using ProgressChangedEventArgs = System.ComponentModel.ProgressChangedEventArgs;
34using ProgressChangedEventHandler = System.ComponentModel.ProgressChangedEventHandler;
35using EraserProgressChangedEventArgs = Eraser.Util.ProgressChangedEventArgs;
36using EraserProgressChangedEventHandler = Eraser.Util.ProgressChangedEventHandler;
37
38namespace 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 overallProgress = 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                ProgressManager reportProgress = new ProgressManager();
71                overallProgress.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)(overallProgress.Progress * 100),
78                    S._("Checking for status of report {0}...", reports[i].Name));
79                if (!uploader.IsNew)
80                    continue;
81
82                if (UploadWorker.CancellationPending)
83                    throw new OperationCanceledException();
84
85                //Upload the report.
86                UploadWorker.ReportProgress((int)(overallProgress.Progress * 100),
87                    S._("Compressing Report {0}: {1:#0.00%}", reports[i].Name, 0));
88
89                uploader.Submit(delegate(object from, EraserProgressChangedEventArgs e2)
90                    {
91                        reportProgress.Completed = (int)(e2.Progress.Progress * reportProgress.Total);
92                        SteppedProgressManager reportSteps = (SteppedProgressManager)e2.Progress;
93                        int step = reportSteps.Steps.IndexOf(reportSteps.CurrentStep);
94
95                        UploadWorker.ReportProgress((int)overallProgress.Progress,
96                            step == 0 ? 
97                                S._("Compressing Report {0}: {1:#0.00%}",
98                                    reports[i].Name, reportSteps.Progress) :
99                                S._("Uploading Report {0}: {1:#0.00%}",
100                                    reports[i].Name, reportSteps.Progress));
101
102                        if (UploadWorker.CancellationPending)
103                            throw new OperationCanceledException();
104                    });
105            }
106        }
107
108        private void UploadWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
109        {
110            if (e.UserState != null)
111                ProgressLbl.Text = (string)e.UserState;
112            ProgressPb.Value = e.ProgressPercentage;
113        }
114
115        private void UploadWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
116        {
117            if (e.Error == null)
118            {
119                ProgressLbl.Text = S._("Reports submitted successfully.");
120                ProgressPb.Value = ProgressPb.Maximum;
121                CancelBtn.Text = S._("Close");
122            }
123            else if (e.Error is OperationCanceledException)
124            {
125                ProgressLbl.Text = S._("Submission was cancelled.");
126                ProgressPb.Value = ProgressPb.Maximum;
127                CancelBtn.Text = S._("Close");
128            }
129            else
130            {
131                MessageBox.Show(this, e.Error.Message,
132                    S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error,
133                    MessageBoxDefaultButton.Button1, S.IsRightToLeft(this) ?
134                        MessageBoxOptions.RtlReading : 0);
135                Close();
136            }
137        }
138
139        private void CancelBtn_Click(object sender, EventArgs e)
140        {
141            if (UploadWorker.IsBusy)
142                UploadWorker.CancelAsync();
143            else
144                Close();
145        }
146    }
147}
Note: See TracBrowser for help on using the repository browser.