public TextReader CreateReader(Book book) { TextReader textReader; if (book.Path.EndsWith("pdf")) textReader = PdfTextReader.Open(book.Path); else if (book.Path.EndsWith("chm")) textReader = new ChmTextReader(book.Path); else textReader = new StreamReader(File.OpenRead(book.Path)); return textReader; }
public LibraryDiff Refresh(Book[] foundBooksThisTime) { List<Book> newBooks = new List<Book>(); List<Book> removedBooks = new List<Book>(); var existingPaths = foundBooksThisTime.Select(x => x.Path); var orphanedBooks = new List<Book>(); var updatedBooks = new List<Book>(); foreach (var book in Books) { if (!existingPaths.Contains(book.Path, StringComparer.InvariantCultureIgnoreCase)) orphanedBooks.Add(book); } foreach (var orphanedBook in orphanedBooks) { Books.Remove(orphanedBook); } removedBooks.AddRange(orphanedBooks); foreach (var foundBook in foundBooksThisTime) { var existingBook = Books.SingleOrDefault(x => x.Path == foundBook.Path); if (existingBook != null) { // if size or timestamp changed if (existingBook.Size != foundBook.Size || foundBook.Timestamp.Subtract(existingBook.Timestamp).Milliseconds > 0) { existingBook.UpdateFileInfo(foundBook); updatedBooks.Add(existingBook); } } else { Books.Add(foundBook); newBooks.Add(foundBook); } } return new LibraryDiff(newBooks, updatedBooks, removedBooks); }
public void UpdateFileInfo(Book book) { this.size = book.Size; this.timestamp = book.Timestamp; }