Example #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;
  }
Example #2
0
 public List<CancelActionDescription> getCancelActionsList() {
   myCancelActionsList.clear();
   if (ShowLibraryInCancelMenuOption.getValue()) {
     myCancelActionsList.add(new CancelActionDescription(CancelActionType.library, null));
   }
   if (ShowNetworkLibraryInCancelMenuOption.getValue()) {
     myCancelActionsList.add(new CancelActionDescription(CancelActionType.networkLibrary, null));
   }
   if (ShowPreviousBookInCancelMenuOption.getValue()) {
     final Book previousBook = Library.Instance().getPreviousBook();
     if (previousBook != null) {
       myCancelActionsList.add(
           new CancelActionDescription(CancelActionType.previousBook, previousBook.getTitle()));
     }
   }
   if (ShowPositionsInCancelMenuOption.getValue()) {
     if (Model != null && Model.Book != null) {
       for (Bookmark bookmark : Library.Instance().invisibleBookmarks(Model.Book)) {
         myCancelActionsList.add(new BookmarkDescription(bookmark));
       }
     }
   }
   myCancelActionsList.add(new CancelActionDescription(CancelActionType.close, null));
   return myCancelActionsList;
 }
Example #3
0
 private void onPreferencesUpdate(int resultCode) {
   final FBReaderApp fbReader = (FBReaderApp) FBReaderApp.Instance();
   switch (resultCode) {
     case RESULT_DO_NOTHING:
       break;
     case RESULT_REPAINT:
       {
         AndroidFontUtil.clearFontCache();
         final BookModel model = fbReader.Model;
         if (model != null) {
           final Book book = model.Book;
           if (book != null) {
             book.reloadInfoFromDatabase();
             ZLTextHyphenator.Instance().load(book.getLanguage());
           }
         }
         fbReader.clearTextCaches();
         fbReader.getViewWidget().repaint();
         break;
       }
     case RESULT_RELOAD_BOOK:
       fbReader.reloadBook();
       break;
   }
 }
Example #4
0
 @Override
 public void detectLanguageAndEncoding(Book book) throws BookReadingException {
   InputStream stream = null;
   try {
     stream = book.File.getInputStream();
     final PdbHeader header = new PdbHeader(stream);
     PdbUtil.skip(stream, header.Offsets[0] + 16 - header.length());
     if (PdbUtil.readInt(stream) != 0x4D4F4249) /* "MOBI" */ {
       throw new BookReadingException("unsupportedFileFormat", book.File);
     }
     final int length = (int) PdbUtil.readInt(stream);
     PdbUtil.skip(stream, 4);
     final int encodingCode = (int) PdbUtil.readInt(stream);
     final Encoding encoding = supportedEncodings().getEncoding(encodingCode);
     final String encodingName = encoding != null ? encoding.Name : "utf-8";
     book.setEncoding(encodingName);
     PdbUtil.skip(stream, 52);
     final int fullNameOffset = (int) PdbUtil.readInt(stream);
     final int fullNameLength = (int) PdbUtil.readInt(stream);
     final int languageCode = (int) PdbUtil.readInt(stream);
     book.setLanguage(
         ZLLanguageUtil.languageByIntCode(languageCode & 0xFF, (languageCode >> 8) & 0xFF));
   } catch (IOException e) {
     throw new BookReadingException(e, book.File);
   } finally {
     if (stream != null) {
       try {
         stream.close();
       } catch (IOException e) {
       }
     }
   }
 }
Example #5
0
 @Override
 public void characterDataHandler(char[] ch, int start, int length) {
   // TODO + length -- remove
   final String text = new String(ch).substring(start, start + length);
   if (myReadTitle) {
     myBook.setTitle(myBook.getTitle() + text);
   }
 }
 @Override
 protected void onDialogClosed(boolean result) {
   super.onDialogClosed(result);
   if (result) {
     final String value = getValue();
     if (!value.equalsIgnoreCase(myBook.getEncoding())) {
       myBook.setEncoding(value);
       ((EditBookInfoActivity) getContext()).setResult(FBReader.RESULT_RELOAD_BOOK);
     }
   }
 }
 @Override
 protected void reloadBook(Book book) {
   final Cursor cursor =
       myDatabase.rawQuery(
           "SELECT title,encoding,language FROM Books WHERE book_id = " + book.getId(), null);
   if (cursor.moveToNext()) {
     book.setTitle(cursor.getString(0));
     book.setEncoding(cursor.getString(1));
     book.setLanguage(cursor.getString(2));
   }
   cursor.close();
 }
 @Override
 protected void onPause() {
   super.onPause();
   if (myBook != null) {
     myBook.save();
   }
 }
 @Override
 protected void onDialogClosed(boolean result) {
   super.onDialogClosed(result);
   if (result) {
     final String value = getValue();
     myBook.setLanguage(value.length() > 0 ? value : null);
   }
 }
