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