예제 #1
0
 private void loadLists() {
   final BooksDatabase database = BooksDatabase.Instance();
   myAuthors = database.loadAuthors(myId);
   myTags = database.loadTags(myId);
   mySeriesInfo = database.loadSeriesInfo(myId);
   myIsSaved = true;
 }
예제 #2
0
  public boolean save() {
    if (myIsSaved) {
      return false;
    }
    final BooksDatabase database = BooksDatabase.Instance();
    database.executeAsATransaction(
        new Runnable() {
          public void run() {
            if (myId >= 0) {
              final FileInfoSet fileInfos = new FileInfoSet(File);
              database.updateBookInfo(myId, fileInfos.getId(File), myEncoding, myLanguage, myTitle);
            } else {
              myId = database.insertBookInfo(File, myEncoding, myLanguage, myTitle);
              storeAllVisitedHyperinks();
            }

            long index = 0;
            database.deleteAllBookAuthors(myId);
            for (Author author : authors()) {
              database.saveBookAuthorInfo(myId, index++, author);
            }
            database.deleteAllBookTags(myId);
            for (Tag tag : tags()) {
              database.saveBookTagInfo(myId, tag);
            }
            database.saveBookSeriesInfo(myId, mySeriesInfo);
          }
        });

    myIsSaved = true;
    return true;
  }
예제 #3
0
 public void setBookFavorite(Book book, boolean favorite) {
   if (favorite) {
     myDatabase.addToFavorites(book.getId());
   } else {
     myDatabase.removeFromFavorites(book.getId());
   }
   fireBookEvent(BookEvent.Updated, book);
 }
예제 #4
0
 public void addBookToRecentList(Book book) {
   final List<Long> ids = myDatabase.loadRecentBookIds();
   final Long bookId = book.getId();
   ids.remove(bookId);
   ids.add(0, bookId);
   if (ids.size() > 12) {
     ids.remove(12);
   }
   myDatabase.saveRecentBookIds(ids);
 }
예제 #5
0
  public void removeBook(Book book, boolean deleteFromDisk) {
    synchronized (myBooksByFile) {
      myBooksByFile.remove(book.File);
      myBooksById.remove(book.getId());

      final List<Long> ids = myDatabase.loadRecentBookIds();
      if (ids.remove(book.getId())) {
        myDatabase.saveRecentBookIds(ids);
      }
      if (deleteFromDisk) {
        book.File.getPhysicalFile().delete();
      }
    }
    fireBookEvent(BookEvent.Removed, book);
  }
예제 #6
0
 private void storeAllVisitedHyperinks() {
   if (myId != -1 && myVisitedHyperlinks != null) {
     for (String linkId : myVisitedHyperlinks) {
       BooksDatabase.Instance().addVisitedHyperlink(myId, linkId);
     }
   }
 }
예제 #7
0
  public static Book getByFile(ZLFile bookFile) {
    if (bookFile == null) {
      return null;
    }

    final ZLPhysicalFile physicalFile = bookFile.getPhysicalFile();
    if (physicalFile != null && !physicalFile.exists()) {
      return null;
    }

    final FileInfoSet fileInfos = new FileInfoSet(bookFile);

    Book book = BooksDatabase.Instance().loadBookByFile(fileInfos.getId(bookFile), bookFile);
    if (book != null) {
      book.loadLists();
    }

    if (book != null && fileInfos.check(physicalFile, physicalFile != bookFile)) {
      return book;
    }
    fileInfos.save();

    if (book == null) {
      book = new Book(bookFile);
    }
    if (book.readMetaInfo()) {
      book.save();
      return book;
    }
    return null;
  }
예제 #8
0
 private void initHyperlinkSet() {
   if (myVisitedHyperlinks == null) {
     myVisitedHyperlinks = new TreeSet<String>();
     if (myId != -1) {
       myVisitedHyperlinks.addAll(BooksDatabase.Instance().loadVisitedHyperlinks(myId));
     }
   }
 }
예제 #9
0
 public void markHyperlinkAsVisited(String linkId) {
   initHyperlinkSet();
   if (!myVisitedHyperlinks.contains(linkId)) {
     myVisitedHyperlinks.add(linkId);
     if (myId != -1) {
       BooksDatabase.Instance().addVisitedHyperlink(myId, linkId);
     }
   }
 }