Example #10
0
 @Override
 public boolean startElementHandler(String tagName, ZLStringMap attributes) {
   switch (FB2Tag.getTagByName(tagName)) {
     case FB2Tag.BODY:
       return true;
     case FB2Tag.TITLE_INFO:
       myReadState = READ_SOMETHING;
       break;
     case FB2Tag.BOOK_TITLE:
       if (myReadState == READ_SOMETHING) {
         myReadState = READ_TITLE;
       }
       break;
     case FB2Tag.GENRE:
       if (myReadState == READ_SOMETHING) {
         myReadState = READ_GENRE;
       }
       break;
     case FB2Tag.AUTHOR:
       if (myReadState == READ_SOMETHING) {
         myReadState = READ_AUTHOR;
       }
       break;
     case FB2Tag.LANG:
       if (myReadState == READ_SOMETHING) {
         myReadState = READ_LANGUAGE;
       }
       break;
     case FB2Tag.FIRST_NAME:
       if (myReadState == READ_AUTHOR) {
         myReadState = READ_AUTHOR_NAME_0;
       }
       break;
     case FB2Tag.MIDDLE_NAME:
       if (myReadState == READ_AUTHOR) {
         myReadState = READ_AUTHOR_NAME_1;
       }
       break;
     case FB2Tag.LAST_NAME:
       if (myReadState == READ_AUTHOR) {
         myReadState = READ_AUTHOR_NAME_2;
       }
       break;
     case FB2Tag.SEQUENCE:
       if (myReadState == READ_SOMETHING) {
         String name = attributes.getValue("name");
         if (name != null) {
           name.trim();
           if (name.length() != 0) {
             myBook.setSeriesInfo(name, attributes.getValue("number"));
           }
         }
       }
       break;
   }
   return false;
 }
 @Override
 protected void setExistingFlag(Collection<Book> books, boolean flag) {
   if (books.isEmpty()) {
     return;
   }
   final StringBuilder bookSet = new StringBuilder("(");
   boolean first = true;
   for (Book b : books) {
     if (first) {
       first = false;
     } else {
       bookSet.append(",");
     }
     bookSet.append(b.getId());
   }
   bookSet.append(")");
   myDatabase.execSQL(
       "UPDATE Books SET `exists` = " + (flag ? 1 : 0) + " WHERE book_id IN " + bookSet);
 }
 @Override
 protected void onActivityResult(int requestCode, int returnCode, Intent intent) {
   if (requestCode == BOOK_INFO_REQUEST && intent != null) {
     final String path = intent.getStringExtra(BookInfoActivity.CURRENT_BOOK_PATH_KEY);
     final Book book = Book.getByFile(ZLFile.createFileByPath(path));
     myLibrary.refreshBookInfo(book);
     getListView().invalidateViews();
   } else {
     super.onActivityResult(requestCode, returnCode, intent);
   }
 }
Example #13
0
 private Book createBookForFile(ZLFile file) {
   if (file == null) {
     return null;
   }
   Book book = Book.getByFile(file);
   if (book != null) {
     book.insertIntoBookList();
     return book;
   }
   if (file.isArchive()) {
     for (ZLFile child : file.children()) {
       book = Book.getByFile(child);
       if (book != null) {
         book.insertIntoBookList();
         return book;
       }
     }
   }
   return null;
 }
  EncodingPreference(Context context, ZLResource rootResource, String resourceKey, Book book) {
    super(context, rootResource, resourceKey);
    myBook = book;

    final FormatPlugin plugin;
    try {
      plugin = book.getPlugin();
    } catch (BookReadingException e) {
      return;
    }

    final List<Encoding> encodings =
        new ArrayList<Encoding>(plugin.supportedEncodings().encodings());
    Collections.sort(
        encodings,
        new Comparator<Encoding>() {
          public int compare(Encoding e1, Encoding e2) {
            return e1.DisplayName.compareTo(e2.DisplayName);
          }
        });
    final String[] codes = new String[encodings.size()];
    final String[] names = new String[encodings.size()];
    int index = 0;
    for (Encoding e : encodings) {
      // addItem(e.Family, e.Name, e.DisplayName);
      codes[index] = e.Name;
      names[index] = e.DisplayName;
      ++index;
    }
    setLists(codes, names);
    if (encodings.size() == 1) {
      setInitialValue(codes[0]);
      setEnabled(false);
    } else {
      final String bookEncoding = book.getEncoding();
      if (bookEncoding != null) {
        setInitialValue(bookEncoding.toLowerCase());
      }
    }
  }
