PageRenderTime 85ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 2ms

/Branches/krishmav/MediaScout/MainWindow.xaml.cs

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

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