Update from old repository.
[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(0), 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             book->open();
95             goToBookmark(book->lastBookmark());
96         }
97         else {
98             setHtml(tr("No book"));
99         }
100     }
101 }
102
103 Book *BookView::book()
104 {
105     return mBook;
106 }
107
108 void BookView::goPrevious()
109 {
110     loadContent(contentIndex - 1);
111 }
112
113 void BookView::goNext()
114 {
115     loadContent(contentIndex + 1);
116 }
117
118 void BookView::setLastBookmark()
119 {
120     qDebug() << "BookView::setLastBookmark";
121     if (mBook) {
122         int height = page()->mainFrame()->contentsSize().height();
123         int pos = page()->mainFrame()->scrollPosition().y();
124         mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
125     }
126 }
127
128 void BookView::goToBookmark(const Book::Bookmark &bookmark)
129 {
130     if (mBook) {
131         restore = true;
132         restorePos = bookmark.pos;
133         loadContent(bookmark.chapter);
134     }
135 }
136
137 void BookView::onLoadFinished(bool ok)
138 {
139     qDebug() << "BookView::onLoadFinished" << ok;
140     loadFinished = true;
141     addNavigationBar();
142     onSettingsChanged("scheme");
143     emit chapterLoaded(contentIndex);
144     if (restore) {
145         restore = false;
146         if (ok && mBook) {
147             int height = page()->mainFrame()->contentsSize().height();
148             int scrollPos = (qreal)height * restorePos;
149             page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
150         }
151     }
152 }
153
154 void BookView::onSettingsChanged(const QString &key)
155 {
156     qDebug() << "BookView::onSettingsChanged" << key;
157     if (key == "zoom") {
158         setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
159     }
160     else if (key == "font") {
161         QString face = Settings::instance()->value("font").toString();
162         qDebug() << "" << face;
163         settings()->setFontFamily(QWebSettings::StandardFont, face);
164     }
165     else if (key == "scheme") {
166         QWebFrame *frame = page()->mainFrame();
167         QString scheme = Settings::instance()->value("scheme").toString();
168         qDebug() << "" << scheme;
169         if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
170             (scheme != "default")) {
171             scheme = "default";
172         }
173         QFile script(":/styles/" + scheme + ".js");
174         script.open(QFile::ReadOnly);
175         QString scriptText = script.readAll();
176         script.close();
177         QVariant ret = frame->evaluateJavaScript(scriptText);
178         qDebug() << "" << script.fileName() << ":" << scriptText;
179         qDebug() << "" << ret;
180     }
181 }
182
183 void BookView::paintEvent(QPaintEvent *e)
184 {
185     QWebView::paintEvent(e);
186     if (!mBook) {
187         return;
188     }
189
190     // Paint bookmarks
191     if (!loadFinished) {
192         return;
193     }
194     QPoint scrollPos = page()->mainFrame()->scrollPosition();
195     QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
196     QPainter painter(this);
197     foreach (Book::Bookmark b, mBook->bookmarks()) {
198         if (b.chapter != contentIndex) {
199             continue;
200         }
201         int height = page()->mainFrame()->contentsSize().height();
202         int bookmarkPos = (qreal)height * (qreal)b.pos;
203         painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
204     }
205 }
206
207 void BookView::mousePressEvent(QMouseEvent *e)
208 {
209     QWebView::mousePressEvent(e);
210 #ifndef Q_WS_MAEMO_5
211     QWebFrame *frame = page()->mainFrame();
212     if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
213         e->accept();
214         return;
215     }
216 #endif
217     e->ignore();
218 }
219
220 void BookView::addBookmark()
221 {
222     int y = page()->mainFrame()->scrollPosition().y();
223     int height = page()->mainFrame()->contentsSize().height();
224     qDebug() << "BookView::addBookMark" << ((qreal)y / (qreal)height);
225     mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
226     repaint();
227 }
228
229 void BookView::addNavigationBar()
230 {
231     if (!mBook) {
232         return;
233     }
234
235     QString naviPrev =
236             "<a href=\"javascript:bv.goPrevious();\">"
237             "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
238             "src=\"file://"
239             + tmpPath() +
240             "/previous.png\" />"
241             "</a>";
242     QString naviNext =
243             "<a href=\"javascript:bv.goNext();\">"
244             "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
245             "src=\"file://"
246             + tmpPath() +
247             "/next.png\" />"
248             "</a>";
249     if (contentIndex == 0) {
250         naviPrev = "";
251     }
252     if (contentIndex >= mBook->toc.size() - 1) {
253         naviNext = "";
254     }
255
256     QWebFrame *frame = page()->currentFrame();
257     frame->addToJavaScriptWindowObject("bv", this);
258     QString headerScript = "document.body.innerHTML = '" +
259         naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
260     QString trailerScript = "document.body.innerHTML += '<br /><br />" +
261         naviPrev + naviNext + "';";
262
263     frame->evaluateJavaScript(headerScript);
264     frame->evaluateJavaScript(trailerScript);
265 }
266
267 QString BookView::tmpPath()
268 {
269     return QDir::tempPath() + "/dorian";
270 }
271
272 void BookView::extractIcons()
273 {
274     qDebug() << "BookView::extractIcons: Extracting to" << tmpPath();
275
276     QFile next(ICON_PREFIX + QString("/next.png"));
277     QFile prev(ICON_PREFIX + QString("/previous.png"));
278
279     QDir().mkpath(tmpPath());
280     next.copy(tmpPath() + "/next.png");
281     prev.copy(tmpPath() + "/previous.png");
282 }
283
284 void BookView::removeIcons()
285 {
286     QFile(ICON_PREFIX + QString("/next.png")).remove();
287     QFile(ICON_PREFIX + QString("/previous.png")).remove();
288     QDir().rmpath(tmpPath());
289 }