Example #15
0
  synchronized void openBookInternal(Book book, Bookmark bookmark) {
    if (book == null) {
      book = Library.Instance().getRecentBook();
      if (book == null || !book.File.exists()) {
        book = Book.getByFile(Library.getHelpFile());
      }
      if (book == null) {
        return;
      }
    }
    if (Model != null) {
      if (bookmark == null & book.File.getPath().equals(Model.Book.File.getPath())) {
        return;
      }
    }

    if (book != null) {
      onViewChanged();

      if (Model != null) {
        Model.Book.storePosition(BookTextView.getStartCursor());
      }
      BookTextView.setModel(null);
      FootnoteView.setModel(null);
      clearTextCaches();

      Model = null;
      System.gc();
      System.gc();
      try {
        Model = BookModel.createModel(book);
        ZLTextHyphenator.Instance().load(book.getLanguage());
        BookTextView.setModel(Model.getTextModel());
        BookTextView.gotoPosition(book.getStoredPosition());
        if (bookmark == null) {
          setView(BookTextView);
        } else {
          gotoBookmark(bookmark);
        }
        Library.Instance().addBookToRecentList(book);
        final StringBuilder title = new StringBuilder(book.getTitle());
        if (!book.authors().isEmpty()) {
          boolean first = true;
          for (Author a : book.authors()) {
            title.append(first ? " (" : ", ");
            title.append(a.DisplayName);
            first = false;
          }
          title.append(")");
        }
        setTitle(title.toString());
      } catch (BookReadingException e) {
        processException(e);
      }
    }
    getViewWidget().reset();
    getViewWidget().repaint();
  }
Example #16
0
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   final FBReaderApp fbreader = (FBReaderApp) FBReaderApp.Instance();
   switch (requestCode) {
     case REPAINT_CODE:
       {
         final BookModel model = fbreader.Model;
         if (model != null) {
           final Book book = model.Book;
           if (book != null) {
             book.reloadInfoFromDatabase();
             ZLTextHyphenator.Instance().load(book.getLanguage());
           }
         }
         fbreader.clearTextCaches();
         fbreader.getViewWidget().repaint();
         break;
       }
     case CANCEL_CODE:
       fbreader.runCancelAction(resultCode - 1);
       break;
   }
 }
Example #17
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;
  }
 private void tryToDeleteBook(Book book) {
   final ZLResource dialogResource = ZLResource.resource("dialog");
   final ZLResource buttonResource = dialogResource.getResource("button");
   final ZLResource boxResource = dialogResource.getResource("deleteBookBox");
   new AlertDialog.Builder(this)
       .setTitle(book.getTitle())
       .setMessage(boxResource.getResource("message").getValue())
       .setIcon(0)
       .setPositiveButton(
           buttonResource.getResource("yes").getValue(),
           new BookDeleter(book, Library.REMOVE_FROM_DISK))
       .setNegativeButton(buttonResource.getResource("no").getValue(), null)
       .create()
       .show();
 }
