PageRenderTime 74ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 2ms

/Branches/seufert/MediaScout/MainWindow.xaml.cs

#
C# | 6494 lines | 5691 code | 519 blank | 284 comment | 778 complexity | 22e5bce88000d27abf43f367c27f5dca MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using System.Windows.Shapes;
  12. using System.Windows.Interop;
  13. using System.IO;
  14. using System.Collections;
  15. using System.Collections.ObjectModel;
  16. using System.Data;
  17. using System.Threading;
  18. using System.Windows.Threading;
  19. using System.Text.RegularExpressions;
  20. using System.Xml;
  21. using System.Xml.Serialization;
  22. using System.Diagnostics;
  23. using System.Windows.Shell;
  24. using MediaScoutGUI.GUITypes;
  25. using MediaScout.Providers;
  26. namespace MediaScoutGUI
  27. {
  28. /// <summary>
  29. /// Interaction logic for Window1.xaml
  30. /// </summary>
  31. public partial class MainWindow : Window
  32. {
  33. //Delegates
  34. public MediaScout.MediaScoutMessage.Message Message;
  35. public delegate void MetadataCompletedHandler(Thread th, String reason, bool Reset);
  36. public event MetadataCompletedHandler MetadataCompleted;
  37. public delegate void TVShowChangedHandler(TVShow ts, bool IsRemoved);
  38. public event TVShowChangedHandler TVShowChanged;
  39. public delegate void SeasonChangedHandler(Season s, bool IsRemoved);
  40. public event SeasonChangedHandler SeasonChanged;
  41. public delegate void EpisodeChangedHandler(Episode e, bool IsRemoved);
  42. public event EpisodeChangedHandler EpisodeChanged;
  43. public delegate void MovieChangedHandler(Movie m, bool IsRemoved);
  44. public event MovieChangedHandler MovieChanged;
  45. public delegate void MovieFileChangedHandler(MovieFile mf, bool IsRemoved);
  46. public event MovieFileChangedHandler MovieFileChanged;
  47. public delegate void ActorThumbChangedHandler(Person p);
  48. public event ActorThumbChangedHandler ActorThumbChanged;
  49. public delegate void FocusChangedHandler();
  50. public delegate void TaskbarProgressValueChangeHandler(int value);
  51. public delegate void TaskbarProgressStatusChangeHandler(TaskbarItemProgressState state);
  52. public delegate void TVShowImageChangedHandler(TVShow ts, TVShowPosterType type, bool IsLoading);
  53. public delegate void SeasonImageChangedHandler(Season s, TVShowPosterType type, bool IsLoading);
  54. public delegate void EpisodeImageChangedHandler(Episode e, String filename, bool IsLoading);
  55. public delegate void MovieImageChangedHandler(Movie m, MoviePosterType type, bool IsLoading);
  56. public delegate void MovieFileImageChangedHandler(MovieFile mf, MoviePosterType type, bool IsLoading);
  57. public delegate void PosterChangedHandler(Object obj, String id, bool IsMovie, Season s, String file);
  58. public delegate void BackdropChangedHandler(Object obj, String id, bool IsMovie, Season s, String file, String file1);
  59. //Collections
  60. private ObservableCollection<TVShow> tvshows = new ObservableCollection<TVShow>();
  61. private ObservableCollection<Movie> movies = new ObservableCollection<Movie>();
  62. private DispatchingCollection<ObservableCollection<TVShow>, TVShow> dispatchtvshows;
  63. private DispatchingCollection<ObservableCollection<Movie>, Movie> dispatchmovies;
  64. //Settings
  65. private List<String> ignoredFiles = new List<String>();
  66. private List<String> AllowedFileTypes;
  67. private List<String> AllowedSubtitleTypes;
  68. MediaScoutApp app;
  69. private bool WindowLoaded = false;
  70. private bool WindowRendered = false;
  71. //private System.Windows.Forms.NotifyIcon notifyIcon;
  72. //private System.Windows.Forms.ToolStripMenuItem mnuCancel;
  73. private JumpList jumplist;
  74. private JumpTask jumpCancel;
  75. private JumpTask jumpCancelAll;
  76. private JumpTask jumpOperationsSeparator;
  77. //Objects
  78. private MediaScout.TVScout TVScout = null;
  79. private MediaScout.MovieScout MovieScout = null;
  80. private FileSystemWatcher TVFSWatcher = null;
  81. private FileSystemWatcher MovieFSWatcher = null;
  82. private TheTVDBProvider tvdb = null;
  83. private TheMovieDBProvider tmdb = null;
  84. private Collection<Thread> tvThreads = new Collection<Thread>();
  85. private bool resetTVfolder = false;
  86. private bool resetMoviefolder = false;
  87. int currentvalue = 0;
  88. int maxvalue = 0;
  89. private TVShow SelectedTVShow;
  90. private int SelectedTVShowIndex = 0;
  91. private TVShow UnsortedEpisodes;
  92. private Season SelectedSeason;
  93. private int SelectedSeasonIndex = 0;
  94. private Episode SelectedEpisode;
  95. private int SelectedEpisodeIndex = 0;
  96. private Movie SelectedMovie;
  97. private int SelectedMovieIndex = 0;
  98. private Movie UnsortedFiles;
  99. private MovieFile SelectedMovieFile;
  100. private int SelectedMovieFileIndex = 0;
  101. private Person SelectedPerson;
  102. //private int SelectedPersonIndex = 0;
  103. private int LogTabIndex = 1;
  104. private int SelectedTabIndex = 0;
  105. #region To Enable/Disable for the Aero glass effect
  106. public Rect GetBoundsForGlassFrame()
  107. {
  108. return VisualTreeHelper.GetContentBounds(tcTabs);
  109. }
  110. public bool SetGlassFrame(bool ExtendGlass)
  111. {
  112. bool success = false;
  113. if (ExtendGlass)
  114. {
  115. // Extend glass
  116. Rect bounds = GetBoundsForGlassFrame();
  117. success = GlassHelper.ExtendGlassFrame(this, new Thickness(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom));
  118. }
  119. else
  120. {
  121. // turn off glass...
  122. GlassHelper.DisableGlassFrame(this);
  123. success = true;
  124. }
  125. return success;
  126. }
  127. public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  128. {
  129. int WM_DWMCOMPOSITIONCHANGED = 0x031E; // for glass (when DWM / glass setting is changed)
  130. // handle the message for DWM when the aero glass is turned on or off
  131. if (msg == WM_DWMCOMPOSITIONCHANGED)
  132. {
  133. SetGlassFrame(GlassHelper.IsGlassEnabled);
  134. handled = true;
  135. }
  136. return IntPtr.Zero;
  137. }
  138. #endregion
  139. #region Window Routines
  140. public MainWindow(int SelectedTabIndex)
  141. {
  142. try
  143. {
  144. this.InitializeComponent();
  145. app = ((MediaScoutApp)Application.Current);
  146. //notifyIcon = app.notifyIcon;
  147. jumplist = app.jumplist;
  148. this.SelectedTabIndex = SelectedTabIndex;
  149. this.Message = new MediaScout.MediaScoutMessage.Message(TVScout_Message);
  150. tvdb = new TheTVDBProvider(Message);
  151. tmdb = new TheMovieDBProvider(Message);
  152. this.MetadataCompleted += new MetadataCompletedHandler(ResetUI);
  153. this.TVShowChanged += new TVShowChangedHandler(ResetTVShow);
  154. this.SeasonChanged += new SeasonChangedHandler(ResetSeason);
  155. this.EpisodeChanged += new EpisodeChangedHandler(ResetEpisode);
  156. this.MovieChanged += new MovieChangedHandler(ResetMovie);
  157. this.MovieFileChanged += new MovieFileChangedHandler(ResetMovieFile);
  158. this.ActorThumbChanged += new ActorThumbChangedHandler(ResetActorThumb);
  159. //creating directories
  160. String CacheDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\MediaScout\Cache";
  161. String TVCacheDir = CacheDir + @"\TVCache";
  162. String MovieCacheDir = CacheDir + @"\MovieCache";
  163. if (!Directory.Exists(CacheDir))
  164. Directory.CreateDirectory(CacheDir);
  165. if (!Directory.Exists(TVCacheDir))
  166. Directory.CreateDirectory(TVCacheDir);
  167. if (!Directory.Exists(MovieCacheDir))
  168. Directory.CreateDirectory(MovieCacheDir);
  169. SetCancelButtons();
  170. dispatchtvshows = new DispatchingCollection<ObservableCollection<TVShow>, TVShow>(tvshows, Dispatcher);
  171. dispatchmovies = new DispatchingCollection<ObservableCollection<Movie>, Movie>(movies, Dispatcher);
  172. }
  173. catch (Exception ex)
  174. {
  175. MessageBox.Show(ex.Message);
  176. }
  177. }
  178. private void Window_Loaded(object sender, RoutedEventArgs e)
  179. {
  180. Rect bounds = GetBoundsForGlassFrame();
  181. HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
  182. source.AddHook(new HwndSourceHook(WndProc));
  183. WindowLoaded = true;
  184. LoadOptions();
  185. tcTabs.SelectedIndex = SelectedTabIndex;
  186. //Dispatcher.Hooks.OperationPosted += new DispatcherHookEventHandler(Hooks_OperationPosted);
  187. }
  188. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  189. {
  190. //Properties.Settings.Default.Zoom = zoomslider.Value.ToString();
  191. //Properties.Settings.Default.Save();
  192. //When the application is closed, check wether the application is
  193. //exiting from menu or forms close button
  194. //if (!isAppExiting)
  195. //{
  196. // //if the forms close button is triggered, cancel the event and hide the form
  197. // //then show the notification ballon tip
  198. // e.Cancel = true;
  199. //}
  200. }
  201. private void Window_StateChanged(object sender, EventArgs e)
  202. {
  203. // if (this.WindowState == WindowState.Minimized)
  204. // {
  205. // //Hide();
  206. // if (app.showballoontip)
  207. // {
  208. // notifyIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
  209. // notifyIcon.BalloonTipTitle = "Media Scout";
  210. // notifyIcon.BalloonTipText = "Application minimized to the system tray. To open the application, double-click the icon in the system tray.";
  211. // notifyIcon.ShowBalloonTip(400);
  212. // }
  213. // }
  214. }
  215. private void Window_Closed(object sender, EventArgs e)
  216. {
  217. AbortAllThreads();
  218. app.Shutdown();
  219. }
  220. private void Window_ContentRendered(object sender, EventArgs e)
  221. {
  222. WindowRendered = true;
  223. StartLoadingItems();
  224. }
  225. #endregion
  226. #region Loads all the options from the user configuration file
  227. private void LoadOptions()
  228. {
  229. //TV Options
  230. txtTVDropBox.Text = Properties.Settings.Default.TVDropBoxLocation;
  231. ChangeMonitorTVFolder();
  232. if(chkTVFSWatcher.IsEnabled)
  233. chkTVFSWatcher.IsChecked = Properties.Settings.Default.TVFSWatcher;
  234. chkSeriesPosters.IsChecked = Properties.Settings.Default.getSeriesPosters;
  235. chkSeasonPosters.IsChecked = Properties.Settings.Default.getSeasonPosters;
  236. chkEpPosters.IsChecked = Properties.Settings.Default.getEpisodePosters;
  237. chkMoveTVFiles.IsChecked = Properties.Settings.Default.moveTVFiles;
  238. chkSaveTVActors.IsChecked = Properties.Settings.Default.SaveTVActors;
  239. txtSeasonFolderName.Text = Properties.Settings.Default.SeasonFolderName;
  240. txtSpecialsFolderName.Text = Properties.Settings.Default.SpecialsFolderName;
  241. chkdownloadAllTVImages.IsChecked = Properties.Settings.Default.downloadAllTVImages;
  242. chkdownloadAllTVPosters.IsChecked = Properties.Settings.Default.downloadAllTVPosters;
  243. chkdownloadAllTVBackdrops.IsChecked = Properties.Settings.Default.downloadAllTVBackdrops;
  244. chkdownloadAllTVBanners.IsChecked = Properties.Settings.Default.downloadAllTVBanners;
  245. chkdownloadAllTVSeasonPosters.IsChecked = Properties.Settings.Default.downloadAllTVSeasonPosters;
  246. chkdownloadAllTVSeasonBackdrops.IsChecked = Properties.Settings.Default.downloadAllTVSeasonBackdrops;
  247. chkRenameTVFiles.IsChecked = Properties.Settings.Default.renameTVFiles;
  248. txtTVRenameFormat.Text = Properties.Settings.Default.TVfileformat;
  249. txtSeasonNumZeroPadding.Text = Properties.Settings.Default.SeasonNumZeroPadding;
  250. txtEpisodeNumZeroPadding.Text = Properties.Settings.Default.EpisodeNumZeroPadding;
  251. //Movie Options
  252. txtMovieDropBox.Text = Properties.Settings.Default.MovieDropBoxLocation;
  253. ChangeMonitorMovieFolder();
  254. if(chkMovieFSWatcher.IsEnabled)
  255. chkMovieFSWatcher.IsChecked = Properties.Settings.Default.MovieFSWatcher;
  256. chkMoviePosters.IsChecked = Properties.Settings.Default.getMoviePosters;
  257. chkMovieFilePosters.IsChecked = Properties.Settings.Default.getMovieFilePosters;
  258. chkMoveMovieFiles.IsChecked = Properties.Settings.Default.moveMovieFiles;
  259. chkdownloadAllMovieImages.IsChecked = Properties.Settings.Default.downloadAllMovieImages;
  260. chkdownloadAllMoviePosters.IsChecked = Properties.Settings.Default.downloadAllMoviePosters;
  261. chkdownloadAllMovieBackdrops.IsChecked = Properties.Settings.Default.downloadAllMovieBackdrops;
  262. chkSaveMovieActors.IsChecked = Properties.Settings.Default.SaveMovieActors;
  263. chkRenameMovieFiles.IsChecked = Properties.Settings.Default.renameMovieFiles;
  264. txtMovieFileRenameFormat.Text = Properties.Settings.Default.Moviefileformat;
  265. txtMovieDirRenameFormat.Text = Properties.Settings.Default.MovieDirformat;
  266. //File Options
  267. txtAllowedFiletypes.Text = Properties.Settings.Default.allowedFileTypes;
  268. txtAllowedSubtitles.Text = Properties.Settings.Default.allowedSubtitles;
  269. txtSearchTermFilters.Text = Properties.Settings.Default.SearchTermFilters;
  270. chkForceUpdate.IsChecked = Properties.Settings.Default.forceUpdate;
  271. chkOverwrite.IsChecked = Properties.Settings.Default.overwriteFiles;
  272. chkSilentMode.IsChecked = Properties.Settings.Default.SilentMode;
  273. chkAutoSelectMovieTitle.IsChecked = Properties.Settings.Default.AutoSelectMovieTitle;
  274. chkforceEnterSearchTerm.IsChecked = Properties.Settings.Default.forceEnterSearchTerm;
  275. chkEnableGlassFrame.IsChecked = Properties.Settings.Default.EnableGlassFrame;
  276. chkSaveXBMCMeta.IsChecked = Properties.Settings.Default.SaveXBMCMeta;
  277. chkSaveMMMeta.IsChecked = Properties.Settings.Default.SaveMyMoviesMeta;
  278. txtImagesByName.Text = Properties.Settings.Default.ImagesByNameLocation;
  279. /* Load Variables */
  280. AllowedFileTypes = new List<string>(Properties.Settings.Default.allowedFileTypes.Split(';'));
  281. AllowedSubtitleTypes = new List<string>(Properties.Settings.Default.allowedSubtitles.Split(';'));
  282. //zoomslider.Value = Double.Parse(Properties.Settings.Default.Zoom);
  283. }
  284. #endregion
  285. #region Thread Completeion Handlers
  286. private void SetTVShowTabItemsVisibility(Visibility v)
  287. {
  288. //lbShowTitle.Visibility = v;
  289. //btnSaveTVShow.Visibility = v;
  290. //lbShowDesc.Visibility = v;
  291. //tcTVShowCategories.Visibility = v;
  292. //tcTVShowPersons.Visibility = v;
  293. //lbTVShowFirstAired.Visibility = v;
  294. //dtTVShowFirstAired.Visibility = v;
  295. //lbShowRating.Visibility = v;
  296. //ratingTVShow.Visibility = v;
  297. //tvTVShows.ContextMenu.Visibility = v;
  298. gridSeriesView.Visibility = v;
  299. gridSeasonsView.Visibility = v;
  300. gridEpisodesView.Visibility = v;
  301. gridSeriesView.ContextMenu.Visibility = v;
  302. }
  303. private void SetMovieTabItemsVisibility(Visibility v)
  304. {
  305. //lbMovieTitle.Visibility = v;
  306. //btnSaveMovie.Visibility = v;
  307. //lbMovieDesc.Visibility = v;
  308. //tcMovieCategories.Visibility = v;
  309. //tcMoviePersons.Visibility = v;
  310. //lbMovieYear.Visibility = v;
  311. //lbMovieRating.Visibility = v;
  312. //ratingMovie.Visibility = v;
  313. //lbMovies.ContextMenu.Visibility = v;
  314. gridMovie.Visibility = v;
  315. gridMovieFile.Visibility = v;
  316. gridMovie.ContextMenu.Visibility = v;
  317. }
  318. public void AbortAllThreads()
  319. {
  320. if (tvThreads.Count > 0)
  321. {
  322. foreach (Thread th in tvThreads)
  323. th.Abort();
  324. tvThreads.Clear();
  325. MetadataCompleted(null, "All Operations aborted", true);
  326. }
  327. }
  328. private void ResetUI(Thread th, String reason, bool Reset)
  329. {
  330. if (!Dispatcher.CheckAccess())
  331. {
  332. Dispatcher.Invoke(DispatcherPriority.Normal, new MetadataCompletedHandler(ResetUI), th, reason, Reset);
  333. return;
  334. }
  335. if (th != null)
  336. {
  337. th.Abort();
  338. tvThreads.Remove(th);
  339. if (th.Name == "Loading TV Shows")
  340. {
  341. LoadingTVShows = false;
  342. if (tvshows.Count > 0)
  343. {
  344. gridSeriesView.ContextMenu.Visibility = Visibility.Visible;
  345. //SetFocusOnTVShow();
  346. txtSearchTVShow.Focus();
  347. //tvTVShows.SelectedIndex = 0;
  348. }
  349. }
  350. if (th.Name == "Loading Movies")
  351. {
  352. LoadingMovies = false;
  353. if (movies.Count > 0)
  354. {
  355. gridMovie.ContextMenu.Visibility = Visibility.Visible;
  356. //SetFocusOnMovie();
  357. txtSearchMovie.Focus();
  358. //lbMovies.SelectedIndex = 0;
  359. }
  360. }
  361. }
  362. if (Reset && tvThreads.Count == 0)
  363. {
  364. HideCancelButtons();
  365. maxvalue = 0;
  366. currentvalue = 0;
  367. TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None;
  368. }
  369. if (reason != null)
  370. Message(((th != null && th.Name != null) ? th.Name + " : " : "") + reason, MediaScout.MediaScoutMessage.MessageType.Task, 0);
  371. }
  372. private void ResetTVShow(TVShow ts, bool IsRemoved)
  373. {
  374. if (!Dispatcher.CheckAccess())
  375. {
  376. Dispatcher.Invoke(DispatcherPriority.Normal, new TVShowChangedHandler(ResetTVShow), ts, IsRemoved);
  377. return;
  378. }
  379. tvTVShows.SelectedIndex = -1;
  380. int tsindex = tvshows.IndexOf(ts);
  381. tvshows.RemoveAt(tsindex);
  382. if (!IsRemoved)
  383. {
  384. ts = new TVShow(ts.Folderpath, ts.Foldername, ts.IsUnsortedEpisodeCollection);
  385. tvshows.Insert(tsindex, ts);
  386. }
  387. tvTVShows.SelectedIndex = SelectedTVShowIndex;
  388. }
  389. private void ResetSeason(Season s, bool IsRemoved)
  390. {
  391. if (!Dispatcher.CheckAccess())
  392. {
  393. Dispatcher.Invoke(DispatcherPriority.Normal, new SeasonChangedHandler(ResetSeason), s, IsRemoved);
  394. return;
  395. }
  396. TVShow ts = s.TVShow;
  397. tvSeasons.SelectedIndex = -1;
  398. int tsindex = tvshows.IndexOf(ts);
  399. int sindex = tvshows[tsindex].Seasons.IndexOf(s);
  400. tvshows[tsindex].Seasons.RemoveAt(sindex);
  401. if (!IsRemoved)
  402. {
  403. s = new Season(s.Folderpath, s.Name, ts);
  404. tvshows[tsindex].Seasons.Insert(sindex, s);
  405. }
  406. tvSeasons.SelectedIndex = SelectedSeasonIndex;
  407. }
  408. private void ResetEpisode(Episode e, bool IsRemoved)
  409. {
  410. if (!Dispatcher.CheckAccess())
  411. {
  412. Dispatcher.Invoke(DispatcherPriority.Normal, new EpisodeChangedHandler(ResetEpisode), e, IsRemoved);
  413. return;
  414. }
  415. Season s = e.Season;
  416. //TVShow ts = s.TVShow;
  417. tvEpisodes.SelectedIndex = -1;
  418. //int tsindex = tvshows.IndexOf(ts);
  419. //int sindex = tvshows[tsindex].Seasons.IndexOf(s);
  420. //int eindex = tvshows[tsindex].Seasons[sindex].Episodes.IndexOf(e);
  421. //tvshows[tsindex].Seasons[sindex].Episodes.RemoveAt(eindex);
  422. int eindex = e.Season.Episodes.IndexOf(e);
  423. e.Season.Episodes.RemoveAt(eindex);
  424. if (!IsRemoved)
  425. {
  426. e = new Episode(e.Filepath, e.Name, s);
  427. //tvshows[tsindex].Seasons[sindex].Episodes.Insert(eindex, e);
  428. s.Episodes.Insert(eindex, e);
  429. }
  430. tvEpisodes.SelectedIndex = SelectedEpisodeIndex;
  431. }
  432. private void ResetMovie(Movie m, bool IsRemoved)
  433. {
  434. if (!Dispatcher.CheckAccess())
  435. {
  436. Dispatcher.Invoke(DispatcherPriority.Normal, new MovieChangedHandler(ResetMovie), m, IsRemoved);
  437. return;
  438. }
  439. lbMovies.SelectedIndex = -1;
  440. int mindex = movies.IndexOf(m);
  441. movies.RemoveAt(mindex);
  442. if (!IsRemoved)
  443. {
  444. m = new Movie(m.Folderpath, m.Foldername, m.IsUnsortedFileCollection);
  445. movies.Insert(mindex, m);
  446. }
  447. lbMovies.SelectedIndex = SelectedMovieIndex;
  448. }
  449. private void ResetMovieFile(MovieFile mf, bool IsRemoved)
  450. {
  451. if (!Dispatcher.CheckAccess())
  452. {
  453. Dispatcher.Invoke(DispatcherPriority.Normal, new MovieFileChangedHandler(ResetMovieFile), mf, IsRemoved);
  454. return;
  455. }
  456. Movie m = mf.Movie;
  457. lbMovieFiles.SelectedIndex = -1;
  458. int mindex = movies.IndexOf(m);
  459. int mfindex = movies[mindex].Files.IndexOf(mf);
  460. movies[mindex].Files.RemoveAt(mfindex);
  461. if (!IsRemoved)
  462. {
  463. mf = new MovieFile(mf.Filepath, mf.Name, m);
  464. movies[mindex].Files.Insert(mfindex, mf);
  465. }
  466. lbMovieFiles.SelectedIndex = SelectedMovieFileIndex;
  467. }
  468. private void ResetActorThumb(Person p)
  469. {
  470. if (!Dispatcher.CheckAccess())
  471. {
  472. Dispatcher.Invoke(DispatcherPriority.Normal, new ActorThumbChangedHandler(ResetActorThumb), p);
  473. return;
  474. }
  475. p.Thumb = p.GetImage(p.XBMCFolderPath);
  476. }
  477. #endregion
  478. #region Reload TV and Movie Button Click Routines
  479. private void btnReloadSelectedTV_Click(object sender, RoutedEventArgs e)
  480. {
  481. TVShow ts = SelectedTVShow;
  482. TVShowChanged(ts, false);
  483. }
  484. private void btnReloadSelectedTVs_Click(object sender, RoutedEventArgs e)
  485. {
  486. foreach(TVShow ts in tvshows)
  487. TVShowChanged(ts, false);
  488. }
  489. private void btnReloadAllTV_Click(object sender, RoutedEventArgs e)
  490. {
  491. loadTVShows();
  492. }
  493. private void btnReloadSelectedMovie_Click(object sender, RoutedEventArgs e)
  494. {
  495. Movie m = SelectedMovie;
  496. MovieChanged(m, false);
  497. }
  498. private void btnReloadSelectedMovies_Click(object sender, RoutedEventArgs e)
  499. {
  500. foreach(Movie m in movies)
  501. MovieChanged(m, false);
  502. }
  503. private void btnReloadAllMovie_Click(object sender, RoutedEventArgs e)
  504. {
  505. loadMovies();
  506. }
  507. #endregion
  508. #region Cancel Button Routines
  509. #region Set/Show/Hide Cancel Button Routines
  510. private void SetCancelButtons()
  511. {
  512. ////Set the NotifyIcon Cancel Button
  513. //mnuCancel = new System.Windows.Forms.ToolStripMenuItem();
  514. //mnuCancel.Text = "Cancel";
  515. //mnuCancel.Click += new EventHandler(mnuCancel_Click);
  516. //mnuCancel.Visible = false;
  517. //notifyIcon.ContextMenuStrip.Items.Add(mnuCancel);
  518. //Set the Jumplist Cancel item
  519. jumpCancel = new JumpTask();
  520. jumpCancel.ApplicationPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
  521. jumpCancel.Arguments = "/Cancel:";
  522. jumpCancel.IconResourcePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
  523. jumpCancel.IconResourceIndex = 4;
  524. jumpCancel.Title = "Cancel";
  525. jumpCancel.Description = "Cancels Last Operation";
  526. jumpCancelAll = new JumpTask();
  527. jumpCancelAll.ApplicationPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
  528. jumpCancelAll.Arguments = "/CancelAll:";
  529. jumpCancelAll.IconResourcePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
  530. jumpCancelAll.IconResourceIndex = 4;
  531. jumpCancelAll.Title = "Cancel All";
  532. jumpCancelAll.Description = "Cancels All Operation";
  533. jumpOperationsSeparator = new JumpTask();
  534. }
  535. private void ShowCancelButtons()
  536. {
  537. btnCancelAll.Visibility = Visibility.Visible;
  538. btnCancel.Visibility = Visibility.Visible;
  539. //mnuCancel.Visible = true;
  540. TaskbarItemInfo.Overlay = (BitmapSource)this.FindResource("imgoverlay");
  541. if (!jumplist.JumpItems.Contains(jumpOperationsSeparator))
  542. {
  543. List<JumpItem> jumplistitems = new List<JumpItem>();
  544. jumplistitems.Add(jumpCancel);
  545. jumplistitems.Add(jumpCancelAll);
  546. jumplistitems.Add(jumpOperationsSeparator);
  547. jumplist.JumpItems.InsertRange(0, jumplistitems);
  548. jumplist.Apply();
  549. }
  550. }
  551. private void HideCancelButtons()
  552. {
  553. btnCancelAll.Visibility = Visibility.Collapsed;
  554. btnCancel.Visibility = Visibility.Collapsed;
  555. //mnuCancel.Visible = false;
  556. TaskbarItemInfo.Overlay = null;
  557. if (jumplist.JumpItems.Contains(jumpOperationsSeparator))
  558. {
  559. jumplist.JumpItems.Remove(jumpOperationsSeparator);
  560. jumplist.JumpItems.Remove(jumpCancel);
  561. jumplist.JumpItems.Remove(jumpCancelAll);
  562. jumplist.Apply();
  563. }
  564. }
  565. #endregion
  566. private void btnCancelAll_Click(object sender, RoutedEventArgs e)
  567. {
  568. AbortAllThreads();
  569. }
  570. private void _tbCancelAllButton_Click(object sender, EventArgs e)
  571. {
  572. AbortAllThreads();
  573. }
  574. public void CancelOperation(Thread th)
  575. {
  576. if (th == null)
  577. th = tvThreads[tvThreads.Count - 1];
  578. MetadataCompleted(th, "Operation Aborted", true);
  579. }
  580. private void btnCancel_Click(object sender, RoutedEventArgs e)
  581. {
  582. CancelOperation(null);
  583. }
  584. private void _tbCancelButton_Click(object sender, EventArgs e)
  585. {
  586. CancelOperation(null);
  587. }
  588. private void _tbPauseButton_Click(object sender, EventArgs e)
  589. {
  590. if (_tbPauseButton.Description == "Pause")
  591. {
  592. _tbPauseButton.Description = "Play";
  593. _tbPauseButton.ImageSource = (BitmapImage)Resources["PlayImage"];
  594. tvThreads[tvThreads.Count - 1].Suspend();
  595. SetTasbkBarProgressStatus(TaskbarItemProgressState.Paused);
  596. }
  597. else
  598. {
  599. _tbPauseButton.Description = "Pause";
  600. _tbPauseButton.ImageSource = (BitmapImage)Resources["PauseImage"];
  601. tvThreads[tvThreads.Count - 1].Resume();
  602. SetTasbkBarProgressStatus(TaskbarItemProgressState.Normal);
  603. }
  604. }
  605. #endregion
  606. #region Focus Set Routines
  607. private void SetFocusOnTVShow()
  608. {
  609. //if (!Dispatcher.CheckAccess())
  610. //{
  611. // Dispatcher.Invoke(DispatcherPriority.Normal, new FocusChangedHandler(SetFocusOnTVShow));
  612. // return;
  613. //}
  614. if (tvTVShows.SelectedIndex == -1)
  615. tvTVShows.SelectedIndex = 0;
  616. if (tvTVShows.Items.Count > 0)
  617. {
  618. if (tvTVShows.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
  619. {
  620. ListBoxItem lbi = tvTVShows.ItemContainerGenerator.ContainerFromIndex(SelectedTVShowIndex) as ListBoxItem;
  621. if (lbi != null)
  622. {
  623. lbi.IsSelected = true;
  624. lbi.Focus();
  625. }
  626. }
  627. }
  628. }
  629. private void SetFocusOnSeason()
  630. {
  631. //if (!Dispatcher.CheckAccess())
  632. //{
  633. // Dispatcher.Invoke(DispatcherPriority.Normal, new FocusChangedHandler(SetFocusOnSeason));
  634. // return;
  635. //}
  636. if (tvSeasons.SelectedIndex == -1)
  637. tvSeasons.SelectedIndex = 0;
  638. if (tvSeasons.Items.Count > 0)
  639. {
  640. if (tvSeasons.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
  641. {
  642. ListBoxItem lbi = tvSeasons.ItemContainerGenerator.ContainerFromIndex(SelectedSeasonIndex) as ListBoxItem;
  643. if (lbi != null)
  644. {
  645. lbi.IsSelected = true;
  646. lbi.Focus();
  647. }
  648. }
  649. }
  650. }
  651. private void SetFocusOnEpisode()
  652. {
  653. //if (!Dispatcher.CheckAccess())
  654. //{
  655. // Dispatcher.Invoke(DispatcherPriority.Normal, new FocusChangedHandler(SetFocusOnEpisode));
  656. // return;
  657. //}
  658. if (tvEpisodes.SelectedIndex == -1)
  659. tvEpisodes.SelectedIndex = 0;
  660. if (tvEpisodes.Items.Count > 0)
  661. {
  662. if (tvEpisodes.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
  663. {
  664. ListBoxItem lbi = tvEpisodes.ItemContainerGenerator.ContainerFromIndex(SelectedEpisodeIndex) as ListBoxItem;
  665. if (lbi != null)
  666. {
  667. lbi.IsSelected = true;
  668. lbi.Focus();
  669. }
  670. }
  671. }
  672. }
  673. private void SetFocusOnMovie()
  674. {
  675. //if (!Dispatcher.CheckAccess())
  676. //{
  677. // Dispatcher.Invoke(DispatcherPriority.Normal, new FocusChangedHandler(SetFocusOnMovie));
  678. // return;
  679. //}
  680. if (lbMovies.SelectedIndex == -1)
  681. lbMovies.SelectedIndex = 0;
  682. if (lbMovies.Items.Count > 0)
  683. {
  684. if (lbMovies.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
  685. {
  686. ListBoxItem lbi = lbMovies.ItemContainerGenerator.ContainerFromIndex(SelectedMovieIndex) as ListBoxItem;
  687. if (lbi != null)
  688. {
  689. lbi.IsSelected = true;
  690. lbi.Focus();
  691. }
  692. }
  693. }
  694. }
  695. private void SetFocusOnMovieFile()
  696. {
  697. //if (!Dispatcher.CheckAccess())
  698. //{
  699. // Dispatcher.Invoke(DispatcherPriority.Normal, new FocusChangedHandler(SetFocusOnMovieFile));
  700. // return;
  701. //}
  702. if (lbMovieFiles.SelectedIndex == -1)
  703. lbMovieFiles.SelectedIndex = 0;
  704. if (lbMovieFiles.Items.Count > 0)
  705. {
  706. if (lbMovieFiles.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
  707. {
  708. ListBoxItem lbi = lbMovieFiles.ItemContainerGenerator.ContainerFromIndex(SelectedMovieFileIndex) as ListBoxItem;
  709. if (lbi != null)
  710. {
  711. lbi.IsSelected = true;
  712. lbi.Focus();
  713. }
  714. }
  715. }
  716. }
  717. private void SetTVTabFocus()
  718. {
  719. if (gridSeriesView.Visibility == Visibility.Visible)
  720. SetFocusOnTVShow();
  721. else if (gridSeasonsView.Visibility == Visibility.Visible)
  722. SetFocusOnSeason();
  723. else if (gridEpisodesView.Visibility == Visibility.Visible)
  724. SetFocusOnEpisode();
  725. }
  726. private void SetMovieTabFocus()
  727. {
  728. if (gridMovie.Visibility == Visibility.Visible)
  729. SetFocusOnMovie();
  730. else if (gridMovieFile.Visibility == Visibility.Visible)
  731. SetFocusOnMovieFile();
  732. }
  733. #endregion
  734. #region Tab Selection and Keyboard Routines
  735. private void tabTVSeries_KeyDown(object sender, KeyEventArgs e)
  736. {
  737. if (e.Key == Key.E && Keyboard.Modifiers == ModifierKeys.Control)
  738. txtSearchTVShow.Focus();
  739. else if (e.Key == Key.Enter)
  740. {
  741. if (gridSeriesView.Visibility == Visibility.Visible)
  742. {
  743. if (SelectedTVShow != null)
  744. ShowSeasons();
  745. }
  746. else if (gridSeasonsView.Visibility == Visibility.Visible)
  747. {
  748. if (SelectedSeason != null)
  749. ShowEpisodeList();
  750. }
  751. }
  752. else if (e.Key == Key.Delete)
  753. {
  754. if (gridSeriesView.Visibility == Visibility.Visible)
  755. {
  756. if (tvTVShows.SelectedItem != null)
  757. btnStripSelectedTV_Click(null, null);
  758. }
  759. else if (gridSeasonsView.Visibility == Visibility.Visible)
  760. {
  761. if (tvSeasons.SelectedItem != null)
  762. btnStripSelectedSeason_Click(null, null);
  763. }
  764. }
  765. else if (e.Key == Key.Back)
  766. {
  767. if (gridSeasonsView.Visibility == Visibility.Visible)
  768. ShowTVShowList();
  769. else if (gridEpisodesView.Visibility == Visibility.Visible)
  770. ShowSeasons();
  771. }
  772. }
  773. private void tabMovies_KeyDown(object sender, KeyEventArgs e)
  774. {
  775. if (e.Key == Key.E && Keyboard.Modifiers == ModifierKeys.Control)
  776. txtSearchMovie.Focus();
  777. else if (e.Key == Key.Enter)
  778. {
  779. if (gridMovie.Visibility == Visibility.Visible)
  780. {
  781. if (SelectedMovie != null)
  782. ShowMovieFileList();
  783. }
  784. }
  785. else if (e.Key == Key.Delete)
  786. {
  787. if (gridMovie.Visibility == Visibility.Visible)
  788. {
  789. if (SelectedMovie != null)
  790. btnStripSelectedMovie_Click(null, null);
  791. }
  792. else if (gridMovieFile.Visibility == Visibility.Visible)
  793. {
  794. if (SelectedMovie != null)
  795. btnStripSelectedMovieFile_Click(null, null);
  796. }
  797. }
  798. else if (e.Key == Key.Back)
  799. {
  800. if (gridMovieFile.Visibility == Visibility.Visible)
  801. ShowMovieList();
  802. }
  803. }
  804. private void tcTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
  805. {
  806. if (WindowRendered)
  807. StartLoadingItems();
  808. }
  809. private void StartLoadingItems()
  810. {
  811. //Find out what tab is selected. If the program is just loading up, then the
  812. // tab control might not be generated or have a selected tab yet, so we have
  813. // to take into account a "null" scenario.
  814. TabItem CurrentTab = tcTabs.SelectedItem as TabItem;
  815. String CurrentTabName = "";
  816. if (CurrentTab != null)
  817. CurrentTabName = CurrentTab.Name;
  818. //switch (tcTabs.SelectedIndex)
  819. switch (CurrentTabName)
  820. {
  821. case "tabTVSeries":
  822. StartLoadingTVShows();
  823. break;
  824. case "tabMovies":
  825. StartLoadingMovies();
  826. break;
  827. default:
  828. break;
  829. }
  830. }
  831. #endregion
  832. #region Taskbar Progress Status Routines
  833. private void SetTasbkBarProgressStatus(TaskbarItemProgressState state)
  834. {
  835. if (!Dispatcher.CheckAccess())
  836. {
  837. Dispatcher.Invoke(DispatcherPriority.Normal, new TaskbarProgressStatusChangeHandler(SetTasbkBarProgressStatus), state);
  838. return;
  839. }
  840. TaskbarItemInfo.ProgressState = state;
  841. }
  842. private void SetTasbkBarProgressValue(int value)
  843. {
  844. if (!Dispatcher.CheckAccess())
  845. {
  846. Dispatcher.Invoke(DispatcherPriority.Send, new TaskbarProgressValueChangeHandler(SetTasbkBarProgressValue), value);
  847. return;
  848. }
  849. if (value != maxvalue)
  850. {
  851. if (TaskbarItemInfo.ProgressState != TaskbarItemProgressState.Normal)
  852. TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Normal;
  853. TaskbarItemInfo.ProgressValue = ((double)value/maxvalue);
  854. }
  855. else
  856. TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None;
  857. }
  858. #endregion
  859. #region Loading TV and Movie Routines
  860. private void StartLoadingTVShows()
  861. {
  862. //Make sure the TVFolder is set before attempting to load in all the TV show data.
  863. System.Collections.Specialized.StringCollection TVFolders = Properties.Settings.Default.TVFolders;
  864. if (TVFolders == null || TVFolders.Count == 0)
  865. {
  866. if (tvshows.Count > 0)
  867. {
  868. tvshows.Clear();
  869. SetTVShowTabItemsVisibility(Visibility.Hidden);
  870. UpdateTVPoster(null, TVShowPosterType.Banner, false);
  871. UpdateTVPoster(null, TVShowPosterType.Poster, false);
  872. UpdateTVPoster(null, TVShowPosterType.Backdrop, false);
  873. }
  874. return;
  875. }
  876. if ((tvshows == null || tvshows.Count == 0 || resetTVfolder) && WindowRendered)
  877. {
  878. resetTVfolder = false;
  879. loadTVShows();
  880. gridTVSeries.DataContext = dispatchtvshows;
  881. }
  882. }
  883. bool LoadingTVShows = false;
  884. private void loadTVShows()
  885. {
  886. if (!LoadingTVShows)
  887. {
  888. LoadingTVShows = true;
  889. tvTVShows.SelectedIndex = -1;
  890. tvshows.Clear();
  891. //Load it on a seperate thread (hence the use of the DispatchingCollection) so as not to lag UI
  892. ShowCancelButtons();
  893. Thread th = null;
  894. th = new Thread(delegate()
  895. {
  896. foreach (String dir in Properties.Settings.Default.TVFolders)
  897. {
  898. if (Directory.Exists(dir))
  899. {
  900. DirectoryInfo TVShows = new DirectoryInfo(dir);
  901. maxvalue += TVShows.GetDirectories().Length;
  902. try
  903. {
  904. int UnsortedFilesLength = 0;
  905. bool containsUnsortedFiles = false;
  906. foreach (FileInfo fi in TVShows.GetFiles())
  907. {
  908. if (AllowedFileTypes.Contains(fi.Extension))
  909. {
  910. containsUnsortedFiles = true;
  911. UnsortedFilesLength++;
  912. }
  913. }
  914. if (containsUnsortedFiles)
  915. {
  916. maxvalue += UnsortedFilesLength;
  917. TVShow t = new TVShow(TVShows.FullName, "Unsorted Episodes", true);
  918. UnsortedEpisodes = t;
  919. tvshows.Add(t);
  920. }
  921. foreach (DirectoryInfo di in TVShows.GetDirectories())
  922. {
  923. TVShow t = new TVShow(di.FullName, di.Name, false);
  924. tvshows.Add(t);
  925. SetTasbkBarProgressValue(++currentvalue);
  926. }
  927. }
  928. catch (Exception ex)
  929. {
  930. if (th.ThreadState != System.Threading.ThreadState.AbortRequested)
  931. {
  932. SetTasbkBarProgressStatus(TaskbarItemProgressState.Error);
  933. MessageBox.Show(ex.Message);
  934. }
  935. }
  936. }
  937. }
  938. MetadataCompleted(th, tvshows.Count.ToString(), true);
  939. });
  940. th.Name = "Loading TV Shows";
  941. th.SetApartmentState(ApartmentState.STA);
  942. th.Start();
  943. tvThreads.Add(th);
  944. }
  945. }
  946. private void StartLoadingMovies()
  947. {
  948. //Make sure the MovieFolder is set before attempting to load in all the movies data.
  949. System.Collections.Specialized.StringCollection MovieFolders = Properties.Settings.Default.MovieFolders;
  950. if (MovieFolders == null || MovieFolders.Count == 0)
  951. {
  952. if (movies.Count > 0)
  953. {
  954. movies.Clear();
  955. SetMovieTabItemsVisibility(Visibility.Hidden);
  956. UpdateMoviePoster(null, MoviePosterType.Poster, false);
  957. UpdateMoviePoster(null, MoviePosterType.Backdrop, false);
  958. }
  959. return;
  960. }
  961. if (movies == null || movies.Count == 0 || resetMoviefolder)
  962. {
  963. resetMoviefolder = false;
  964. loadMovies();
  965. gridMovies.DataContext = dispatchmovies;
  966. }
  967. }
  968. bool LoadingMovies = false;
  969. private void loadMovies()
  970. {
  971. if (!LoadingMovies)
  972. {
  973. LoadingMovies = true;
  974. lbMovies.SelectedIndex = -1;
  975. movies.Clear();
  976. ShowCancelButtons();
  977. Thread th = null;
  978. th = new Thread(delegate()
  979. {
  980. foreach (String dir in Properties.Settings.Default.MovieFolders)
  981. {
  982. if (Directory.Exists(dir))
  983. {
  984. DirectoryInfo Movies = new DirectoryInfo(dir);
  985. maxvalue += Movies.GetDirectories().Length;
  986. try
  987. {
  988. int UnsortedFilesLength = 0;
  989. bool containsUnsortedFiles = false;
  990. foreach (FileInfo fi in Movies.GetFiles())
  991. {
  992. if (AllowedFileTypes.Contains(fi.Extension))
  993. {
  994. containsUnsortedFiles = true;
  995. UnsortedFilesLength++;
  996. }
  997. }
  998. maxvalue += UnsortedFilesLength;
  999. if (containsUnsortedFiles)
  1000. {
  1001. Movie m = new Movie(Movies.FullName, "Unsorted Files", true);
  1002. UnsortedFiles = m;
  1003. movies.Add(m);
  1004. }
  1005. foreach (DirectoryInfo di in Movies.GetDirectories())
  1006. {
  1007. Movie m = new Movie(di.FullName, di.Name, false);
  1008. movies.Add(m);
  1009. SetTasbkBarProgressValue(++currentvalue);
  1010. }
  1011. }
  1012. catch (Exception ex)
  1013. {
  1014. if (th.ThreadState != System.Threading.ThreadState.AbortRequested)
  1015. {
  1016. SetTasbkBarProgressStatus(TaskbarItemProgressState.Error);
  1017. MessageBox.Show(ex.Message);
  1018. }
  1019. }
  1020. }
  1021. }
  1022. MetadataCompleted(th, movies.Count.ToString(), true);
  1023. });
  1024. th.Name = "Loading Movies";
  1025. th.SetApartmentState(ApartmentState.STA);
  1026. th.Start();
  1027. tvThreads.Add(th);
  1028. }
  1029. }
  1030. #endregion
  1031. #region Open In Explorer Routines
  1032. private void btnOpenTVShow_Click(object sender, RoutedEventArgs e)
  1033. {
  1034. System.Diagnostics.Process.Start("explorer.exe", "/select," + SelectedTVShow.Folderpath);
  1035. }
  1036. private void btnOpenSeason_Click(object sender, RoutedEventArgs e)
  1037. {
  1038. System.Diagnostics.Process.Start("explorer.exe", "/select," + SelectedSeason.Folderpath);
  1039. }
  1040. private void btnOpenSeasonMetadata_Click(object sender, RoutedEventArgs e)
  1041. {
  1042. String dir = SelectedSeason.MetadataFolderPath;
  1043. if(Directory.Exists(dir))
  1044. System.Diagnostics.Process.Start("explorer.exe", dir);
  1045. else
  1046. MessageBox.Show("Directory doesn't Exist");
  1047. }
  1048. private void btnOpenEpisode_Click(object sender, RoutedEventArgs e)
  1049. {
  1050. String file = SelectedEpisode.Filepath;
  1051. if (File.Exists(file))
  1052. System.Diagnostics.Process.Start("explorer.exe", "/select," + file);
  1053. else
  1054. MessageBox.Show("File doesn't Exist");
  1055. }
  1056. private void btnOpenEpisodeXMLMetadata_Click(object sender, RoutedEventArgs e)
  1057. {
  1058. String file = SelectedEpisode.XMLFile;
  1059. if(File.Exists(file))
  1060. System.Diagnostics.Process.Start("explorer.exe", "/select," + file);
  1061. else
  1062. MessageBox.Show("File doesn't Exist");
  1063. }
  1064. private void btnOpenMovie_Click(object sender, RoutedEventArgs e)
  1065. {
  1066. System.Diagnostics.Process.Start("explorer.exe", "/select," + SelectedMovie.Folderpath);
  1067. }
  1068. p

Large files files are truncated, but you can click here to view the full file