Remove non-working settings from Symbian build. Make settigs window fit
[dorian] / bookview.cpp
1 #include <QDir>
2 #include <QtGui>
3 #include <QWebFrame>
4
5 #if defined(Q_WS_MAEMO_5)
6 #   include <QAbstractKineticScroller>
7 #elif defined(Q_OS_SYMBIAN)
8 #   include "flickcharm.h"
9 #endif
10
11 #include "book.h"
12 #include "bookview.h"
13 #include "library.h"
14 #include "settings.h"
15 #include "trace.h"
16 #include "progress.h"
17 #include "progressdialog.h"
18
19 BookView::BookView(QWidget *parent):
20     QWebView(parent), contentIndex(-1), mBook(0),
21     restorePositionAfterLoad(false), positionAfterLoad(0), loaded(false),
22     contentsHeight(0)
23 {
24     TRACE;
25     settings()->setAttribute(QWebSettings::AutoLoadImages, true);
26     settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
27     settings()->setAttribute(QWebSettings::JavaEnabled, false);
28     settings()->setAttribute(QWebSettings::PluginsEnabled, false);
29     settings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
30     settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, false);
31     settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard, false);
32     settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, false);
33     settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled,
34                              false);
35     settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
36     settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
37     settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
38                              false);
39     settings()->setDefaultTextEncoding("utf-8");
40     page()->setContentEditable(false);
41
42 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
43     // Suppress unwanted text selections on Maemo and Symbian
44     installEventFilter(this);
45 #endif
46     QWebFrame *frame = page()->mainFrame();
47 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
48     frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
49 #endif
50     frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
51
52     bookmarkImage = QImage(":/icons/bookmark.png");
53
54     connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
55     connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
56             this, SLOT(addJavaScriptObjects()));
57     connect(frame, SIGNAL(contentsSizeChanged(const QSize &)),
58             this, SLOT(onContentsSizeChanged(const QSize &)));
59     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
60             this, SLOT(onSettingsChanged(const QString &)));
61     Settings *s = Settings::instance();
62     s->setValue("zoom", s->value("zoom", 160));
63     s->setValue("font", s->value("font",
64 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_X11)
65                                  "Serif"
66 #elif defined(Q_WS_MAC)
67                                  "Hoefler Text"
68 #else
69                                  "Times New Roman"
70 #endif
71                                  ));
72     s->setValue("scheme", s->value("scheme", "default"));
73     setBook(0);
74
75 #if defined(Q_WS_MAEMO_5)
76     scrollerMonitor = 0;
77     scroller = property("kineticScroller").value<QAbstractKineticScroller *>();
78 #elif defined(Q_OS_SYMBIAN)
79     FlickCharm *charm = new FlickCharm(this);
80     charm->activateOn(this);
81 #endif
82 }
83
84 BookView::~BookView()
85 {
86     TRACE;
87 }
88
89 void BookView::loadContent(int index)
90 {
91     TRACE;
92     if (!mBook) {
93         return;
94     }
95     if ((index < 0) || (index >= mBook->parts.size())) {
96         return;
97     }
98
99     QString contentFile(mBook->content[mBook->parts[index]].href);
100     if (mBook->parts[index] == "error") {
101         setHtml(contentFile);
102     }
103     else {
104         loaded = false;
105         emit partLoadStart(index);
106         QUrl u = QUrl::fromLocalFile(QDir(mBook->rootPath()).
107                                      absoluteFilePath(contentFile));
108         qDebug() << "Loading" << u;
109         load(u);
110     }
111     contentIndex = index;
112 }
113
114 void BookView::setBook(Book *book)
115 {
116     TRACE;
117
118     // Save position in current book
119     setLastBookmark();
120
121     // Open new book, restore last position
122     if (book != mBook) {
123         mBook = book;
124         if (book) {
125             contentIndex = -1;
126             if (book->open()) {
127                 restoreLastBookmark();
128             } else {
129                 mBook = 0;
130                 contentIndex = 0;
131                 setHtml(tr("Failed to open book"));
132             }
133         }
134         else {
135             contentIndex = 0;
136             setHtml(tr("No book"));
137         }
138     }
139 }
140
141 Book *BookView::book()
142 {
143     return mBook;
144 }
145
146 void BookView::goPrevious()
147 {
148     TRACE;
149     if (mBook && (contentIndex > 0)) {
150         mBook->setLastBookmark(contentIndex - 1, 0);
151         loadContent(contentIndex - 1);
152     }
153 }
154
155 void BookView::goNext()
156 {
157     TRACE;
158     if (mBook && (contentIndex < (mBook->parts.size() - 1))) {
159         mBook->setLastBookmark(contentIndex + 1, 0);
160         loadContent(contentIndex + 1);
161     }
162 }
163
164 void BookView::setLastBookmark()
165 {
166     TRACE;
167     if (mBook) {
168         int height = contentsHeight;
169         int pos = page()->mainFrame()->scrollPosition().y();
170         qDebug() << QString("At %1 (%2%, height %3)").
171                 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height);
172         mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
173     }
174 }
175
176 void BookView::restoreLastBookmark()
177 {
178     TRACE;
179     if (mBook) {
180         goToBookmark(mBook->lastBookmark());
181     }
182 }
183
184 void BookView::goToBookmark(const Book::Bookmark &bookmark)
185 {
186     TRACE;
187     if (mBook) {
188         if (bookmark.part != contentIndex) {
189             qDebug () << "Loading new part" << bookmark.part;
190             mBook->setLastBookmark(bookmark.part, bookmark.pos);
191             restorePositionAfterLoad = true;
192             positionAfterLoad = bookmark.pos;
193             loadContent(bookmark.part);
194         } else {
195             goToPosition(bookmark.pos);
196         }
197     }
198 }
199
200 void BookView::onLoadFinished(bool ok)
201 {
202     TRACE;
203     if (!ok) {
204         qDebug() << "Not OK";
205         return;
206     }
207     loaded = true;
208     onSettingsChanged("scheme");
209     emit partLoadEnd(contentIndex);
210     showProgress();
211 }
212
213 void BookView::onSettingsChanged(const QString &key)
214 {
215     TRACE;
216     if (key == "zoom") {
217         setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
218     }
219     else if (key == "font") {
220         QString face = Settings::instance()->value("font").toString();
221         settings()->setFontFamily(QWebSettings::StandardFont, face);
222     }
223     else if (key == "scheme") {
224         QWebFrame *frame = page()->mainFrame();
225         QString scheme = Settings::instance()->value("scheme").toString();
226         if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
227             (scheme != "default")) {
228             scheme = "default";
229         }
230         QFile script(":/styles/" + scheme + ".js");
231         script.open(QFile::ReadOnly);
232         QString scriptText = script.readAll();
233         script.close();
234         QVariant ret = frame->evaluateJavaScript(scriptText);
235     }
236 }
237
238 void BookView::paintEvent(QPaintEvent *e)
239 {
240     QWebView::paintEvent(e);
241     if (!mBook || !loaded) {
242         return;
243     }
244
245     // Paint bookmarks
246     QPoint scrollPos = page()->mainFrame()->scrollPosition();
247     QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
248     QPainter painter(this);
249     foreach (Book::Bookmark b, mBook->bookmarks()) {
250         if (b.part != contentIndex) {
251             continue;
252         }
253         int height = contentsHeight;
254         int bookmarkPos = (int)((qreal)height * (qreal)b.pos);
255         painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
256     }
257 }
258
259 void BookView::mousePressEvent(QMouseEvent *e)
260 {
261     QWebView::mousePressEvent(e);
262 #ifdef Q_WS_MAEMO_5
263     // Start monitoring kinetic scroll
264     if (scrollerMonitor) {
265         killTimer(scrollerMonitor);
266         scrollerMonitor = 0;
267     }
268     if (scroller) {
269         scrollerMonitor = startTimer(500);
270     }
271 #else
272     // Handle mouse presses on the scroll bar
273     QWebFrame *frame = page()->mainFrame();
274     if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
275         e->accept();
276         return;
277     }
278 #endif // Q_WS_MAEMO_5
279     e->ignore();
280 }
281
282 void BookView::wheelEvent(QWheelEvent *e)
283 {
284     QWebView::wheelEvent(e);
285     showProgress();
286 }
287
288 void BookView::addBookmark(const QString &note)
289 {
290     TRACE;
291     if (!mBook) {
292         return;
293     }
294     int y = page()->mainFrame()->scrollPosition().y();
295     int height = page()->mainFrame()->contentsSize().height();
296     qDebug() << ((qreal)y / (qreal)height);
297     mBook->addBookmark(contentIndex, (qreal)y / (qreal)height, note);
298     update();
299 }
300
301 QString BookView::tmpPath()
302 {
303     return QDir::tempPath() + "/dorian";
304 }
305
306 bool BookView::eventFilter(QObject *o, QEvent *e)
307 {
308     if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
309         if (e->type() == QEvent::Resize) {
310             qDebug() << "BookView::eventFilter QEvent::Resize to"
311                     << page()->mainFrame()->contentsSize().height();
312         } else if (e->type() == QEvent::Timer) {
313             qDebug() << "BookView::eventFilter" << "QEvent::Timer"
314                     << ((QTimerEvent *)e)->timerId();
315         } else {
316             qDebug() << "BookView::eventFilter" << Trace::event(e->type());
317         }
318     }
319
320     // Work around Qt bug that sometimes selects web view contents during swipe
321     switch (e->type()) {
322     case QEvent::MouseButtonPress:
323         emit suppressedMouseButtonPress();
324         mousePressed = true;
325         break;
326     case QEvent::MouseButtonRelease:
327         showProgress();
328         mousePressed = false;
329         break;
330     case QEvent::MouseMove:
331         if (mousePressed) {
332             return true;
333         }
334         break;
335     case QEvent::MouseButtonDblClick:
336         return true;
337     default:
338         break;
339     }
340
341     return QObject::eventFilter(o, e);
342 }
343
344 void BookView::addJavaScriptObjects()
345 {
346     page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
347 }
348
349 void BookView::onContentsSizeChanged(const QSize &size)
350 {
351     contentsHeight = size.height();
352     if (restorePositionAfterLoad) {
353         qDebug() << "BookView::onContentSizeChanged: Time to restore";
354         restorePositionAfterLoad = false;
355         goToPosition(positionAfterLoad);
356     }
357 }
358
359 void BookView::leaveEvent(QEvent *e)
360 {
361     TRACE;
362     // Save current position, to be restored later
363     setLastBookmark();
364     QWebView::leaveEvent(e);
365 }
366
367 void BookView::enterEvent(QEvent *e)
368 {
369     TRACE;
370     // Restore position saved at Leave event. This seems to be required,
371     // after temporarily switching from portrait to landscape and back
372     restoreLastBookmark();
373     QWebView::enterEvent(e);
374 }
375
376 void BookView::goToPosition(qreal position)
377 {
378     int scrollPos = (int)((qreal)contentsHeight * position);
379     page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
380     // FIXME: update();
381     qDebug() << "BookView::goToPosition: To" << scrollPos << "("
382             << (position * 100) << "%, height" << contentsHeight << ")";
383 }
384
385 void BookView::showProgress()
386 {
387     if (mBook) {
388         qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
389                     (qreal)contentsHeight;
390         emit progress(mBook->getProgress(contentIndex, pos));
391     }
392 }
393
394 void BookView::timerEvent(QTimerEvent *e)
395 {
396 #ifdef Q_WS_MAEMO_5
397     if (e->timerId() == scrollerMonitor) {
398         if (scroller &&
399             ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
400              (scroller->state() == QAbstractKineticScroller::Pushing))) {
401             showProgress();
402         } else {
403             killTimer(scrollerMonitor);
404         }
405     }
406 #endif
407     QWebView::timerEvent(e);
408 }
409
410 void BookView::keyPressEvent(QKeyEvent* event)
411 {
412     switch (event->key()) {
413     case Qt::Key_F7:
414         goNextPage();
415         event->accept();
416         break;
417     case Qt::Key_F8:
418         goPreviousPage();
419         event->accept();
420         break;
421     default:
422         ;
423     }
424     QWebView::keyPressEvent(event);
425 }
426
427 void BookView::goPreviousPage()
428 {
429     QWebFrame *frame = page()->mainFrame();
430     int pos = frame->scrollPosition().y();
431     frame->scroll(0, -height());
432     if (pos == frame->scrollPosition().y()) {
433         if (contentIndex > 0) {
434             Book::Bookmark bookmark(contentIndex - 1, 1.0);
435             mBook->setLastBookmark(contentIndex - 1, 1.0);
436             goToBookmark(bookmark);
437         }
438     } else {
439         showProgress();
440     }
441 }
442
443 void BookView::goNextPage()
444 {
445     TRACE;
446     QWebFrame *frame = page()->mainFrame();
447     int pos = frame->scrollPosition().y();
448     frame->scroll(0, height());
449     if (pos == frame->scrollPosition().y()) {
450         goNext();
451     } else {
452         setLastBookmark();
453         showProgress();
454     }
455 }