/CBR/CBR/ViewModels/MainViewModel.cs
# · C# · 691 lines · 543 code · 86 blank · 62 comment · 88 complexity · e3e9fb51c80cee89e39eca4a51f985e2 MD5 · raw file
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Windows;
- using System.Windows.Input;
- using CBR.Components;
- using CBR.Core.Files;
- using CBR.Core.Helpers;
- using CBR.Core.Models;
- using CBR.Core.Services;
- using System.ComponentModel;
- using System.Reflection;
- using System.Collections.ObjectModel;
- using System.Windows.Data;
- using System.Collections.Specialized;
- using CBR.Components.Controls;
- using System.Globalization;
- using CBR.Core.Helpers.Localization;
-
- namespace CBR.ViewModels
- {
- public partial class MainViewModel : ViewModelBase
- {
- #region ----------------CONSTRUCTOR----------------
-
- public MainViewModel(string param)
- {
- //register to the mediator for messages
- Mediator.Instance.RegisterHandler<Catalog>(ViewModelMessages.CatalogChanged,
- (Catalog o) =>
- {
- Data = o;
- } );
-
- Mediator.Instance.RegisterHandler(ViewModelMessages.CatalogRefresh,
- (Object o) =>
- {
- List<string> lst = o as List<string>;
- foreach( string file in lst )
- AddBookFileCommand.Execute(file);
- } );
-
- Mediator.Instance.RegisterHandler<CommandContext>(ViewModelMessages.ContextCommand,
- (CommandContext o) =>
- {
- ExecuteDistantCommand( o );
- } );
-
- Mediator.Instance.RegisterHandler<BookViewModelBase>(ViewModelMessages.SwapTwoPageView,
- (BookViewModelBase o) =>
- {
- SwapTwoPageMode(o);
- });
-
- BackStageIsOpen = false;
-
- ViewModels.Add(new HomeViewModel());
-
- if (!string.IsNullOrEmpty(param))
- HandleStartingDocument(param);
- }
-
- #endregion
-
- #region ----------------PROPERTIES----------------
-
- public string Title
- {
- get
- {
- // no catalog
- if (Data == null)
- return "CRB";
-
- // no open book
- if (BookViewModel == null)
- return "CBR : " + Data.CatalogFilePath;
- else
- return "CBR : " + BookViewModel.Data != null ? BookViewModel.Data.FilePath : string.Empty;
- }
- }
-
- public ObservableCollection<LanguageMenuItemViewModel> Languages
- {
- get
- {
- ObservableCollection<LanguageMenuItemViewModel> result = new ObservableCollection<LanguageMenuItemViewModel>();
-
- foreach( CultureItem info in CultureManager.Instance.GetAvailableCultures() )
- result.Add( new LanguageMenuItemViewModel(info) );
-
- return result;
- }
- }
-
- //private BookViewModelBase _currentBookViewModel = null;
- //public BookViewModelBase CurrentBookViewModel
- //{
- // get { return _currentBookViewModel; }
- // set
- // {
- // if (_currentBookViewModel != value)
- // {
- // _currentBookViewModel = value;
- // RaisePropertyChanged("CurrentBookViewModel");
- // RaisePropertyChanged("Title");
- // }
- // }
- //}
-
-
- public bool IsInEditMode
- {
- get { if (BookViewModel != null) return BookViewModel.IsInEditMode; else return false; }
- set
- {
- if (BookViewModel.IsInEditMode != value)
- {
- BookViewModel.IsInEditMode = value;
- RaisePropertyChanged("IsInEditMode");
- }
- }
- }
-
- private bool _backStageIsOpen = false;
- public bool BackStageIsOpen
- {
- get { return _backStageIsOpen; }
- set
- {
- if (_backStageIsOpen != value)
- {
- _backStageIsOpen = value;
- RaisePropertyChanged("BackStageIsOpen");
- }
- }
- }
-
-
- private int _backStageIndex = -1;
- public int BackStageIndex
- {
- get { return _backStageIndex; }
- set
- {
- if (_backStageIndex != value)
- {
- _backStageIndex = value;
- RaisePropertyChanged("BackStageIndex");
- }
- }
- }
-
- new public Catalog Data
- {
- get { return base.Data as Catalog; }
- set { base.Data = value; }
- }
-
- private ObservableCollection<ViewModelBase> _viewModels = null;
- public ObservableCollection<ViewModelBase> ViewModels
- {
- get
- {
- if (_viewModels == null)
- {
- _viewModels = new ObservableCollection<ViewModelBase>();
- _viewModels.CollectionChanged += OnViewModelsChanged;
- }
- return _viewModels;
- }
- //set
- //{
- // if (_viewModels != value)
- // {
- // _viewModels = value;
- // RaisePropertyChanged("ViewModels");
- // RaisePropertyChanged("BookViewModel");
-
- // RaisePropertyChanged("USBDeviceViewModel");
- // RaisePropertyChanged("HasUSBDevice");
- // RaisePropertyChanged("HasDevice");
- // RaisePropertyChanged("HasBook");
- // }
- //}
- }
-
- void OnViewModelsChanged(object sender, NotifyCollectionChangedEventArgs e)
- {
- RaisePropertyChanged("ViewModels");
- RaisePropertyChanged("BookViewModel");
- RaisePropertyChanged("USBDeviceViewModel");
-
- RaisePropertyChanged("HasUSBDevice");
- RaisePropertyChanged("HasDevice");
- RaisePropertyChanged("HasBook");
-
- RaisePropertyChanged("IsFitModeWidth");
- RaisePropertyChanged("IsFitModeHeight");
- RaisePropertyChanged("IsFitModeNone");
-
- RaisePropertyChanged("BookmarkCommand");
- RaisePropertyChanged("GotoBookmarkCommand");
- RaisePropertyChanged("ClearBookmarkCommand");
-
- RaisePropertyChanged("FitModeCommand");
- RaisePropertyChanged("BookGotoPageCommand");
- RaisePropertyChanged("BookChangePageCommand");
- RaisePropertyChanged("BookGotoLastPageCommand");
- RaisePropertyChanged("TwoPageCommand");
- }
-
- /// <summary>
- /// The one and only one in the view collection
- /// </summary>
- public BookViewModelBase BookViewModel
- {
- get
- {
- if (HasBook)
- return ViewModels.Where(p => p is BookViewModelBase).First() as BookViewModelBase;
- else
- return null;
- }
- //set
- //{
- // if (HasBook)
- // ViewModels.Remove(ViewModels.Where(p => p is BookViewModelBase).First());
-
- // if (value != null)
- // {
- // ViewModels.Add(value);
- // SetActiveView(value);
-
- // RaisePropertyChanged("BookViewModel");
- // RaisePropertyChanged("Title");
- // RaisePropertyChanged("HasBook");
-
- // RaisePropertyChanged("BookmarkCommand");
- // RaisePropertyChanged("GotoBookmarkCommand");
- // RaisePropertyChanged("ClearBookmarkCommand");
-
- // RaisePropertyChanged("FitModeCommand");
- // RaisePropertyChanged("BookGotoPageCommand");
- // RaisePropertyChanged("BookChangePageCommand");
- // }
- //}
- }
-
- public bool HasBook
- {
- get
- {
- return ViewModels.Where(p => p is TwoPageViewModel || p is ComicViewModel ||
- p is ePUBBookViewModel || p is XpsBookViewModel).Count() != 0;
- }
- }
-
-
- public bool HasDevice
- {
- get
- {
- return ViewModels.Where(p => p is USBDeviceViewModel).Count() != 0;
- }
- }
-
- public USBDeviceViewModel USBDeviceViewModel
- {
- get
- {
- if (ViewModels.Where(p => p is USBDeviceViewModel).Count() != 0)
- return ViewModels.Where(p => p is USBDeviceViewModel).First() as USBDeviceViewModel;
- else
- return null;
- }
- //set
- //{
- // if (ViewModels.Where(p => p is USBDeviceViewModel).Count() != 0)
- // ViewModels.Remove(ViewModels.Where(p => p is USBDeviceViewModel).First());
-
- // ViewModels.Add(value);
-
- // RaisePropertyChanged("USBDeviceViewModel");
- // RaisePropertyChanged("HasUSBDevice");
- //}
- }
-
- public bool HasUSBDevice
- {
- get
- {
- return (USBDeviceViewModel != null);
- }
- }
-
- #endregion
-
- #region ---------------- SYSTEM ----------------
-
- public void HandleStartingDocument(string param)
- {
- FileInfo fi = new FileInfo( param );
- if( fi.Exists )
- {
- if (FileService.Instance.FindCatalogFilterByExt(fi.Extension) != null)
- this.OpenFileCatalog(param);
-
- if (FileService.Instance.FindBookFilterByExt(fi.Extension) != null)
- this.OpenFileBook(param);
- }
- }
-
- #region help command
- private ICommand sysHelpCommand;
- public ICommand SysHelpCommand
- {
- get
- {
- if (sysHelpCommand == null)
- sysHelpCommand = new DelegateCommand(
- delegate()
- {
- ProcessHelper.LaunchWebUri(new Uri(CBR.Properties.Settings.Default.HelpUrl));
- },
- delegate() { return true;}
- );
- return sysHelpCommand;
- }
- }
- #endregion
-
- #region exit command
- private ICommand sysExitCommand;
- public ICommand SysExitCommand
- {
- get
- {
- if (sysExitCommand == null)
- sysExitCommand = new DelegateCommand(
- delegate() { CloseCatalog(); Application.Current.MainWindow.Close(); },
- delegate()
- {
- if (Application.Current != null && Application.Current.MainWindow != null)
- return true;
- return false;
- });
- return sysExitCommand;
- }
- }
- #endregion
-
- #region view command
- private ICommand sysExplorerViewCommand;
- public ICommand SysExplorerViewCommand
- {
- get
- {
- if (sysExplorerViewCommand == null)
- sysExplorerViewCommand = new DelegateCommand<string>(
- delegate(string param) { Mediator.Instance.NotifyColleagues(ViewModelMessages.ExplorerView, param); },
- delegate(string param)
- {
- return Data != null;
- });
- return sysExplorerViewCommand;
- }
- }
- #endregion
-
- #region add usb device command
- private ICommand sysDeviceAddCommand;
- public ICommand SysDeviceAddCommand
- {
- get
- {
- if (sysDeviceAddCommand == null)
- sysDeviceAddCommand = new DelegateCommand<USBDiskInfo>( DeviceAdd, delegate(USBDiskInfo param) { return true; } );
- return sysDeviceAddCommand;
- }
- }
-
- void DeviceAdd( USBDiskInfo param )
- {
- try
- {
- if (this.USBDeviceViewModel == null)
- this.ViewModels.Add(new USBDeviceViewModel(param));
- else
- Mediator.Instance.NotifyColleagues(ViewModelMessages.DeviceAdded, param);
- }
- catch (Exception err)
- {
- ExceptionHelper.Manage("MainViewModel:DeviceAdd", err);
- }
- }
-
- #endregion
-
- #region remove usb device command
- private ICommand sysDeviceRemoveCommand;
- public ICommand SysDeviceRemoveCommand
- {
- get
- {
- if (sysDeviceRemoveCommand == null)
- sysDeviceRemoveCommand = new DelegateCommand<USBDiskInfo>( DeviceRemove,
- delegate(USBDiskInfo param) { return this.USBDeviceViewModel != null; }
- );
- return sysDeviceRemoveCommand;
- }
- }
-
- void DeviceRemove(USBDiskInfo param)
- {
- try
- {
- if (this.USBDeviceViewModel != null)
- Mediator.Instance.NotifyColleagues(ViewModelMessages.DeviceRemoved, param);
- }
- catch (Exception err)
- {
- ExceptionHelper.Manage("MainViewModel:DeviceAdd", err);
- }
- }
-
- #endregion
-
- #endregion
-
- #region ----------------CATALOG----------------
-
- #region new catalog command
- private ICommand catalogNewCommand;
- public ICommand CatalogNewCommand
- {
- get
- {
- if (catalogNewCommand == null)
- catalogNewCommand = new DelegateCommand(NewCatalog, delegate() { return true; });
- return catalogNewCommand;
- }
- }
-
- void NewCatalog()
- {
- try
- {
- // check if opened and not save before
-
- //create a new one
- using (System.Windows.Forms.SaveFileDialog browser = new System.Windows.Forms.SaveFileDialog())
- {
- browser.AddExtension = true;
- browser.Filter = FileService.Instance.CatalogFilterAll;
- browser.DefaultExt = FileService.Instance.CatalogFilterDefaultExtension;
- browser.FilterIndex = FileService.Instance.CatalogFilterDefaultIndex;
-
- if (browser.ShowDialog(new Wpf32Window()) == System.Windows.Forms.DialogResult.OK)
- {
- Mediator.Instance.NotifyColleagues(ViewModelMessages.CatalogChanged, new Catalog(browser.FileName));
- }
- }
- }
- catch (Exception err)
- {
- ExceptionHelper.Manage("MainViewModel:OpenCatalog", err);
- }
- }
- #endregion
-
- #region open catalog command
- private ICommand catalogOpenCommand;
- public ICommand CatalogOpenCommand
- {
- get
- {
- if (catalogOpenCommand == null)
- catalogOpenCommand = new DelegateCommand(OpenCatalog, delegate() { return true; });
- return catalogOpenCommand;
- }
- }
-
- void OpenCatalog()
- {
- try
- {
- using (System.Windows.Forms.OpenFileDialog browser = new System.Windows.Forms.OpenFileDialog())
- {
- browser.Filter= FileService.Instance.CatalogFilterAll;
- browser.FilterIndex = FileService.Instance.CatalogFilterDefaultIndex;
-
- if (browser.ShowDialog(new Wpf32Window()) == System.Windows.Forms.DialogResult.OK)
- {
- OpenFileCatalog(browser.FileName);
- }
- }
- }
- catch (Exception err)
- {
- ExceptionHelper.Manage("MainViewModel:OpenCatalog", err);
- }
- }
- #endregion
-
- #region open file catalog command
- private ICommand catalogOpenFileCommand;
- public ICommand CatalogOpenFileCommand
- {
- get
- {
- if (catalogOpenFileCommand == null)
- catalogOpenFileCommand = new DelegateCommand<string>(OpenFileCatalog, delegate(string param) { return true; });
- return catalogOpenFileCommand;
- }
- }
-
- void OpenFileCatalog(string param)
- {
- try
- {
- if( File.Exists( param ) )
- {
- Mediator.Instance.NotifyColleagues(ViewModelMessages.CatalogChanged, CatalogService.Instance.Open(param));
- }
- }
- catch (Exception err)
- {
- ExceptionHelper.Manage("MainViewModel:OpenFileCatalog", err);
- }
- }
- #endregion
-
- #region save catalog command
- private ICommand catalogSaveCommand;
- public ICommand CatalogSaveCommand
- {
- get
- {
- if (catalogSaveCommand == null)
- catalogSaveCommand = new DelegateCommand(SaveCatalog, delegate() { return (Data != null); });
- return catalogSaveCommand;
- }
- }
-
- void SaveCatalog()
- {
- CatalogService.Instance.Save(Data);
- }
- #endregion
-
- #region save as catalog command
- private ICommand catalogSaveAsCommand;
- public ICommand CatalogSaveAsCommand
- {
- get
- {
- if (catalogSaveAsCommand == null)
- catalogSaveAsCommand = new DelegateCommand(SaveAsCatalog, delegate() { return (Data != null); });
- return catalogSaveAsCommand;
- }
- }
-
- void SaveAsCatalog()
- {
- try
- {
- using (System.Windows.Forms.OpenFileDialog browser = new System.Windows.Forms.OpenFileDialog())
- {
- browser.Filter = FileService.Instance.CatalogFilterAll;
- browser.FilterIndex = FileService.Instance.CatalogFilterDefaultIndex;
- browser.DefaultExt = FileService.Instance.CatalogFilterDefaultExtension;
-
- if (browser.ShowDialog(new Wpf32Window()) == System.Windows.Forms.DialogResult.OK)
- {
- CatalogService.Instance.SaveAs(Data, browser.FileName);
- }
- }
- }
- catch (Exception err)
- {
- ExceptionHelper.Manage("MainViewModel:SaveAsCatalog", err);
- }
- }
- #endregion
-
- #region refresh catalog command
- private ICommand catalogRefreshCommand;
- public ICommand CatalogRefreshCommand
- {
- get
- {
- if (catalogRefreshCommand == null)
- catalogRefreshCommand = new DelegateCommand(RefreshCatalog,
- delegate()
- {
- return (Data != null);
- });
- return catalogRefreshCommand;
- }
- }
-
- void RefreshCatalog()
- {
- CatalogService.Instance.Refresh(Data);
- }
- #endregion
-
- #region close catalog command
- private ICommand catalogCloseCommand;
- public ICommand CatalogCloseCommand
- {
- get
- {
- if (catalogCloseCommand == null)
- catalogCloseCommand = new DelegateCommand(CloseCatalog,
- delegate()
- {
- return Data != null;
- });
- return catalogCloseCommand;
- }
- }
-
- void CloseCatalog()
- {
- if (Data != null)
- {
- if (CatalogService.Instance.IsDirty(Data))
- {
- if( MessageBox.Show( "Save the catalog and book changes ?", "Warning", MessageBoxButton.YesNo ) == MessageBoxResult.Yes )
- CatalogService.Instance.Save(Data);
- }
-
- Mediator.Instance.NotifyColleagues<Catalog>(ViewModelMessages.CatalogChanged, null);
- }
- }
- #endregion
-
- #endregion
-
- #region ----------------INTERNALS----------------
-
- internal void SwapTwoPageMode(BookViewModelBase o)
- {
- ViewModels.Remove(o);
-
- BookViewModelBase newModel = null;
- BookViewModelBase oldModel = null;
- if (o is ComicViewModel)
- {
- ComicViewModel comic = o as ComicViewModel;
- newModel = new TwoPageViewModel(o.Data, comic.CurrentPage.Index, comic.FitMode, comic.PreviousScale);
-
- }
- else
- {
- TwoPageViewModel comic = o as TwoPageViewModel;
- newModel = new ComicViewModel(o.Data, comic.CurrentPageIndex, comic.FitMode, comic.PreviousScale);
-
- }
-
- oldModel = o;
- ViewModels.Add(newModel);
- SetActiveView(newModel);
-
- ViewModels.Remove(oldModel);
- }
-
- internal void ExecuteDistantCommand(CommandContext context)
- {
- if (context != null)
- {
- new ReflectionHelper().ExecuteICommand( this, context.CommandName, context.CommandParameter );
- }
- }
-
- public void SetActiveView(ViewModelBase wmb)
- {
- if (wmb != null)
- {
- ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.ViewModels);
- if (collectionView != null)
- collectionView.MoveCurrentTo(wmb);
- }
- }
-
- #endregion
- }
- }