Battle with include files (lost).
[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 #endif
103
104     // Buttons on top of the book view
105     previousButton = new TranslucentButton("back", this);
106     nextButton = new TranslucentButton("forward", this);
107
108     // Handle model changes
109     connect(Library::instance(), SIGNAL(nowReadingChanged()),
110             this, SLOT(onCurrentBookChanged()));
111
112     // Load library, upgrade it if needed
113     libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
114     Library *library = Library::instance();
115     connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
116     connect(library, SIGNAL(upgrading(const QString &)),
117             this, SLOT(onUpgrading(const QString &)));
118     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
119     connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
120     connect(library, SIGNAL(loading(const QString &)),
121             this, SLOT(onLoading(const QString &)));
122     connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
123     library->upgrade();
124     library->load();
125
126     // Load book on command line, or load last read book, or load default book
127     if (QCoreApplication::arguments().size() == 2) {
128         QString path = QCoreApplication::arguments()[1];
129         library->add(path);
130         QModelIndex index = library->find(path);
131         if (index.isValid()) {
132             library->setNowReading(index);
133         }
134     } else {
135         QModelIndex index = library->nowReading();
136         if (index.isValid()) {
137             library->setNowReading(index);
138         } else {
139             if (!library->rowCount()) {
140                 library->add(":/books/2BR02B.epub");
141             }
142             library->setNowReading(library->index(0));
143         }
144     }
145
146     // Handle loading book parts
147     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
148     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
149
150     // Handle progress
151     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
152
153     // Shadow window for full screen reading
154     fullScreenWindow = new FullScreenWindow(this);
155     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
156
157     // Handle settings changes
158     Settings *settings = Settings::instance();
159     connect(settings, SIGNAL(valueChanged(const QString &)),
160             this, SLOT(onSettingsChanged(const QString &)));
161     settings->setValue("orientation", settings->value("orientation"));
162     settings->setValue("lightson", settings->value("lightson"));
163     settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
164
165     // Handle book view buttons
166     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
167     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
168
169 #ifdef DORIAN_TEST_MODEL
170     (void)new ModelTest(Library::instance(), this);
171 #endif
172 }
173
174 MainWindow::~MainWindow()
175 {
176 }
177
178 void MainWindow::onCurrentBookChanged()
179 {
180     setCurrentBook(Library::instance()->nowReading());
181 }
182
183 void MainWindow::showRegular()
184 {
185     TRACE;
186
187     // Re-parent children
188     fullScreenWindow->leaveChildren();
189     QList<QWidget *> otherChildren;
190     otherChildren << progress << previousButton << nextButton;
191     takeChildren(view, otherChildren);
192
193     // Adjust geometry of decorations
194     QRect geo = geometry();
195     progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
196 #if defined(Q_WS_MAEMO_5)
197     previousButton->setGeometry(0,
198         geo.height() - toolBar->height() - TranslucentButton::pixels,
199         TranslucentButton::pixels, TranslucentButton::pixels);
200     nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
201         TranslucentButton::pixels, TranslucentButton::pixels);
202 #elif defined(Q_OS_SYMBIAN)
203     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
204         TranslucentButton::pixels, TranslucentButton::pixels);
205     nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
206         0, TranslucentButton::pixels, TranslucentButton::pixels);
207 #else
208     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
209         TranslucentButton::pixels, TranslucentButton::pixels);
210     nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
211         toolBar->height(), TranslucentButton::pixels,
212         TranslucentButton::pixels);
213 #endif // Q_WS_MAEMO_5
214     qDebug() << "previousButton geometry" << previousButton->geometry();
215
216     fullScreenWindow->hide();
217     show();
218 #if defined(Q_OS_SYMBIAN)
219     activateWindow();
220 #elif defined(Q_WS_MAEMO_5)
221     // FIXME: This is ugly.
222     view->restoreLastBookmark();
223 #endif
224     progress->flash();
225     nextButton->flash(1500);
226     previousButton->flash(1500);
227 }
228
229 void MainWindow::showBig()
230 {
231     TRACE;
232
233     // Re-parent children
234     leaveChildren();
235     QList<QWidget *> otherChildren;
236     otherChildren << progress << nextButton << previousButton;
237     fullScreenWindow->takeChildren(view, otherChildren);
238
239     // Adjust geometry of decorations
240     QRect screen = QApplication::desktop()->screenGeometry();
241     progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
242 #if defined(Q_WS_MAEMO_5)
243     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
244         TranslucentButton::pixels, TranslucentButton::pixels);
245 #else
246     nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
247         TranslucentButton::pixels, TranslucentButton::pixels);
248 #endif // Q_WS_MAEMO_5
249     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
250         TranslucentButton::pixels, TranslucentButton::pixels);
251
252 #ifdef Q_OS_SYMBIAN
253     hide();
254 #endif
255     fullScreenWindow->showFullScreen();
256 #ifdef Q_OS_SYMBIAN
257     fullScreenWindow->activateWindow();
258 #endif
259     progress->flash();
260     nextButton->flash(1500);
261     previousButton->flash(1500);
262 }
263
264 void MainWindow::setCurrentBook(const QModelIndex &current)
265 {
266     mCurrent = current;
267     Book *book = Library::instance()->book(current);
268     view->setBook(book);
269     setWindowTitle(book? book->shortName(): tr("Dorian"));
270 }
271
272 void MainWindow::showLibrary()
273 {
274     (new LibraryDialog(this))->show();
275 }
276
277 void MainWindow::showSettings()
278 {
279     (new SettingsWindow(this))->show();
280 }
281
282 void MainWindow::showInfo()
283 {
284     if (mCurrent.isValid()) {
285         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
286                 exec();
287     }
288 }
289
290 void MainWindow::showDevTools()
291 {
292     (new DevTools())->exec();
293 }
294
295 void MainWindow::showBookmarks()
296 {
297     Book *book = Library::instance()->book(mCurrent);
298     if (book) {
299         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
300         bookmarks->setWindowModality(Qt::WindowModal);
301         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
302                 this, SLOT(onAddBookmark(const QString &)));
303         connect(bookmarks, SIGNAL(goToBookmark(int)),
304                 this, SLOT(onGoToBookmark(int)));
305         bookmarks->show();
306     }
307 }
308
309 void MainWindow::closeEvent(QCloseEvent *event)
310 {
311     TRACE;
312     view->setLastBookmark();
313     event->accept();
314 }
315
316 void MainWindow::onSettingsChanged(const QString &key)
317 {
318 #ifdef Q_WS_MAEMO_5
319     if (key == "orientation") {
320         QString value = Settings::instance()->value(key).toString();
321         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
322         if (value == "portrait") {
323             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
324             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
325         } else {
326             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
327             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
328         }
329     } else if (key == "lightson") {
330         bool enable = Settings::instance()->value(key, false).toBool();
331         qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
332         killTimer(preventBlankingTimer);
333         if (enable) {
334             preventBlankingTimer = startTimer(29 * 1000);
335         }
336     } else if (key == "usevolumekeys") {
337         bool value = Settings::instance()->value(key).toBool();
338         qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
339         grabZoomKeys(value);
340         fullScreenWindow->grabZoomKeys(value);
341     }
342 #else
343     Q_UNUSED(key);
344 #endif // Q_WS_MAEMO_5
345 }
346
347 void MainWindow::onPartLoadStart()
348 {
349     TRACE;
350 #ifdef Q_WS_MAEMO_5
351     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
352 #endif
353 }
354
355 void MainWindow::onPartLoadEnd(int index)
356 {
357     TRACE;
358     bool enablePrevious = false;
359     bool enableNext = false;
360     Book *book = Library::instance()->book(mCurrent);
361     if (book) {
362         if (index > 0) {
363             enablePrevious = true;
364         }
365         if (index < (book->parts.size() - 1)) {
366             enableNext = true;
367         }
368     }
369 #ifdef Q_WS_MAEMO_5
370     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
371 #endif // Q_WS_MAEMO_5
372 }
373
374 void MainWindow::onAddBookmark(const QString &note)
375 {
376     TRACE;
377     view->addBookmark(note);
378 }
379
380 void MainWindow::onGoToBookmark(int index)
381 {
382     TRACE;
383     Book *book = Library::instance()->book(mCurrent);
384     view->goToBookmark(book->bookmarks()[index]);
385 }
386
387 void MainWindow::showChapters()
388 {
389     Book *book = Library::instance()->book(mCurrent);
390     if (book) {
391         ChaptersDialog *chapters = new ChaptersDialog(book, this);
392         chapters->setWindowModality(Qt::WindowModal);
393         connect(chapters, SIGNAL(goToChapter(int)),
394                 this, SLOT(onGoToChapter(int)));
395         chapters->show();
396     }
397 }
398
399 void MainWindow::onGoToChapter(int index)
400 {
401     TRACE;
402
403     Book *book = Library::instance()->book(mCurrent);
404     if (book) {
405         int partIndex = book->partFromChapter(index);
406         if (partIndex != -1) {
407             view->goToBookmark(Book::Bookmark(partIndex, 0));
408         }
409     }
410 }
411
412 void MainWindow::timerEvent(QTimerEvent *event)
413 {
414     if (event->timerId() == preventBlankingTimer) {
415 #ifdef Q_WS_MAEMO_5
416         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
417                            MCE_REQUEST_IF, QDBusConnection::systemBus());
418         mce.call(MCE_PREVENT_BLANK_REQ);
419 #endif // Q_WS_MAEMO_5
420         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
421     }
422     AdopterWindow::timerEvent(event);
423 }
424
425 void MainWindow::resizeEvent(QResizeEvent *e)
426 {
427     TRACE;
428     progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
429 #if defined(Q_WS_MAEMO_5)
430     previousButton->setGeometry(0,
431         e->size().height() - toolBar->height() - TranslucentButton::pixels,
432         TranslucentButton::pixels, TranslucentButton::pixels);
433     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
434         TranslucentButton::pixels, TranslucentButton::pixels);
435 #elif defined(Q_OS_SYMBIAN)
436     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
437         TranslucentButton::pixels, TranslucentButton::pixels);
438     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
439         0, TranslucentButton::pixels, TranslucentButton::pixels);
440 #else
441     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
442         TranslucentButton::pixels, TranslucentButton::pixels);
443     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
444         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
445 #endif // Q_WS_MAEMO_5
446     qDebug() << "previousButton geometry" << previousButton->geometry();
447     previousButton->flash(1500);
448     nextButton->flash(1500);
449     QMainWindow::resizeEvent(e);
450 }
451
452 void MainWindow::about()
453 {
454     Dyalog *aboutDialog = new Dyalog(this, false);
455     aboutDialog->setWindowTitle(tr("About Dorian"));
456     QLabel *label = new QLabel(aboutDialog);
457     label->setTextFormat(Qt::RichText);
458     label->setOpenExternalLinks(true);
459     label->setWordWrap(true);
460     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
461         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
462         "Licensed under GNU General Public License, Version 3<br>"
463         "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
464         "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
465     aboutDialog->addWidget(label);
466     aboutDialog->addStretch();
467     aboutDialog->show();
468 }
469
470
471 void MainWindow::goToNextPage()
472 {
473     nextButton->flash(1500);
474     previousButton->flash(1500);
475     view->goNextPage();
476 }
477
478 void MainWindow::goToPreviousPage()
479 {
480     nextButton->flash(1500);
481     previousButton->flash(1500);
482     view->goPreviousPage();
483 }
484
485 void MainWindow::onBeginUpgrade(int total)
486 {
487     libraryProgress->setVisible(total > 0);
488     libraryProgress->setWindowTitle(tr("Upgrading library"));
489     libraryProgress->setMaximum(total);
490 }
491
492 void MainWindow::onUpgrading(const QString &path)
493 {
494     libraryProgress->setLabelText(tr("Upgrading %1").
495                                   arg(QFileInfo(path).fileName()));
496     libraryProgress->setValue(libraryProgress->value() + 1);
497 }
498
499 void MainWindow::onEndUpgrade()
500 {
501     libraryProgress->hide();
502     libraryProgress->reset();
503 }
504
505
506 void MainWindow::onBeginLoad(int total)
507 {
508     libraryProgress->setVisible(total > 0);
509     libraryProgress->setWindowTitle(tr("Loading library"));
510     libraryProgress->setMaximum(total);
511 }
512
513 void MainWindow::onLoading(const QString &path)
514 {
515     libraryProgress->setLabelText(tr("Loading %1").
516                                   arg(QFileInfo(path).fileName()));
517     libraryProgress->setValue(libraryProgress->value() + 1);
518 }
519
520 void MainWindow::onEndLoad()
521 {
522     libraryProgress->hide();
523     libraryProgress->reset();
524 }
525