Click here to Skip to main content
15,880,543 members
Articles / Desktop Programming / WPF

C.B.R.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (52 votes)
22 Oct 2012GPL329 min read 123.9K   1.8K   132  
Comic and electronic publication reader with library management, extended file conversion, and devices support.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using BookReader.Common;
using BookReader.Dialogs;

namespace BookReader
{
    public partial class MainWindow : Window
    {
        private static RoutedUICommand _BookmarkCmd =
           new RoutedUICommand("Bookmark", "BookmarkCmd", typeof(MainWindow));

        public static RoutedUICommand BookmarkCmd
        {
            get { return MainWindow._BookmarkCmd; }
        }

        private static RoutedUICommand _GotoBookmarkCmd =
           new RoutedUICommand("Goto Bookmark", "GotoBookmarkCmd", typeof(MainWindow));

        public static RoutedUICommand GotoBookmarkCmd
        {
            get { return MainWindow._GotoBookmarkCmd; }
        }

        private static RoutedUICommand _ClearBookmarkCmd =
           new RoutedUICommand("Clear Bookmark", "ClearBookmarkCmd", typeof(MainWindow));

        public static RoutedUICommand ClearBookmarkCmd
        {
            get { return MainWindow._ClearBookmarkCmd; }
        }

		private static RoutedUICommand _ReadCmd =
		   new RoutedUICommand("Read", "ReadCmd", typeof(MainWindow));

		public static RoutedUICommand ReadCmd
		{
			get { return MainWindow._ReadCmd; }
		}

        private static RoutedUICommand _MarkReadCmd =
           new RoutedUICommand("Mark as (Un)Read", "MarkReadCmd", typeof(MainWindow));

        public static RoutedUICommand MarkReadCmd
        {
            get { return MainWindow._MarkReadCmd; }
        }

        private static RoutedUICommand _ProtectCmd =
           new RoutedUICommand("(Un)Protect", "_ProtectCmd", typeof(MainWindow));

        public static RoutedUICommand ProtectCmd
        {
            get { return MainWindow._ProtectCmd; }
        }

		private static RoutedUICommand _ChangeThemeCmd =
		   new RoutedUICommand("Theme", "_ChangeThemeCmd", typeof(MainWindow));

		public static RoutedUICommand ChangeThemeCmd
		{
			get { return MainWindow._ChangeThemeCmd; }
		}

