-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cs
337 lines (264 loc) · 11.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using tv_organize.Data;
using tv_organize.SubtitlesAPI;
namespace tv_organizer
{
public partial class MainWindow : Form
{
private BindingList<Episode> _episodeList;
public BindingList<Episode> EpisodeList
{
get { return _episodeList; }
set { _episodeList = value;}
}
public MainWindow()
{
try
{
InitializeComponent();
EpisodeList = new BindingList<Episode>();
dataGridViewEpisodes.DataSource = EpisodeList;
DataGridViewComboBoxColumn colCombo = new DataGridViewComboBoxColumn();
colCombo.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
colCombo.HeaderText = "Available Languages";
colCombo.Name = "AvailableLanguages";
dataGridViewEpisodes.Columns.Add(colCombo);
checkBoxManualPath.DataBindings.Add(new Binding("Checked", textBoxSelectedPath, "Enabled"));
}
catch { }
}
private void dataGridViewEpisodes_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dataGridViewEpisodes.Rows)
{
var episode = row.DataBoundItem as Episode;
if (episode != null)
{
if (episode.ErrorHash) row.DefaultCellStyle.BackColor = Color.PaleVioletRed;
var combo = row.Cells["AvailableLanguages"] as DataGridViewComboBoxCell;
if (episode.AvailableLanguages != null)
{
combo.Items.Clear();
foreach (string language in episode.AvailableLanguages)
{
combo.Items.Add(language);
}
}
}
}
}
private void buttonChoosePath_Click(object sender, EventArgs e)
{
folderBrowserDialog.ShowDialog();
textBoxSelectedPath.Text = folderBrowserDialog.SelectedPath;
}
private void textBoxSelectedPath_TextChanged(object sender, EventArgs e)
{
if (Text.Length > 0)
{
buttonSearchForEpisodes.Enabled = true;
buttonDownloadSubtitles.Enabled = false;
buttonSearchSubtitles.Enabled = false;
buttonOrganizeFolders.Enabled = false;
comboBoxPrimaryLanguage.Enabled = false;
}
else
{
buttonSearchForEpisodes.Enabled = false;
}
}
private void buttonSearchForEpisodes_Click(object sender, EventArgs e)
{
buttonChoosePath.Enabled = false;
buttonSearchSubtitles.Enabled = false;
buttonOrganizeFolders.Enabled = false;
backgroundWorkerSearchEpisodes.RunWorkerAsync(textBoxSelectedPath.Text);
toolStripProgressBarLabel.Text = "Search for Episodes Started!";
}
#region Background Worker Search Episodes
private void backgroundWorkerSearchEpisodes_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker searchEpisodesWorker = sender as BackgroundWorker;
string path = e.Argument as string;
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
.Where(file => file.EndsWith("avi", true, null) || file.EndsWith("mp4", true, null) || file.EndsWith("mkv", true, null))
.ToList();
//if files.Count > 250 send object true (maybe file list is too big to be processed)
searchEpisodesWorker.ReportProgress(100, files.Count > 250);
List<Episode> episodeList = new List<Episode>();
for (int i = 0; i < files.Count; i++)
{
episodeList.Add(new Episode(files[i]));
searchEpisodesWorker.ReportProgress((int)((i+1) * 100.0 / (double)files.Count));
}
e.Result = episodeList;
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
toolStripProgressBar.Value = e.ProgressPercentage;
}
private void backgroundWorkerSearchEpisodes_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
EpisodeList.Clear();
List<Episode> list = e.Result as List<Episode>;
foreach (Episode ep in list)
{
EpisodeList.Add(ep);
}
buttonChoosePath.Enabled = true;
if (EpisodeList.Count > 0)
{
buttonSearchSubtitles.Enabled = true;
buttonOrganizeFolders.Enabled = true;
}
toolStripProgressBarLabel.Text = "Search for Episodes Complete!";
}
#endregion
private void buttonSearchSubtitles_Click(object sender, EventArgs e)
{
toolStripProgressBarLabel.Text = "Search for Subtitles Started!";
backgroundWorkerSearchSubtitles.RunWorkerAsync(EpisodeList.ToList());
}
#region Background Worker Search Subtitles
private void backgroundWorkerSearchSubtitles_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker searchEpisodesWorker = sender as BackgroundWorker;
List<Episode> episodes = e.Argument as List<Episode>;
ISubtitlesAPI client = new EasySubtitles();
for (int i = 0; i < episodes.Count; i++)
{
client.Search(episodes[i]);
searchEpisodesWorker.ReportProgress((int)((i+1) * 100.0 / (double)episodes.Count));
}
}
private void backgroundWorkerSearchSubtitles_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
toolStripProgressBarLabel.Text = "Search for Subtitles Complete!";
buttonDownloadSubtitles.Enabled = true;
comboBoxPrimaryLanguage.Enabled = true;
}
#endregion
private void buttonDownloadSubtitles_Click(object sender, EventArgs e)
{
toolStripProgressBarLabel.Text = "Downloading Subtitles!";
backgroundWorkerDownloadSubtitles.RunWorkerAsync(EpisodeList.ToList());
}
#region Background Worker Download Subtitles
private void backgroundWorkerDownloadSubtitles_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker searchEpisodesWorker = sender as BackgroundWorker;
List<Episode> episodes = e.Argument as List<Episode>;
ISubtitlesAPI client = new EasySubtitles();
for (int i = 0; i < episodes.Count; i++)
{
client.Download(episodes[i]);
searchEpisodesWorker.ReportProgress((int)(i*100.0 / episodes.Count));
}
}
private void backgroundWorkerDownloadSubtitles_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
toolStripProgressBarLabel.Text = "Subtitles Download Complete!";
}
#endregion
private void buttonOrganizeFolders_Click(object sender, EventArgs e)
{
toolStripProgressBarLabel.Text = "Organizing Folders!";
backgroundWorkerOrganizeFolders.RunWorkerAsync(new { EpisodeList = EpisodeList.ToList(), Path = textBoxSelectedPath.Text });
}
#region Background Worker Organize Folders
private void backgroundWorkerOrganizeFolders_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker organizeFoldersWorker = sender as BackgroundWorker;
var argument = Cast(e.Argument, new { EpisodeList = new List<Episode>(), Path = "" });
List<Episode> episodes = argument.EpisodeList;
int showsCount = Util.CountShows(episodes);
int seasonsCount = Util.CountSeasons(episodes);
for (int i = 0; i < episodes.Count; i++)
{
OrganizeEpisode(episodes[i], argument.Path, showsCount, seasonsCount);
}
}
private void backgroundWorkerOrganizeFolders_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
toolStripProgressBarLabel.Text = "Files Organized!";
buttonSearchForEpisodes.PerformClick();
}
void OrganizeEpisode(Episode ep, string path, int showsCount, int seasonsCount)
{
//Set Current path
Directory.SetCurrentDirectory(path);
if (showsCount > 1)
{
if (!Directory.Exists(ep.Show))
Directory.CreateDirectory(ep.Show);
Directory.SetCurrentDirectory(ep.Show);
}
if (seasonsCount > 1)
{
var dirname = "Season " + ep.Season;
if (!Directory.Exists(dirname))
Directory.CreateDirectory(dirname);
Directory.SetCurrentDirectory(dirname);
}
var foldername = ep.Season + (ep.EpisodeNumber < 10 ? "0" : "") + ep.EpisodeNumber;
Directory.CreateDirectory(foldername);
Directory.SetCurrentDirectory(foldername);
File.Move(ep.FilePath, Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(ep.FilePath)));
string pathtosrt = Path.Combine(Path.GetDirectoryName(ep.FilePath), Path.GetFileNameWithoutExtension(ep.FilePath)) + ".srt";
if (File.Exists(pathtosrt))
{
File.Move(pathtosrt, Path.GetFileNameWithoutExtension(ep.FilePath) + ".srt");
}
}
T Cast<T>(object obj, T type)
{
return (T)obj;
}
#endregion
private void linkLabelEasySubtitles_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://thesubdb.com");
}
private void linkLabelDevelopedBy_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://jpsfs.com");
}
private void dataGridViewEpisodes_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView dataGrid = sender as DataGridView;
if (dataGrid.Columns[dataGrid.CurrentCell.ColumnIndex].Name == "AvailableLanguages" && e.Control is ComboBox)
{
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectionChangeCommitted -= LastColumnComboSelectionChanged;
comboBox.SelectionChangeCommitted += LastColumnComboSelectionChanged;
}
}
private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
ComboBox combo = sender as ComboBox;
DataGridView dataGrid = combo.Parent.Parent as DataGridView;
Episode ep = EpisodeList[dataGrid.CurrentRow.Index];
if(combo.SelectedItem != null) ep.Language = combo.SelectedItem.ToString();
}
private void comboBoxPrimaryLanguage_SelectionChangeCommitted(object sender, EventArgs e)
{
foreach (Episode episode in EpisodeList)
{
if(episode.AvailableLanguages == null) continue;
if(episode.AvailableLanguages.Contains(comboBoxPrimaryLanguage.SelectedItem.ToString())){
episode.Language = comboBoxPrimaryLanguage.SelectedItem.ToString();
}
}
}
}
}