Example #19
0
 @Override
 public String getSummary() {
   StringBuilder builder = new StringBuilder();
   int count = 0;
   for (Author author : Book.authors()) {
     if (count++ > 0) {
       builder.append(",  ");
     }
     builder.append(author.DisplayName);
     if (count == 5) {
       break;
     }
   }
   return builder.toString();
 }
 private void createBookContextMenu(ContextMenu menu, Book book) {
   final ZLResource resource = Library.resource();
   menu.setHeaderTitle(book.getTitle());
   menu.add(0, OPEN_BOOK_ITEM_ID, 0, resource.getResource("openBook").getValue());
   menu.add(0, SHOW_BOOK_INFO_ITEM_ID, 0, resource.getResource("showBookInfo").getValue());
   if (myLibrary.isBookInFavorites(book)) {
     menu.add(
         0,
         REMOVE_FROM_FAVORITES_ITEM_ID,
         0,
         resource.getResource("removeFromFavorites").getValue());
   } else {
     menu.add(0, ADD_TO_FAVORITES_ITEM_ID, 0, resource.getResource("addToFavorites").getValue());
   }
   if ((myLibrary.getRemoveBookMode(book) & Library.REMOVE_FROM_DISK) != 0) {
     menu.add(0, DELETE_BOOK_ITEM_ID, 0, resource.getResource("deleteBook").getValue());
   }
 }
Example #21
0
 public static void shareBook(Activity activity, Book book) {
   try {
     final ZLPhysicalFile file = book.File.getPhysicalFile();
     if (file == null) {
       // That should be impossible
       return;
     }
     final CharSequence sharedFrom =
         Html.fromHtml(ZLResource.resource("sharing").getResource("sharedFrom").getValue());
     activity.startActivity(
         new Intent(Intent.ACTION_SEND)
             .setType(FileTypeCollection.Instance.simplifiedMimeType(file).Name)
             .putExtra(Intent.EXTRA_SUBJECT, book.getTitle())
             .putExtra(Intent.EXTRA_TEXT, sharedFrom)
             .putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file.javaFile())));
   } catch (ActivityNotFoundException e) {
     // TODO: show toast
   }
 }
  @Override
  protected void init(Intent intent) {
    if (SQLiteBooksDatabase.Instance() == null) {
      new SQLiteBooksDatabase(this, "LIBRARY");
    }

    final String path = intent.getStringExtra(BookInfoActivity.CURRENT_BOOK_PATH_KEY);
    final ZLFile file = ZLFile.createFileByPath(path);
    myBook = Book.getByFile(file);
    setResult(FBReader.RESULT_REPAINT);

    if (myBook == null) {
      finish();
      return;
    }

    addPreference(new BookTitlePreference(this, Resource, "title", myBook));
    addPreference(new LanguagePreference(this, Resource, "language", myBook));
    addPreference(new EncodingPreference(this, Resource, "encoding", myBook));
  }
Example #23
0
 private void gotoBookmark(Bookmark bookmark) {
   bookmark.onOpen();
   final FBReader fbreader = (FBReader) FBReader.Instance();
   final long bookId = bookmark.getBookId();
   if ((fbreader.Model == null) || (fbreader.Model.Book.getId() != bookId)) {
     final Book book = Book.getById(bookId);
     if (book != null) {
       finish();
       fbreader.openBook(book, bookmark);
     } else {
       Toast.makeText(
               this,
               ZLResource.resource("errorMessage").getResource("cannotOpenBook").getValue(),
               Toast.LENGTH_SHORT)
           .show();
     }
   } else {
     finish();
     fbreader.gotoBookmark(bookmark);
   }
 }
  LanguagePreference(Context context, ZLResource rootResource, String resourceKey, Book book) {
    super(context, rootResource, resourceKey);
    myBook = book;
    final TreeSet<String> set = new TreeSet<String>(new ZLLanguageUtil.CodeComparator());
    set.addAll(ZLTextHyphenator.Instance().languageCodes());
    set.add(ZLLanguageUtil.OTHER_LANGUAGE_CODE);

    final int size = set.size();
    String[] codes = new String[size];
    String[] names = new String[size];
    int index = 0;
    for (String code : set) {
      codes[index] = code;
      names[index] = ZLLanguageUtil.languageName(code);
      ++index;
    }
    setLists(codes, names);
    final String language = myBook.getLanguage();
    if (language == null || !setInitialValue(language)) {
      setInitialValue(ZLLanguageUtil.OTHER_LANGUAGE_CODE);
    }
  }
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    myDatabase = SQLiteBooksDatabase.Instance();
    if (myDatabase == null) {
      myDatabase = new SQLiteBooksDatabase(this, "LIBRARY");
    }
    if (myLibrary == null) {
      myLibrary = Library.Instance();
      myLibrary.addChangeListener(this);
      myLibrary.startBuild();
    }

    final String selectedBookPath = getIntent().getStringExtra(SELECTED_BOOK_PATH_KEY);
    mySelectedBook = null;
    if (selectedBookPath != null) {
      final ZLFile file = ZLFile.createFileByPath(selectedBookPath);
      if (file != null) {
        mySelectedBook = Book.getByFile(file);
      }
    }

    new LibraryTreeAdapter(this);
    myActivity = this;

    init(getIntent());

    // commenting out to prevent soft keyboard when using Eyes-Free keyboard
    // getListView().setTextFilterEnabled(true);
    getListView().setOnCreateContextMenuListener(this);

    dialog = new Dialog(this);
    dialog.setContentView(R.layout.accessible_long_press_dialog);
    list = (ListView) dialog.findViewById(R.id.accessible_list);
  }