예제 #10
0
  public Book getBookByFile(ZLFile bookFile) {
    if (bookFile == null) {
      return null;
    }
    final FormatPlugin plugin = PluginCollection.Instance().getPlugin(bookFile);
    if (plugin == null) {
      return null;
    }
    try {
      bookFile = plugin.realBookFile(bookFile);
    } catch (BookReadingException e) {
      return null;
    }

    Book book = myBooksByFile.get(bookFile);
    if (book != null) {
      return book;
    }

    final ZLPhysicalFile physicalFile = bookFile.getPhysicalFile();
    if (physicalFile != null && !physicalFile.exists()) {
      return null;
    }

    final FileInfoSet fileInfos = new FileInfoSet(myDatabase, bookFile);

    book = myDatabase.loadBookByFile(fileInfos.getId(bookFile), bookFile);
    if (book != null) {
      book.loadLists(myDatabase);
    }

    if (book != null && fileInfos.check(physicalFile, physicalFile != bookFile)) {
      saveBook(book, false);
      // saved
      addBook(book, false);
      return book;
    }
    fileInfos.save();

    try {
      if (book == null) {
        book = new Book(bookFile);
      } else {
        book.readMetaInfo();
      }
    } catch (BookReadingException e) {
      return null;
    }

    saveBook(book, false);
    return book;
  }
예제 #11
0
 public void reloadInfoFromDatabase() {
   final BooksDatabase database = BooksDatabase.Instance();
   database.reloadBook(this);
   myAuthors = database.loadAuthors(myId);
   myTags = database.loadTags(myId);
   mySeriesInfo = database.loadSeriesInfo(myId);
   myIsSaved = true;
 }
예제 #12
0
  public Book getBookById(long id) {
    Book book = myBooksById.get(id);
    if (book != null) {
      return book;
    }

    book = myDatabase.loadBook(id);
    if (book == null) {
      return null;
    }
    book.loadLists(myDatabase);

    final ZLFile bookFile = book.File;
    final ZLPhysicalFile physicalFile = bookFile.getPhysicalFile();
    if (physicalFile == null) {
      // loaded from db
      addBook(book, false);
      return book;
    }
    if (!physicalFile.exists()) {
      return null;
    }

    FileInfoSet fileInfos = new FileInfoSet(myDatabase, physicalFile);
    if (fileInfos.check(physicalFile, physicalFile != bookFile)) {
      // loaded from db
      addBook(book, false);
      return book;
    }
    fileInfos.save();

    try {
      book.readMetaInfo();
      // loaded from db
      addBook(book, false);
      return book;
    } catch (BookReadingException e) {
      return null;
    }
  }
예제 #13
0
  public static Book getById(long bookId) {
    final Book book = BooksDatabase.Instance().loadBook(bookId);
    if (book == null) {
      return null;
    }
    book.loadLists();

    final ZLFile bookFile = book.File;
    final ZLPhysicalFile physicalFile = bookFile.getPhysicalFile();
    if (physicalFile == null) {
      return book;
    }
    if (!physicalFile.exists()) {
      return null;
    }

    FileInfoSet fileInfos = new FileInfoSet(physicalFile);
    if (fileInfos.check(physicalFile, physicalFile != bookFile)) {
      return book;
    }
    fileInfos.save();

    return book.readMetaInfo() ? book : null;
  }
예제 #14
0
 public List<Bookmark> invisibleBookmarks(Book book) {
   final List<Bookmark> list = myDatabase.loadBookmarks(book.getId(), false);
   Collections.sort(list, new Bookmark.ByTimeComparator());
   return list;
 }
예제 #15
0
 public List<Bookmark> bookmarks(long fromId, int limitCount) {
   return myDatabase.loadVisibleBookmarks(fromId, limitCount);
 }
