-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cs
515 lines (467 loc) · 18.9 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Collections;
namespace CSL
{
public partial class MainWindow : Form
{
#region Local Variables
TorrentBuilder tb = new TorrentBuilder();
OptionsWindow ow = new OptionsWindow();
DirectoryHandler dh = new DirectoryHandler();
DataGridViewHandler dgvh;
List<FileInfo> items;
TorrentDataHandler data;
//Timing
DateTime startime;
DateTime endtime;
static System.Timers.Timer timer = new System.Timers.Timer();
static public bool HideSent;
string[] files;
string[] torrents;
string[] zips;
delegate void ProcessTorrentsInvoker();
delegate void RefreshInvoker();
private Object lockingobject = new Object();
#endregion
public MainWindow()
{
InitializeComponent();
this.ResizeRedraw = true;
this.MainWindow_Resize(new object(), new EventArgs()); //Force a resize to make things look nice
this.torrentsTableTableAdapter.Fill(this.dataset.TorrentsTable);
this.moviesTableTableAdapter.Fill(this.dataset.MoviesTable);
this.othersTableTableAdapter.Fill(this.dataset.OthersTable);
//Allow another class to handle the deleting/handling of the datagridviews to keep this class slimmer
dgvh = new DataGridViewHandler(ref torrentsTableDataGridView, ref torrentsTableBindingSource,
ref moviesTableDataGridView, ref moviesTableBindingSource,
ref othersTableDataGridView, ref othersTableBindingSource);
data = new TorrentDataHandler(ref dataset, ref torrentsTableTableAdapter);
timer.Interval = (double)SettingsHandler.GetAutoHandleTime();
if (timer.Interval < (1000*10))
{
SettingsHandler.SetAutoHandleTime(10000);
timer.Interval = 10000;
}
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
tb.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BuildWorkerCompleted);
tb.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
if (SettingsHandler.GetAutoHandleBool())
timer.Start();
HideSent = (HideSentTorrentsCheckBox.Checked) ? true : false;
Arrow.Visible = false;
StatusLabel.Visible = false;
ArrowText.Visible = false;
notifyIcon.Visible = false;
try
{
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
{
System.Deployment.Application.ApplicationDeployment ad =
System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
string version = ad.CurrentVersion.ToString();
if (SettingsHandler.GetCurrentVersion().CompareTo(version) < 0)
{
UpdatedInformationWindow uw = new UpdatedInformationWindow();
uw.ShowDialog();
SettingsHandler.SetCurrentVersion(version);
}
}
}
catch (Exception) { }
test();
}
#region Timer
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (ProcessTorrentsButton.InvokeRequired)
this.Invoke(new ProcessTorrentsInvoker(PerformClickProcessTorrents));
else
ProcessTorrentsButton.PerformClick();
}
static public void UpdateTimer(bool alive, decimal time)
{
timer.Enabled = alive;
timer.Interval = (double)time;
}
#endregion
#region Testing
private void test()
{
/*
UTorrentWebClient web = new Cleverscape.UTorrentClient.WebClient.UTorrentWebClient();
Cleverscape.UTorrentClient.WebClient.Torrent torrent = new Cleverscape.UTorrentClient.WebClient.Torrent();
torrent;
Cleverscape.UTorrentClient.WebClient.*/
}
#endregion
#region General
/*GENERAL*/
private void RefreshData()
{
Thread.Sleep(100);
dataGridViewProgressBar.Visible = false;
StatusLabel.Visible = false;
ProcessTorrentsButton.Enabled = true;
RefreshButton.Enabled = true;
DeleteButton.Enabled = true;
dgvh.ResumeLayout();
if (timer.Enabled == false && SettingsHandler.GetAutoHandleBool())
timer.Start();
try
{
dgvh = new DataGridViewHandler();
}
catch { }
}
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
timer.Stop();
timer.Close();
//TorrentXMLHandler.SaveAndClose();
this.Dispose();
}
private void MainWindow_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
if (SettingsHandler.GetMinimizeToTray())
{
Hide();
notifyIcon.Visible = true;
}
}
//DATAGRIDVIEW
TabbedContainer.Location = new System.Drawing.Point(0, 187);
TabbedContainer.Size = new System.Drawing.Size(this.Width - 15, this.Height - 240);
//DELETE BUTTON
DeleteButton.Location = new System.Drawing.Point(this.Width - 105, 113);
//uTORRENT SEND BUTTON
uTorrentSendIndividualButton.Location = new System.Drawing.Point(this.Width - 145, 150);
//REFRESH BUTTON
RefreshButton.Location = new System.Drawing.Point(0, 151);
HideSentTorrentsCheckBox.Location = new System.Drawing.Point(RefreshButton.Width + 5, 155);
//PROGRESS BAR
dataGridViewProgressBar.Location = new System.Drawing.Point(0, this.Height - 80);
dataGridViewProgressBar.Width = this.Width;
//STATUS LABEL
StatusLabel.Location = new System.Drawing.Point(0, this.Height - 100);
}
private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
notifyIcon.Visible = false;
}
private void HideSentTorrentsCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (HideSentTorrentsCheckBox.Checked)
{
HideSent = true;
dgvh.HideSentTorrents();
}
else
{
HideSent = false;
dgvh.ShowSentTorrents();
}
}
#endregion
#region Buttons
/*<BUTTONS>*/
//Click
private void DeleteButton_Click(object sender, EventArgs e)
{
dgvh.DeleteTorrents();
torrentsTableTableAdapter.Update(dataset.TorrentsTable);
torrentsTableBindingSource.EndEdit();
moviesTableTableAdapter.Update(dataset.MoviesTable);
moviesTableBindingSource.EndEdit();
othersTableTableAdapter.Update(dataset.OthersTable);
othersTableBindingSource.EndEdit();
tableAdapterManager.UpdateAll(dataset);
}
private void RefreshButton_Click(object sender, EventArgs e)
{
torrentsTableDataGridView.Refresh();
moviesTableDataGridView.Refresh();
othersTableDataGridView.Refresh();
}
private void ProcessTorrentsButton_Click(object sender, EventArgs e)
{
items = new List<FileInfo>();
torrents = DirectoryHandler.GetTorrents();
zips = DirectoryHandler.GetTorrentZips();
if (torrents != null || zips != null)
{
startime = DateTime.Now;
//Prevent anything complicated from happening..
dgvh.SuspendLayout();
ProcessTorrentsButton.Enabled = false;
RefreshButton.Enabled = false;
DeleteButton.Enabled = false;
StatusLabel.Visible = true;
dataGridViewProgressBar.Visible = true;
timer.Stop();
if (torrents != null)
foreach (string torrent in torrents)
items.Add(new FileInfo(torrent));
if (zips != null)
items.AddRange(dh.UnzipFiles(zips));
tb.RunWorkerAsync(items);
}
}
private void uTorrentSendIndividualButton_Click(object sender, EventArgs e)
{
switch (TabbedContainer.SelectedIndex)
{
case 0:
dgvh.SendIndividualTorrent("music");
torrentsTableTableAdapter.Update(dataset.TorrentsTable);
torrentsTableBindingSource.EndEdit();
break;
case 1:
dgvh.SendIndividualTorrent("movies");
moviesTableTableAdapter.Update(dataset.MoviesTable);
moviesTableBindingSource.EndEdit();
break;
case 2:
dgvh.SendIndividualTorrent("others");
othersTableTableAdapter.Update(dataset.OthersTable);
othersTableBindingSource.EndEdit();
break;
}
tableAdapterManager.UpdateAll(dataset);
}
private void uTorrentSendAllButton_Click(object sender, EventArgs e)
{
torrentsTableDataGridView.SelectAll();
moviesTableDataGridView.SelectAll();
othersTableDataGridView.SelectAll();
dgvh.SendIndividualTorrent("music");
dgvh.SendIndividualTorrent("movies");
dgvh.SendIndividualTorrent("others");
torrentsTableTableAdapter.Update(dataset.TorrentsTable);
torrentsTableBindingSource.EndEdit();
moviesTableTableAdapter.Update(dataset.MoviesTable);
moviesTableBindingSource.EndEdit();
othersTableTableAdapter.Update(dataset.OthersTable);
othersTableBindingSource.EndEdit();
tableAdapterManager.UpdateAll(dataset);
}
private void PerformClickProcessTorrents()
{
ProcessTorrentsButton.PerformClick();
}
private void PerformClickRefreshButton()
{
RefreshButton.PerformClick();
}
private void ShowProcessTorrentsButton()
{
ProcessTorrentsButton.Visible = true;
}
private void UpdateAdapter()
{
torrentsTableTableAdapter.Update(dataset.TorrentsTable);
}
//Hover
private void DeleteButton_MouseEnter(object sender, EventArgs e)
{
RefreshButton.Visible = false;
Arrow.Visible = true;
ArrowText.Visible = true;
}
private void DeleteButton_MouseLeave(object sender, EventArgs e)
{
RefreshButton.Visible = true;
ArrowText.Visible = false;
Arrow.Visible = false;
}
#endregion
#region Tool Strip
/*TOOL STRIP*/
private void optionsToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void optionsToolStripMenuItem2_Click(object sender, EventArgs e)
{
ow.Show();
}
#endregion
#region DataGridView
/*DataGridView*/
private void dataGridView_DragDrop(object sender, DragEventArgs e)
{
items = new List<FileInfo>();
files = (string[])e.Data.GetData(DataFormats.FileDrop);
dataGridViewProgressBar.Visible = true;
StatusLabel.Visible = true;
foreach (string file in files)
{
string extension = Path.GetExtension(file);
if (Path.GetExtension(file).Equals(".zip") || Path.GetExtension(file).Equals(".rar"))
{
foreach (string unzipped in DirectoryHandler.UnzipFile(file))
items.Add(new FileInfo(unzipped));
}
else if (Path.GetExtension(file).Equals(".torrent"))
{
items.Add(new FileInfo(file));
}
}
tb.RunWorkerAsync(items);
}
private void dataGridView_DragEnter(object sender, DragEventArgs e)
{
// make sure they're actually dropping files
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
// allow them to continue
// (without this, the cursor stays a "NO" symbol)
e.Effect = DragDropEffects.All;
}
private void dataGridView_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData.Equals(Keys.Delete))
DeleteButton_Click(sender, e);
}
#endregion
#region BackgroundWorker
void BuildWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
DirectoryHandler.DeleteTempFolder();
DirectoryHandler.DeleteZipFiles();
ProcessTorrentsButton.Enabled = true;
RefreshButton.Enabled = true;
DeleteButton.Enabled = true;
dataGridViewProgressBar.Visible = false;
StatusLabel.Visible = false;
if (SettingsHandler.GetAutoHandleBool())
timer.Start();
torrentsTableTableAdapter.Update(dataset.TorrentsTable);
torrentsTableBindingSource.EndEdit();
moviesTableTableAdapter.Update(dataset.MoviesTable);
moviesTableBindingSource.EndEdit();
othersTableTableAdapter.Update(dataset.OthersTable);
othersTableBindingSource.EndEdit();
tableAdapterManager.UpdateAll(dataset);
endtime = DateTime.Now;
TimeSpan duration = endtime - startime;
ProcessTimer.Text = duration.Seconds + " secs";
dgvh.ResumeLayout();
}
void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string precedence = "TorrentBuilder";
int progress = 0;
if (!(e.ProgressPercentage > 100 || e.ProgressPercentage < 0))
progress = e.ProgressPercentage;
if ((e.ProgressPercentage % 10) == 0)
{
string test = sender.ToString();
switch (test)
{
case "CSL.TorrentBuilder":
StatusLabel.Text = "Building " + 10 * (e.ProgressPercentage / 10) + "%";
if (precedence.Equals("TorrentBuilder"))
{
if (!StatusLabel.Text.Contains("Building"))
StatusLabel.Text = "Building";
if (progress == 100)
{
StatusLabel.Text = "Finalizing...";
precedence = "DirectoryHandler";
dataGridViewProgressBar.Value = 100;
}
else
{
dataGridViewProgressBar.Value = progress;
}
}
break;
case "CSL.DirectoryHandler":
StatusLabel.Text = "Moving files " + 10 * (e.ProgressPercentage / 10) + "%";
if (precedence.Equals("DirectoryHandler"))
{
if (!StatusLabel.Text.Contains("Moving files"))
StatusLabel.Text = "Moving files";
if (progress == 100)
{
precedence = "TorrentBuilder";
dataGridViewProgressBar.Value = 100;
}
else
{
dataGridViewProgressBar.Value = progress;
}
}
break;
default:
StatusLabel.Text = "Working...";
break;
}
}
else
dataGridViewProgressBar.Value = progress;
}
#endregion
private void MainWindow_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
e.Effect = DragDropEffects.All;
}
private void MainWindow_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
FileInfo fi;
List<FileInfo> torrents = new List<FileInfo>();
foreach (string file in files)
{
fi = new FileInfo(file);
if (fi.Extension.Equals(".torrent"))
torrents.Add(fi);
else if (fi.Extension.Equals(".rar") || fi.Extension.Equals(".zip"))
torrents.AddRange(DirectoryHandler.GetFileInfos(DirectoryHandler.UnzipFile(fi.FullName)));
}
if (torrents.Count > 0)
{
dgvh.SuspendLayout();
ProcessTorrentsButton.Enabled = false;
RefreshButton.Enabled = false;
DeleteButton.Enabled = false;
StatusLabel.Visible = true;
dataGridViewProgressBar.Visible = true;
timer.Stop();
tb.RunWorkerAsync(torrents);
}
}
private void createDirectoriesToolStripMenuItem_Click(object sender, EventArgs e)
{
ErrorWindow ew = new ErrorWindow();
bool response = ew.IssueCreateEmptyDirectoriesWarning("Click OK to proceed", "This option will create the save structure\n directories of the current torrents");
if (response)
{
try
{
List<String> save_structures = new List<string>();
foreach (DataGridViewRow r in dgvh.dv.Rows)
{
try
{
save_structures.Add((string)r.Cells["Save Structure"].Value);
}
catch { }
}
DirectoryHandler.CreateEmptyDirectories(save_structures);
}
catch
{ }
}
}
}
}