Navigate with volume keys.
[dorian] / bookview.cpp
index 25a18b2..59c0949 100644 (file)
@@ -5,12 +5,16 @@
 #include <QDir>
 #include <QTimer>
 
+#ifdef Q_WS_MAEMO_5
+#   include <QAbstractKineticScroller>
+#endif
+
 #include "book.h"
 #include "bookview.h"
 #include "library.h"
-#include "selectionsuppressor.h"
 #include "settings.h"
 #include "trace.h"
+#include "progress.h"
 
 #ifdef Q_WS_MAC
 #   define ICON_PREFIX ":/icons/mac/"
 #endif
 
 BookView::BookView(QWidget *parent):
-    QWebView(parent), contentIndex(-1), mBook(0), restore(true),
-    positionAfterLoad(0), loaded(false)
+    QWebView(parent), contentIndex(-1), mBook(0),
+    restorePositionAfterLoad(false), positionAfterLoad(0), loaded(false),
+    contentsHeight(0), decorated(false), scrollerMonitor(-1)
 {
     Trace t("BookView::BookView");
     settings()->setAttribute(QWebSettings::AutoLoadImages, true);
     settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
+    settings()->setAttribute(QWebSettings::JavaEnabled, false);
     settings()->setAttribute(QWebSettings::PluginsEnabled, false);
+    settings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
+    settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, false);
+    settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard, false);
+    settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, false);
+    settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled,
+                             false);
+    settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
     settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
     settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
                              false);
+    settings()->setDefaultTextEncoding("utf-8");
     page()->setContentEditable(false);
 
 #if defined(Q_WS_MAEMO_5)
-    (void)new SelectionSuppressor(this);
+    // Suppress unwanted text selections on Maemo
+    installEventFilter(this);
 #endif
     QWebFrame *frame = page()->mainFrame();
 #if defined(Q_WS_MAEMO_5)
@@ -43,6 +58,10 @@ BookView::BookView(QWidget *parent):
     bookmarkImage = QImage(":/icons/bookmark.png");
 
     connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
+    connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
+            this, SLOT(addJavaScriptObjects()));
+    connect(frame, SIGNAL(contentsSizeChanged(const QSize &)),
+            this, SLOT(onContentsSizeChanged(const QSize &)));
     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
             this, SLOT(onSettingsChanged(const QString &)));
     Settings *s = Settings::instance();
@@ -60,6 +79,9 @@ BookView::BookView(QWidget *parent):
     setBook(0);
 
     extractIcons();
+#ifdef Q_WS_MAEMO_5
+    scroller = property("kineticScroller").value<QAbstractKineticScroller *>();
+#endif
 }
 
 BookView::~BookView()
@@ -74,17 +96,18 @@ void BookView::loadContent(int index)
     if (!mBook) {
         return;
     }