예제 #16
0
  private void build() {
    // Step 0: get database books marked as "existing"
    final FileInfoSet fileInfos = new FileInfoSet(myDatabase);
    final Map<Long, Book> savedBooksByFileId = myDatabase.loadBooks(fileInfos, true);
    final Map<Long, Book> savedBooksByBookId = new HashMap<Long, Book>();
    for (Book b : savedBooksByFileId.values()) {
      savedBooksByBookId.put(b.getId(), b);
    }

    // Step 1: check if files corresponding to "existing" books really exists;
    //         add books to library if yes (and reload book info if needed);
    //         remove from recent/favorites list if no;
    //         collect newly "orphaned" books
    final Set<Book> orphanedBooks = new HashSet<Book>();
    final Set<ZLPhysicalFile> physicalFiles = new HashSet<ZLPhysicalFile>();
    int count = 0;
    for (Book book : savedBooksByFileId.values()) {
      final ZLPhysicalFile file = book.File.getPhysicalFile();
      if (file != null) {
        physicalFiles.add(file);
      }
      if (file != book.File && file != null && file.getPath().endsWith(".epub")) {
        continue;
      }
      if (book.File.exists()) {
        boolean doAdd = true;
        if (file == null) {
          continue;
        }
        if (!fileInfos.check(file, true)) {
          try {
            book.readMetaInfo();
            saveBook(book, false);
          } catch (BookReadingException e) {
            doAdd = false;
          }
          file.setCached(false);
        }
        if (doAdd) {
          // loaded from db
          addBook(book, false);
        }
      } else {
        orphanedBooks.add(book);
      }
    }
    myDatabase.setExistingFlag(orphanedBooks, false);

    // Step 2: collect books from physical files; add new, update already added,
    //         unmark orphaned as existing again, collect newly added
    final Map<Long, Book> orphanedBooksByFileId = myDatabase.loadBooks(fileInfos, false);
    final Set<Book> newBooks = new HashSet<Book>();

    final List<ZLPhysicalFile> physicalFilesList = collectPhysicalFiles(BookDirectories);
    for (ZLPhysicalFile file : physicalFilesList) {
      if (physicalFiles.contains(file)) {
        continue;
      }
      collectBooks(
          file,
          fileInfos,
          savedBooksByFileId,
          orphanedBooksByFileId,
          newBooks,
          !fileInfos.check(file, true));
      file.setCached(false);
    }

    // Step 3: add help file
    try {
      final ZLFile helpFile = BookUtil.getHelpFile();
      Book helpBook = savedBooksByFileId.get(fileInfos.getId(helpFile));
      if (helpBook == null) {
        helpBook = new Book(helpFile);
      }
      saveBook(helpBook, false);
      // saved
      addBook(helpBook, false);
    } catch (BookReadingException e) {
      // that's impossible
      e.printStackTrace();
    }

    // Step 4: save changes into database
    fileInfos.save();

    myDatabase.executeAsTransaction(
        new Runnable() {
          public void run() {
            for (Book book : newBooks) {
              saveBook(book, false);
            }
          }
        });
    myDatabase.setExistingFlag(newBooks, true);
  }
예제 #17
0
 public ZLTextPosition getStoredPosition() {
   return BooksDatabase.Instance().getStoredPosition(myId);
 }
예제 #18
0
 public void storePosition(ZLTextPosition position) {
   if (myId != -1) {
     BooksDatabase.Instance().storePosition(myId, position);
   }
 }
예제 #19
0
 public boolean isFavorite(Book book) {
   if (book == null) {
     return false;
   }
   return myDatabase.isFavorite(book.getId());
 }
예제 #20
0
 public boolean hasFavorites() {
   return myDatabase.hasFavorites();
 }
예제 #21
0
 public Book getRecentBook(int index) {
   List<Long> recentIds = myDatabase.loadRecentBookIds();
   return recentIds.size() > index ? getBookById(recentIds.get(index)) : null;
 }
예제 #22
0
 public void insertIntoBookList() {
   if (myId != -1) {
     BooksDatabase.Instance().insertIntoBookList(myId);
   }
 }
예제 #23
0
 public ZLTextPosition getStoredPosition(long bookId) {
   return myDatabase.getStoredPosition(bookId);
 }
예제 #24
0
 public void saveBookmark(Bookmark bookmark) {
   if (bookmark != null) {
     bookmark.setId(myDatabase.saveBookmark(bookmark));
   }
 }
예제 #25
0
 public void deleteBookmark(Bookmark bookmark) {
   if (bookmark != null && bookmark.getId() != -1) {
     myDatabase.deleteBookmark(bookmark);
   }
 }
예제 #26
0
 public List<Book> recentBooks() {
   return books(myDatabase.loadRecentBookIds());
 }
예제 #27
0
 public void storePosition(long bookId, ZLTextPosition position) {
   if (bookId != -1) {
     myDatabase.storePosition(bookId, position);
   }
 }
예제 #28
0
 public List<Book> favorites() {
   return books(myDatabase.loadFavoriteIds());
 }