Click here to Skip to main content
13,257,586 members (54,307 online)
Click here to Skip to main content

Stats

76.1K views
132 bookmarked
Posted 3 Dec 2011

C.B.R.

, 22 Oct 2012
Comic and electronic publication reader with library management, extended file conversion, and devices support.
Release
CBR.Core.dll
CBR.exe
Dependencies
7z.dll
Fluent.dll
itextsharp.dll
Microsoft.Expression.Drawing.dll
Microsoft.Expression.Interactions.dll
SevenZipSharp.dll
System.Windows.Interactivity.dll
Languages
flag_france.png
flag_great_britain.png
CBR.exe
CBR.Core.dll
Fluent.dll
itextsharp.dll
Microsoft.Expression.Drawing.dll
Microsoft.Expression.Interactions.dll
SevenZipSharp.dll
System.Windows.Interactivity.dll
Dependencies
7z.dll
cbrz.ico
epub.ico
pdf.ico
rar.ico
CBR
CBR.Core
Dependencies
7z.dll
CoreDiagram.cd
itextsharp.dll
SevenZipSharp.dll
Files
Conversion
IMG
PDF
RAR
XPS
Helpers
MVVM
Commands
Events
Messages
Models
Workspace
Properties
Services
Book
CBR
Components
Controls
Converters
Designer
Adorners
Dialogs
Helpers
Dependencies
Fluent.dll
ProgrammDiagram.cd
Properties
Resources
Images
16x16
pin_no.png
pin_yes.png
sort_alpha.png
sort_category.png
view_details.png
view_thumb.png
32x32
book.png
book_add.png
book_close.png
book_convert.png
book_delete.png
book_edit.png
book_open.png
book_protect.png
book_read.png
book_save.png
book_test.png
book_type_none.png
book_type_rar.png
book_type_xps.png
book_type_zip.png
exit.png
fit_height.png
fit_width.png
forbidden.png
full_screen.png
help.png
info.png
library_close.png
library_new.png
library_open.png
library_view.png
open_folder.png
page_clear_mark.png
page_goto_mark.png
page_mark.png
page_test.png
picture_order.png
picture_time.png
picture_zone.png
push_pin.png
read.png
refresh.png
save.png
save_as.png
tools.png
book.ico
phone_horizontal.png
search.png
XAML
Controls
ViewModels
Book
Explorer
File
Views
Books
File
BookReader
BookReader.vssscc
BookReader
BookReader.csproj.vspscc
Common
Controls
Dependencies
7z.dll
pdftohtml.exe
SevenZipSharp.dll
WPFToolkit.dll
Dialogs
Properties
app.manifest
Reader
Catalog
Common
Images
RAR
Resources
book.ico
Images
acroread.png
document.png
ecran.png
exit.png
filter.png
FitHeight.png
FitWidth.png
forbidden.png
GotoPage.png
info.png
open_folder.png
protect.png
refresh.png
Search.png
Style.png
TheRedPushPin.png
tools.png
Themes
Black
Blue
Silver
XAML
BuildProcessTemplates
CBR.Core.csproj.vspscc
7z.dll
CoreDiagram.cd
itextsharp.dll
SevenZipSharp.dll
DragDrop
ePUB
HTML
WMI
XPS
ePUB
Books
CBR.vssscc
CBR.csproj.vspscc
Devices.xlsx
Fluent.dll
ProgrammDiagram.cd
disk.png
file.png
folder.png
pin_no.png
pin_yes.png
sort_alpha.png
sort_category.png
view_details.png
view_thumb.png
book.png
book_add.png
book_close.png
book_convert.png
book_delete.png
book_edit.png
book_open.png
book_protect.png
book_read.png
book_remove.png
book_save.png
book_test.png
book_type_epub.png
book_type_none.png
book_type_rar.png
book_type_xps.png
book_type_zip.png
device_refresh.png
device_sync.png
device_view.png
exit.png
fit_height.png
fit_width.png
forbidden.png
full_screen.png
help.png
info.png
library_close.png
library_new.png
library_open.png
library_view.png
open_folder.png
page_clear_mark.png
page_first.png
page_goto_mark.png
page_last.png
page_mark.png
page_next.png
page_previous.png
page_test.png
picture_order.png
picture_time.png
picture_zone.png
push_pin.png
read.png
refresh.png
save.png
save_as.png
tools.png
book.ico
phone_horizontal.png
search.png
Books
Devices
SysItems
Devices
BookReader.vssscc
BookReader.csproj.vspscc
7z.dll
pdftohtml.exe
SevenZipSharp.dll
WPFToolkit.dll
app.manifest
book.ico
acroread.png
document.png
ecran.png
exit.png
filter.png
FitHeight.png
FitWidth.png
forbidden.png
GotoPage.png
info.png
open_folder.png
protect.png
refresh.png
Search.png
Style.png
TheRedPushPin.png
tools.png
CBR.Core.csproj.vspscc
7z.dll
cbrz.ico
CoreDiagram.cd
epub.ico
itextsharp.dll
pdf.ico
rar.ico
SevenZipSharp.dll
Files
Localization
Core
Providers
Files
Resx
Mediator
CBR.vssscc
CBR.csproj.vspscc
Devices.xlsx
Fluent.dll
ProgrammDiagram.cd
Languages
flag_france.png
flag_great_britain.png
disk.png
file.png
folder.png
pin_no.png
pin_yes.png
sort_alpha.png
sort_category.png
view_details.png
view_thumb.png
book.png
book_add.png
book_close.png
book_convert.png
book_delete.png
book_edit.png
book_new.png
book_open.png
book_protect.png
book_read.png
book_remove.png
book_save.png
book_test.png
book_type_epub.png
book_type_none.png
book_type_rar.png
book_type_xps.png
book_type_zip.png
delete.png
device_refresh.png
device_sync.png
device_view.png
exit.png
fit_height.png
fit_width.png
forbidden.png
full_screen.png
help.png
info.png
is_read.png
library_close.png
library_new.png
library_open.png
library_view.png
localize.png
localize_new.png
localize_select.png
lock.png
open_folder.png
page_clear_mark.png
page_first.png
page_goto_mark.png
page_last.png
page_mark.png
page_next.png
page_previous.png
page_test.png
picture_order.png
picture_time.png
picture_zone.png
push_pin.png
read.png
refresh.png
resfresh.png
save.png
save_as.png
search.png
tools.png
book.ico
phone_horizontal.png
search.png
themes
Extended
Menus
Others
Explorer
Others
CBR.Core.dll
CBR.Core.pdb
CBR.exe
CBR.pdb
CBR.vshost.exe
7z.dll
cbl.ico
cbrz.ico
epub.ico
pdf.ico
rar.ico
Fluent.dll
itextsharp.dll
Languages
flag_france.png
flag_great_britain.png
Microsoft.Expression.Drawing.dll
Microsoft.Expression.Interactions.dll
SevenZipSharp.dll
System.Windows.Interactivity.dll
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;

