Parse NCX directory for chapter titles. Show chapter titles for bookmarks.
[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         settings()->setFontFamily(QWebSettings::StandardFont, face);
171     }
172     else if (key == "scheme") {
173         QWebFrame *frame = page()->mainFrame();
174         QString scheme = Settings::instance()->value("scheme").toString();
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     }
185 }
186
187 void BookView::paintEvent(QPaintEvent *e)
188 {
189     QWebView::paintEvent(e);
190     if (!mBook) {
191         return;
192     }
193
194     // Paint bookmarks
195     if (!loadFinished) {
196         return;
197     }
198     QPoint scrollPos = page()->mainFrame()->scrollPosition();
199     QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
200     QPainter painter(this);
201     foreach (Book::Bookmark b, mBook->bookmarks()) {
202         if (b.chapter != contentIndex) {
203             continue;
204         }
205         int height = page()->mainFrame()->contentsSize().height();
206         int bookmarkPos = (qreal)height * (qreal)b.pos;
207         painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
208     }
209 }
210
211 void BookView::mousePressEvent(QMouseEvent *e)
212 {
213     QWebView::mousePressEvent(e);
214 #ifndef Q_WS_MAEMO_5
215     QWebFrame *frame = page()->mainFrame();
216     if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
217         e->accept();
218         return;
219     }
220 #endif
221     e->ignore();
222 }
223
224 void BookView::addBookmark()
225 {
226     int y = page()->mainFrame()->scrollPosition().y();
227     int height = page()->mainFrame()->contentsSize().height();
228     qDebug() << "BookView::addBookMark" << ((qreal)y / (qreal)height);
229     mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
230     repaint();
231 }
232
233 void BookView::addNavigationBar()
234 {
235     if (!mBook) {
236         return;
237     }
238
239     QString naviPrev =
240             "<a href=\"javascript:bv.goPrevious();\">"
241             "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
242             "src=\"file://"
243             + tmpPath() +
244             "/previous.png\" />"
245             "</a>";
246     QString naviNext =
247             "<a href=\"javascript:bv.goNext();\">"
248             "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
249             "src=\"file://"
250             + tmpPath() +
251             "/next.png\" />"
252             "</a>";
253     if (contentIndex == 0) {
254         naviPrev = "";
255     }
256     if (contentIndex >= mBook->toc.size() - 1) {
257         naviNext = "";
258     }
259
260     QWebFrame *frame = page()->currentFrame();
261     frame->addToJavaScriptWindowObject("bv", this);
262     QString headerScript = "document.body.innerHTML = '" +
263         naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
264     QString trailerScript = "document.body.innerHTML += '<br /><br />" +
265         naviPrev + naviNext + "';";
266
267     frame->evaluateJavaScript(headerScript);
268     frame->evaluateJavaScript(trailerScript);
269 }
270
271 QString BookView::tmpPath()
272 {
273     return QDir::tempPath() + "/dorian";
274 }
275
276 void BookView::extractIcons()
277 {
278     qDebug() << "BookView::extractIcons: Extracting to" << tmpPath();
279
280     QFile next(ICON_PREFIX + QString("/next.png"));
281     QFile prev(ICON_PREFIX + QString("/previous.png"));
282
283     QDir().mkpath(tmpPath());
284     next.copy(tmpPath() + "/next.png");
285     prev.copy(tmpPath() + "/previous.png");
286 }
287
288 void BookView::removeIcons()
289 {
290     QFile(ICON_PREFIX + QString("/next.png")).remove();
291     QFile(ICON_PREFIX + QString("/previous.png")).remove();
292     QDir().rmpath(tmpPath());
293 }