PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/JMMClient/JMMClient/UserControls/Downloads/DownloadsSearchTorrentsControl.xaml.cs

https://bitbucket.org/gibwar/jmm-test
C# | 561 lines | 447 code | 106 blank | 8 comment | 77 complexity | f996c7be08f886065dcda8c9b7de4b49 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.ComponentModel;
  15. using System.Collections.ObjectModel;
  16. using JMMClient.Downloads;
  17. using JMMClient.ViewModel;
  18. using System.Diagnostics;
  19. using System.Net;
  20. namespace JMMClient.UserControls
  21. {
  22. /// <summary>
  23. /// Interaction logic for DownloadsSearchTorrentsControl.xaml
  24. /// </summary>
  25. public partial class DownloadsSearchTorrentsControl : UserControl
  26. {
  27. BackgroundWorker torrentDetailsWorker = new BackgroundWorker();
  28. BackgroundWorker searchWorker = new BackgroundWorker();
  29. public ICollectionView ViewTorrentLinks { get; set; }
  30. public ObservableCollection<TorrentLinkVM> TorrentLinks { get; set; }
  31. public ObservableCollection<SubGroupSimple> SubGroups { get; set; }
  32. public static readonly DependencyProperty TorrentCountProperty = DependencyProperty.Register("TorrentCount",
  33. typeof(int), typeof(DownloadsSearchTorrentsControl), new UIPropertyMetadata(0, null));
  34. public int TorrentCount
  35. {
  36. get { return (int)GetValue(TorrentCountProperty); }
  37. set { SetValue(TorrentCountProperty, value); }
  38. }
  39. public static readonly DependencyProperty HasAttachedSeriesProperty = DependencyProperty.Register("HasAttachedSeries",
  40. typeof(bool), typeof(DownloadsSearchTorrentsControl), new UIPropertyMetadata(false, null));
  41. public bool HasAttachedSeries
  42. {
  43. get { return (bool)GetValue(HasAttachedSeriesProperty); }
  44. set { SetValue(HasAttachedSeriesProperty, value); }
  45. }
  46. public static readonly DependencyProperty TorrentSearchDescriptionProperty = DependencyProperty.Register("TorrentSearchDescription",
  47. typeof(string), typeof(DownloadsSearchTorrentsControl), new UIPropertyMetadata("", null));
  48. public string TorrentSearchDescription
  49. {
  50. get { return (string)GetValue(TorrentSearchDescriptionProperty); }
  51. set { SetValue(TorrentSearchDescriptionProperty, value); }
  52. }
  53. public static readonly DependencyProperty CurrentSearchCriteriaProperty = DependencyProperty.Register("CurrentSearchCriteria",
  54. typeof(DownloadSearchCriteria), typeof(DownloadsSearchTorrentsControl), new UIPropertyMetadata(null, null));
  55. public DownloadSearchCriteria CurrentSearchCriteria
  56. {
  57. get { return (DownloadSearchCriteria)GetValue(CurrentSearchCriteriaProperty); }
  58. set { SetValue(CurrentSearchCriteriaProperty, value); }
  59. }
  60. public static readonly DependencyProperty TorrentSearchStatusProperty = DependencyProperty.Register("TorrentSearchStatus",
  61. typeof(string), typeof(DownloadsSearchTorrentsControl), new UIPropertyMetadata("", null));
  62. public string TorrentSearchStatus
  63. {
  64. get { return (string)GetValue(TorrentSearchStatusProperty); }
  65. set { SetValue(TorrentSearchStatusProperty, value); }
  66. }
  67. public DownloadsSearchTorrentsControl()
  68. {
  69. InitializeComponent();
  70. TorrentLinks = new ObservableCollection<TorrentLinkVM>();
  71. ViewTorrentLinks = CollectionViewSource.GetDefaultView(TorrentLinks);
  72. ViewTorrentLinks.Filter = LinkSearchFilter;
  73. SubGroups = new ObservableCollection<SubGroupSimple>();
  74. btnClearSearch.Click += new RoutedEventHandler(btnClearSearch_Click);
  75. txtFileSearch.TextChanged += new TextChangedEventHandler(txtFileSearch_TextChanged);
  76. dgTorrents.SelectionChanged += new SelectionChangedEventHandler(dgTorrents_SelectionChanged);
  77. dgTorrents.MouseLeftButtonUp += new MouseButtonEventHandler(dgTorrents_MouseLeftButtonUp);
  78. dgTorrents.LoadingRow += new EventHandler<DataGridRowEventArgs>(dgTorrents_LoadingRow);
  79. torrentDetailsWorker.DoWork += new DoWorkEventHandler(torrentDetailsWorker_DoWork);
  80. torrentDetailsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(torrentDetailsWorker_RunWorkerCompleted);
  81. searchWorker.DoWork += new DoWorkEventHandler(searchWorker_DoWork);
  82. searchWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(searchWorker_RunWorkerCompleted);
  83. btnSearch.Click += new RoutedEventHandler(btnSearch_Click);
  84. }
  85. private void CommandBinding_ToggleSource(object sender, ExecutedRoutedEventArgs e)
  86. {
  87. //object obj = lbGroupsSeries.SelectedItem;
  88. object obj = e.Parameter;
  89. if (obj == null) return;
  90. try
  91. {
  92. if (obj.GetType() == typeof(TorrentSourceVM))
  93. {
  94. TorrentSourceVM src = (TorrentSourceVM)obj;
  95. src.IsEnabled = !src.IsEnabled;
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. Utils.ShowErrorMessage(ex);
  101. }
  102. }
  103. private void CommandBinding_AddSubGroupSearch(object sender, ExecutedRoutedEventArgs e)
  104. {
  105. //object obj = lbGroupsSeries.SelectedItem;
  106. object obj = e.Parameter;
  107. if (obj == null) return;
  108. try
  109. {
  110. if (obj.GetType() == typeof(SubGroupSimple))
  111. {
  112. SubGroupSimple sub = (SubGroupSimple)obj;
  113. txtSearch.Text = txtSearch.Text + " " + sub.GroupNameShort;
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. Utils.ShowErrorMessage(ex);
  119. }
  120. }
  121. private void CommandBinding_AddSubGroupFilter(object sender, ExecutedRoutedEventArgs e)
  122. {
  123. //object obj = lbGroupsSeries.SelectedItem;
  124. object obj = e.Parameter;
  125. if (obj == null) return;
  126. try
  127. {
  128. if (obj.GetType() == typeof(SubGroupSimple))
  129. {
  130. SubGroupSimple sub = (SubGroupSimple)obj;
  131. string newSearch = txtFileSearch.Text.Trim();
  132. if (!string.IsNullOrEmpty(newSearch))
  133. newSearch += " ";
  134. newSearch += sub.GroupNameShort;
  135. txtFileSearch.Text = newSearch;
  136. }
  137. }
  138. catch (Exception ex)
  139. {
  140. Utils.ShowErrorMessage(ex);
  141. }
  142. }
  143. void searchWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  144. {
  145. try
  146. {
  147. List<TorrentLinkVM> links = e.Result as List<TorrentLinkVM>;
  148. TorrentCount = links.Count;
  149. foreach (TorrentLinkVM link in links)
  150. TorrentLinks.Add(link);
  151. ViewTorrentLinks.Refresh();
  152. List<GroupVideoQualityVM> vidQualListTemp = new List<GroupVideoQualityVM>();
  153. if (CurrentSearchCriteria.SearchType == DownloadSearchType.Episode)
  154. {
  155. AnimeEpisodeVM ep = CurrentSearchCriteria.SearchParameter as AnimeEpisodeVM;
  156. if (ep.AniDB_Anime == null) ep.RefreshAnime();
  157. if (ep.AniDB_Anime != null)
  158. {
  159. List<JMMServerBinary.Contract_GroupVideoQuality> summ = JMMServerVM.Instance.clientBinaryHTTP.GetGroupVideoQualitySummary(ep.AniDB_Anime.AnimeID);
  160. foreach (JMMServerBinary.Contract_GroupVideoQuality contract in summ)
  161. {
  162. GroupVideoQualityVM vidQual = new GroupVideoQualityVM(contract);
  163. vidQualListTemp.Add(vidQual);
  164. }
  165. }
  166. }
  167. if (CurrentSearchCriteria.SearchType == DownloadSearchType.Series)
  168. {
  169. AniDB_AnimeVM anime = CurrentSearchCriteria.SearchParameter as AniDB_AnimeVM;
  170. if (anime != null)
  171. {
  172. List<JMMServerBinary.Contract_GroupVideoQuality> summ = JMMServerVM.Instance.clientBinaryHTTP.GetGroupVideoQualitySummary(anime.AnimeID);
  173. foreach (JMMServerBinary.Contract_GroupVideoQuality contract in summ)
  174. {
  175. GroupVideoQualityVM vidQual = new GroupVideoQualityVM(contract);
  176. vidQualListTemp.Add(vidQual);
  177. }
  178. }
  179. }
  180. ShowSubGroupSuggestions(vidQualListTemp);
  181. TorrentSearchStatus = string.Format("{0} Results", links.Count);
  182. }
  183. catch (Exception ex)
  184. {
  185. Utils.ShowErrorMessage(ex);
  186. }
  187. finally
  188. {
  189. this.Cursor = Cursors.Arrow;
  190. this.IsEnabled = true;
  191. }
  192. }
  193. private void ShowSubGroupSuggestions(List<GroupVideoQualityVM> vidQualList)
  194. {
  195. SubGroups.Clear();
  196. Dictionary<string, GroupVideoQualityVM> vidQuals = new Dictionary<string, GroupVideoQualityVM>();
  197. foreach (GroupVideoQualityVM vidQual in vidQualList)
  198. vidQuals[vidQual.GroupNameShort] = vidQual;
  199. foreach (GroupVideoQualityVM vidq in vidQuals.Values)
  200. {
  201. if (vidq.GroupNameShort != "NO GROUP INFO")
  202. {
  203. SubGroupSimple sub = new SubGroupSimple();
  204. sub.GroupName = vidq.GroupName;
  205. sub.GroupNameShort = vidq.GroupNameShort;
  206. SubGroups.Add(sub);
  207. }
  208. }
  209. }
  210. void searchWorker_DoWork(object sender, DoWorkEventArgs e)
  211. {
  212. try
  213. {
  214. DownloadSearchCriteria crit = e.Argument as DownloadSearchCriteria;
  215. List<TorrentLinkVM> links = DownloadHelper.SearchTorrents(crit);
  216. e.Result = links;
  217. }
  218. catch (Exception ex)
  219. {
  220. }
  221. }
  222. public void PerformSearch(DownloadSearchCriteria crit)
  223. {
  224. this.Cursor = Cursors.Wait;
  225. TorrentSearchStatus = string.Format("Searching...");
  226. try
  227. {
  228. CurrentSearchCriteria = crit;
  229. this.IsEnabled = false;
  230. if (crit.SearchType != DownloadSearchType.Manual)
  231. {
  232. string desc = "";
  233. foreach (string parm in crit.GetParms())
  234. {
  235. if (!string.IsNullOrEmpty(desc))
  236. desc += " ";
  237. desc += parm;
  238. }
  239. txtSearch.Text = desc;
  240. }
  241. SubGroups.Clear();
  242. TorrentLinks.Clear();
  243. ViewTorrentLinks.Refresh();
  244. searchWorker.RunWorkerAsync(crit);
  245. }
  246. catch (Exception ex)
  247. {
  248. Utils.ShowErrorMessage(ex);
  249. }
  250. }
  251. void btnSearch_Click(object sender, RoutedEventArgs e)
  252. {
  253. if (string.IsNullOrEmpty(txtSearch.Text)) return;
  254. DownloadSearchCriteria crit = new DownloadSearchCriteria(DownloadSearchType.Manual, txtSearch.Text.Trim());
  255. PerformSearch(crit);
  256. }
  257. private void ShowTorrentDetails(DetailsContainer details)
  258. {
  259. SetAttachedSeries(null);
  260. if (!torrentDetailsWorker.IsBusy)
  261. torrentDetailsWorker.RunWorkerAsync(details);
  262. }
  263. private void SetAttachedSeries(AnimeSeriesVM ser)
  264. {
  265. try
  266. {
  267. HasAttachedSeries = ser != null;
  268. if (ser == null)
  269. {
  270. this.DataContext = null;
  271. this.ucFileSummary.DataContext = null;
  272. return;
  273. }
  274. this.DataContext = ser;
  275. this.ucFileSummary.DataContext = ser.AniDB_Anime;
  276. List<GroupVideoQualityVM> vidQuals = new List<GroupVideoQualityVM>(this.ucFileSummary.VideoQualityRecords);
  277. ShowSubGroupSuggestions(vidQuals);
  278. }
  279. catch (Exception ex)
  280. {
  281. Utils.ShowErrorMessage(ex);
  282. }
  283. }
  284. void torrentDetailsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  285. {
  286. AnimeSeriesVM ser = e.Result as AnimeSeriesVM;
  287. SetAttachedSeries(ser);
  288. }
  289. void torrentDetailsWorker_DoWork(object sender, DoWorkEventArgs e)
  290. {
  291. DetailsContainer details = e.Argument as DetailsContainer;
  292. if (details == null) return;
  293. if (details.SearchCritera != null)
  294. {
  295. if (details.SearchCritera.SearchType == DownloadSearchType.Episode)
  296. {
  297. AnimeEpisodeVM ep = details.SearchCritera.SearchParameter as AnimeEpisodeVM;
  298. AnimeSeriesVM ser = MainListHelperVM.Instance.GetSeries(ep.AnimeSeriesID);
  299. if (ser != null)
  300. {
  301. e.Result = ser;
  302. return;
  303. }
  304. }
  305. if (details.SearchCritera.SearchType == DownloadSearchType.Series)
  306. {
  307. AniDB_AnimeVM anime = details.SearchCritera.SearchParameter as AniDB_AnimeVM;
  308. AnimeSeriesVM ser = MainListHelperVM.Instance.GetSeriesForAnime(anime.AnimeID);
  309. if (ser != null)
  310. {
  311. e.Result = ser;
  312. return;
  313. }
  314. }
  315. }
  316. // try and find the series
  317. foreach (AniDB_AnimeVM anime in AniDB_AnimeVM.BestLevenshteinDistanceMatchesCache(details.TorLink.ClosestAnimeMatchString, 10))
  318. {
  319. // get the series for the anime
  320. AnimeSeriesVM ser = MainListHelperVM.Instance.GetSeriesForAnime(anime.AnimeID);
  321. if (ser != null)
  322. {
  323. e.Result = ser;
  324. return;
  325. }
  326. }
  327. e.Result = null;
  328. }
  329. void dgTorrents_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  330. {
  331. DataGrid _DataGrid = sender as DataGrid;
  332. TorrentLinkVM torLink = _DataGrid.SelectedItem as TorrentLinkVM;
  333. if (torLink == null) return;
  334. DetailsContainer details = new DetailsContainer();
  335. details.TorLink = torLink;
  336. details.SearchCritera = CurrentSearchCriteria;
  337. ShowTorrentDetails(details);
  338. }
  339. void dgTorrents_SelectionChanged(object sender, SelectionChangedEventArgs e)
  340. {
  341. DataGrid _DataGrid = sender as DataGrid;
  342. TorrentLinkVM torLink = _DataGrid.SelectedItem as TorrentLinkVM;
  343. if (torLink == null) return;
  344. DetailsContainer details = new DetailsContainer();
  345. details.TorLink = torLink;
  346. details.SearchCritera = CurrentSearchCriteria;
  347. ShowTorrentDetails(details);
  348. }
  349. void dgTorrents_LoadingRow(object sender, DataGridRowEventArgs e)
  350. {
  351. e.Row.MouseRightButtonDown += new MouseButtonEventHandler(Row_MouseRightButtonDown);
  352. }
  353. void Row_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  354. {
  355. DataGridRow dgr = sender as DataGridRow;
  356. if (dgr == null) return;
  357. dgTorrents.SelectedItem = dgr.DataContext;
  358. ContextMenu m = new ContextMenu();
  359. TorrentLinkVM torLink = dgTorrents.SelectedItem as TorrentLinkVM;
  360. if (torLink == null) return;
  361. MenuItem itemStart = new MenuItem();
  362. itemStart.Header = "Download";
  363. itemStart.Click += new RoutedEventHandler(torrentDownload);
  364. itemStart.CommandParameter = torLink;
  365. m.Items.Add(itemStart);
  366. if (!string.IsNullOrEmpty(torLink.TorrentLink))
  367. {
  368. MenuItem itemLink = new MenuItem();
  369. itemLink.Header = "Go to Website";
  370. itemLink.Click += new RoutedEventHandler(torrentBrowseWebsite);
  371. itemLink.CommandParameter = torLink;
  372. m.Items.Add(itemLink);
  373. }
  374. m.IsOpen = true;
  375. }
  376. void torrentBrowseWebsite(object sender, RoutedEventArgs e)
  377. {
  378. try
  379. {
  380. MenuItem item = e.Source as MenuItem;
  381. MenuItem itemSender = sender as MenuItem;
  382. if (item == null || itemSender == null) return;
  383. if (!item.Header.ToString().Equals(itemSender.Header.ToString())) return;
  384. if (item != null && item.CommandParameter != null)
  385. {
  386. Window parentWindow = Window.GetWindow(this);
  387. parentWindow.Cursor = Cursors.Wait;
  388. this.IsEnabled = false;
  389. TorrentLinkVM torLink = item.CommandParameter as TorrentLinkVM;
  390. Uri uri = new Uri(torLink.TorrentLinkFull);
  391. Process.Start(new ProcessStartInfo(uri.AbsoluteUri));
  392. parentWindow.Cursor = Cursors.Arrow;
  393. this.IsEnabled = true;
  394. }
  395. }
  396. catch (Exception ex)
  397. {
  398. Utils.ShowErrorMessage(ex);
  399. }
  400. }
  401. void torrentDownload(object sender, RoutedEventArgs e)
  402. {
  403. try
  404. {
  405. this.Cursor = Cursors.Wait;
  406. this.IsEnabled = false;
  407. MenuItem item = e.Source as MenuItem;
  408. MenuItem itemSender = sender as MenuItem;
  409. if (item == null || itemSender == null) return;
  410. if (!item.Header.ToString().Equals(itemSender.Header.ToString())) return;
  411. if (item != null && item.CommandParameter != null)
  412. {
  413. Window parentWindow = Window.GetWindow(this);
  414. parentWindow.Cursor = Cursors.Wait;
  415. this.IsEnabled = false;
  416. TorrentLinkVM torLink = item.CommandParameter as TorrentLinkVM;
  417. torLink.Source.PopulateTorrentDownloadLink(ref torLink);
  418. UTorrentHelperVM.Instance.AddTorrentFromURL(torLink.TorrentDownloadLink);
  419. parentWindow.Cursor = Cursors.Arrow;
  420. this.IsEnabled = true;
  421. }
  422. }
  423. catch (Exception ex)
  424. {
  425. Utils.ShowErrorMessage(ex);
  426. }
  427. finally
  428. {
  429. this.Cursor = Cursors.Arrow;
  430. this.IsEnabled = true;
  431. }
  432. }
  433. private bool LinkSearchFilter(object obj)
  434. {
  435. TorrentLinkVM torLink = obj as TorrentLinkVM;
  436. if (torLink == null) return true;
  437. int index = torLink.TorrentName.IndexOf(txtFileSearch.Text.Trim(), 0, StringComparison.InvariantCultureIgnoreCase);
  438. if (index > -1) return true;
  439. return false;
  440. }
  441. void txtFileSearch_TextChanged(object sender, TextChangedEventArgs e)
  442. {
  443. ViewTorrentLinks.Refresh();
  444. }
  445. void btnClearSearch_Click(object sender, RoutedEventArgs e)
  446. {
  447. txtFileSearch.Text = "";
  448. }
  449. }
  450. public class DetailsContainer
  451. {
  452. public TorrentLinkVM TorLink { get; set; }
  453. public DownloadSearchCriteria SearchCritera { get; set; }
  454. }
  455. }