Click here to Skip to main content
15,881,380 members
Articles / Desktop Programming / WPF

WPF Book Reader

Rate me:
Please Sign up or sign in to vote.
4.29/5 (16 votes)
10 May 2012GPL35 min read 110.7K   3.1K   120  
A WPF book reader for cbz/cbr files
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Media.Imaging;

namespace BookReader
{
	internal class BaseBook : IBook
	{
		#region -----------------constructors-----------------

		public BaseBook()
		{
		}

		public BaseBook(string filePath)
		{
			_filePath = filePath;
			RaisePropetyChanged("FilePath");
			RaisePropetyChanged("FileName");
		}
		#endregion

		#region -----------------properties-----------------
		private ImageCache _Cache = new ImageCache();
		public ImageCache Cache
		{
			get { if (_Cache == null) _Cache = new ImageCache();  return _Cache; }
			set { _Cache = value; }
		}

		private string _filePath = string.Empty;
		public string FilePath
		{
			get { return _filePath; }
			set { _filePath = value; }
		}

		[NonSerialized()]
		private IBookItem _CurrentPage = null;
		public IBookItem CurrentPage
		{
			get { return _CurrentPage; }
			set { _CurrentPage = value; }
		}

		private List<IBookItem> _Pages = new List<IBookItem>();
		public List<IBookItem> Pages
		{
			get { if (_Pages == null) _Pages = new List<IBookItem>(); return _Pages; }
			set { _Pages = value; }
		}
		
		private string _Bookmark = string.Empty;
		public string Bookmark
		{
			get { return _Bookmark; }
			set { _Bookmark = value; RaisePropetyChanged("Bookmark"); }
		}

        private bool _IsRead= false;
        public bool IsRead
        {
            get { return _IsRead; }
            set { _IsRead = value; RaisePropetyChanged("IsRead"); }
        }


		private BitmapImage _Cover = null;
		public BitmapImage Cover
		{
			get { return _Cover; }
			set { _Cover = value; RaisePropetyChanged("Cover"); }
		}

		public string FileName
		{
			get
			{
				return Path.GetFileName(this._filePath);
			}
		}

		private int _NbPages;
		public int NbPages
		{
			get { return _NbPages; }
			set { _NbPages = value; }
		}

		private long _Size;
		public long Size
		{
			get { return _Size; }
			set { _Size = value; }
		}


		#endregion

		#region -----------------loading/unloading-----------------

		virtual public void Load()
		{
			_CurrentPage = Pages[0];
		}

		virtual public void UnLoad()
		{
			Pages.Clear();
			Cache.Clear();
		}
		#endregion

		#region -----------------virtual's-----------------

		virtual public BitmapImage GetCurrentPageImage()
		{
			throw new NotSupportedException();
		}

		#endregion

		#region -----------------bookmark-----------------

		public void SetMark()
		{
			Bookmark = _CurrentPage.FilePath;
		}

		public void GotoMark()
		{
			if (!string.IsNullOrEmpty(_Bookmark))
			{
				foreach (IBookItem pg in Pages)
				{
					if( pg.FilePath == _Bookmark )
						_CurrentPage = pg;
				}
			}
		}
		#endregion

		#region -----------------page navigation management-----------------

		public bool GotoPage( IBookItem page )
		{
			foreach (IBookItem pg in Pages)
			{
				if (pg == page)
				{
					_CurrentPage = pg;
					return true;
				}
			}
			return false;
		}

		public bool GotoNextPage()
		{
			int next = Pages.IndexOf(_CurrentPage);
			if (next >= Pages.Count-1)
				return false;
			else
			{
				next = next + 1;
				_CurrentPage = Pages[next];
				return true;
			}
		}

		public bool GotoPreviousPage()
		{
			int next = Pages.IndexOf(_CurrentPage);
			if (next == 0)
				return false;
			else
			{
				next = next - 1;
				_CurrentPage = Pages[next];
				return true;
			}
		}
		#endregion

		#region -----------------cache management-----------------

		internal IBookItem GetNextPage(IBookItem fromPage )
		{
			int next = Pages.IndexOf(fromPage);
			if (next >= Pages.Count-1)
				return null;
			else
			{
				next = next + 1;
				return Pages[next];
			}
		}

		virtual public void ManageCache()
		{
			Cache.ManageCache();
		}

		#endregion

		#region -----------------INotifyPropertyChanged-----------------

		private void RaisePropetyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		#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