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

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

        public void SaveBookInfo(object param)
        {
            Book bk = param as Book;

            Stream stream = null;
            try
            {
                IFormatter formatter = new BinaryFormatter();
                stream = new FileStream(bk.BookInfoFilePath, FileMode.Create, FileAccess.Write, FileShare.None);

                if (stream != null)
                {
                    //the thumbnail
                    using (Stream img = StreamToImage.GetStreamFromImage(bk.Cover))
                    {
                        formatter.Serialize(stream, img);
                    }
                    //mySelf file to be restored
                    formatter.Serialize(stream, bk.BookInfoFilePath);
                    // comic file
                    formatter.Serialize(stream, bk.FilePath);
                    //bookmark tag
                    formatter.Serialize(stream, bk.Bookmark);
                    //IsRead tag
                    formatter.Serialize(stream, bk.IsRead);
                    //IsSecured tag
                    formatter.Serialize(stream, bk.IsSecured);
                    //Password tag
                    formatter.Serialize(stream, bk.Password);
                    //page number
                    formatter.Serialize(stream, bk.PageCount);
                    //Size tag
                    formatter.Serialize(stream, bk.Size);
                    //Rating tag
                    formatter.Serialize(stream, bk.Rating);

                    //manage the dynamic properties
                    IDictionary<string, object> dict = bk.Dynamics as IDictionary<string, object>;
                    int counter = dict.Count(p => !string.IsNullOrEmpty(p.Value.ToString()));

                    //not null property counter
                    formatter.Serialize(stream, counter);

                    //then the key/values
                    foreach (string k in dict.Keys)
                    {
                        if (!string.IsNullOrEmpty(k))
                        {
                            formatter.Serialize(stream, k);
                            formatter.Serialize(stream, dict[k]);
                        }
                    }
                }
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("BookInfoService:SaveBookInfo", err);
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
        }

        public void LoadBookInfo(object param)
        {
            ThreadExchangeData ted = param as ThreadExchangeData;

            Stream stream = null;
            try
            {
                Book bk = LoadBookInfo( ted.BookPath );

                if( bk != null )
                    Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, (ThreadStart)delegate
                    {
                        ted.ThreadCatalog.Books.Add(bk);
                    });
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("BookInfoService:LoadBookInfo", err);
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
        }

        public Book LoadBookInfo(string fileInfoPath)
        {
            try
            {
                //the file exist, try to load
                if (File.Exists(fileInfoPath))
                {
                    Book bk = new Book();

                    IFormatter formatter = new BinaryFormatter();
                    using (Stream stream = new FileStream(fileInfoPath, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        //the thumbnail
                        using (MemoryStream coverStream = (MemoryStream)formatter.Deserialize(stream))
                        {
                            MemoryStream tempStream = new MemoryStream();
                            coverStream.WriteTo(tempStream);
                            coverStream.Flush();
                            coverStream.Close();

                            tempStream.Position = 0;

                            Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, (ThreadStart)delegate
                            {
                                BitmapImage myImage = new BitmapImage();
                                myImage.BeginInit();
                                myImage.CacheOption = BitmapCacheOption.OnLoad;
                                myImage.StreamSource = tempStream;
                                myImage.EndInit();
                                bk.Cover = myImage;
                            });
                            tempStream = null;
                        }
                        //mySelf file to be restored
                        bk.BookInfoFilePath = (string)formatter.Deserialize(stream);
                        // comic file
                        bk.FilePath = (string)formatter.Deserialize(stream);
                        //bookmark tag
                        bk.Bookmark = (string)formatter.Deserialize(stream);
                        //IsRead tag
                        bk.IsRead = (bool)formatter.Deserialize(stream);
                        //IsSecured tag
                        bk.IsSecured = (bool)formatter.Deserialize(stream);
                        //Password tag
                        bk.Password = (string)formatter.Deserialize(stream);
                        //page number
                        bk.PageCount = (int)formatter.Deserialize(stream);
                        //Size tag
                        bk.Size = (long)formatter.Deserialize(stream);
                        //Rating tag
                        bk.Rating = (int)formatter.Deserialize(stream);

                        try
                        {
                            //manage the dynamic properties = not null property counter
                            int counter = (int)formatter.Deserialize(stream);

                            //then the key/values
                            for (int i = 0; i < counter; i++)
                            {
                                ((IDictionary<string, object>)bk.Dynamics).Add((string)formatter.Deserialize(stream), (string)formatter.Deserialize(stream));
                            }
                        }
                        catch (Exception err)
                        {
                            ExceptionHelper.Manage("BookInfoService:LoadBookInfo no dynamics", err);
                        }

                        new BookServiceBase().SynchronizeProperties(bk);
                           
                        bk.IsDirty = false;
                    }

                    return bk;
                }
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("BookInfoService:LoadBookInfo", err);
            }

            return null;
        }

        #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