示例#1
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;
  }
  private void updateTables4() {
    final FileInfoSet fileInfos = new FileInfoSet();
    final Cursor cursor = myDatabase.rawQuery("SELECT file_name FROM Books", null);
    while (cursor.moveToNext()) {
      fileInfos.check(ZLFile.createFileByPath(cursor.getString(0)).getPhysicalFile(), false);
    }
    cursor.close();
    fileInfos.save();

    myDatabase.execSQL(
        "CREATE TABLE RecentBooks("
            + "book_index INTEGER PRIMARY KEY,"
            + "book_id INTEGER REFERENCES Books(book_id))");
    final ArrayList<Long> ids = new ArrayList<Long>();

    final SQLiteStatement statement =
        myDatabase.compileStatement("SELECT book_id FROM Books WHERE file_name = ?");

    for (int i = 0; i < 20; ++i) {
      final ZLStringOption option = new ZLStringOption("LastOpenedBooks", "Book" + i, "");
      final String fileName = option.getValue();
      option.setValue("");
      try {
        statement.bindString(1, fileName);
        final long bookId = statement.simpleQueryForLong();
        if (bookId != -1) {
          ids.add(bookId);
        }
      } catch (SQLException e) {
      }
    }
    saveRecentBookIds(ids);
  }
  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;
  }
 @Override
 protected long insertBookInfo(ZLFile file, String encoding, String language, String title) {
   if (myInsertBookInfoStatement == null) {
     myInsertBookInfoStatement =
         myDatabase.compileStatement(
             "INSERT OR IGNORE INTO Books (encoding,language,title,file_id) VALUES (?,?,?,?)");
   }
   SQLiteUtil.bindString(myInsertBookInfoStatement, 1, encoding);
   SQLiteUtil.bindString(myInsertBookInfoStatement, 2, language);
   myInsertBookInfoStatement.bindString(3, title);
   final FileInfoSet infoSet = new FileInfoSet(file);
   myInsertBookInfoStatement.bindLong(4, infoSet.getId(file));
   return myInsertBookInfoStatement.executeInsert();
 }
  private void updateTables6() {
    myDatabase.execSQL("ALTER TABLE Bookmarks ADD COLUMN model_id TEXT");

    myDatabase.execSQL("ALTER TABLE Books ADD COLUMN file_id INTEGER");

    myDatabase.execSQL("DELETE FROM Files");
    final FileInfoSet infoSet = new FileInfoSet();
    Cursor cursor = myDatabase.rawQuery("SELECT file_name FROM Books", null);
    while (cursor.moveToNext()) {
      infoSet.check(ZLFile.createFileByPath(cursor.getString(0)).getPhysicalFile(), false);
    }
    cursor.close();
    infoSet.save();

    cursor = myDatabase.rawQuery("SELECT book_id,file_name FROM Books", null);
    final SQLiteStatement deleteStatement =
        myDatabase.compileStatement("DELETE FROM Books WHERE book_id = ?");
    final SQLiteStatement updateStatement =
        myDatabase.compileStatement("UPDATE Books SET file_id = ? WHERE book_id = ?");
    while (cursor.moveToNext()) {
      final long bookId = cursor.getLong(0);
      final long fileId = infoSet.getId(ZLFile.createFileByPath(cursor.getString(1)));

      if (fileId == -1) {
        deleteStatement.bindLong(1, bookId);
        deleteStatement.execute();
      } else {
        updateStatement.bindLong(1, fileId);
        updateStatement.bindLong(2, bookId);
        updateStatement.execute();
      }
    }
    cursor.close();

    myDatabase.execSQL("ALTER TABLE Books RENAME TO Books_Obsolete");
    myDatabase.execSQL(
        "CREATE TABLE Books("
            + "book_id INTEGER PRIMARY KEY,"
            + "encoding TEXT,"
            + "language TEXT,"
            + "title TEXT NOT NULL,"
            + "file_id INTEGER UNIQUE NOT NULL REFERENCES Files(file_id))");
    myDatabase.execSQL(
        "INSERT INTO Books (book_id,encoding,language,title,file_id) SELECT book_id,encoding,language,title,file_id FROM Books_Obsolete");
    myDatabase.execSQL("DROP TABLE Books_Obsolete");
  }
  private void collectBooks(
      ZLFile file,
      FileInfoSet fileInfos,
      Map<Long, Book> savedBooksByFileId,
      Map<Long, Book> orphanedBooksByFileId,
      Set<Book> newBooks,
      boolean doReadMetaInfo) {
    final long fileId = fileInfos.getId(file);
    if (savedBooksByFileId.get(fileId) != null) {
      return;
    }

    try {
      final Book book = orphanedBooksByFileId.get(fileId);
      if (book != null) {
        if (doReadMetaInfo) {
          book.readMetaInfo();
        }
        newBooks.add(book);
        return;
      }
    } catch (BookReadingException e) {
      // ignore
    }

    try {
      final Book book = new Book(file);
      newBooks.add(book);
      return;
    } catch (BookReadingException e) {
      // ignore
    }

    if (file.isArchive()) {
      for (ZLFile entry : fileInfos.archiveEntries(file)) {
        collectBooks(
            entry, fileInfos, savedBooksByFileId, orphanedBooksByFileId, newBooks, doReadMetaInfo);
      }
    }
  }
  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;
    }
  }
