Make library full screen on Maemo. Show busy indicator while loading
[dorian] / bookview.cpp
1 #include <QDebug>
2 #include <QWebFrame>
3 #include <QMouseEvent>
4 #include <QFile>
5 #include <QDir>
6
7 #include "book.h"
8 #include "bookview.h"
9 #include "library.h"
10 #include "selectionsuppressor.h"
11 #include "settings.h"
12
13 #ifdef Q_WS_MAC
14 #   define ICON_PREFIX ":/icons/mac/"
15 #else
16 #   define ICON_PREFIX ":/icons/"
17 #endif
18
19 BookView::BookView(QWidget *parent):
20     QWebView(parent), contentIndex(-1), mBook(0),
21     restore(true), restorePos(0), loadFinished(false)
22 {
23     settings()->setAttribute(QWebSettings::AutoLoadImages, true);
24     settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
25     settings()->setAttribute(QWebSettings::PluginsEnabled, false);
26     settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
27     settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
28                              false);
29     page()->setContentEditable(false);
30
31 #if defined(Q_WS_MAEMO_5)
32     (void)new SelectionSuppressor(this);
33 #endif
34     QWebFrame *frame = page()->mainFrame();
35 #if defined(Q_WS_MAEMO_5)
36     frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
37 #endif
38     frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
39
40     bookmarkImage = QImage(":/icons/bookmark.png");
41
42     connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
43     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
44             this, SLOT(onSettingsChanged(const QString &)));
45     Settings *s = Settings::instance();
46     s->setValue("zoom", s->value("zoom", 160));
47     s->setValue("font", s->value("font",
48 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_X11)
49                                  "Serif"
50 #elif defined(Q_WS_MAC)
51                                  "Hoefler Text"
52 #else
53                                  "Times New Roman"
54 #endif
55                                  ));
56     s->setValue("scheme", s->value("scheme", "default"));
57     setBook(0);
58
59     extractIcons();
60 }
61
62 BookView::~BookView()
63 {
64     removeIcons();
65 }
66
67 void BookView::loadContent(int index)
68 {
69     if (!mBook) {
70         return;
71     }
72     if ((index < 0) || (index >= mBook->toc.size())) {
73         return;
74     }
75
76     QString contentFile(mBook->content[mBook->toc[index]].href);
77     if (mBook->toc[index] == "error") {
78         setHtml(contentFile);
79     }
80     else {
81         loadFinished = false;
82         emit chapterLoadStart(index);
83         load(QUrl(contentFile));
84     }
85     contentIndex = index;
86 }
87
88 void BookView::setBook(Book *book)
89 {
90     qDebug() << "Book::setBook" << (book? book->path(): "");
91
92     setLastBookmark();
93     if (book != mBook) {
94         mBook = book;
95         if (book) {
96             contentIndex = -1;
97             book->open();
98             goToBookmark(book->lastBookmark());
99         }
100         else {
101             contentIndex = 0;
102             setHtml(tr("No book"));
103         }
104     }
105 }
106
107 Book *BookView::book()
108 {
109     return mBook;
110 }
111
112 void BookView::goPrevious()
113 {
114     loadContent(contentIndex - 1);
115 }
116
117 void BookView::goNext()
118 {
119     loadContent(contentIndex + 1);
120 }
121
122 void BookView::setLastBookmark()
123 {
124     qDebug() << "BookView::setLastBookmark";
125     if (mBook) {
126         int height = page()->mainFrame()->contentsSize().height();
127         int pos = page()->mainFrame()->scrollPosition().y();
128         mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
129     }
130 }
131
132 void BookView::goToBookmark(const Book::Bookmark &bookmark)
133 {
134     if (mBook) {
135         restore = true;
136         restorePos = bookmark.pos;
137         if (bookmark.chapter != contentIndex) {
138             loadContent(bookmark.chapter);
139         } else {
140             onLoadFinished(true);
141         }
142     }
143 }
144
145 void BookView::onLoadFinished(bool ok)
146 {
147     qDebug() << "BookView::onLoadFinished" << ok;
148     loadFinished = true;
149     addNavigationBar();
150     onSettingsChanged("scheme");
151     emit chapterLoadEnd(contentIndex);
152     if (restore) {
153         restore = false;
154         if (ok && mBook) {
155             int height = page()->mainFrame()->contentsSize().height();
156             int scrollPos = (qreal)height * restorePos;
157             page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
158         }
159     }
160 }
161
162 void BookView::onSettingsChanged(const QString &key)
163 {
164     qDebug() << "BookView::onSettingsChanged" << key;
165     if (key == "zoom") {
166         setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
167     }
168     else if (key == "font") {
169         QString face = Settings::instance()->value("font").toString();
170         qDebug() << "" << face;
171         settings()->setFontFamily(QWebSettings::StandardFont, face);
172     }
173     else if (key == "scheme") {
174         QWebFrame *frame = page()->mainFrame();
175         QString scheme = Settings::instance()->value("scheme").toString();
176         qDebug() << "" << scheme;
177         if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
178             (scheme != "default")) {
179             scheme = "default";
180         }
181         QFile script(":/styles/" + scheme + ".js");
182         script.open(QFile::ReadOnly);
183         QString scriptText = script.readAll();
184         script.close();
185         QVariant ret = frame->evaluateJavaScript(scriptText);
186         qDebug() << "" << script.fileName() << ":" << scriptText;
187         qDebug() << "" << ret;
188     }
189 }
190
191 void BookView::paintEvent(QPaintEvent *e)
192 {
193     QWebView::paintEvent(e);
194     if (!mBook) {
195         return;
196     }
197
198     // Paint bookmarks
199     if (!loadFinished) {
200         return;
201     }
202     QPoint scrollPos = page()->mainFrame()->scrollPosition();
203     QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
204     QPainter painter(this);
205     foreach (Book::Bookmark b, mBook->bookmarks()) {
206         if (b.chapter != contentIndex) {
207             continue;
208         }
209         int height = page()->mainFrame()->contentsSize().height();
210         int bookmarkPos = (qreal)height * (qreal)b.pos;
211         painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
212     }
213 }
214
215 void BookView::mousePressEvent(QMouseEvent *e)
216 {
217     QWebView::mousePressEvent(e);
218 #ifndef Q_WS_MAEMO_5
219     QWebFrame *frame = page()->mainFrame();
220     if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
221         e->accept();
222         return;
223     }
224 #endif
225     e->ignore();
226 }
227
228 void BookView::addBookmark()
229 {
230     int y = page()->mainFrame()->scrollPosition().y();
231     int height = page()->mainFrame()->contentsSize().height();
232     qDebug() << "BookView::addBookMark" << ((qreal)y / (qreal)height);
233     mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
234     repaint();
235 }
236
237 void BookView::addNavigationBar()
238 {
239     if (!mBook) {
240         return;
241     }
242
243     QString naviPrev =
244             "<a href=\"javascript:bv.goPrevious();\">"
245             "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
246             "src=\"file://"
247             + tmpPath() +
248             "/previous.png\" />"
249             "</a>";
250     QString naviNext =
251             "<a href=\"javascript:bv.goNext();\">"
252             "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
253             "src=\"file://"
254             + tmpPath() +
255             "/next.png\" />"
256             "</a>";
257     if (contentIndex == 0) {
258         naviPrev = "";
259     }
260     if (contentIndex >= mBook->toc.size() - 1) {
261         naviNext = "";
262     }
263
264     QWebFrame *frame = page()->currentFrame();
265     frame->addToJavaScriptWindowObject("bv", this);
266     QString headerScript = "document.body.innerHTML = '" +
267         naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
268     QString trailerScript = "document.body.innerHTML += '<br /><br />" +
269         naviPrev + naviNext + "';";
270
271     frame->evaluateJavaScript(headerScript);
272     frame->evaluateJavaScript(trailerScript);
273 }
274
275 QString BookView::tmpPath()
276 {
277     return QDir::tempPath() + "/dorian";
278 }
279
280 void BookView::extractIcons()
281 {
282     qDebug() << "BookView::extractIcons: Extracting to" << tmpPath();
283
284     QFile next(ICON_PREFIX + QString("/next.png"));
285     QFile prev(ICON_PREFIX + QString("/previous.png"));
286
287     QDir().mkpath(tmpPath());
288     next.copy(tmpPath() + "/next.png");
289     prev.copy(tmpPath() + "/previous.png");
290 }
291
292 void BookView::removeIcons()
293 {
294     QFile(ICON_PREFIX + QString("/next.png")).remove();
295     QFile(ICON_PREFIX + QString("/previous.png")).remove();
296     QDir().rmpath(tmpPath());
297 }