-    if ((index < 0) || (index >= mBook->toc.size())) {
+    if ((index < 0) || (index >= mBook->parts.size())) {
         return;
     }
 
-    QString contentFile(mBook->content[mBook->toc[index]].href);
-    if (mBook->toc[index] == "error") {
+    QString contentFile(mBook->content[mBook->parts[index]].href);
+    if (mBook->parts[index] == "error") {
         setHtml(contentFile);
     }
     else {
         loaded = false;
-        emit chapterLoadStart(index);
+        decorated = false;
+        emit partLoadStart(index);
         load(QUrl(contentFile));
     }
     contentIndex = index;
@@ -93,13 +116,22 @@ void BookView::loadContent(int index)
 void BookView::setBook(Book *book)
 {
     Trace t("BookView::setBook");
+
+    // Save position in current book
     setLastBookmark();
+
+    // Open new book, restore last position
     if (book != mBook) {
         mBook = book;
         if (book) {
             contentIndex = -1;
-            book->open();
-            goToBookmark(book->lastBookmark());
+            if (book->open()) {
+                restoreLastBookmark();
+            } else {
+                mBook = 0;
+                contentIndex = 0;
+                setHtml(tr("Failed to open book"));
+            }
         }
         else {
             contentIndex = 0;
@@ -127,45 +159,52 @@ void BookView::goNext()
 
 void BookView::setLastBookmark()
 {
-    Trace t("BookView::saveLastBookmark");
+    Trace t("BookView::setLastBookmark");
     if (mBook) {
-        int height = page()->mainFrame()->contentsSize().height();
+        int height = contentsHeight;
         int pos = page()->mainFrame()->scrollPosition().y();
+        t.trace(QString("At %1 (%2%, height %3)").
+                arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height));
         mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
     }
 }
 
+void BookView::restoreLastBookmark()
+{
+    Trace t("BookView::restoreLastBookmark");
+    if (mBook) {
+        goToBookmark(mBook->lastBookmark());
+    }
+}
+
 void BookView::goToBookmark(const Book::Bookmark &bookmark)
 {
     Trace t("BookView::goToBookmark");
     if (mBook) {
-        restore = true;
-        positionAfterLoad = bookmark.pos;
-        if (bookmark.chapter != contentIndex) {
-            loadContent(bookmark.chapter);
+        if (bookmark.part != contentIndex) {
+            t.trace(QString("Loading new part %1").arg(bookmark.part));
+            mBook->setLastBookmark(bookmark.part, bookmark.pos);
+            restorePositionAfterLoad = true;
+            positionAfterLoad = bookmark.pos;
+            loadContent(bookmark.part);
         } else {
-            onLoadFinished(true);
+            goToPosition(bookmark.pos);
         }
     }
 }
 
 void BookView::onLoadFinished(bool ok)
 {
-    Trace t(QString("BookView::onLoadFinished: %1").arg(ok));
-    loaded = true;
-    if (ok) {
-        addNavigationBar();
+    Trace t("BookView::onLoadFinished");
+    if (!ok) {
+        t.trace("Not OK");
+        return;
     }
+    loaded = true;
+    addNavigationBar();
     onSettingsChanged("scheme");
-    emit chapterLoadEnd(contentIndex);
-    if (restore) {
-        restore = false;
-        if (ok && mBook) {
-            int height = page()->mainFrame()->contentsSize().height();
-            int scrollPos = (qreal)height * positionAfterLoad;
-            page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
-        }
-    }
+    emit partLoadEnd(contentIndex);
+    showProgress();
 }
 
 void BookView::onSettingsChanged(const QString &key)
@@ -205,10 +244,10 @@ void BookView::paintEvent(QPaintEvent *e)
     QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
     QPainter painter(this);
     foreach (Book::Bookmark b, mBook->bookmarks()) {
-        if (b.chapter != contentIndex) {
+        if (b.part != contentIndex) {
             continue;
         }
-        int height = page()->mainFrame()->contentsSize().height();
+        int height = contentsHeight;
         int bookmarkPos = (qreal)height * (qreal)b.pos;
         painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
     }
@@ -217,7 +256,11 @@ void BookView::paintEvent(QPaintEvent *e)
 void BookView::mousePressEvent(QMouseEvent *e)
 {
     QWebView::mousePressEvent(e);
-#ifndef Q_WS_MAEMO_5
+#ifdef Q_WS_MAEMO_5
+    if (scroller) {
+        scrollerMonitor = startTimer(250);
+    }
+#else
     QWebFrame *frame = page()->mainFrame();
     if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
         e->accept();
@@ -227,14 +270,23 @@ void BookView::mousePressEvent(QMouseEvent *e)
     e->ignore();
 }
 
+void BookView::wheelEvent(QWheelEvent *e)
+{
+    QWebView::wheelEvent(e);
+    showProgress();
+}
+
 void BookView::addBookmark()
 {
     Trace t("BookView::addBookmark");
+    if (!mBook) {
+        return;
+    }
     int y = page()->mainFrame()->scrollPosition().y();
     int height = page()->mainFrame()->contentsSize().height();
     t.trace(QString().setNum((qreal)y / (qreal)height));
     mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
-    repaint();
+    update();
 }
 
 void BookView::addNavigationBar()
@@ -261,12 +313,11 @@ void BookView::addNavigationBar()
     if (contentIndex == 0) {
         naviPrev = "";
     }
-    if (contentIndex >= mBook->toc.size() - 1) {
+    if (contentIndex >= mBook->parts.size() - 1) {
         naviNext = "";
     }
 
     QWebFrame *frame = page()->currentFrame();
-    frame->addToJavaScriptWindowObject("bv", this);
     QString headerScript = "document.body.innerHTML = '" +
         naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
     QString trailerScript = "document.body.innerHTML += '<br /><br />" +
@@ -274,6 +325,7 @@ void BookView::addNavigationBar()
 
     frame->evaluateJavaScript(headerScript);
     frame->evaluateJavaScript(trailerScript);
+    decorated = true;
 }
 
 QString BookView::tmpPath()
@@ -297,3 +349,149 @@ void BookView::removeIcons()
     QFile(ICON_PREFIX + QString("/previous.png")).remove();
     QDir().rmpath(tmpPath());
 }
+
+bool BookView::eventFilter(QObject *o, QEvent *e)
+{
+    if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
+        if (e->type() == QEvent::Resize) {
+            Trace::trace(QString("BookView::eventFilter QEvent::Resize to %1").
+                         arg(page()->mainFrame()->contentsSize().height()));
+        } else {
+            Trace::trace(QString("BookView::eventFilter %1").
+                         arg(Trace::event(e->type())));
+        }
+    }
+
+    switch (e->type()) {
+    case QEvent::MouseButtonPress:
+        emit suppressedMouseButtonPress();
+        mousePressed = true;
+        break;
+    case QEvent::MouseButtonRelease:
+        showProgress();
+        mousePressed = false;
+        break;
+    case QEvent::MouseMove:
+        if (mousePressed) {
+            return true;
+        }
+        break;
+    case QEvent::MouseButtonDblClick:
+        return true;
+    default:
+        break;
+    }
+
+    return QObject::eventFilter(o, e);
+}
+
+void BookView::addJavaScriptObjects()
+{
+    page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
+}
+
+void BookView::onContentsSizeChanged(const QSize &size)
+{
+    contentsHeight = size.height();
+    if (decorated) {
+        if (restorePositionAfterLoad) {
+            Trace::trace("BookView::onContentSizeChanged: Time to restore");
+            restorePositionAfterLoad = false;
+            goToPosition(positionAfterLoad);
+        }
+    }
+}
+
+void BookView::leaveEvent(QEvent *e)
+{
+    Trace t("BookView::leaveEvent");
+    // Save current position, to be restored later
+    setLastBookmark();
+    QWebView::leaveEvent(e);
+}
+
+void BookView::enterEvent(QEvent *e)
+{
+    Trace t("BookView::enterEvent");
+    // Restore position saved at Leave event. This seems to be required,
+    // after temporarily switching from portrait to landscape and back
+    restoreLastBookmark();
+    QWebView::enterEvent(e);
+}
+
+void BookView::goToPosition(qreal position)
+{
+    int scrollPos = (qreal)contentsHeight * position;
+    page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
+    // FIXME: update();
+    Trace::trace(QString("BookView::goToPosition: To %1 (%2%, height %3)").
+            arg(scrollPos).arg(position * 100).arg(contentsHeight));
+}
+
+void BookView::showProgress()
+{
+    if (mBook) {
+        qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
+                    (qreal)contentsHeight;
+        emit progress(mBook->getProgress(contentIndex, pos));
+    }
+}
+
+void BookView::timerEvent(QTimerEvent *e)
+{
+    if (e->timerId() == scrollerMonitor) {
+#ifdef Q_WS_MAEMO_5
+        if (scroller &&
+            ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
+             (scroller->state() == QAbstractKineticScroller::Pushing))) {
+            showProgress();
+        } else {
+            killTimer(scrollerMonitor);
+        }
+#endif // Q_WS_MAEMO_5
+    }
+}
+
+void BookView::keyPressEvent(QKeyEvent* event)
+{
+    switch (event->key()) {
+    case Qt::Key_F7:
+        goNextPage();
+        event->accept();
+        break;
+    case Qt::Key_F8:
+        goPreviousPage();
+        event->accept();
+        break;
+    default:
+        ;
+    }
+    QWebView::keyPressEvent(event);
+}
+
+void BookView::goPreviousPage()
+{
+    QWebFrame *frame = page()->mainFrame();
+    int pos = frame->scrollPosition().y();
+    frame->scroll(0, -height());
+    if (pos == frame->scrollPosition().y()) {
+        if (contentIndex > 0) {
+            goToBookmark(Book::Bookmark(contentIndex - 1, 1.0));
+        }
+    } else {
+        showProgress();
+    }
+}
+
+void BookView::goNextPage()
+{
+    Trace t("BookView::goNextPage");
+    QWebFrame *frame = page()->mainFrame();
+    int pos = frame->scrollPosition().y();
+    frame->scroll(0, height());
+    if (pos == frame->scrollPosition().y()) {
+        goNext();
+    } else {
+        showProgress();
+    }
+}