示例#8
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;
  }
 protected Book createBook(long id, long fileId, String title, String encoding, String language) {
   final FileInfoSet infos = new FileInfoSet(fileId);
   return createBook(id, infos.getFile(fileId), title, encoding, language);
 }
示例#10
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);
  }
  @Override
  protected Map<Long, Book> loadBooks(FileInfoSet infos, boolean existing) {
    Cursor cursor =
        myDatabase.rawQuery(
            "SELECT book_id,file_id,title,encoding,language FROM Books WHERE `exists` = "
                + (existing ? 1 : 0),
            null);
    final HashMap<Long, Book> booksById = new HashMap<Long, Book>();
    final HashMap<Long, Book> booksByFileId = new HashMap<Long, Book>();
    while (cursor.moveToNext()) {
      final long id = cursor.getLong(0);
      final long fileId = cursor.getLong(1);
      final Book book =
          createBook(
              id,
              infos.getFile(fileId),
              cursor.getString(2),
              cursor.getString(3),
              cursor.getString(4));
      if (book != null) {
        booksById.put(id, book);
        booksByFileId.put(fileId, book);
      }
    }
    cursor.close();

    initTagCache();

    cursor = myDatabase.rawQuery("SELECT author_id,name,sort_key FROM Authors", null);
    final HashMap<Long, Author> authorById = new HashMap<Long, Author>();
    while (cursor.moveToNext()) {
      authorById.put(cursor.getLong(0), new Author(cursor.getString(1), cursor.getString(2)));
    }
    cursor.close();

    cursor =
        myDatabase.rawQuery("SELECT book_id,author_id FROM BookAuthor ORDER BY author_index", null);
    while (cursor.moveToNext()) {
      Book book = booksById.get(cursor.getLong(0));
      if (book != null) {
        Author author = authorById.get(cursor.getLong(1));
        if (author != null) {
          addAuthor(book, author);
        }
      }
    }
    cursor.close();

    cursor = myDatabase.rawQuery("SELECT book_id,tag_id FROM BookTag", null);
    while (cursor.moveToNext()) {
      Book book = booksById.get(cursor.getLong(0));
      if (book != null) {
        addTag(book, getTagById(cursor.getLong(1)));
      }
    }
    cursor.close();

    cursor = myDatabase.rawQuery("SELECT series_id,name FROM Series", null);
    final HashMap<Long, String> seriesById = new HashMap<Long, String>();
    while (cursor.moveToNext()) {
      seriesById.put(cursor.getLong(0), cursor.getString(1));
    }
    cursor.close();

    cursor = myDatabase.rawQuery("SELECT book_id,series_id,book_index FROM BookSeries", null);
    while (cursor.moveToNext()) {
      Book book = booksById.get(cursor.getLong(0));
      if (book != null) {
        final String series = seriesById.get(cursor.getLong(1));
        if (series != null) {
          setSeriesInfo(book, series, cursor.getString(2));
        }
      }
    }
    cursor.close();
    return booksByFileId;
  }