public void AddBook(Catalog catlog, string bookFile) { ThreadExchangeData ted = new ThreadExchangeData(); if (LogHelper.CanDebug()) { LogHelper.Begin("CatalogService.AddBook", "catalog: {0}, bookFile: {1}", catlog, bookFile); } try { FileInfo fi = new FileInfo(bookFile); if (DocumentFactory.Instance.FindBookFilterByExtWithModel(fi.Extension) != null) { if (!BookExistInCollection(catlog, fi.FullName)) { Book bkk = DocumentFactory.Instance.GetService(fi.FullName).CreateBookWithCover(DirectoryHelper.BookInfoPath, fi); catlog.Books.Add(bkk); catlog.BookInfoFilePath.Add(bkk.BookInfoFilePath); catlog.IsDirty = true; RepositoryProcessItemChange(catlog); } } } catch (Exception err) { LogHelper.Manage("CatalogService.AddBook", err); } finally { LogHelper.End("CatalogService.AddBook"); } }
/// <summary> /// Find the service and create the book info and cover image /// </summary> /// <param name="ted"></param> /// <param name="file"></param> internal void CreateBookInfo(ThreadExchangeData ted, FileInfo file) { if (LogHelper.CanDebug()) { LogHelper.Begin("CatalogService.CreateBookInfo"); } try { Book bkk = DocumentFactory.Instance.GetService(file.FullName).CreateBookWithCover(DirectoryHelper.BookInfoPath, file); Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart) delegate { ted.ThreadCatalog.Books.Add(bkk); }); ted.ThreadCatalog.BookInfoFilePath.Add(bkk.BookInfoFilePath); ted.ThreadCatalog.IsDirty = true; } catch (Exception err) { LogHelper.Manage("CatalogService.CreateBookInfo", err); } finally { LogHelper.End("CatalogService.CreateBookInfo"); } }
/// <summary> /// Load the given BookInfo through ThreadExchangeData /// </summary> /// <param name="param"></param> public void LoadBookInfo(object param) { ThreadExchangeData ted = param as ThreadExchangeData; Stream stream = null; if (LogHelper.CanDebug()) { LogHelper.Begin("BookInfoService.LoadBookInfo"); } 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) { LogHelper.Manage("BookInfoService.LoadBookInfo", err); } finally { if (stream != null) { stream.Close(); } LogHelper.End("BookInfoService.LoadBookInfo"); } }
internal void ParseDirectoryRecursive(object param) { ThreadExchangeData ted = param as ThreadExchangeData; if (LogHelper.CanDebug()) { LogHelper.Begin("CatalogService.ParseDirectoryRecursive"); } try { DirectoryInfo directory = new DirectoryInfo((string)ted.BookPath); if (!directory.Exists) { Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart) delegate { string msg = CultureManager.Instance.GetLocalization("ByCode", "Warning.CatalogPath", "Catalog path does not exist! Please check the options box"); MessageBox.Show(msg); }); return; } foreach (FileInfo file in directory.GetFiles("*.*")) { if (DocumentFactory.Instance.FindBookFilterByExtWithModel(file.Extension) != null) { if (!BookExistInCollection(ted.ThreadCatalog, file.FullName)) { CreateBookInfo(ted, file); } } } foreach (DirectoryInfo dir in directory.GetDirectories("*", SearchOption.TopDirectoryOnly)) { ted.BookPath = dir.FullName; ParseDirectoryRecursive(ted); } } catch (Exception err) { LogHelper.Manage("CatalogService.ParseDirectoryRecursive", err); } finally { LogHelper.End("CatalogService.ParseDirectoryRecursive"); } }
/// <summary> /// Refresh thread method /// </summary> /// <param name="param"></param> internal void RefreshThread(object param) { ThreadExchangeData ted = param as ThreadExchangeData; if (LogHelper.CanDebug()) { LogHelper.Begin("CatalogService.RefreshThread"); } try { //first, remove unfounded books List <Book> temp = new List <Book>(); foreach (Book bk in ted.ThreadCatalog.Books) { if (!File.Exists(bk.FilePath)) { temp.Add(bk); } } foreach (Book bk in temp) { Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart) delegate { ted.ThreadCatalog.BookInfoFilePath.Remove(bk.BookInfoFilePath); ted.ThreadCatalog.Books.Remove(bk); ted.ThreadCatalog.IsDirty = true; }); ted.ThreadCatalog.IsDirty = true; } //then add the new ones ParseDirectoryRecursive(ted); } catch (Exception err) { LogHelper.Manage("CatalogService.RefreshThread", err); } finally { LogHelper.End("CatalogService.RefreshThread"); } }