source: trunk/eraser/Eraser.BlackBox/BlackBoxUploadForm.cs @ 2788

Revision 2788, 5.1 KB checked in by lowjoel, 10 months ago (diff)

Fix the progress bar display in the report upload progress dialog. The dialog was displaying all sorts of incoherent information!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Rev URL
Line 
1/*
2 * $Id$
3 * Copyright 2008-2012 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;
32using Eraser.Plugins;
33
34using ProgressChangedEventArgs = System.ComponentModel.ProgressChangedEventArgs;
35using EraserProgressChangedEventArgs = Eraser.Plugins.ProgressChangedEventArgs;
36
37namespace Eraser.BlackBox
38{
39    public partial class BlackBoxUploadForm : Form
40    {
41        /// <summary>
42        /// Constructor.
43        /// </summary>
44        /// <param name="reports">The list of reports to upload.</param>
45        public BlackBoxUploadForm(IList<BlackBoxReport> reports)
46        {
47            InitializeComponent();
48            Theming.ApplyTheme(this);
49            UploadWorker.RunWorkerAsync(reports);
50        }
51
52        private void BlackBoxUploadForm_FormClosing(object sender, FormClosingEventArgs e)
53        {
54            if (UploadWorker.IsBusy)
55            {
56                UploadWorker.CancelAsync();
57                e.Cancel = true;
58            }
59        }
60
61        private void UploadWorker_DoWork(object sender, DoWorkEventArgs e)
62        {
63            IList<BlackBoxReport> reports = (IList<BlackBoxReport>)e.Argument;
64            SteppedProgressManager overallProgress = new SteppedProgressManager();
65
66            for (int i = 0; i < reports.Count; ++i)
67            {
68                //Create the progress object that will handle the progress for this report.
69                ProgressManager reportProgress = new ProgressManager();
70                overallProgress.Steps.Add(new SteppedProgressManagerStep(reportProgress,
71                    1.0f / reports.Count));
72
73                //Allow us to bail out.
74                if (UploadWorker.CancellationPending)
75                    throw new OperationCanceledException();
76
77                //If we have not submitted the report before upload it.
78                if (reports[i].Status == BlackBoxReportStatus.New)
79                    Upload(reports[i], overallProgress, reportProgress);
80
81                //Otherwise check for solutions.
82                else
83                    CheckStatus(reports[i], overallProgress, reportProgress);
84            }
85        }
86
87        private void Upload(BlackBoxReport report, SteppedProgressManager overallProgress,
88            ProgressManager reportProgress)
89        {
90            //Upload the report.
91            UploadWorker.ReportProgress((int)(overallProgress.Progress * 100),
92                S._("Compressing Report {0}: {1:#0.00%}", report.Name, 0));
93
94            reportProgress.Total = int.MaxValue;
95            BlackBoxReportUploader uploader = new BlackBoxReportUploader(report);
96            uploader.Submit(delegate(object from, EraserProgressChangedEventArgs e2)
97                {
98                    SteppedProgressManager reportSteps = (SteppedProgressManager)e2.Progress;
99                    reportProgress.Completed = (int)(reportSteps.Progress * reportProgress.Total);
100                    int step = reportSteps.Steps.IndexOf(reportSteps.CurrentStep);
101
102                    UploadWorker.ReportProgress((int)(overallProgress.Progress * 100),
103                        step == 0 ?
104                            S._("Compressing Report {0}: {1:#0.00%}",
105                                report.Name, reportSteps.CurrentStep.Progress.Progress) :
106                            S._("Uploading Report {0}: {1:#0.00%}",
107                                report.Name, reportSteps.CurrentStep.Progress.Progress));
108
109                    if (UploadWorker.CancellationPending)
110                        throw new OperationCanceledException();
111                });
112        }
113
114        private void CheckStatus(BlackBoxReport report, SteppedProgressManager overallProgress,
115            ProgressManager reportProgress)
116        {
117            //Upload the report.
118            UploadWorker.ReportProgress((int)(overallProgress.Progress * 100),
119                S._("Checking for solution for {0}...", report.Name));
120
121            BlackBoxReportUploader uploader = new BlackBoxReportUploader(report);
122            report.Status = uploader.Status;
123        }
124
125        private void UploadWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
126        {
127            if (e.UserState != null)
128                ProgressLbl.Text = (string)e.UserState;
129            ProgressPb.Value = e.ProgressPercentage;
130        }
131
132        private void UploadWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
133        {
134            if (e.Error == null)
135            {
136                ProgressLbl.Text = S._("Reports submitted successfully.");
137                ProgressPb.Value = ProgressPb.Maximum;
138                CancelBtn.Text = S._("Close");
139            }
140            else if (e.Error is OperationCanceledException)
141            {
142                ProgressLbl.Text = S._("Submission was cancelled.");
143                ProgressPb.Value = ProgressPb.Maximum;
144                CancelBtn.Text = S._("Close");
145            }
146            else
147            {
148                MessageBox.Show(this, e.Error.Message,
149                    S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error,
150                    MessageBoxDefaultButton.Button1, Localisation.IsRightToLeft(this) ?
151                        MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0);
152                Close();
153            }
154        }
155
156        private void CancelBtn_Click(object sender, EventArgs e)
157        {
158            if (UploadWorker.IsBusy)
159                UploadWorker.CancelAsync();
160            else
161                Close();
162        }
163    }
164}
Note: See TracBrowser for help on using the repository browser.