PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/CBR/CBR/Views/MainView.xaml.cs

#
C# | 471 lines | 332 code | 60 blank | 79 comment | 34 complexity | 2d62ae9a936881be35844b825f6b27bd MD5 | raw file
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Threading;
  8. using CBR.Components.Dialogs;
  9. using CBR.Core.Helpers;
  10. using CBR.Core.Helpers.Localization;
  11. using CBR.Core.Helpers.Splash;
  12. using CBR.Core.Helpers.State;
  13. using CBR.Core.Models;
  14. using CBR.Core.Services;
  15. using CBR.ViewModels;
  16. using CBR.Views.Others;
  17. using Fluent;
  18. using GalaSoft.MvvmLight.Messaging;
  19. using Xceed.Wpf.AvalonDock.Layout.Serialization;
  20. namespace CBR.Views
  21. {
  22. /// <summary>
  23. /// Interaction logic for MainView.xaml
  24. /// </summary>
  25. public partial class MainView : RibbonWindow
  26. {
  27. #region ----------------CONSTRUCTOR----------------
  28. private WMIEventWatcher _wmiWatcher = null;
  29. /// <summary>
  30. /// Constructor
  31. /// </summary>
  32. public MainView(string[] param)
  33. {
  34. using (new TimeLogger("MainView.MainView"))
  35. {
  36. InitializeComponent();
  37. if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
  38. {
  39. DataContext = new MainViewModel(param.Count() > 0 ? param[0] : string.Empty);
  40. Messenger.Default.Register<NotificationMessage>(this, "Color", (s) =>
  41. {
  42. Uri source = new Uri(
  43. string.Format("/CBR;Component/Resources/XAML/Colors/Colors.{0}.xaml", s.Notification),
  44. UriKind.Relative);
  45. Uri source2 = new Uri(
  46. string.Format("/Fluent;Component/Themes/Office2010/{0}.xaml", s.Notification),
  47. UriKind.Relative);
  48. Application.Current.Resources.BeginInit();
  49. Application.Current.Resources.MergedDictionaries.RemoveAt(0);
  50. Application.Current.Resources.MergedDictionaries.RemoveAt(0);
  51. Application.Current.Resources.MergedDictionaries.Insert(0, (ResourceDictionary)Application.LoadComponent(source));
  52. Application.Current.Resources.MergedDictionaries.Insert(0, (ResourceDictionary)Application.LoadComponent(source2));
  53. Application.Current.Resources.EndInit();
  54. });
  55. }
  56. //start wmi watcher and load existing devices
  57. Task.Factory.StartNew(() =>
  58. {
  59. try
  60. {
  61. _wmiWatcher = new WMIEventWatcher();
  62. _wmiWatcher.StartWatchUSB();
  63. _wmiWatcher.EventArrived += new WMIEventArrived(wmi_EventArrived);
  64. }
  65. catch (Exception err)
  66. {
  67. LogHelper.Manage("MainView.MainView start wmi watcher", err);
  68. }
  69. });
  70. //start task to manage ByCode translations
  71. Task.Factory.StartNew(() =>
  72. {
  73. try
  74. {
  75. CultureManager.Instance.GetLocalization("ByCode", "Warning", "Warning");
  76. CultureManager.Instance.GetLocalization("ByCode", "Warning.Save", "Save the catalog and book changes ?");
  77. CultureManager.Instance.GetLocalization("ByCode", "Warning.Delete", "Please, confirm the deletion");
  78. CultureManager.Instance.GetLocalization("ByCode", "Warning.ScanFolder", "Your book folder is allready defined. Do you want to replace it ? Refreshing will work only with the new one.");
  79. CultureManager.Instance.GetLocalization("ByCode", "Warning.CatalogPath", "Catalog path does not exist! Please check the options box");
  80. CultureManager.Instance.GetLocalization("ByCode", "DocumentTitle.Home", "Home");
  81. CultureManager.Instance.GetLocalization("ByCode", "DocumentTitle.Drives", "Drives");
  82. CultureManager.Instance.GetLocalization("ByCode", "DocumentTitle.Libraries", "Libraries");
  83. CultureManager.Instance.GetLocalization("ByCode", "ExplorerView.Title", "Library Explorer");
  84. CultureManager.Instance.GetLocalization("ByCode", "TocView.Title", "Table of content");
  85. CultureManager.Instance.GetLocalization("ByCode", "DriveExplorerView.Title", "Drive Explorer");
  86. CultureManager.Instance.GetLocalization("ByCode", "MEGA", "(Mb)");
  87. CultureManager.Instance.GetLocalization("ByCode", "KILO", "(Kb)");
  88. CultureManager.Instance.GetLocalization("ByCode", "Convert.Start", "Starting conversion...");
  89. CultureManager.Instance.GetLocalization("ByCode", "Convert.ParsingFolder", "Parsing folder {0}...");
  90. CultureManager.Instance.GetLocalization("ByCode", "Convert.Converting", "Converting {0}...");
  91. CultureManager.Instance.GetLocalization("ByCode", "Convert.ImageFound", "{0} images founded...");
  92. CultureManager.Instance.GetLocalization("ByCode", "Convert.ImageSaved", "{0} images saved...");
  93. CultureManager.Instance.GetLocalization("ByCode", "Convert.ImageCountingKO", "Extracting {0} : {1} images for {2} pages - Try to merge !");
  94. CultureManager.Instance.GetLocalization("ByCode", "Convert.ImageMerge", "Merge to {0} new images...");
  95. CultureManager.Instance.GetLocalization("ByCode", "Convert.ImageError", "Error extracting {0} : {1} images for {2} pages !!");
  96. CultureManager.Instance.GetLocalization("ByCode", "Convert.ImageCountingOK", "Extracting {0} images in {1} pages");
  97. CultureManager.Instance.GetLocalization("ByCode", "Convert.ImageExtracted", "{0} images extracted...");
  98. CultureManager.Instance.GetLocalization("ByCode", "Convert.ImageZipped", "{0} images zipped...");
  99. CultureManager.Instance.GetLocalization("ByCode", "Convert.Output", "Output file written !");
  100. }
  101. catch (Exception err)
  102. {
  103. LogHelper.Manage("MainView.MainView start localize", err);
  104. }
  105. });
  106. }
  107. }
  108. #endregion
  109. #region ----------------OTHERS----------------
  110. /// <summary>
  111. /// Display the about box
  112. /// </summary>
  113. /// <param name="sender"></param>
  114. /// <param name="e"></param>
  115. private void btnAbout_Click(object sender, RoutedEventArgs e)
  116. {
  117. AboutDialog dlg = new AboutDialog();
  118. dlg.Owner = Application.Current.MainWindow;
  119. dlg.ShowDialog();
  120. }
  121. #endregion
  122. #region ----------------FULL SCREEN----------------
  123. /// <summary>
  124. /// Full screen state
  125. /// </summary>
  126. private bool IsFullScreen { get; set; }
  127. private WindowState FullScreenPreviousMode { get; set; }
  128. /// <summary>
  129. /// full screen button click handler (binding is not working...)
  130. /// </summary>
  131. /// <param name="sender"></param>
  132. /// <param name="e"></param>
  133. private void btnFullScreen_Click(object sender, RoutedEventArgs e)
  134. {
  135. SwapFullScreenMode();
  136. }
  137. /// <summary>
  138. /// Change the full screen state
  139. /// </summary>
  140. private void SwapFullScreenMode()
  141. {
  142. try
  143. {
  144. if (IsFullScreen)
  145. {
  146. this.WindowState = FullScreenPreviousMode;
  147. IsFullScreen = false;
  148. }
  149. else
  150. {
  151. FullScreenPreviousMode = this.WindowState;
  152. this.WindowState = WindowState.Maximized;
  153. IsFullScreen = true;
  154. }
  155. if (WorkspaceService.Instance.Settings.Extended.ShowFullScreenOptimized)
  156. {
  157. this.GlassBorderThickness = IsFullScreen ? new Thickness(0, 0, 0, 0) : new Thickness(8, 50, 8, 8);
  158. this.statusBarInfo.Visibility = IsFullScreen ? Visibility.Collapsed : Visibility.Visible;
  159. this.ribbonMain.Visibility = IsFullScreen ? Visibility.Collapsed : Visibility.Visible;
  160. }
  161. else
  162. {
  163. this.ribbonMain.IsMinimized = IsFullScreen;
  164. }
  165. }
  166. catch (Exception err)
  167. {
  168. LogHelper.Manage("MainView:SwapFullScreenMode", err);
  169. }
  170. }
  171. #endregion
  172. #region ----------------WINDOW EVENTS----------------
  173. /// <summary>
  174. /// handle file drop on the main surface
  175. /// </summary>
  176. /// <param name="sender"></param>
  177. /// <param name="e"></param>
  178. private void RibbonWindow_Drop(object sender, DragEventArgs e)
  179. {
  180. try
  181. {
  182. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  183. {
  184. MainViewModel mvm = DataContext as MainViewModel;
  185. string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];
  186. if (DocumentFactory.Instance.FindCatalogFilterByExt(System.IO.Path.GetExtension(droppedFilePaths[0])) != null)
  187. mvm.CatalogOpenFileCommand.Execute(droppedFilePaths[0]);
  188. else
  189. if (DocumentFactory.Instance.FindBookFilterByExtWithModel(System.IO.Path.GetExtension(droppedFilePaths[0])) != null)
  190. mvm.BookOpenFileCommand.Execute(droppedFilePaths[0]);
  191. }
  192. }
  193. catch (Exception err)
  194. {
  195. LogHelper.Manage("MainView:RibbonWindow_Drop", err);
  196. }
  197. }
  198. /// <summary>
  199. /// start wmi watcher and load existing devices
  200. /// </summary>
  201. /// <param name="sender"></param>
  202. /// <param name="e"></param>
  203. private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
  204. {
  205. if (LogHelper.CanDebug())
  206. LogHelper.Begin("MainView.RibbonWindow_Loaded");
  207. try
  208. {
  209. SplashScreenManager.Splash.Message = "Localizing...";
  210. LoadLayout("AvalonDockConfig.xml");
  211. CultureManager.Instance.AvailableCulturesChanged += new CultureEventArrived(CultureManager_AvailableCulturesChanged);
  212. MainViewModel mvm = DataContext as MainViewModel;
  213. if (mvm != null)
  214. {
  215. //add all existing disks
  216. foreach (LogicalDiskInfo disk in _wmiWatcher.Devices)
  217. mvm.SysDeviceAddCommand.Execute(disk);
  218. }
  219. this.WindowState = System.Windows.WindowState.Maximized;
  220. this.Visibility = System.Windows.Visibility.Visible;
  221. this.Activate();
  222. }
  223. catch (Exception err)
  224. {
  225. LogHelper.Manage("MainView.RibbonWindow_Loaded", err);
  226. }
  227. finally
  228. {
  229. LogHelper.End("MainView.RibbonWindow_Loaded");
  230. }
  231. }
  232. /// <summary>
  233. /// dispose the splash window
  234. /// </summary>
  235. /// <param name="sender"></param>
  236. /// <param name="e"></param>
  237. private void RibbonWindow_Activated(object sender, EventArgs e)
  238. {
  239. SplashScreenManager.Splash.Dispose();
  240. }
  241. /// <summary>
  242. /// on closing, stop wmi watcher
  243. /// </summary>
  244. /// <param name="sender"></param>
  245. /// <param name="e"></param>
  246. private void RibbonWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  247. {
  248. CultureManager.Instance.AvailableCulturesChanged -= new CultureEventArrived(CultureManager_AvailableCulturesChanged);
  249. if( _wmiWatcher != null )
  250. _wmiWatcher.StopWatchUSB();
  251. SaveLayout("AvalonDockConfig.xml");
  252. }
  253. /// <summary>
  254. /// Update the language menu
  255. /// </summary>
  256. /// <param name="sender"></param>
  257. /// <param name="e"></param>
  258. private void CultureManager_AvailableCulturesChanged(object sender, CultureEventArgs e)
  259. {
  260. MainViewModel mvm = DataContext as MainViewModel;
  261. if (mvm != null)
  262. mvm.Languages.Add( new LanguageMenuItemViewModel( e.Culture ) );
  263. }
  264. #endregion
  265. #region ----------------OTHER EVENTS----------------
  266. /// <summary>
  267. /// handle wmi events
  268. /// </summary>
  269. /// <param name="sender"></param>
  270. /// <param name="e"></param>
  271. void wmi_EventArrived(object sender, WMIEventArgs e)
  272. {
  273. Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, (ThreadStart)delegate
  274. {
  275. MainViewModel mvm = DataContext as MainViewModel;
  276. if (mvm != null)
  277. {
  278. if (e.EventType == WMIActions.Added)
  279. mvm.SysDeviceAddCommand.Execute(e.Disk);
  280. else
  281. if (e.EventType == WMIActions.Removed)
  282. mvm.SysDeviceRemoveCommand.Execute(e.Disk);
  283. }
  284. });
  285. }
  286. /// <summary>
  287. /// hnadle the key down event in the book ribbon for goto page function
  288. /// </summary>
  289. /// <param name="sender"></param>
  290. /// <param name="e"></param>
  291. private void PageNumber_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  292. {
  293. if (e.Key == System.Windows.Input.Key.Enter)
  294. {
  295. MainViewModel mvm = DataContext as MainViewModel;
  296. if (mvm != null && mvm.ActiveDocument is BookViewModelBase)
  297. {
  298. (mvm.ActiveDocument as BookViewModelBase).BookGotoPageCommand.Execute(this.PageNumber.Text);
  299. }
  300. }
  301. }
  302. /// <summary>
  303. /// Update the culture regarding the dropdown selection
  304. /// </summary>
  305. /// <param name="sender"></param>
  306. /// <param name="e"></param>
  307. private void languageGallery_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  308. {
  309. if( e.AddedItems.Count>0)
  310. CultureManager.Instance.UICulture = (e.AddedItems[0] as LanguageMenuItemViewModel).Data;
  311. }
  312. /// <summary>
  313. /// Show localization editor
  314. /// </summary>
  315. /// <param name="sender"></param>
  316. /// <param name="e"></param>
  317. private void btnLocalize_Click(object sender, RoutedEventArgs e)
  318. {
  319. LocalizeView view = new LocalizeView();
  320. view.Show();
  321. }
  322. /// <summary>
  323. /// manage escaping the full screen mode when optimized
  324. /// </summary>
  325. /// <param name="sender"></param>
  326. /// <param name="e"></param>
  327. private void RibbonWindow_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  328. {
  329. if ( e.Key == System.Windows.Input.Key.Escape && IsFullScreen )
  330. {
  331. SwapFullScreenMode();
  332. e.Handled = true;
  333. }
  334. }
  335. private void FeedMenuItem_Click(object sender, RoutedEventArgs e)
  336. {
  337. MainViewModel mvm = this.DataContext as MainViewModel;
  338. FeedViewModel fvm = new FeedViewModel(((sender as MenuItem).DataContext) as FeedItemInfo);
  339. mvm.Documents.Add(fvm);
  340. mvm.SetActiveWorkspace(fvm);
  341. }
  342. #endregion
  343. private void LoadLayout( string fileName )
  344. {
  345. if (LogHelper.CanDebug())
  346. LogHelper.Begin("MainView.LoadLayout");
  347. try
  348. {
  349. MainViewModel mvm = this.DataContext as MainViewModel;
  350. string filePath = DirectoryHelper.Combine(CBRFolders.Cache, fileName);
  351. if (File.Exists(filePath))
  352. {
  353. var serializer = new XmlLayoutSerializer(dockManager);
  354. serializer.LayoutSerializationCallback += (s, e) =>
  355. {
  356. if (!string.IsNullOrEmpty(e.Model.ContentId))
  357. {
  358. e.Content = ViewModelFactory.Instance.CreateFrom(mvm, e.Model.ContentId);
  359. }
  360. };
  361. using (var stream = new StreamReader(filePath))
  362. serializer.Deserialize(stream);
  363. }
  364. else
  365. {
  366. mvm.ResetLayout();
  367. SaveLayout("AvalonDockBackup.xml");
  368. }
  369. }
  370. catch (Exception err)
  371. {
  372. LogHelper.Manage("MainView.LoadLayout", err);
  373. }
  374. finally
  375. {
  376. LogHelper.End("MainView.LoadLayout");
  377. }
  378. }
  379. private void SaveLayout( string fileName )
  380. {
  381. if (LogHelper.CanDebug())
  382. LogHelper.Begin("MainView.SaveLayout");
  383. try
  384. {
  385. var serializer = new XmlLayoutSerializer(dockManager);
  386. string filePath = DirectoryHelper.Combine(CBRFolders.Cache, fileName);
  387. using (var stream = new StreamWriter(filePath))
  388. serializer.Serialize(stream);
  389. }
  390. catch (Exception err)
  391. {
  392. LogHelper.Manage("MainView.SaveLayout", err);
  393. }
  394. finally
  395. {
  396. LogHelper.End("MainView.SaveLayout");
  397. }
  398. }
  399. private void ButtonResetLayout_Click(object sender, RoutedEventArgs e)
  400. {
  401. MainViewModel mvm = this.DataContext as MainViewModel;
  402. mvm.ResetLayout();
  403. //avalon
  404. //LoadLayout("AvalonDockBackup.config");
  405. //fluent
  406. this.ribbonMain.ClearQuickAccessToolBar();
  407. //grid
  408. ElementStateOperations.Reset();
  409. }
  410. }
  411. }