Example #26
0
 @Override
 public void readMetaInfo(Book book) throws BookReadingException {
   InputStream stream = null;
   try {
     stream = book.File.getInputStream();
     final PdbHeader header = new PdbHeader(stream);
     PdbUtil.skip(stream, header.Offsets[0] + 16 - header.length());
     if (PdbUtil.readInt(stream) != 0x4D4F4249) /* "MOBI" */ {
       throw new BookReadingException("unsupportedFileFormat", book.File);
     }
     final int length = (int) PdbUtil.readInt(stream);
     PdbUtil.skip(stream, 4);
     final int encodingCode = (int) PdbUtil.readInt(stream);
     final Encoding encoding = supportedEncodings().getEncoding(encodingCode);
     final String encodingName = encoding != null ? encoding.Name : "utf-8";
     book.setEncoding(encodingName);
     PdbUtil.skip(stream, 52);
     final int fullNameOffset = (int) PdbUtil.readInt(stream);
     final int fullNameLength = (int) PdbUtil.readInt(stream);
     final int languageCode = (int) PdbUtil.readInt(stream);
     book.setLanguage(
         ZLLanguageUtil.languageByIntCode(languageCode & 0xFF, (languageCode >> 8) & 0xFF));
     PdbUtil.skip(stream, 32);
     int offset = 132;
     if ((PdbUtil.readInt(stream) & 0x40) != 0) {
       PdbUtil.skip(stream, length - 116);
       offset = length + 20;
       if (PdbUtil.readInt(stream) == 0x45585448) /* "EXTH" */ {
         PdbUtil.skip(stream, 4);
         final int recordsNumber = (int) PdbUtil.readInt(stream);
         offset += 8;
         for (int i = 0; i < recordsNumber; ++i) {
           final int type = (int) PdbUtil.readInt(stream);
           final int size = (int) PdbUtil.readInt(stream);
           offset += size;
           if (size <= 8) {
             continue;
           }
           switch (type) {
             default:
               PdbUtil.skip(stream, size - 8);
               break;
             case 100:
               {
                 final byte[] buffer = new byte[size - 8];
                 stream.read(buffer);
                 String author = new String(buffer, encodingName);
                 final int index = author.indexOf(',');
                 if (index != -1) {
                   author =
                       author.substring(index + 1).trim()
                           + ' '
                           + author.substring(0, index).trim();
                 } else {
                   author = author.trim();
                 }
                 book.addAuthor(author);
                 break;
               }
             case 105:
               {
                 final byte[] buffer = new byte[size - 8];
                 stream.read(buffer);
                 book.addTag(new String(buffer, encodingName));
                 break;
               }
           }
         }
       }
     }
     PdbUtil.skip(stream, fullNameOffset - offset);
     final byte[] titleBuffer = new byte[fullNameLength];
     stream.read(titleBuffer);
     book.setTitle(new String(titleBuffer, encodingName));
   } catch (IOException e) {
     throw new BookReadingException(e, book.File);
   } finally {
     if (stream != null) {
       try {
         stream.close();
       } catch (IOException e) {
       }
     }
   }
 }
 @Override
 protected void setValue(String value) {
   super.setValue(value);
   myBook.setTitle(value);
 }
 BookTitlePreference(Context context, ZLResource rootResource, String resourceKey, Book book) {
   super(context, rootResource, resourceKey);
   myBook = book;
   super.setValue(book.getTitle());
 }
 protected void setSeriesInfo(Book book, String series, String index) {
   book.setSeriesInfoWithNoCheck(series, SeriesInfo.createIndex(index));
 }
Example #30
0
 public HtmlMetaInfoReader(Book book) {
   myBook = book;
   myBook.setTitle("");
 }