namespace CBR.ViewModels
{
    public partial class MainViewModel : ViewModelBase
    {
        private DelegateCommand _emptyCommand = new DelegateCommand(delegate { return; }, delegate { return false; });

        #region ----------------BOOK COMMANDS----------------

        #region open command
        private ICommand bookOpenCommand;
        public ICommand BookOpenCommand
        {
            get
            {
                if (bookOpenCommand == null)
                    bookOpenCommand = new DelegateCommand(OpenBook, delegate() { return true; });
                return bookOpenCommand;
            }
        }

        void OpenBook()
        {
            try
            {
                using (System.Windows.Forms.OpenFileDialog browser = new System.Windows.Forms.OpenFileDialog())
                {
                    browser.FilterIndex = FileService.Instance.BookFilterDefaultIndex;
                    browser.Filter = FileService.Instance.BookFilterAllEditable;

                    if (browser.ShowDialog(new Wpf32Window()) == System.Windows.Forms.DialogResult.OK)
                    {
                        OpenFileBook(browser.FileName);
                    }
                }
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("BookViewModelBase:OpenBook", err);
            }
        }
        #endregion

        #region open file command
        private ICommand bookOpenFileCommand;
        public ICommand BookOpenFileCommand
        {
            get
            {
                if (bookOpenFileCommand == null)
                    bookOpenFileCommand = new DelegateCommand<string>(OpenFileBook, delegate(string param) { return true; });
                return bookOpenFileCommand;
            }
        }

