-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cs
210 lines (179 loc) · 5.6 KB
/
MainWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.IO;
using System.ComponentModel;
using System.Threading;
using Gtk;
using Scraper;
public partial class MainWindow: Gtk.Window
{
ModelController model;
Thread thread;
string overallProgress = "Overall Progress";
public MainWindow (ModelController model): base (Gtk.WindowType.Toplevel)
{
Build ();
this.model = model;
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Invoke (delegate {
if (model.Active) {
model.CancelTask ();
}
Application.Quit ();
a.RetVal = true;
});
}
protected void OnInputUrlChanged (object sender, System.EventArgs e)
{
if (!model.Active) {
Reset ();
}
}
protected void OnNumberValueChanged (object sender, System.EventArgs e)
{
if (!model.Active) {
Reset ();
}
}
protected void OnScrapeButtonClicked (object sender, System.EventArgs e)
{
var number = int.Parse (NumberButton.Text);
var inputUrl = UrlEntry.Text;
string outputFileName;
thread = new Thread (this.model.RunTask);
if (!model.Active) {
if (SaveDialog (out outputFileName)) {
// validate and run
bool valid = false;
try {
valid = model.ValidateInputs (inputUrl, outputFileName, number);
} catch (Exception ex) {
ErrorAlert (ex.Message);
}
if (valid) {
model.TaskStarted += OnTaskStarted;
model.TaskCancelled += OnTaskCancelled;
model.TaskCompleted += OnTaskCompleted;
model.FileProgress += OnIndividualProgressUpdate;
model.FileDownloaded += OnOverallProgressUpdate;
OverallProgressBar.Fraction = 0;
thread.Start ();
}
} else {
return;
}
} else {
model.CancelTask ();
}
}
protected void OnTaskStarted (object sender, EventArgs e)
{
Application.Invoke (delegate {
IndividualProgressLabel.Text = String.Format ("Individual Progress: {0}", model.Status.ToLower ());
ScrapeButton.Label = "Cancel";
});
}
protected void OnTaskCompleted (object sender, EventArgs e)
{
// Gtk.Application.Invoke makes sure that these events are handled on the Gtk main thread
// otherwise they would be invoked on the thread where the event was invoked.
// Thus we are not touching the UI from background threads.
Application.Invoke (delegate {
IndividualProgressBar.Fraction = 1.0;
IndividualProgressLabel.Text = String.Format ("Individual Progress: {0}", model.Status.ToLower ());
OverallProgressBar.Fraction = 1.0;
OverallProgressLabel.Text = String.Format ("{0}: 100%", overallProgress);
ScrapeButton.Label = "Scrape";
FinishedAlert ();
});
}
protected void OnTaskCancelled (object sender, EventArgs e)
{
Application.Invoke (delegate {
IndividualProgressLabel.Text = String.Format ("Individual Progress: {0}", model.Status.ToLower ());
ScrapeButton.Label = "Scrape";
});
}
protected void OnOverallProgressUpdate (object sender, EventArgs e)
{
Application.Invoke (delegate {
if (model.Active) {
// 100% individual progress
IndividualProgressBar.Fraction = 1.0;
IndividualProgressLabel.Text = String.Format ("Individual Progress: {0}", model.Status.ToLower ());
// update overall progress
double frac = (double)model.NumberDownloaded / (double)model.NumberToDownload;
OverallProgressBar.Fraction = frac;
OverallProgressLabel.Text = String.Format ("{0}: {1}%", overallProgress, Math.Round (100 * frac));
// 0% individual progress
// IndividualProgressBar.Fraction = 0;
}
});
}
protected void OnIndividualProgressUpdate (object sender, ProgressChangedEventArgs e)
{
Application.Invoke (delegate {
if (model.Active) {
IndividualProgressBar.Fraction = e.ProgressPercentage / (double)100;
}
});
}
protected void Reset ()
{
IndividualProgressBar.Fraction = 0;
IndividualProgressLabel.Text = "Individual Progress";
OverallProgressBar.Fraction = 0;
OverallProgressLabel.Text = "Overall Progress";
ScrapeButton.Label = "Scrape";
}
protected bool SaveDialog (out string outputFileName)
{
var chooser = new FileChooserDialog ("Save as...",
this,
FileChooserAction.Save,
Stock.Save,
ResponseType.Ok,
Stock.Cancel,
ResponseType.Cancel);
// present dialog
chooser.DoOverwriteConfirmation = true;
chooser.ShowHidden = false;
var ans = chooser.Run () == (int)ResponseType.Ok;
var temp = chooser.Filename;
chooser.Destroy ();
outputFileName = temp;
return ans;
}
protected void FinishedAlert ()
{
var md = new MessageDialog (this,
DialogFlags.DestroyWithParent,
MessageType.Info,
ButtonsType.Ok,
"Scraping finished!");
md.Run ();
md.Destroy ();
}
protected void ErrorAlert (string message)
{
var md = new MessageDialog (this,
DialogFlags.DestroyWithParent,
MessageType.Warning,
ButtonsType.Ok,
message);
md.Run ();
md.Destroy ();
}
// text entries (including spin buttons) invoke Activate on enter key hit
protected void OnActivated (object sender, System.EventArgs e)
{
if (sender.GetType () == typeof(Gtk.SpinButton)) {
ScrapeButton.Click ();
} else if (sender.GetType () == typeof(Gtk.Entry)) {
NumberButton.GrabFocus ();
} else {
// pass
}
}
}