Click here to Skip to main content
15,895,799 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.5K   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 System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using CBR.Core.Helpers;
using System.Windows;
using System.Windows.Threading;
using System.Threading;
using System.Windows.Media.Imaging;

namespace CBR.Core.Services
{
    public class BookServiceBase
    {
        #region -----------------BOOK INFO-----------------

        public Book CreateBook(string filePath)
        {
            return new Book(null, filePath);
        }

		public Book CreateBookWithCover(string folder, FileInfo file)
		{
            string fileInfo = Path.Combine(folder, file.DirectoryName.Replace(file.Directory.Root.Name, "").Replace('\\', '.') + "." + file.Name + ".cbb");

            if (File.Exists(fileInfo))
            {
                return new BookInfoService().LoadBookInfo(fileInfo);
            }
            else
            {
                Book bk = new Book(fileInfo, file.FullName);
                bk.Size = file.Length / 1024 / 1024;

                Thread t = new Thread(new ParameterizedThreadStart(LoadCoverThread));
                t.IsBackground = true;
                t.Priority = ThreadPriority.Lowest;
                t.Start(bk);
                return bk;
            }
		}

		virtual internal void LoadCoverThread(object param)
		{
            throw new NotImplementedException();
		}

		virtual public object LoadBook(Book bk)
		{
            WorkspaceService.Instance.AddRecent(bk);

            return null;
		}

		virtual public void UnloadBook(Book bk)
		{
			bk.Pages.Clear();
		}

        #endregion

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

        virtual public void EditBook(Book bk)
		{
            throw new NotImplementedException();
		}

        virtual public Book SaveBook(Book bk)
        {
            throw new NotImplementedException();
        }

        public bool IsDynamic(Book bk)
        {
            try
            {
                if( bk != null )
                   return (bk.Pages.AsParallel().Count(p => p.Frames.Count != 0) != 0);
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("BookServiceBase:IsDynamic", err);
            }

            return false;
        }

        public void SynchronizeProperties(Book bk)
        {
            try
            {
                IDictionary<string, object> dict = (IDictionary<string, object>)bk.Dynamics;

                // add the properties from the settings if missing
                foreach( string k in WorkspaceService.Instance.Settings.Dynamics )
                {
                    if (!dict.Keys.Contains(k))
                        dict.Add(k, string.Empty);
                }

                // remove old properties that were removed from settings
                foreach (string k in dict.Keys)
                {
                    if (!WorkspaceService.Instance.Settings.Dynamics.Contains(k))
                        dict.Keys.Remove(k);
                }
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("BookServiceBase:SynchronizeProperties", err);
            }
        }

		#endregion

		#region -----------------BOOKMARK-----------------

		virtual public void SetMark(Book bk, Page pg)
		{
            throw new NotImplementedException();
		}

        virtual public Page GotoMark(Book bk)
		{
            throw new NotImplementedException();
		}

        virtual public void ClearMark(Book bk)
        {
            if (bk != null)
                bk.Bookmark = string.Empty;
        }

        virtual public bool HasMark(Book bk)
        {
            if (bk != null)
                return !string.IsNullOrEmpty(bk.Bookmark);
            else
                return false;
        }

		#endregion

		#region -----------------PAGE NAVIGATION-----------------

        virtual public Page GotoPage(Book bk, Page currentPage, int step)
		{
			int pos = 0;

			if (currentPage != null)
				pos = bk.Pages.IndexOf(currentPage);

			if (step == -1)
			{
				return (pos == 0) ? currentPage : bk.Pages[pos - 1];
			}
			else
			{
				return (pos >= bk.Pages.Count - 1) ? currentPage : bk.Pages[pos + 1];
			}
		}

        virtual public bool CheckPageRange(Book bk, Page currentPage, int step)
		{
			int pos = 0;

			if (currentPage != null)
				pos = bk.Pages.IndexOf(currentPage);

			if (step == -1)
			{
				return (pos == 0) ? false : true;
			}
			else
			{
				return (pos >= bk.Pages.Count - 1) ? false : true;
			}
		}

        public void GotoFrame(Book bk, ref Page currentPage, ref Zone currentFrame, int step)
        {
            currentFrame = GotoFrame(currentPage, currentFrame, step);

            //we reach the min or max, change the page
            if (currentFrame == null)
                currentPage = GotoPage(bk, currentPage, step);

            GotoFrame(currentPage, currentFrame, step);
        }

        internal Zone GotoFrame(Page currentPage, Zone currentFrame, int step)
        {
            //if no frames, return a page frame
            if (currentPage.Frames.Count <= 0)
            {
                if (currentFrame == null)
                    return new Zone() { Duration = 15, OrderNum = 0, X = 0, Y = 0, Type = FrameType.Page };
                else
                    return null;
            }

            //search current frame position
            int posFrame = 0;
            if (currentFrame != null)
                posFrame = currentPage.Frames.IndexOf(currentFrame);

            //we go back, manage the min
            if (step == -1)
            {
                return (posFrame == 0) ? null : currentPage.Frames[posFrame - 1];
            }
            else //we go forward, manage the max
            {
                return (posFrame >= currentPage.Frames.Count - 1) ? null : currentPage.Frames[posFrame + 1];
            }
        }

		#endregion

        virtual public void Delete(Book bk)
        {
            try
            {
                //delete the book
                File.Delete(bk.FilePath);
                //delete the bin
                File.Delete(bk.BookInfoFilePath);
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("BookServiceBase:Delete", err);
            }
        }

        virtual public void Protect(Book bk, bool status, string password)
        {
            if (bk != null)
            {
                if (status == false) //remove protection
                {
                    if (bk.Password != password)
                        return;
                }
                bk.Password = password;
                bk.IsSecured = status;
            }
        }

        virtual public long ManageCache(Book bk)
		{
			return 0;
		}
    }
}

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