Click here to Skip to main content
15,896,269 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 125.7K   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 CBR.Core.Models;
using CBR.Core.Services;
using System.IO;
using CBR.Core.Files.Conversion;

namespace CBR.Core.Files
{
    public class FileService
    {
        #region ----------------SINGLETON----------------

        public static readonly FileService Instance = new FileService();

        /// <summary>
        /// Private constructor for singleton pattern
        /// </summary>
        private FileService()
        {
        }

        #endregion

        #region ----------------IMAGES----------------

        public List<string> ImageExtension = new List<string>() { ".PNG", ".BMP", ".JPG" };
        
        #endregion

        #region ----------------BOOKS----------------

        public List<FileExtension> BookFilters = new List<FileExtension>()
        {
			new FileExtension()
            { Extension= ".*", Description = "All Files (*.*)", Model=typeof(Book), Type= BookType.All },
			new FileExtension()
            { Extension= ".CBR", Description = "Comic RAR Files (*.CBR)", Model=typeof(Book), Service=typeof(BookService), Type= BookType.RARBased, ViewModel = "CBR.ViewModels.ComicViewModel" },
			new FileExtension()
            { Extension= ".CBZ", Description = "Comic ZIP Files (*.CBZ)", Model=typeof(Book), Service=typeof(BookService), Type= BookType.ZIPBased, ViewModel = "CBR.ViewModels.ComicViewModel" },
			new FileExtension()
            { Extension= ".ZIP", Description = "ZIP Files (*.ZIP)", Model=typeof(Book), Service=typeof(BookService), Type= BookType.ZIPBased, ViewModel = "CBR.ViewModels.ComicViewModel" },
			new FileExtension()
            { Extension= ".RAR", Description = "RAR Files (*.RAR)", Model=typeof(Book), Service=typeof(BookService), Type= BookType.RARBased, ViewModel = "CBR.ViewModels.ComicViewModel" },
            //new FileExtension()
            //{ Extension= ".CBRD", Description = "Dynamic Comic RAR Files (*.CBRD)", Model=typeof(Book), Service=typeof(BookService), Type= BookType.RARBased },
			new FileExtension()
            { Extension= ".CBZD", Description = "Dynamic Comic ZIP Files (*.CBZD)", Model=typeof(Book), Service=typeof(BookService), Type= BookType.ZIPBased, ViewModel = "CBR.ViewModels.ComicViewModel" },
			new FileExtension()
            { Extension= ".XPS", Description = "XPS Files (*.XPS)", Model=typeof(Book), Service=typeof(XpsBookService), Type= BookType.XPS, ViewModel = "CBR.ViewModels.XpsBookViewModel" },
            new FileExtension()
            { Extension= ".EPUB", Description = "ePUB Files (*.EPUB)", Model=typeof(Book), Service=typeof(ePUBBookService), Type= BookType.ePUB, ViewModel = "CBR.ViewModels.ePUBBookViewModel" },
            new FileExtension()
            { Extension= ".PDF", Description = "PDF Files (*.PDF)", Type= BookType.PDF }
        };

        public string BookFilterAllEditable
        {
            get { return BookFilters.Where(p => p.Model != null).Select(query => query.DialogFilter).Aggregate((a, b) => a + "|" + b); }
        }

        public string BookFilterAll
        {
            get { return BookFilters.Select(query => query.DialogFilter).Aggregate((a, b) => a + "|" + b); }
        }        

        public int BookFilterDefaultIndex
        {
            get { return 1; }
        }

        public FileExtension FindBookFilterByExt(string ext)
        {
            return BookFilters.Find(p => p.Extension == ext.ToUpper());
        }
        #endregion

        #region ----------------CATALOGS----------------

        public List<FileExtension> CatalogFilters = new List<FileExtension>()
        {
            new FileExtension() { Extension= ".*", Description = "All Files (*.*)" },
            new FileExtension() { Extension= ".CBL", Description = "Comic Book Reader Librairies (*.CBL)" }
        };

        public string CatalogFilterAll
        {
            get { return CatalogFilters.Select(p => p.DialogFilter).Aggregate((a, b) => a + "|" + b); }
        }

        public string CatalogFilterDefaultExtension
        {
            get { return CatalogFilters.Select(p => p.Extension).Last(); }
        }

        public int CatalogFilterDefaultIndex
        {
            get { return 2; }
        }

        public FileExtension FindCatalogFilterByExt( string ext )
        {
            return CatalogFilters.Find(p => p.Extension == ext.ToUpper());
        }

        #endregion

        #region ----------------OPERATIONS----------------

        public bool CopyToDevice(string srcPath, string destPath, DeviceInfo device)
        {
            // first, find the source format
            FileExtension fe = FindBookFilterByExt(Path.GetExtension(srcPath));

            // device support the source
            if (device.SupportedFormats.Contains(fe.Type))
            {
                File.Copy(srcPath, destPath, true);  //==> TODO Message for overwrite ?
                return true;
            }
            else
            {
                // find the best matching destination format
                if( fe.Type == BookType.ZIPBased )
                {
                    if (device.SupportedFormats.Contains(BookType.ImageFile))
                        new RARImageReader().Read(srcPath, Path.Combine(destPath, Path.GetFileNameWithoutExtension(srcPath)), null, null, null, null);
                }
                if( fe.Type == BookType.RARBased )
                {
                }
                if( fe.Type == BookType.XPS )
                {
                }
                if( fe.Type == BookType.ePUB )
                {
                }



                return true;
            }
        }

        #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)


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