        void OpenFileBook(string param)
        {
            try
            {
                ReadBook(BookServiceFactory.Instance.GetService(param).CreateBook(param));
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("MainViewModel:OpenFileBook", err);
            }
        }
        #endregion

        #region read command
        private ICommand bookReadCommand;
        public ICommand BookReadCommand
        {
            get
            {
                if (bookReadCommand == null)
                    bookReadCommand = new DelegateCommand<Book>(ReadBook, delegate(Book bk) { return bk != null && !bk.IsSecured; });
                return bookReadCommand;
            }
        }

        void ReadBook(Book bookAsParam)
        {
            try
            {
                if (BookViewModel != null)
                    BookViewModel.CloseCommandExecute();

                BookViewModelBase newElem = (BookViewModelBase)
                    Activator.CreateInstance(Type.GetType(BookServiceFactory.Instance.GetViewModel(bookAsParam)), new object[] { bookAsParam });
                ViewModels.Add( newElem );
                SetActiveView(newElem);
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("MainViewModel:OpenBook", err);
            }
        }
        #endregion

        #region close command

        private ICommand bookCloseCommand;
        public ICommand BookCloseCommand
        {
            get
            {
                if (bookCloseCommand == null)
                    bookCloseCommand = new DelegateCommand(CloseCommandExecute, CloseCommandCanExecute);
                return bookCloseCommand;
            }
        }

        bool CloseCommandCanExecute()
        {
            return BookViewModel != null;
        }

        void CloseCommandExecute()
        {
            BookViewModel.CloseCommandExecute();
            ViewModels.Remove(BookViewModel);
        }

        #endregion

        #region save command
        public ICommand BookSaveCommand
        {
            get
            {
                if (BookViewModel != null)
                    return BookViewModel.BookSaveCommand;
                else
                    return _emptyCommand;
            }
        }

        #endregion

        #region edit command
        private ICommand bookEditCommand;
        public ICommand BookEditCommand
        {
            get
            {
                if (bookEditCommand == null)
                    bookEditCommand = new DelegateCommand<Book>(EditBook, CanEditBook);
                return bookEditCommand;
            }
        }

        bool CanEditBook(Book bk)
        {
            try
            {
                if (bk != null || (BookViewModel != null && BookViewModel.Data != null && !BookViewModel.IsInEditMode))
                    return true;
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("MainViewModel:CanEditBook", err);
            }
            return false;
        }

        void EditBook(Book bk)
        {
            try
            {
                IsInEditMode = !IsInEditMode;

                if (BookViewModel != null)
                    BookViewModel.IsInEditMode = IsInEditMode;
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("MainViewModel:EditBook", err);
            }
        }
        #endregion

        #region delete command
        private ICommand bookDeleteCommand;
        public ICommand BookDeleteCommand
        {
            get
            {
                if (bookDeleteCommand == null)
                    bookDeleteCommand = new DelegateCommand<Book>(DeleteBook, delegate(Book bk) { return bk != null; });
                return bookDeleteCommand;
            }
        }

