Jump to exact position on page, as identified by TOC entry. Handle
[dorian] / mainwindow.cpp
1 #include <QtGui>
2
3 #ifdef Q_WS_MAEMO_5
4 #   include <QtMaemo5/QMaemo5InformationBox>
5 #   include <QtDBus>
6 #   include <QtGui/QX11Info>
7 #   include <X11/Xlib.h>
8 #   include <X11/Xatom.h>
9 #   include <mce/mode-names.h>
10 #   include <mce/dbus-names.h>
11 #endif // Q_WS_MAEMO_5
12
13 #include "bookview.h"
14 #include "book.h"
15 #include "library.h"
16 #include "infodialog.h"
17 #include "librarydialog.h"
18 #include "devtools.h"
19 #include "mainwindow.h"
20 #include "settingswindow.h"
21 #include "bookmarksdialog.h"
22 #include "settings.h"
23 #include "chaptersdialog.h"
24 #include "fullscreenwindow.h"
25 #include "trace.h"
26 #include "bookfinder.h"
27 #include "progress.h"
28 #include "dyalog.h"
29 #include "translucentbutton.h"
30 #include "platform.h"
31 #include "progressdialog.h"
32
33 #ifdef DORIAN_TEST_MODEL
34 #   include "modeltest.h"
35 #endif
36
37 const int DORIAN_PROGRESS_HEIGHT = 17;
38
39 MainWindow::MainWindow(QWidget *parent):
40     AdopterWindow(parent), view(0), preventBlankingTimer(-1)
41 {
42     TRACE;
43 #ifdef Q_WS_MAEMO_5
44     setAttribute(Qt::WA_Maemo5StackedWindow, true);
45 #endif
46     setWindowTitle("Dorian");
47
48     // Central widget. Must be an intermediate, because the book view widget
49     // can be re-parented later
50     QFrame *central = new QFrame(this);
51     QVBoxLayout *layout = new QVBoxLayout(central);
52     layout->setMargin(0);
53     central->setLayout(layout);
54     setCentralWidget(central);
55
56     // Book view
57     view = new BookView(central);
58     view->show();
59     layout->addWidget(view);
60
61     // Progress
62     progress = new Progress(central);
63
64     // Settings dialog
65     settings = new QDialog(this);
66
67     // Tool bar actions
68
69 #ifdef Q_OS_SYMBIAN
70     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
71                                         "view-fullscreen", tr("Full screen"));
72 #endif
73
74     chaptersAction = addToolBarAction(this, SLOT(showChapters()),
75                                       "chapters", tr("Chapters"));
76     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
77                                        "bookmarks", tr("Bookmarks"));
78     infoAction = addToolBarAction(this, SLOT(showInfo()),
79                                   "info", tr("Book info"));
80     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
81                                      "library", tr("Library"));
82
83 #ifdef Q_WS_MAEMO_5
84     settingsAction = menuBar()->addAction(tr("Settings"));
85     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
86     devToolsAction = menuBar()->addAction(tr("Developer"));
87     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
88     QAction *aboutAction = menuBar()->addAction(tr("About"));
89     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
90 #else
91     settingsAction = addToolBarAction(this, SLOT(showSettings()),
92                                       "preferences-system", tr("Settings"));
93     devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
94                                       "developer", tr("Developer"));
95     addToolBarAction(this, SLOT(about()), "about", tr("About"));
96 #endif // Q_WS_MAEMO_5
97
98 #ifndef Q_OS_SYMBIAN
99     addToolBarSpace();
100     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
101                                         "view-fullscreen", tr("Full screen"));
102 #else
103     (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
104 #endif
105
106     // Buttons on top of the book view
107     previousButton = new TranslucentButton("back", this);
108     nextButton = new TranslucentButton("forward", this);
109
110     // Handle model changes
111     connect(Library::instance(), SIGNAL(nowReadingChanged()),
112             this, SLOT(onCurrentBookChanged()));
113
114     // Load library, upgrade it if needed
115     libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
116     Library *library = Library::instance();
117     connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
118     connect(library, SIGNAL(upgrading(const QString &)),
119             this, SLOT(onUpgrading(const QString &)));
120     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
121     connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
122     connect(library, SIGNAL(loading(const QString &)),
123             this, SLOT(onLoading(const QString &)));
124     connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
125     library->upgrade();
126     library->load();
127
128     // Load book on command line, or load last read book, or load default book
129     if (QCoreApplication::arguments().size() == 2) {
130         QString path = QCoreApplication::arguments()[1];
131         library->add(path);
132         QModelIndex index = library->find(path);
133         if (index.isValid()) {
134             library->setNowReading(index);
135         }
136     } else {
137         QModelIndex index = library->nowReading();
138         if (index.isValid()) {
139             library->setNowReading(index);
140         } else {
141             if (!library->rowCount()) {
142                 library->add(":/books/2BR02B.epub");
143             }
144             library->setNowReading(library->index(0));
145         }
146     }
147
148     // Handle loading book parts
149     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
150     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
151
152     // Handle progress
153     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
154
155     // Shadow window for full screen reading
156     fullScreenWindow = new FullScreenWindow(this);
157     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
158
159     // Handle settings changes
160     Settings *settings = Settings::instance();
161     connect(settings, SIGNAL(valueChanged(const QString &)),
162             this, SLOT(onSettingsChanged(const QString &)));
163     settings->setValue("orientation", settings->value("orientation"));
164     settings->setValue("lightson", settings->value("lightson"));
165     settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
166
167     // Handle book view buttons
168     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
169     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
170
171 #ifdef DORIAN_TEST_MODEL
172     (void)new ModelTest(Library::instance(), this);
173 #endif
174 }
175
176 MainWindow::~MainWindow()
177 {
178 }
179
180 void MainWindow::onCurrentBookChanged()
181 {
182     setCurrentBook(Library::instance()->nowReading());
183 }
184
185 void MainWindow::showRegular()
186 {
187     TRACE;
188
189     // Re-parent children
190     fullScreenWindow->leaveChildren();
191     QList<QWidget *> otherChildren;
192     otherChildren << progress << previousButton << nextButton;
193     takeChildren(view, otherChildren);
194
195     // Adjust geometry of decorations
196     QRect geo = geometry();
197     progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
198 #if defined(Q_WS_MAEMO_5)
199     previousButton->setGeometry(0,
200         geo.height() - toolBar->height() - TranslucentButton::pixels,
201         TranslucentButton::pixels, TranslucentButton::pixels);
202     nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
203         TranslucentButton::pixels, TranslucentButton::pixels);
204 #elif defined(Q_OS_SYMBIAN)
205     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
206         TranslucentButton::pixels, TranslucentButton::pixels);
207     nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
208         0, TranslucentButton::pixels, TranslucentButton::pixels);
209 #else
210     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
211         TranslucentButton::pixels, TranslucentButton::pixels);
212     nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
213         toolBar->height(), TranslucentButton::pixels,
214         TranslucentButton::pixels);
215 #endif // Q_WS_MAEMO_5
216     qDebug() << "previousButton geometry" << previousButton->geometry();
217
218     fullScreenWindow->hide();
219     show();
220 #if defined(Q_OS_SYMBIAN)
221     activateWindow();
222 #elif defined(Q_WS_MAEMO_5)
223     // FIXME: This is ugly.
224     view->restoreLastBookmark();
225 #endif
226     progress->flash();
227     nextButton->flash(1500);
228     previousButton->flash(1500);
229 }
230
231 void MainWindow::showBig()
232 {
233     TRACE;
234
235     // Re-parent children
236     leaveChildren();
237     QList<QWidget *> otherChildren;
238     otherChildren << progress << nextButton << previousButton;
239     fullScreenWindow->takeChildren(view, otherChildren);
240
241     // Adjust geometry of decorations
242     QRect screen = QApplication::desktop()->screenGeometry();
243     progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
244 #if defined(Q_WS_MAEMO_5)
245     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
246         TranslucentButton::pixels, TranslucentButton::pixels);
247 #else
248     nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
249         TranslucentButton::pixels, TranslucentButton::pixels);
250 #endif // Q_WS_MAEMO_5
251     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
252         TranslucentButton::pixels, TranslucentButton::pixels);
253
254 #ifdef Q_OS_SYMBIAN
255     hide();
256 #endif
257     fullScreenWindow->showFullScreen();
258 #ifdef Q_OS_SYMBIAN
259     fullScreenWindow->activateWindow();
260 #endif
261     progress->flash();
262     nextButton->flash(1500);
263     previousButton->flash(1500);
264 }
265
266 void MainWindow::setCurrentBook(const QModelIndex &current)
267 {
268     mCurrent = current;
269     Book *book = Library::instance()->book(current);
270     view->setBook(book);
271     setWindowTitle(book? book->shortName(): tr("Dorian"));
272 }
273
274 void MainWindow::showLibrary()
275 {
276     (new LibraryDialog(this))->show();
277 }
278
279 void MainWindow::showSettings()
280 {
281     (new SettingsWindow(this))->show();
282 }
283
284 void MainWindow::showInfo()
285 {
286     if (mCurrent.isValid()) {
287         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
288                 exec();
289     }
290 }
291
292 void MainWindow::showDevTools()
293 {
294     (new DevTools())->exec();
295 }
296
297 void MainWindow::showBookmarks()
298 {
299     Book *book = Library::instance()->book(mCurrent);
300     if (book) {
301         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
302         bookmarks->setWindowModality(Qt::WindowModal);
303         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
304                 this, SLOT(onAddBookmark(const QString &)));
305         connect(bookmarks, SIGNAL(goToBookmark(int)),
306                 this, SLOT(onGoToBookmark(int)));
307         bookmarks->show();
308     }
309 }
310
311 void MainWindow::closeEvent(QCloseEvent *event)
312 {
313     TRACE;
314     view->setLastBookmark();
315     event->accept();
316 }
317
318 void MainWindow::onSettingsChanged(const QString &key)
319 {
320 #if defined(Q_WS_MAEMO_5)
321     if (key == "orientation") {
322         QString value = Settings::instance()->value(key).toString();
323         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
324         if (value == "portrait") {
325             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
326             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
327         } else {
328             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
329             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
330         }
331     } else if (key == "lightson") {
332         bool enable = Settings::instance()->value(key, false).toBool();
333         qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
334         killTimer(preventBlankingTimer);
335         if (enable) {
336             preventBlankingTimer = startTimer(29 * 1000);
337         }
338     } else if (key == "usevolumekeys") {
339         bool value = Settings::instance()->value(key).toBool();
340         qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
341         grabZoomKeys(value);
342         fullScreenWindow->grabZoomKeys(value);
343     }
344 #else
345     Q_UNUSED(key);
346 #endif // Q_WS_MAEMO_5
347 }
348
349 void MainWindow::onPartLoadStart()
350 {
351     TRACE;
352 #ifdef Q_WS_MAEMO_5
353     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
354 #endif
355 }
356
357 void MainWindow::onPartLoadEnd(int index)
358 {
359     TRACE;
360     bool enablePrevious = false;
361     bool enableNext = false;
362     Book *book = Library::instance()->book(mCurrent);
363     if (book) {
364         if (index > 0) {
365             enablePrevious = true;
366         }
367         if (index < (book->parts.size() - 1)) {
368             enableNext = true;
369         }
370     }
371 #ifdef Q_WS_MAEMO_5
372     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
373 #endif // Q_WS_MAEMO_5
374 }
375
376 void MainWindow::onAddBookmark(const QString &note)
377 {
378     TRACE;
379     view->addBookmark(note);
380 #ifdef Q_WS_MAEMO_5
381     QMaemo5InformationBox::information(this,
382         tr("Bookmarked current position"), QMaemo5InformationBox::DefaultTimeout);
383 #else
384     (void)QMessageBox::information(this, tr("Dorian"),
385         tr("Bookmarked current position"), QMessageBox::Ok);
386 #endif // Q_WS_MAEMO_5}
387 }
388
389 void MainWindow::onGoToBookmark(int index)
390 {
391     TRACE;
392     Book *book = Library::instance()->book(mCurrent);
393     view->goToBookmark(book->bookmarks()[index]);
394 }
395
396 void MainWindow::showChapters()
397 {
398     Book *book = Library::instance()->book(mCurrent);
399     if (book) {
400         ChaptersDialog *chapters = new ChaptersDialog(book, this);
401         chapters->setWindowModality(Qt::WindowModal);
402         connect(chapters, SIGNAL(goToChapter(int)),
403                 this, SLOT(onGoToChapter(int)));
404         chapters->show();
405     }
406 }
407
408 void MainWindow::onGoToChapter(int index)
409 {
410     TRACE;
411
412     Book *book = Library::instance()->book(mCurrent);
413     if (book) {
414         QString fragment;
415         int partIndex = book->partFromChapter(index, fragment);
416         if (partIndex != -1) {
417             view->goToPart(partIndex, fragment);
418         }
419     }
420 }
421
422 void MainWindow::timerEvent(QTimerEvent *event)
423 {
424     if (event->timerId() == preventBlankingTimer) {
425 #ifdef Q_WS_MAEMO_5
426         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
427                            MCE_REQUEST_IF, QDBusConnection::systemBus());
428         mce.call(MCE_PREVENT_BLANK_REQ);
429 #endif // Q_WS_MAEMO_5
430         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
431     }
432     AdopterWindow::timerEvent(event);
433 }
434
435 void MainWindow::resizeEvent(QResizeEvent *e)
436 {
437     TRACE;
438     progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
439 #if defined(Q_WS_MAEMO_5)
440     previousButton->setGeometry(0,
441         e->size().height() - toolBar->height() - TranslucentButton::pixels,
442         TranslucentButton::pixels, TranslucentButton::pixels);
443     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
444         TranslucentButton::pixels, TranslucentButton::pixels);
445 #elif defined(Q_OS_SYMBIAN)
446     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
447         TranslucentButton::pixels, TranslucentButton::pixels);
448     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
449         0, TranslucentButton::pixels, TranslucentButton::pixels);
450 #else
451     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
452         TranslucentButton::pixels, TranslucentButton::pixels);
453     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
454         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
455 #endif // Q_WS_MAEMO_5
456     qDebug() << "previousButton geometry" << previousButton->geometry();
457     previousButton->flash(1500);
458     nextButton->flash(1500);
459     QMainWindow::resizeEvent(e);
460 }
461
462 void MainWindow::about()
463 {
464     Dyalog *aboutDialog = new Dyalog(this, false);
465     aboutDialog->setWindowTitle(tr("About Dorian"));
466     QLabel *label = new QLabel(aboutDialog);
467     label->setTextFormat(Qt::RichText);
468     label->setOpenExternalLinks(true);
469     label->setWordWrap(true);
470     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
471         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
472         "Licensed under GNU General Public License, Version 3<br>"
473         "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
474         "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
475     aboutDialog->addWidget(label);
476     aboutDialog->addStretch();
477     aboutDialog->show();
478 }
479
480
481 void MainWindow::goToNextPage()
482 {
483     nextButton->flash(1500);
484     previousButton->flash(1500);
485     view->goNextPage();
486 }
487
488 void MainWindow::goToPreviousPage()
489 {
490     nextButton->flash(1500);
491     previousButton->flash(1500);
492     view->goPreviousPage();
493 }
494
495 void MainWindow::onBeginUpgrade(int total)
496 {
497     libraryProgress->setVisible(total > 0);
498     libraryProgress->setWindowTitle(tr("Upgrading library"));
499     libraryProgress->setMaximum(total);
500 }
501
502 void MainWindow::onUpgrading(const QString &path)
503 {
504     libraryProgress->setLabelText(tr("Upgrading %1").
505                                   arg(QFileInfo(path).fileName()));
506     libraryProgress->setValue(libraryProgress->value() + 1);
507 }
508
509 void MainWindow::onEndUpgrade()
510 {
511     libraryProgress->hide();
512     libraryProgress->reset();
513 }
514
515
516 void MainWindow::onBeginLoad(int total)
517 {
518     libraryProgress->setVisible(total > 0);
519     libraryProgress->setWindowTitle(tr("Loading library"));
520     libraryProgress->setMaximum(total);
521 }
522
523 void MainWindow::onLoading(const QString &path)
524 {
525     libraryProgress->setLabelText(tr("Loading %1").
526                                   arg(QFileInfo(path).fileName()));
527     libraryProgress->setValue(libraryProgress->value() + 1);
528 }
529
530 void MainWindow::onEndLoad()
531 {
532     libraryProgress->hide();
533     libraryProgress->reset();
534 }
535