/CBR/CBR/ViewModels/MainViewModel.cs

# · C# · 691 lines · 543 code · 86 blank · 62 comment · 88 complexity · e3e9fb51c80cee89e39eca4a51f985e2 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Input;
  7. using CBR.Components;
  8. using CBR.Core.Files;
  9. using CBR.Core.Helpers;
  10. using CBR.Core.Models;
  11. using CBR.Core.Services;
  12. using System.ComponentModel;
  13. using System.Reflection;
  14. using System.Collections.ObjectModel;
  15. using System.Windows.Data;
  16. using System.Collections.Specialized;
  17. using CBR.Components.Controls;
  18. using System.Globalization;
  19. using CBR.Core.Helpers.Localization;
  20. namespace CBR.ViewModels
  21. {
  22. public partial class MainViewModel : ViewModelBase
  23. {
  24. #region ----------------CONSTRUCTOR----------------
  25. public MainViewModel(string param)
  26. {
  27. //register to the mediator for messages
  28. Mediator.Instance.RegisterHandler<Catalog>(ViewModelMessages.CatalogChanged,
  29. (Catalog o) =>
  30. {
  31. Data = o;
  32. } );
  33. Mediator.Instance.RegisterHandler(ViewModelMessages.CatalogRefresh,
  34. (Object o) =>
  35. {
  36. List<string> lst = o as List<string>;
  37. foreach( string file in lst )
  38. AddBookFileCommand.Execute(file);
  39. } );
  40. Mediator.Instance.RegisterHandler<CommandContext>(ViewModelMessages.ContextCommand,
  41. (CommandContext o) =>
  42. {
  43. ExecuteDistantCommand( o );
  44. } );
  45. Mediator.Instance.RegisterHandler<BookViewModelBase>(ViewModelMessages.SwapTwoPageView,
  46. (BookViewModelBase o) =>
  47. {
  48. SwapTwoPageMode(o);
  49. });
  50. BackStageIsOpen = false;
  51. ViewModels.Add(new HomeViewModel());
  52. if (!string.IsNullOrEmpty(param))
  53. HandleStartingDocument(param);
  54. }
  55. #endregion
  56. #region ----------------PROPERTIES----------------
  57. public string Title
  58. {
  59. get
  60. {
  61. // no catalog
  62. if (Data == null)
  63. return "CRB";
  64. // no open book
  65. if (BookViewModel == null)
  66. return "CBR : " + Data.CatalogFilePath;
  67. else
  68. return "CBR : " + BookViewModel.Data != null ? BookViewModel.Data.FilePath : string.Empty;
  69. }
  70. }
  71. public ObservableCollection<LanguageMenuItemViewModel> Languages
  72. {
  73. get
  74. {
  75. ObservableCollection<LanguageMenuItemViewModel> result = new ObservableCollection<LanguageMenuItemViewModel>();
  76. foreach( CultureItem info in CultureManager.Instance.GetAvailableCultures() )
  77. result.Add( new LanguageMenuItemViewModel(info) );
  78. return result;
  79. }
  80. }
  81. //private BookViewModelBase _currentBookViewModel = null;
  82. //public BookViewModelBase CurrentBookViewModel
  83. //{
  84. // get { return _currentBookViewModel; }
  85. // set
  86. // {
  87. // if (_currentBookViewModel != value)
  88. // {
  89. // _currentBookViewModel = value;
  90. // RaisePropertyChanged("CurrentBookViewModel");
  91. // RaisePropertyChanged("Title");
  92. // }
  93. // }
  94. //}
  95. public bool IsInEditMode
  96. {
  97. get { if (BookViewModel != null) return BookViewModel.IsInEditMode; else return false; }
  98. set
  99. {
  100. if (BookViewModel.IsInEditMode != value)
  101. {
  102. BookViewModel.IsInEditMode = value;
  103. RaisePropertyChanged("IsInEditMode");
  104. }
  105. }
  106. }
  107. private bool _backStageIsOpen = false;
  108. public bool BackStageIsOpen
  109. {
  110. get { return _backStageIsOpen; }
  111. set
  112. {
  113. if (_backStageIsOpen != value)
  114. {
  115. _backStageIsOpen = value;
  116. RaisePropertyChanged("BackStageIsOpen");
  117. }
  118. }
  119. }
  120. private int _backStageIndex = -1;
  121. public int BackStageIndex
  122. {
  123. get { return _backStageIndex; }
  124. set
  125. {
  126. if (_backStageIndex != value)
  127. {
  128. _backStageIndex = value;
  129. RaisePropertyChanged("BackStageIndex");
  130. }
  131. }
  132. }
  133. new public Catalog Data
  134. {
  135. get { return base.Data as Catalog; }
  136. set { base.Data = value; }
  137. }
  138. private ObservableCollection<ViewModelBase> _viewModels = null;
  139. public ObservableCollection<ViewModelBase> ViewModels
  140. {
  141. get
  142. {
  143. if (_viewModels == null)
  144. {
  145. _viewModels = new ObservableCollection<ViewModelBase>();
  146. _viewModels.CollectionChanged += OnViewModelsChanged;
  147. }
  148. return _viewModels;
  149. }
  150. //set
  151. //{
  152. // if (_viewModels != value)
  153. // {
  154. // _viewModels = value;
  155. // RaisePropertyChanged("ViewModels");
  156. // RaisePropertyChanged("BookViewModel");
  157. // RaisePropertyChanged("USBDeviceViewModel");
  158. // RaisePropertyChanged("HasUSBDevice");
  159. // RaisePropertyChanged("HasDevice");
  160. // RaisePropertyChanged("HasBook");
  161. // }
  162. //}
  163. }
  164. void OnViewModelsChanged(object sender, NotifyCollectionChangedEventArgs e)
  165. {
  166. RaisePropertyChanged("ViewModels");
  167. RaisePropertyChanged("BookViewModel");
  168. RaisePropertyChanged("USBDeviceViewModel");
  169. RaisePropertyChanged("HasUSBDevice");
  170. RaisePropertyChanged("HasDevice");
  171. RaisePropertyChanged("HasBook");
  172. RaisePropertyChanged("IsFitModeWidth");
  173. RaisePropertyChanged("IsFitModeHeight");
  174. RaisePropertyChanged("IsFitModeNone");
  175. RaisePropertyChanged("BookmarkCommand");
  176. RaisePropertyChanged("GotoBookmarkCommand");
  177. RaisePropertyChanged("ClearBookmarkCommand");
  178. RaisePropertyChanged("FitModeCommand");
  179. RaisePropertyChanged("BookGotoPageCommand");
  180. RaisePropertyChanged("BookChangePageCommand");
  181. RaisePropertyChanged("BookGotoLastPageCommand");
  182. RaisePropertyChanged("TwoPageCommand");
  183. }
  184. /// <summary>
  185. /// The one and only one in the view collection
  186. /// </summary>
  187. public BookViewModelBase BookViewModel
  188. {
  189. get
  190. {
  191. if (HasBook)
  192. return ViewModels.Where(p => p is BookViewModelBase).First() as BookViewModelBase;
  193. else
  194. return null;
  195. }
  196. //set
  197. //{
  198. // if (HasBook)
  199. // ViewModels.Remove(ViewModels.Where(p => p is BookViewModelBase).First());
  200. // if (value != null)
  201. // {
  202. // ViewModels.Add(value);
  203. // SetActiveView(value);
  204. // RaisePropertyChanged("BookViewModel");
  205. // RaisePropertyChanged("Title");
  206. // RaisePropertyChanged("HasBook");
  207. // RaisePropertyChanged("BookmarkCommand");
  208. // RaisePropertyChanged("GotoBookmarkCommand");
  209. // RaisePropertyChanged("ClearBookmarkCommand");
  210. // RaisePropertyChanged("FitModeCommand");
  211. // RaisePropertyChanged("BookGotoPageCommand");
  212. // RaisePropertyChanged("BookChangePageCommand");
  213. // }
  214. //}
  215. }
  216. public bool HasBook
  217. {
  218. get
  219. {
  220. return ViewModels.Where(p => p is TwoPageViewModel || p is ComicViewModel ||
  221. p is ePUBBookViewModel || p is XpsBookViewModel).Count() != 0;
  222. }
  223. }
  224. public bool HasDevice
  225. {
  226. get
  227. {
  228. return ViewModels.Where(p => p is USBDeviceViewModel).Count() != 0;
  229. }
  230. }
  231. public USBDeviceViewModel USBDeviceViewModel
  232. {
  233. get
  234. {
  235. if (ViewModels.Where(p => p is USBDeviceViewModel).Count() != 0)
  236. return ViewModels.Where(p => p is USBDeviceViewModel).First() as USBDeviceViewModel;
  237. else
  238. return null;
  239. }
  240. //set
  241. //{
  242. // if (ViewModels.Where(p => p is USBDeviceViewModel).Count() != 0)
  243. // ViewModels.Remove(ViewModels.Where(p => p is USBDeviceViewModel).First());
  244. // ViewModels.Add(value);
  245. // RaisePropertyChanged("USBDeviceViewModel");
  246. // RaisePropertyChanged("HasUSBDevice");
  247. //}
  248. }
  249. public bool HasUSBDevice
  250. {
  251. get
  252. {
  253. return (USBDeviceViewModel != null);
  254. }
  255. }
  256. #endregion
  257. #region ---------------- SYSTEM ----------------
  258. public void HandleStartingDocument(string param)
  259. {
  260. FileInfo fi = new FileInfo( param );
  261. if( fi.Exists )
  262. {
  263. if (FileService.Instance.FindCatalogFilterByExt(fi.Extension) != null)
  264. this.OpenFileCatalog(param);
  265. if (FileService.Instance.FindBookFilterByExt(fi.Extension) != null)
  266. this.OpenFileBook(param);
  267. }
  268. }
  269. #region help command
  270. private ICommand sysHelpCommand;
  271. public ICommand SysHelpCommand
  272. {
  273. get
  274. {
  275. if (sysHelpCommand == null)
  276. sysHelpCommand = new DelegateCommand(
  277. delegate()
  278. {
  279. ProcessHelper.LaunchWebUri(new Uri(CBR.Properties.Settings.Default.HelpUrl));
  280. },
  281. delegate() { return true;}
  282. );
  283. return sysHelpCommand;
  284. }
  285. }
  286. #endregion
  287. #region exit command
  288. private ICommand sysExitCommand;
  289. public ICommand SysExitCommand
  290. {
  291. get
  292. {
  293. if (sysExitCommand == null)
  294. sysExitCommand = new DelegateCommand(
  295. delegate() { CloseCatalog(); Application.Current.MainWindow.Close(); },
  296. delegate()
  297. {
  298. if (Application.Current != null && Application.Current.MainWindow != null)
  299. return true;
  300. return false;
  301. });
  302. return sysExitCommand;
  303. }
  304. }
  305. #endregion
  306. #region view command
  307. private ICommand sysExplorerViewCommand;
  308. public ICommand SysExplorerViewCommand
  309. {
  310. get
  311. {
  312. if (sysExplorerViewCommand == null)
  313. sysExplorerViewCommand = new DelegateCommand<string>(
  314. delegate(string param) { Mediator.Instance.NotifyColleagues(ViewModelMessages.ExplorerView, param); },
  315. delegate(string param)
  316. {
  317. return Data != null;
  318. });
  319. return sysExplorerViewCommand;
  320. }
  321. }
  322. #endregion
  323. #region add usb device command
  324. private ICommand sysDeviceAddCommand;
  325. public ICommand SysDeviceAddCommand
  326. {
  327. get
  328. {
  329. if (sysDeviceAddCommand == null)
  330. sysDeviceAddCommand = new DelegateCommand<USBDiskInfo>( DeviceAdd, delegate(USBDiskInfo param) { return true; } );
  331. return sysDeviceAddCommand;
  332. }
  333. }
  334. void DeviceAdd( USBDiskInfo param )
  335. {
  336. try
  337. {
  338. if (this.USBDeviceViewModel == null)
  339. this.ViewModels.Add(new USBDeviceViewModel(param));
  340. else
  341. Mediator.Instance.NotifyColleagues(ViewModelMessages.DeviceAdded, param);
  342. }
  343. catch (Exception err)
  344. {
  345. ExceptionHelper.Manage("MainViewModel:DeviceAdd", err);
  346. }
  347. }
  348. #endregion
  349. #region remove usb device command
  350. private ICommand sysDeviceRemoveCommand;
  351. public ICommand SysDeviceRemoveCommand
  352. {
  353. get
  354. {
  355. if (sysDeviceRemoveCommand == null)
  356. sysDeviceRemoveCommand = new DelegateCommand<USBDiskInfo>( DeviceRemove,
  357. delegate(USBDiskInfo param) { return this.USBDeviceViewModel != null; }
  358. );
  359. return sysDeviceRemoveCommand;
  360. }
  361. }
  362. void DeviceRemove(USBDiskInfo param)
  363. {
  364. try
  365. {
  366. if (this.USBDeviceViewModel != null)
  367. Mediator.Instance.NotifyColleagues(ViewModelMessages.DeviceRemoved, param);
  368. }
  369. catch (Exception err)
  370. {
  371. ExceptionHelper.Manage("MainViewModel:DeviceAdd", err);
  372. }
  373. }
  374. #endregion
  375. #endregion
  376. #region ----------------CATALOG----------------
  377. #region new catalog command
  378. private ICommand catalogNewCommand;
  379. public ICommand CatalogNewCommand
  380. {
  381. get
  382. {
  383. if (catalogNewCommand == null)
  384. catalogNewCommand = new DelegateCommand(NewCatalog, delegate() { return true; });
  385. return catalogNewCommand;
  386. }
  387. }
  388. void NewCatalog()
  389. {
  390. try
  391. {
  392. // check if opened and not save before
  393. //create a new one
  394. using (System.Windows.Forms.SaveFileDialog browser = new System.Windows.Forms.SaveFileDialog())
  395. {
  396. browser.AddExtension = true;
  397. browser.Filter = FileService.Instance.CatalogFilterAll;
  398. browser.DefaultExt = FileService.Instance.CatalogFilterDefaultExtension;
  399. browser.FilterIndex = FileService.Instance.CatalogFilterDefaultIndex;
  400. if (browser.ShowDialog(new Wpf32Window()) == System.Windows.Forms.DialogResult.OK)
  401. {
  402. Mediator.Instance.NotifyColleagues(ViewModelMessages.CatalogChanged, new Catalog(browser.FileName));
  403. }
  404. }
  405. }
  406. catch (Exception err)
  407. {
  408. ExceptionHelper.Manage("MainViewModel:OpenCatalog", err);
  409. }
  410. }
  411. #endregion
  412. #region open catalog command
  413. private ICommand catalogOpenCommand;
  414. public ICommand CatalogOpenCommand
  415. {
  416. get
  417. {
  418. if (catalogOpenCommand == null)
  419. catalogOpenCommand = new DelegateCommand(OpenCatalog, delegate() { return true; });
  420. return catalogOpenCommand;
  421. }
  422. }
  423. void OpenCatalog()
  424. {
  425. try
  426. {
  427. using (System.Windows.Forms.OpenFileDialog browser = new System.Windows.Forms.OpenFileDialog())
  428. {
  429. browser.Filter= FileService.Instance.CatalogFilterAll;
  430. browser.FilterIndex = FileService.Instance.CatalogFilterDefaultIndex;
  431. if (browser.ShowDialog(new Wpf32Window()) == System.Windows.Forms.DialogResult.OK)
  432. {
  433. OpenFileCatalog(browser.FileName);
  434. }
  435. }
  436. }
  437. catch (Exception err)
  438. {
  439. ExceptionHelper.Manage("MainViewModel:OpenCatalog", err);
  440. }
  441. }
  442. #endregion
  443. #region open file catalog command
  444. private ICommand catalogOpenFileCommand;
  445. public ICommand CatalogOpenFileCommand
  446. {
  447. get
  448. {
  449. if (catalogOpenFileCommand == null)
  450. catalogOpenFileCommand = new DelegateCommand<string>(OpenFileCatalog, delegate(string param) { return true; });
  451. return catalogOpenFileCommand;
  452. }
  453. }
  454. void OpenFileCatalog(string param)
  455. {
  456. try
  457. {
  458. if( File.Exists( param ) )
  459. {
  460. Mediator.Instance.NotifyColleagues(ViewModelMessages.CatalogChanged, CatalogService.Instance.Open(param));
  461. }
  462. }
  463. catch (Exception err)
  464. {
  465. ExceptionHelper.Manage("MainViewModel:OpenFileCatalog", err);
  466. }
  467. }
  468. #endregion
  469. #region save catalog command
  470. private ICommand catalogSaveCommand;
  471. public ICommand CatalogSaveCommand
  472. {
  473. get
  474. {
  475. if (catalogSaveCommand == null)
  476. catalogSaveCommand = new DelegateCommand(SaveCatalog, delegate() { return (Data != null); });
  477. return catalogSaveCommand;
  478. }
  479. }
  480. void SaveCatalog()
  481. {
  482. CatalogService.Instance.Save(Data);
  483. }
  484. #endregion
  485. #region save as catalog command
  486. private ICommand catalogSaveAsCommand;
  487. public ICommand CatalogSaveAsCommand
  488. {
  489. get
  490. {
  491. if (catalogSaveAsCommand == null)
  492. catalogSaveAsCommand = new DelegateCommand(SaveAsCatalog, delegate() { return (Data != null); });
  493. return catalogSaveAsCommand;
  494. }
  495. }
  496. void SaveAsCatalog()
  497. {
  498. try
  499. {
  500. using (System.Windows.Forms.OpenFileDialog browser = new System.Windows.Forms.OpenFileDialog())
  501. {
  502. browser.Filter = FileService.Instance.CatalogFilterAll;
  503. browser.FilterIndex = FileService.Instance.CatalogFilterDefaultIndex;
  504. browser.DefaultExt = FileService.Instance.CatalogFilterDefaultExtension;
  505. if (browser.ShowDialog(new Wpf32Window()) == System.Windows.Forms.DialogResult.OK)
  506. {
  507. CatalogService.Instance.SaveAs(Data, browser.FileName);
  508. }
  509. }
  510. }
  511. catch (Exception err)
  512. {
  513. ExceptionHelper.Manage("MainViewModel:SaveAsCatalog", err);
  514. }
  515. }
  516. #endregion
  517. #region refresh catalog command
  518. private ICommand catalogRefreshCommand;
  519. public ICommand CatalogRefreshCommand
  520. {
  521. get
  522. {
  523. if (catalogRefreshCommand == null)
  524. catalogRefreshCommand = new DelegateCommand(RefreshCatalog,
  525. delegate()
  526. {
  527. return (Data != null);
  528. });
  529. return catalogRefreshCommand;
  530. }
  531. }
  532. void RefreshCatalog()
  533. {
  534. CatalogService.Instance.Refresh(Data);
  535. }
  536. #endregion
  537. #region close catalog command
  538. private ICommand catalogCloseCommand;
  539. public ICommand CatalogCloseCommand
  540. {
  541. get
  542. {
  543. if (catalogCloseCommand == null)
  544. catalogCloseCommand = new DelegateCommand(CloseCatalog,
  545. delegate()
  546. {
  547. return Data != null;
  548. });
  549. return catalogCloseCommand;
  550. }
  551. }
  552. void CloseCatalog()
  553. {
  554. if (Data != null)
  555. {
  556. if (CatalogService.Instance.IsDirty(Data))
  557. {
  558. if( MessageBox.Show( "Save the catalog and book changes ?", "Warning", MessageBoxButton.YesNo ) == MessageBoxResult.Yes )
  559. CatalogService.Instance.Save(Data);
  560. }
  561. Mediator.Instance.NotifyColleagues<Catalog>(ViewModelMessages.CatalogChanged, null);
  562. }
  563. }
  564. #endregion
  565. #endregion
  566. #region ----------------INTERNALS----------------
  567. internal void SwapTwoPageMode(BookViewModelBase o)
  568. {
  569. ViewModels.Remove(o);
  570. BookViewModelBase newModel = null;
  571. BookViewModelBase oldModel = null;
  572. if (o is ComicViewModel)
  573. {
  574. ComicViewModel comic = o as ComicViewModel;
  575. newModel = new TwoPageViewModel(o.Data, comic.CurrentPage.Index, comic.FitMode, comic.PreviousScale);
  576. }
  577. else
  578. {
  579. TwoPageViewModel comic = o as TwoPageViewModel;
  580. newModel = new ComicViewModel(o.Data, comic.CurrentPageIndex, comic.FitMode, comic.PreviousScale);
  581. }
  582. oldModel = o;
  583. ViewModels.Add(newModel);
  584. SetActiveView(newModel);
  585. ViewModels.Remove(oldModel);
  586. }
  587. internal void ExecuteDistantCommand(CommandContext context)
  588. {
  589. if (context != null)
  590. {
  591. new ReflectionHelper().ExecuteICommand( this, context.CommandName, context.CommandParameter );
  592. }
  593. }
  594. public void SetActiveView(ViewModelBase wmb)
  595. {
  596. if (wmb != null)
  597. {
  598. ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.ViewModels);
  599. if (collectionView != null)
  600. collectionView.MoveCurrentTo(wmb);
  601. }
  602. }
  603. #endregion
  604. }
  605. }