        void DeleteBook(Book bk)
        {
            try
            {
                //message de confirmation
                if (MessageBox.Show("Are you sure ?", "Warning", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    Data.BookInfoFilePath.Remove(bk.BookInfoFilePath);
                    Data.Books.Remove(bk);
                    Data.IsDirty = true;
                    BookServiceFactory.Instance.GetService(bk).Delete(bk);
                }
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("MainViewModel:EditBook", err);
            }
        }
        #endregion

        #region remove command
        private ICommand bookRemoveCommand;
        public ICommand BookRemoveCommand
        {
            get
            {
                if (bookRemoveCommand == null)
                    bookRemoveCommand = new DelegateCommand<Book>(RemoveBook, delegate(Book bk) { return bk != null; });
                return bookRemoveCommand;
            }
        }

        void RemoveBook(Book bk)
        {
            try
            {
                    Data.BookInfoFilePath.Remove(bk.BookInfoFilePath);
                    Data.Books.Remove(bk);
                    Data.IsDirty = true;
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("MainViewModel:RemoveBook", err);
            }
        }
        #endregion

        #region protect command
        private ICommand bookProtectCommand;
        public ICommand BookProtectCommand
        {
            get
            {
                if (bookProtectCommand == null)
                    bookProtectCommand = new DelegateCommand<Book>(ProtectBook, delegate(Book bk) { return bk != null; });
                return bookProtectCommand;
            }
        }

        void ProtectBook(Book bk)
        {
            try
            {
                PasswordDialog dlg = new PasswordDialog();
                dlg.Owner = Application.Current.MainWindow;
                dlg.ShowDialog();
                if (dlg.DialogResult == true)
                    BookServiceFactory.Instance.GetService(bk).Protect(bk, !bk.IsSecured, dlg.PassBox.Password);
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("MainViewModel:EditBook", err);
            }
        }
        #endregion

        #region mark as read command
        private ICommand bookMarkAsReadCommand;
        public ICommand BookMarkAsReadCommand
        {
            get
            {
                if (bookMarkAsReadCommand == null)
                    bookMarkAsReadCommand = new DelegateCommand<Book>(MarkAsRead, delegate(Book bk) { return bk != null; });
                return bookMarkAsReadCommand;
            }
        }

        void MarkAsRead(Book bk)
        {
            try
            {
                bk.IsRead = !bk.IsRead;
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("MainViewModel:MarkAsRead", err);
            }
        }
        #endregion

        #region add book command
        private ICommand bookAddCommand;
        public ICommand BookAddCommand
        {
            get
            {
                if (bookAddCommand == null)
                    bookAddCommand = new DelegateCommand<string>(AddBook, delegate(string param) { return Data != null; });
                return bookAddCommand;
            }
        }

        void AddBook(string param)
        {
            try
            {
                //add one file to the library
                if (param == "One")
                {
                    using (System.Windows.Forms.OpenFileDialog browser = new System.Windows.Forms.OpenFileDialog())
                    {
                        browser.AddExtension = true;
                        browser.Filter = FileService.Instance.BookFilterAllEditable;
                        browser.FilterIndex = FileService.Instance.BookFilterDefaultIndex;

                        if (browser.ShowDialog(new Wpf32Window()) == System.Windows.Forms.DialogResult.OK)
                        {
                            CatalogService.Instance.AddBook(Data, browser.FileName);
                        }
                    }
                }
                else // or all founded in a folder
                {
                    if (!string.IsNullOrEmpty(Data.BookFolder))
                    {
                        if (MessageBox.Show("Your book folder is allready defined. Do you want to replace it ? Refreshing will work only with the new one.", "Warning", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                            return;
                    }

                    using (System.Windows.Forms.FolderBrowserDialog browser = new System.Windows.Forms.FolderBrowserDialog())
                    {
                        if (browser.ShowDialog(new Wpf32Window()) == System.Windows.Forms.DialogResult.OK)
                        {
                            Data.BookFolder = browser.SelectedPath;
                            CatalogService.Instance.Refresh(Data);
                        }
                    }

                }
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("MainViewModel:AddBook", err);
            }
        }

        private ICommand addBookFileCommand;
        public ICommand AddBookFileCommand
        {
            get
            {
                if (addBookFileCommand == null)
                    addBookFileCommand = new DelegateCommand<string>(AddFileBook, delegate(string param) { return Data != null; });
                return addBookFileCommand;
            }
        }

        void AddFileBook(string fileBook)
        {
            try
            {
                CatalogService.Instance.AddBook(Data, fileBook);
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("MainViewModel:AddFileBook", err);
            }
        }

        #endregion

        #region property command
        private ICommand bookPropertyCommand;
        public ICommand BookPropertyCommand
        {
            get
            {
                if (bookPropertyCommand == null)
                    bookPropertyCommand = new DelegateCommand<Book>(DisplayProperty, delegate(Book bk) { return bk != null; });
                return bookPropertyCommand;
            }
        }

        void DisplayProperty(Book bk)
        {
            try
            {
                BackStageIndex = 4;
                BackStageIsOpen = true;
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("MainViewModel:DisplayProperty", err);
            }
        }
        #endregion

        #region bookmark command

        public ICommand BookmarkCommand
        {
            get
            {
                if (BookViewModel != null)
                    return BookViewModel.BookmarkCommand;
                else
                    return _emptyCommand;
            }
        }

        public ICommand GotoBookmarkCommand
        {
            get
            {
                if (BookViewModel != null)
                    return BookViewModel.GotoBookmarkCommand;
                else
                    return _emptyCommand;
            }
        }

        public ICommand ClearBookmarkCommand
        {
            get
            {
                if (BookViewModel != null)
                    return BookViewModel.ClearBookmarkCommand;
                else
                    return _emptyCommand;
            }
        }

        #endregion

        #region fit mode command

        public ICommand FitModeCommand
        {
            get
            {
                if (BookViewModel != null)
                    return BookViewModel.FitModeCommand;
                else
                    return _emptyCommand;
            }
        }

        #endregion

        #region change page command

        public ICommand BookChangePageCommand
        {
            get
            {
                if (BookViewModel != null)
                    return BookViewModel.BookChangePageCommand;
                else
                    return _emptyCommand;
            }
        }

        #endregion

        #region goto page command

        public ICommand BookGotoPageCommand
        {
            get
            {
                if (BookViewModel != null)
                    return BookViewModel.BookGotoPageCommand;
                else
                    return _emptyCommand;
            }
        }

        #endregion

        #region goto last page command

        public ICommand BookGotoLastPageCommand
        {
            get
            {
                if (BookViewModel != null)
                    return BookViewModel.BookGotoLastPageCommand;
                else
                    return _emptyCommand;
            }
        }

        #endregion

        #endregion

        #region ----------------BOOK PROPERTIES----------------

        #region fit mode

        private PageControlFitMode _FitMode = (PageControlFitMode)WorkspaceService.Instance.Settings.AutoFitMode;
        public PageControlFitMode FitMode
        {
            get { return _FitMode; }
            set
            {
                if (_FitMode != value)
                {
                    _FitMode = value;
                    RaisePropertyChanged("IsFitModeWidth");
                    RaisePropertyChanged("IsFitModeHeight");

                    Mediator.Instance.NotifyColleagues(ViewModelMessages.FitModeChanged, _FitMode);
                }
            }
        }

        public bool IsFitModeWidth
        {
            get { return FitMode == PageControlFitMode.Width; }
            set
            {
                if (value == true) FitMode = PageControlFitMode.Width;
            }
        }

        public bool IsFitModeHeight
        {
            get { return FitMode == PageControlFitMode.Height; }
            set
            {
                if (value == true) FitMode = PageControlFitMode.Height;
            }
        }
        
        #endregion

        #endregion
    }
}

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)

Share

About the Author

Guillaume Waser
Architect XEROX
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/

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 22 Oct 2012
Article Copyright 2011 by Guillaume Waser
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid