Search in sources :

Example 1 with OPDSBookItem

use of org.geometerplus.fbreader.network.opds.OPDSBookItem in project FBReaderJ by geometer.

the class LitResXMLReader method endElementHandler.

@Override
public boolean endElementHandler(String tag) {
    tag = tag.intern();
    switch(myState) {
        case CATALOG:
            if (TAG_CATALOG == tag) {
                myState = START;
            }
            break;
        case BOOK:
            if (TAG_BOOK == tag) {
                myUrls.addInfo(new UrlInfo(UrlInfo.Type.SingleEntry, "http://data.fbreader.org/catalogs/litres2/full.php5?id=" + myBookId, MimeType.APP_ATOM_XML_ENTRY));
                Books.add(new OPDSBookItem(myLibrary, Link, myBookId, myIndex++, myTitle, mySummary, //myDate,
                myAuthors, myTags, mySeriesTitle, myIndexInSeries, myUrls));
                myBookId = myTitle = /*myLanguage = myDate = */
                mySeriesTitle = null;
                mySummary = null;
                myIndexInSeries = 0;
                myAuthors.clear();
                myTags.clear();
                myUrls.clear();
                myState = CATALOG;
            }
            break;
        case BOOK_DESCRIPTION:
            if (TAG_TEXT_DESCRIPTION == tag) {
                myState = BOOK;
            }
            break;
        case HIDDEN:
            if (TAG_HIDDEN == tag) {
                myState = BOOK_DESCRIPTION;
            }
            break;
        case TITLE_INFO:
            if (TAG_TITLE_INFO == tag) {
                myState = HIDDEN;
            }
            break;
        case AUTHOR:
            if (TAG_AUTHOR == tag) {
                StringBuilder displayName = new StringBuilder();
                if (myAuthorFirstName != null) {
                    displayName.append(myAuthorFirstName).append(" ");
                }
                if (myAuthorMiddleName != null) {
                    displayName.append(myAuthorMiddleName).append(" ");
                }
                if (myAuthorLastName != null) {
                    displayName.append(myAuthorLastName).append(" ");
                }
                myAuthors.add(new OPDSBookItem.AuthorData(displayName.toString().trim(), myAuthorLastName));
                myAuthorFirstName = null;
                myAuthorMiddleName = null;
                myAuthorLastName = null;
                myState = TITLE_INFO;
            }
            break;
        case FIRST_NAME:
            if (TAG_FIRST_NAME == tag) {
                myAuthorFirstName = myBuffer.toString();
                myState = AUTHOR;
            }
            break;
        case MIDDLE_NAME:
            if (TAG_MIDDLE_NAME == tag) {
                myAuthorMiddleName = myBuffer.toString();
                myState = AUTHOR;
            }
            break;
        case LAST_NAME:
            if (TAG_LAST_NAME == tag) {
                myAuthorLastName = myBuffer.toString();
                myState = AUTHOR;
            }
            break;
        case GENRE:
            if (TAG_GENRE == tag) {
                /*if (myBuffer.length() != 0) {
						const std::map<std::string,shared_ptr<LitResGenre> > &genresMap =
							LitResGenreMap::Instance().genresMap();
						const std::map<shared_ptr<LitResGenre>,std::string> &genresTitles =
							LitResGenreMap::Instance().genresTitles();

						std::map<std::string, shared_ptr<LitResGenre> >::const_iterator it = genresMap.find(myBuffer);
						if (it != genresMap.end()) {
							std::map<shared_ptr<LitResGenre>, std::string>::const_iterator jt = genresTitles.find(it->second);
							if (jt != genresTitles.end()) {
								myTags.push_back(jt->second);
							}
						}
					}*/
                myState = TITLE_INFO;
            }
            break;
        case BOOK_TITLE:
            if (TAG_BOOK_TITLE == tag) {
                myTitle = myBuffer.toString();
                myState = TITLE_INFO;
            }
            break;
        case ANNOTATION:
            myAnnotationBuffer.appendText(myBuffer);
            if (TAG_ANNOTATION == tag) {
                mySummary = myAnnotationBuffer.getText();
                myAnnotationBuffer.reset();
                myState = TITLE_INFO;
            } else {
                myAnnotationBuffer.appendEndTag(tag);
            }
            break;
        case DATE:
            if (TAG_DATE == tag) {
                //myDate = myBuffer.toString();
                myState = TITLE_INFO;
            }
            break;
        case LANGUAGE:
            if (TAG_LANGUAGE == tag) {
                //myLanguage = myBuffer.toString();
                myState = TITLE_INFO;
            }
            break;
    }
    myBuffer.delete(0, myBuffer.length());
    return false;
}
Also used : OPDSBookItem(org.geometerplus.fbreader.network.opds.OPDSBookItem)

Example 2 with OPDSBookItem

use of org.geometerplus.fbreader.network.opds.OPDSBookItem in project FBReaderJ by geometer.

the class DisplayBookPopupAction method run.

