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

Revision 1617, 5.2 KB checked in by lowjoel, 5 years ago (diff)

Move the BlackBoxReportUploader? to the Eraser.Util assembly. Also move the MultipartFormDataBuilder? to Eraser.Util. 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 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}
Note: See TracBrowser for help on using the repository browser.