        internal void SetupCommandBinding()
        {
			this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, CloseCmdExecuted, CmdCanAllaysExecute));
			this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, OpenBookCmdExecuted, CmdCanAllaysExecute));
			this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, DeleteCmdExecuted, CanExecuteWithItem));

			this.CommandBindings.Add(new CommandBinding(MainWindow.BookmarkCmd, BookmarkCmdExecuted, CanExecuteWithItem));
			this.CommandBindings.Add(new CommandBinding(MainWindow.GotoBookmarkCmd, GotoBookmarkCmdExecuted, CanExecuteWithItemBookmarked));
			this.CommandBindings.Add(new CommandBinding(MainWindow.ClearBookmarkCmd, ClearBookmarkCmdExecuted, CanExecuteWithItemBookmarked));

			this.CommandBindings.Add(new CommandBinding(MainWindow.ReadCmd, ReadCmdExecuted, CmdCanAllaysExecute));
            this.CommandBindings.Add(new CommandBinding(MainWindow.MarkReadCmd, MarkReadCmdExecuted, CanExecuteWithItem));
            this.CommandBindings.Add(new CommandBinding(MainWindow.ProtectCmd, ProtectCmdExecuted, CanExecuteWithItem));

			this.CommandBindings.Add(new CommandBinding(MainWindow.ChangeThemeCmd, ChangeThemeCmdExecuted, CmdCanAllaysExecute));
        }

        void CmdCanAllaysExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        void CanExecuteWithItem(object sender, CanExecuteRoutedEventArgs e)
        {
			string parameter = e.Parameter as string;

			if (parameter != null)
			{
				if( parameter == "LIST" )
					e.CanExecute = CurrentListBoxBook != null ? true : false;
				else
				if( parameter == "VIEW" )
					e.CanExecute = Catalog.Instance.CurrentBook != null ? true : false;
			}
			else
				e.CanExecute = CurrentListBoxBook != null ? true : false;
		}

		void CanExecuteWithItemBookmarked(object sender, CanExecuteRoutedEventArgs e)
		{
			string parameter = e.Parameter as string;

			if (parameter != null)
			{
				if (parameter == "LIST")
					e.CanExecute = string.IsNullOrEmpty(CurrentListBoxBook.Bookmark) ? false : true;
				else
					if (parameter == "VIEW")
						e.CanExecute = string.IsNullOrEmpty(Catalog.Instance.CurrentBook.Bookmark) ? false : true;
			}
			else
				e.CanExecute = false;
		}

        void CloseCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
            try
            {
                Properties.Settings.Default.ExplorerSize = this.Splitter.CatalogSize.Value;
                Properties.Settings.Default.Save();

                Catalog.Instance.Save();
                this.Close();
            }
            catch (Exception err)
            {
                ExceptionManagement.Manage("Main:CloseCmdExecuted", err);
            }
        }

        void OpenBookCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
            try
            {
                using (System.Windows.Forms.OpenFileDialog browser = new System.Windows.Forms.OpenFileDialog())
                {
                    if (browser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        LoadBook((IBook)Catalog.Instance.AddExternalBook(browser.FileName));
                    }
                }
            }
            catch (Exception err)
            {
                ExceptionManagement.Manage("Main:OpenBookCmdExecuted", err);
            }
        }

        void DeleteCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
            try
            {
                Catalog.Instance.Delete(CurrentListBoxBook);
            }
            catch (Exception err)
            {
                ExceptionManagement.Manage("Main:DeleteCmdExecuted", err);
            }
        }

		void BookmarkCmdExecuted(object target, ExecutedRoutedEventArgs e)
		{
			try
			{
				Catalog.Instance.SetBookmark(Catalog.Instance.CurrentBook);
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage("Main:BookmarkCmdExecuted", err);
			}
		}

		void GotoBookmarkCmdExecuted(object target, ExecutedRoutedEventArgs e)
		{
			string parameter = e.Parameter as string;

			if (parameter != null)
			try
			{
				if ( parameter == "LIST" && Catalog.Instance.CurrentBook != CurrentListBoxBook)
				{
					LoadBook(CurrentListBoxBook);
				}

				Catalog.Instance.CurrentBook.GotoMark();
				UpdateContent();
				this.SimplePageView.ScrollToHome();
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage("Main:GotoBookmarkCmdExecuted", err);
			}
		}

		void ClearBookmarkCmdExecuted(object target, ExecutedRoutedEventArgs e)
		{
			string parameter = e.Parameter as string;

			if (parameter != null)
			try
			{
				if ( parameter == "LIST" )
					Catalog.Instance.ClearBookmark(CurrentListBoxBook);
				else
					Catalog.Instance.ClearBookmark(Catalog.Instance.CurrentBook);
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage("Main:ClearBookmarkCmdExecuted", err);
			}
		}

		void ReadCmdExecuted(object target, ExecutedRoutedEventArgs e)
		{
			try
			{
				LoadBook(CurrentListBoxBook);
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage("Main:ReadCmdExecuted", err);
			}
		}

        void MarkReadCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
            try
            {
				Catalog.Instance.MarkAsRead(CurrentListBoxBook, !CurrentListBoxBook.IsRead);
            }
            catch (Exception err)
            {
				ExceptionManagement.Manage("Main:MarkReadCmdExecuted", err);
            }
        }

        void ProtectCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
            try
            {
                PasswordWindow Dlg = new PasswordWindow();
                Dlg.ShowDialog();

                if (Dlg.DialogResult == true)
                    Catalog.Instance.Protect(CurrentListBoxBook, !CurrentListBoxBook.IsSecured, Dlg.PassBox.Password);
            }
            catch (Exception err)
            {
                ExceptionManagement.Manage("Main:ProtectCmdExecuted", err);
            }
        }

		void ChangeThemeCmdExecuted(object target, ExecutedRoutedEventArgs e)
		{
			string parameter = e.Parameter as string;

			if (parameter != null)

			try
			{
				ThemeHelper.LoadTheme(parameter);
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage("Main:ChangeThemeCmdExecuted", err);
			}
		}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions