/
FileSearchTaskDialog.cs
207 lines (178 loc) · 8.1 KB
/
FileSearchTaskDialog.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
namespace RoliSoft.TVShowTracker.TaskDialogs
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.WindowsAPICodePack.Taskbar;
using FileNames;
using Tables;
using VistaControls.TaskDialog;
/// <summary>
/// Provides a <c>TaskDialog</c> frontend to the <c>FileSearch</c> class.
/// </summary>
public class FileSearchTaskDialog
{
private TaskDialog _td;
private Result _res;
private FileSearch _fs;
private Episode _ep;
private volatile bool _active;
/// <summary>
/// Searches for the specified show and its episode.
/// </summary>
/// <param name="episode">The episode to search for.</param>
public void Search(Episode episode)
{
_ep = episode;
var paths = Settings.Get<List<string>>("Download Paths");
if (paths.Count == 0)
{
new TaskDialog
{
CommonIcon = TaskDialogIcon.Stop,
Title = "Search path not configured",
Instruction = "Search path not configured",
Content = "To use this feature you must set your download path." + Environment.NewLine + Environment.NewLine + "To do so, click on the logo on the upper left corner of the application, then select 'Configure Software'. On the new window click the 'Browse' button under 'Download Path'."
}.Show();
return;
}
_td = new TaskDialog
{
Title = "Searching...",
Instruction = string.Format("{0} S{1:00}E{2:00}", _ep.Show.Name, _ep.Season, _ep.Number),
Content = "Searching for the episode...",
CommonButtons = TaskDialogButton.Cancel,
ShowProgressBar = true
};
_td.SetMarqueeProgressBar(true);
_td.Destroyed += TaskDialogDestroyed;
_td.ButtonClick += TaskDialogDestroyed;
_active = true;
new Thread(() =>
{
Thread.Sleep(500);
if (_active)
{
_res = _td.Show().CommonButton;
}
}).Start();
_fs = new FileSearch(paths, _ep);
_fs.FileSearchDone += FileSearchDone;
_fs.FileSearchProgressChanged += FileSearchProgressChanged;
_fs.BeginSearch();
Utils.Win7Taskbar(state: TaskbarProgressBarState.Indeterminate);
}
/// <summary>
/// Handles the Destroyed event of the _td control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void TaskDialogDestroyed(object sender, EventArgs e)
{
if (_active && (_res == Result.Cancel || (e is ClickEventArgs && (e as ClickEventArgs).ButtonID == 2)))
{
Utils.Win7Taskbar(state: TaskbarProgressBarState.NoProgress);
_active = false;
_res = Result.Cancel;
_fs.CancelSearch();
}
}
/// <summary>
/// Event handler for <c>FileSearch.FileSearchProgressChanged</c>.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="string"/> instance containing the event data.</param>
private void FileSearchProgressChanged(object sender, EventArgs<string> e)
{
if (_td != null)
{
_td.Content = e.Data;
}
}
/// <summary>
/// Event handler for <c>FileSearch.FileSearchDone</c>.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="RoliSoft.TVShowTracker.EventArgs<System.Collections.Generic.List<System.String>>"/> instance containing the event data.</param>
private void FileSearchDone(object sender, EventArgs<List<string>> e)
{
_active = false;
Utils.Win7Taskbar(state: TaskbarProgressBarState.NoProgress);
if (_td != null && _td.IsShowing)
{
_td.SimulateButtonClick(-1);
}
if (_res == Result.Cancel || e.Data == null)
{
return;
}
switch (e.Data.Count)
{
case 0:
new TaskDialog
{
CommonIcon = TaskDialogIcon.Stop,
Title = "No files found",
Instruction = string.Format("{0} S{1:00}E{2:00}", _ep.Show.Name, _ep.Season, _ep.Number),
Content = "No files were found for this episode.",
ExpandedControlText = "Troubleshoot",
ExpandedInformation = "If you have the episode in the specified download folder but the search fails, it might be because the file name slightly differs.\r\nIf this is the case, click on the 'Guides' tab, select this show, click on the wrench icon and then follow the instructions under the 'Custom release name' section.",
CommonButtons = TaskDialogButton.OK
}.Show();
break;
case 1:
if (OpenArchiveTaskDialog.SupportedArchives.Contains(Path.GetExtension(e.Data[0]).ToLower()))
{
new OpenArchiveTaskDialog().OpenArchive(e.Data[0]);
}
else
{
Utils.Run(e.Data[0]);
}
break;
default:
var mfftd = new TaskDialog
{
Title = "Multiple files found",
Instruction = string.Format("{0} S{1:00}E{2:00}", _ep.Show.Name, _ep.Season, _ep.Number),
Content = "Multiple files were found for this episode:",
CommonButtons = TaskDialogButton.Cancel,
CustomButtons = new CustomButton[e.Data.Count],
UseCommandLinks = true
};
mfftd.ButtonClick += (s, c) =>
{
if (c.ButtonID < e.Data.Count)
{
if (OpenArchiveTaskDialog.SupportedArchives.Contains(Path.GetExtension(e.Data[c.ButtonID]).ToLower()))
{
new OpenArchiveTaskDialog().OpenArchive(e.Data[c.ButtonID]);
}
else
{
Utils.Run(e.Data[c.ButtonID]);
}
}
};
var i = 0;
foreach (var file in e.Data)
{
var fi = new FileInfo(file);
var quality = Parser.ParseQuality(file);
var instr = fi.Name + "\n";
if (quality != Parsers.Downloads.Qualities.Unknown)
{
instr += quality.GetAttribute<DescriptionAttribute>().Description + " – ";
}
mfftd.CustomButtons[i] = new CustomButton(i, instr + Utils.GetFileSize(fi.Length) + "\n" + fi.DirectoryName);
i++;
}
mfftd.Show();
break;
}
}
}
}