PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/JMMClient/JMMClient/UserControls/Downloads/DownloadsTorrentMonitorControl.xaml.cs

https://bitbucket.org/gibwar/jmm-test
C# | 423 lines | 327 code | 88 blank | 8 comment | 83 complexity | b967950de7ab3a3a4005a66640b94fd0 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 JMMClient.Downloads;
  15. using System.Diagnostics;
  16. using System.ComponentModel;
  17. namespace JMMClient.UserControls
  18. {
  19. /// <summary>
  20. /// Interaction logic for DownloadsTorrentMonitorControl.xaml
  21. /// </summary>
  22. public partial class DownloadsTorrentMonitorControl : UserControl
  23. {
  24. BackgroundWorker torrentDetailsWorker = new BackgroundWorker();
  25. public static readonly DependencyProperty HasAttachedSeriesProperty = DependencyProperty.Register("HasAttachedSeries",
  26. typeof(bool), typeof(DownloadsTorrentMonitorControl), new UIPropertyMetadata(false, null));
  27. public bool HasAttachedSeries
  28. {
  29. get { return (bool)GetValue(HasAttachedSeriesProperty); }
  30. set { SetValue(HasAttachedSeriesProperty, value); }
  31. }
  32. public DownloadsTorrentMonitorControl()
  33. {
  34. InitializeComponent();
  35. dgTorrents.SelectionChanged += new SelectionChangedEventHandler(dgTorrents_SelectionChanged);
  36. dgTorrents.MouseLeftButtonUp += new MouseButtonEventHandler(dgTorrents_MouseLeftButtonUp);
  37. btnRefresh.Click += new RoutedEventHandler(btnRefresh_Click);
  38. dgTorrents.LoadingRow += new EventHandler<DataGridRowEventArgs>(dgTorrents_LoadingRow);
  39. torrentDetailsWorker.DoWork += new DoWorkEventHandler(torrentDetailsWorker_DoWork);
  40. torrentDetailsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(torrentDetailsWorker_RunWorkerCompleted);
  41. }
  42. void dgTorrents_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  43. {
  44. DataGrid _DataGrid = sender as DataGrid;
  45. Torrent tor = _DataGrid.SelectedItem as Torrent;
  46. if (tor == null) return;
  47. ShowTorrentDetails(tor);
  48. }
  49. void torrentDetailsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  50. {
  51. TorrentDetails det = e.Result as TorrentDetails;
  52. // show files
  53. dgTorrentFiles.ItemsSource = det.TorrentFiles;
  54. // show details
  55. SetAttachedSeries(det.AnimeSeries);
  56. }
  57. void torrentDetailsWorker_DoWork(object sender, DoWorkEventArgs e)
  58. {
  59. Torrent tor = e.Argument as Torrent;
  60. List<TorrentFile> files = UTorrentHelperVM.Instance.GetFilesForTorrent(tor.Hash);
  61. TorrentDetails det = new TorrentDetails();
  62. det.TorrentFiles = files;
  63. // try and find the series
  64. foreach (AniDB_AnimeVM anime in AniDB_AnimeVM.BestLevenshteinDistanceMatchesCache(tor.ClosestAnimeMatchString, 10))
  65. {
  66. // get the series for the anime
  67. AnimeSeriesVM ser = MainListHelperVM.Instance.GetSeriesForAnime(anime.AnimeID);
  68. if (ser != null)
  69. {
  70. det.AnimeSeries = ser;
  71. break;
  72. }
  73. }
  74. e.Result = det;
  75. }
  76. void dgTorrents_LoadingRow(object sender, DataGridRowEventArgs e)
  77. {
  78. e.Row.MouseRightButtonDown += new MouseButtonEventHandler(Row_MouseRightButtonDown);
  79. }
  80. void Row_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  81. {
  82. DataGridRow dgr = sender as DataGridRow;
  83. if (dgr == null) return;
  84. if (dgTorrents.Items == null || dgTorrents.Items.Count == 0) return;
  85. if (dgTorrents.SelectedItems == null || dgTorrents.SelectedItems.Count == 0)
  86. dgTorrents.SelectedItem = dgr.DataContext;
  87. if (dgTorrents.SelectedItems.Count == 1)
  88. dgTorrents.SelectedItem = dgr.DataContext;
  89. ContextMenu m = new ContextMenu();
  90. List<Torrent> selectedTorrents = new List<Torrent>();
  91. foreach (object obj in dgTorrents.SelectedItems)
  92. {
  93. Torrent tor = obj as Torrent;
  94. selectedTorrents.Add(tor);
  95. }
  96. MenuItem itemStart = new MenuItem();
  97. itemStart.Header = "Start";
  98. itemStart.Click += new RoutedEventHandler(torrentStart);
  99. itemStart.CommandParameter = selectedTorrents;
  100. MenuItem itemStop = new MenuItem();
  101. itemStop.Header = "Stop";
  102. itemStop.Click += new RoutedEventHandler(torrentStop);
  103. itemStop.CommandParameter = selectedTorrents;
  104. MenuItem itemPause = new MenuItem();
  105. itemPause.Header = "Pause";
  106. itemPause.Click += new RoutedEventHandler(torrentPause);
  107. itemPause.CommandParameter = selectedTorrents;
  108. MenuItem itemRemove = new MenuItem();
  109. itemRemove.Header = "Remove Torrent";
  110. itemRemove.Click += new RoutedEventHandler(torrentRemove);
  111. itemRemove.CommandParameter = selectedTorrents;
  112. MenuItem itemRemoveData = new MenuItem();
  113. itemRemoveData.Header = "Remove Torrent and Files";
  114. itemRemoveData.Click += new RoutedEventHandler(torrentRemoveData);
  115. itemRemoveData.CommandParameter = selectedTorrents;
  116. if (selectedTorrents.Count == 1)
  117. {
  118. Torrent tor = selectedTorrents[0];
  119. if (tor.IsNotRunning || tor.IsPaused)
  120. m.Items.Add(itemStart);
  121. if (tor.IsRunning || tor.IsPaused)
  122. m.Items.Add(itemStop);
  123. if (tor.IsRunning)
  124. m.Items.Add(itemPause);
  125. }
  126. else
  127. {
  128. m.Items.Add(itemStart);
  129. m.Items.Add(itemStop);
  130. m.Items.Add(itemPause);
  131. }
  132. m.Items.Add(itemRemove);
  133. m.Items.Add(itemRemoveData);
  134. m.IsOpen = true;
  135. }
  136. void torrentRemoveData(object sender, RoutedEventArgs e)
  137. {
  138. try
  139. {
  140. MenuItem item = e.Source as MenuItem;
  141. MenuItem itemSender = sender as MenuItem;
  142. if (item == null || itemSender == null) return;
  143. if (!item.Header.ToString().Equals(itemSender.Header.ToString())) return;
  144. if (item != null && item.CommandParameter != null)
  145. {
  146. List<Torrent> selectedTorrents = item.CommandParameter as List<Torrent>;
  147. string msg = "";
  148. if (selectedTorrents.Count == 1)
  149. msg = string.Format("Are you sure you want to remove this torrent and delete associated files: {0}", selectedTorrents[0].Name);
  150. else
  151. msg = string.Format("Are you sure you want to remove these {0} torrents and delete associated files?", selectedTorrents.Count);
  152. MessageBoxResult res = MessageBox.Show(msg, "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
  153. if (res == MessageBoxResult.Yes)
  154. {
  155. Window parentWindow = Window.GetWindow(this);
  156. parentWindow.Cursor = Cursors.Wait;
  157. this.IsEnabled = false;
  158. foreach (Torrent tor in selectedTorrents)
  159. UTorrentHelperVM.Instance.RemoveTorrentAndData(tor.Hash);
  160. parentWindow.Cursor = Cursors.Arrow;
  161. this.IsEnabled = true;
  162. }
  163. }
  164. }
  165. catch (Exception ex)
  166. {
  167. Utils.ShowErrorMessage(ex);
  168. }
  169. }
  170. void torrentRemove(object sender, RoutedEventArgs e)
  171. {
  172. try
  173. {
  174. MenuItem item = e.Source as MenuItem;
  175. MenuItem itemSender = sender as MenuItem;
  176. if (item == null || itemSender == null) return;
  177. if (!item.Header.ToString().Equals(itemSender.Header.ToString())) return;
  178. if (item != null && item.CommandParameter != null)
  179. {
  180. List<Torrent> selectedTorrents = item.CommandParameter as List<Torrent>;
  181. string msg = "";
  182. if (selectedTorrents.Count == 1)
  183. msg = string.Format("Are you sure you want to remove this torrent: {0}", selectedTorrents[0].Name);
  184. else
  185. msg = string.Format("Are you sure you want to remove these {0} torrents?", selectedTorrents.Count);
  186. MessageBoxResult res = MessageBox.Show(msg,"Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
  187. if (res == MessageBoxResult.Yes)
  188. {
  189. Window parentWindow = Window.GetWindow(this);
  190. parentWindow.Cursor = Cursors.Wait;
  191. this.IsEnabled = false;
  192. foreach (Torrent tor in selectedTorrents)
  193. UTorrentHelperVM.Instance.RemoveTorrent(tor.Hash);
  194. parentWindow.Cursor = Cursors.Arrow;
  195. this.IsEnabled = true;
  196. }
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. Utils.ShowErrorMessage(ex);
  202. }
  203. }
  204. void torrentPause(object sender, RoutedEventArgs e)
  205. {
  206. try
  207. {
  208. MenuItem item = e.Source as MenuItem;
  209. MenuItem itemSender = sender as MenuItem;
  210. if (item == null || itemSender == null) return;
  211. if (!item.Header.ToString().Equals(itemSender.Header.ToString())) return;
  212. if (item != null && item.CommandParameter != null)
  213. {
  214. Window parentWindow = Window.GetWindow(this);
  215. parentWindow.Cursor = Cursors.Wait;
  216. this.IsEnabled = false;
  217. List<Torrent> selectedTorrents = item.CommandParameter as List<Torrent>;
  218. foreach (Torrent tor in selectedTorrents)
  219. UTorrentHelperVM.Instance.PauseTorrent(tor.Hash);
  220. parentWindow.Cursor = Cursors.Arrow;
  221. this.IsEnabled = true;
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. Utils.ShowErrorMessage(ex);
  227. }
  228. }
  229. void torrentStop(object sender, RoutedEventArgs e)
  230. {
  231. try
  232. {
  233. MenuItem item = e.Source as MenuItem;
  234. MenuItem itemSender = sender as MenuItem;
  235. if (item == null || itemSender == null) return;
  236. if (!item.Header.ToString().Equals(itemSender.Header.ToString())) return;
  237. if (item != null && item.CommandParameter != null)
  238. {
  239. Window parentWindow = Window.GetWindow(this);
  240. parentWindow.Cursor = Cursors.Wait;
  241. this.IsEnabled = false;
  242. List<Torrent> selectedTorrents = item.CommandParameter as List<Torrent>;
  243. foreach (Torrent tor in selectedTorrents)
  244. UTorrentHelperVM.Instance.StopTorrent(tor.Hash);
  245. parentWindow.Cursor = Cursors.Arrow;
  246. this.IsEnabled = true;
  247. }
  248. }
  249. catch (Exception ex)
  250. {
  251. Utils.ShowErrorMessage(ex);
  252. }
  253. }
  254. void torrentStart(object sender, RoutedEventArgs e)
  255. {
  256. try
  257. {
  258. MenuItem item = e.Source as MenuItem;
  259. MenuItem itemSender = sender as MenuItem;
  260. if (item == null || itemSender == null) return;
  261. if (!item.Header.ToString().Equals(itemSender.Header.ToString())) return;
  262. if (item != null && item.CommandParameter != null)
  263. {
  264. Window parentWindow = Window.GetWindow(this);
  265. parentWindow.Cursor = Cursors.Wait;
  266. this.IsEnabled = false;
  267. List<Torrent> selectedTorrents = item.CommandParameter as List<Torrent>;
  268. foreach (Torrent tor in selectedTorrents)
  269. {
  270. //Debug.WriteLine(tor.ToString());
  271. UTorrentHelperVM.Instance.StartTorrent(tor.Hash);
  272. }
  273. parentWindow.Cursor = Cursors.Arrow;
  274. this.IsEnabled = true;
  275. }
  276. }
  277. catch (Exception ex)
  278. {
  279. Utils.ShowErrorMessage(ex);
  280. }
  281. }
  282. void btnRefresh_Click(object sender, RoutedEventArgs e)
  283. {
  284. if (UserSettingsVM.Instance.UTorrentAutoRefresh)
  285. {
  286. MessageBox.Show("Only use when auto refresh is disabled", "Warning", MessageBoxButton.OK, MessageBoxImage.Information);
  287. return;
  288. }
  289. Window parentWindow = Window.GetWindow(this);
  290. parentWindow.Cursor = Cursors.Wait;
  291. this.IsEnabled = false;
  292. UTorrentHelperVM.Instance.RefreshTorrents();
  293. parentWindow.Cursor = Cursors.Arrow;
  294. this.IsEnabled = true;
  295. }
  296. void dgTorrents_SelectionChanged(object sender, SelectionChangedEventArgs e)
  297. {
  298. DataGrid _DataGrid = sender as DataGrid;
  299. Torrent tor = _DataGrid.SelectedItem as Torrent;
  300. if (tor == null) return;
  301. ShowTorrentDetails(tor);
  302. }
  303. private void ShowTorrentDetails(Torrent tor)
  304. {
  305. SetAttachedSeries(null);
  306. dgTorrentFiles.ItemsSource = null;
  307. if (!torrentDetailsWorker.IsBusy)
  308. torrentDetailsWorker.RunWorkerAsync(tor);
  309. }
  310. private void SetAttachedSeries(AnimeSeriesVM ser)
  311. {
  312. try
  313. {
  314. HasAttachedSeries = ser != null;
  315. if (ser == null)
  316. {
  317. this.DataContext = null;
  318. this.ucFileSummary.DataContext = null;
  319. return;
  320. }
  321. this.DataContext = ser;
  322. this.ucFileSummary.DataContext = ser.AniDB_Anime;
  323. }
  324. catch (Exception ex)
  325. {
  326. Utils.ShowErrorMessage(ex);
  327. }
  328. }
  329. }
  330. public class TorrentDetails
  331. {
  332. public List<TorrentFile> TorrentFiles { get; set; }
  333. public AnimeSeriesVM AnimeSeries { get; set; }
  334. }
  335. }