@Override
protected void run(Object... params) {
    if (params.length != 1 || !(params[0] instanceof ZLTextRegion)) {
        return;
    }
    final ZLTextRegion region = (ZLTextRegion) params[0];
    if (!(region.getSoul() instanceof ExtensionRegionSoul)) {
        return;
    }
    final ExtensionElement e = ((ExtensionRegionSoul) region.getSoul()).Element;
    if (!(e instanceof BookElement)) {
        return;
    }
    final BookElement element = (BookElement) e;
    if (!element.isInitialized()) {
        return;
    }
    final View mainView = (View) BaseActivity.getViewWidget();
    final View bookView = BaseActivity.getLayoutInflater().inflate(ColorProfile.NIGHT.equals(Reader.ViewOptions.ColorProfileName.getValue()) ? R.layout.book_popup_night : R.layout.book_popup, null);
    final int inch = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, 1, BaseActivity.getResources().getDisplayMetrics());
    final PopupWindow popup = new PopupWindow(bookView, Math.min(4 * inch, mainView.getWidth() * 9 / 10), Math.min(3 * inch, mainView.getHeight() * 9 / 10));
    popup.setFocusable(true);
    popup.setOutsideTouchable(true);
    final ImageView coverView = (ImageView) bookView.findViewById(R.id.book_popup_cover);
    if (coverView != null) {
        final ZLAndroidImageData imageData = (ZLAndroidImageData) element.getImageData();
        if (imageData != null) {
            coverView.setImageBitmap(imageData.getFullSizeBitmap());
        }
    }
    final OPDSBookItem item = element.getItem();
    final TextView headerView = (TextView) bookView.findViewById(R.id.book_popup_header_text);
    final StringBuilder text = new StringBuilder();
    for (OPDSBookItem.AuthorData author : item.Authors) {
        text.append("<p><i>").append(author.DisplayName).append("</i></p>");
    }
    text.append("<h3>").append(item.Title).append("</h3>");
    headerView.setText(Html.fromHtml(text.toString()));
    final TextView descriptionView = (TextView) bookView.findViewById(R.id.book_popup_description_text);
    descriptionView.setText(item.getSummary());
    descriptionView.setMovementMethod(new LinkMovementMethod());
    final ZLResource buttonResource = ZLResource.resource("dialog").getResource("button");
    final View buttonsView = bookView.findViewById(R.id.book_popup_buttons);
    final Button downloadButton = (Button) buttonsView.findViewById(R.id.ok_button);
    downloadButton.setText(buttonResource.getResource("download").getValue());
    final List<UrlInfo> infos = item.getAllInfos(UrlInfo.Type.Book);
    if (infos.isEmpty() || !(infos.get(0) instanceof BookUrlInfo)) {
        downloadButton.setEnabled(false);
    } else {
        final BookUrlInfo bookInfo = (BookUrlInfo) infos.get(0);
        final String fileName = bookInfo.makeBookFileName(UrlInfo.Type.Book);
        final Book book = Reader.Collection.getBookByFile(fileName);
        if (book != null) {
            downloadButton.setText(buttonResource.getResource("openBook").getValue());
            downloadButton.setOnClickListener(new Button.OnClickListener() {

                public void onClick(View v) {
                    openBook(popup, book);
                }
            });
        } else {
            final File file = new File(fileName);
            if (file.exists()) {
                file.delete();
            }
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            downloadButton.setOnClickListener(new Button.OnClickListener() {

                public void onClick(View v) {
                    UIUtil.wait("downloadingBook", item.Title.toString(), new Runnable() {

                        public void run() {
                            try {
                                new QuietNetworkContext().downloadToFile(bookInfo.Url, file);
                                openBook(popup, Reader.Collection.getBookByFile(fileName));
                            } catch (ZLNetworkException e) {
                                UIMessageUtil.showErrorMessage(BaseActivity, "downloadFailed");
                                e.printStackTrace();
                            }
                        }
                    }, BaseActivity);
                }
            });
        }
    }
    final Button cancelButton = (Button) buttonsView.findViewById(R.id.cancel_button);
    cancelButton.setText(buttonResource.getResource("cancel").getValue());
    cancelButton.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {
            popup.dismiss();
        }
    });
    downloadButton.setTextColor(0xFF777777);
    cancelButton.setTextColor(0xFF777777);
    popup.setOnDismissListener(new PopupWindow.OnDismissListener() {

        public void onDismiss() {
        }
    });
    popup.showAtLocation(BaseActivity.getCurrentFocus(), Gravity.CENTER, 0, 0);
}
Also used : BookElement(org.geometerplus.fbreader.fbreader.BookElement) PopupWindow(android.widget.PopupWindow) ZLResource(org.geometerplus.zlibrary.core.resources.ZLResource) BookUrlInfo(org.geometerplus.fbreader.network.urlInfo.BookUrlInfo) ZLAndroidImageData(org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData) BookUrlInfo(org.geometerplus.fbreader.network.urlInfo.BookUrlInfo) UrlInfo(org.geometerplus.fbreader.network.urlInfo.UrlInfo) Book(org.geometerplus.fbreader.book.Book) ZLNetworkException(org.geometerplus.zlibrary.core.network.ZLNetworkException) OPDSBookItem(org.geometerplus.fbreader.network.opds.OPDSBookItem) LinkMovementMethod(android.text.method.LinkMovementMethod) View(android.view.View) QuietNetworkContext(org.geometerplus.zlibrary.core.network.QuietNetworkContext) File(java.io.File)

Aggregations

OPDSBookItem (org.geometerplus.fbreader.network.opds.OPDSBookItem)2 LinkMovementMethod (android.text.method.LinkMovementMethod)1 View (android.view.View)1 PopupWindow (android.widget.PopupWindow)1 File (java.io.File)1 Book (org.geometerplus.fbreader.book.Book)1 BookElement (org.geometerplus.fbreader.fbreader.BookElement)1 BookUrlInfo (org.geometerplus.fbreader.network.urlInfo.BookUrlInfo)1 UrlInfo (org.geometerplus.fbreader.network.urlInfo.UrlInfo)1 QuietNetworkContext (org.geometerplus.zlibrary.core.network.QuietNetworkContext)1 ZLNetworkException (org.geometerplus.zlibrary.core.network.ZLNetworkException)1 ZLResource (org.geometerplus.zlibrary.core.resources.ZLResource)1 ZLAndroidImageData (org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData)1