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
}
}