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