Exemple #1
0
 public void markHyperlinkAsVisited(String linkId) {
   initHyperlinkSet();
   if (!myVisitedHyperlinks.contains(linkId)) {
     myVisitedHyperlinks.add(linkId);
     if (myId != -1) {
       BooksDatabase.Instance().addVisitedHyperlink(myId, linkId);
     }
   }
 }
 public List<String> series() {
   final Set<String> series = new TreeSet<String>();
   synchronized (myBooksByFile) {
     for (Book book : myBooksByFile.values()) {
       final SeriesInfo info = book.getSeriesInfo();
       if (info != null) {
         series.add(info.Title);
       }
     }
   }
   return new ArrayList<String>(series);
 }
 public List<Author> authors() {
   final Set<Author> authors = new TreeSet<Author>();
   synchronized (myBooksByFile) {
     for (Book book : myBooksByFile.values()) {
       final List<Author> bookAuthors = book.authors();
       if (bookAuthors.isEmpty()) {
         authors.add(Author.NULL);
       } else {
         authors.addAll(bookAuthors);
       }
     }
   }
   return new ArrayList<Author>(authors);
 }
Exemple #4
0
 private void initHyperlinkSet() {
   if (myVisitedHyperlinks == null) {
     myVisitedHyperlinks = new TreeSet<String>();
     if (myId != -1) {
       myVisitedHyperlinks.addAll(BooksDatabase.Instance().loadVisitedHyperlinks(myId));
     }
   }
 }
 public List<Tag> tags() {
   final Set<Tag> tags = new HashSet<Tag>();
   synchronized (myBooksByFile) {
     for (Book book : myBooksByFile.values()) {
       final List<Tag> bookTags = book.tags();
       if (bookTags.isEmpty()) {
         tags.add(Tag.NULL);
       } else {
         for (Tag t : bookTags) {
           for (; t != null; t = t.Parent) {
             tags.add(t);
           }
         }
       }
     }
   }
   return new ArrayList<Tag>(tags);
 }
  private void processFilesQueue() {
    synchronized (myFilesToRescan) {
      if (!myStatus.IsCompleted) {
        return;
      }

      final Set<ZLFile> filesToRemove = new HashSet<ZLFile>();
      for (String path : myFilesToRescan) {
        path = new ZLPhysicalFile(new File(path)).getPath();
        synchronized (myBooksByFile) {
          for (ZLFile f : myBooksByFile.keySet()) {
            if (f.getPath().startsWith(path)) {
              filesToRemove.add(f);
            }
          }
        }
      }

      for (ZLFile file : collectPhysicalFiles(myFilesToRescan)) {
        // TODO:
        // collect books from archives
        // rescan files and check book id
        filesToRemove.remove(file);
        final Book book = getBookByFile(file);
        if (book != null) {
          saveBook(book, false);
        }
      }

      for (ZLFile f : filesToRemove) {
        synchronized (myBooksByFile) {
          final Book book = myBooksByFile.remove(f);
          if (book != null) {
            myBooksById.remove(book.getId());
            fireBookEvent(BookEvent.Removed, book);
          }
        }
      }

      myFilesToRescan.clear();
    }
  }
  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);
      }
    }
  }
  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);
  }
Exemple #9
0
 public boolean isHyperlinkVisited(String linkId) {
   initHyperlinkSet();
   return myVisitedHyperlinks.contains(linkId);
 }