Show splash screen on Symbian.
[dorian] / mainwindow.cpp
1 #include <QtGui>
2 #include <QEvent>
3
4 #ifdef Q_WS_MAEMO_5
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(this);
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 #ifndef Q_OS_SYMBIAN
122     connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
123     connect(library, SIGNAL(loading(const QString &)),
124             this, SLOT(onLoading(const QString &)));
125     connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
126 #endif
127     library->upgrade();
128     library->load();
129
130     // Load book on command line, or load last read book, or load default book
131     if (QCoreApplication::arguments().size() == 2) {
132         QString path = QCoreApplication::arguments()[1];
133         library->add(path);
134         QModelIndex index = library->find(path);
135         if (index.isValid()) {
136             library->setNowReading(index);
137         }
138     } else {
139         QModelIndex index = library->nowReading();
140         if (index.isValid()) {
141             library->setNowReading(index);
142         } else {
143             if (!library->rowCount()) {
144                 library->add(":/books/2BR02B.epub");
145             }
146             library->setNowReading(library->index(0));
147         }
148     }
149
150     // Handle loading book parts
151     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
152     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
153
154     // Handle progress
155     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
156
157     // Shadow window for full screen reading
158     fullScreenWindow = new FullScreenWindow(this);
159     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
160
161     // Handle settings changes
162     Settings *settings = Settings::instance();
163     connect(settings, SIGNAL(valueChanged(const QString &)),
164             this, SLOT(onSettingsChanged(const QString &)));
165     settings->setValue("orientation", settings->value("orientation"));
166     settings->setValue("lightson", settings->value("lightson"));
167
168     // Handle book view buttons
169     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
170     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
171
172     // Adopt view, show window
173     showRegular();
174
175 #ifdef DORIAN_TEST_MODEL
176     (void)new ModelTest(Library::instance(), this);
177 #endif
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();
228     previousButton->flash();
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();
263     previousButton->flash();
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     TRACE;
321     qDebug() << key;
322 #if defined(Q_WS_MAEMO_5)
323     if (key == "orientation") {
324         QString value = Settings::instance()->value(key).toString();
325         if (value == "portrait") {
326             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
327             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
328         } else {
329             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
330             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
331         }
332     } else if (key == "lightson") {
333         bool enable = Settings::instance()->value(key, false).toBool();
334         killTimer(preventBlankingTimer);
335         if (enable) {
336             preventBlankingTimer = startTimer(29 * 1000);
337         }
338     }
339 #endif // Q_WS_MAEMO_5
340 }
341
342 void MainWindow::onPartLoadStart()
343 {
344     TRACE;
345     Platform::showBusy(this, true);
346 }
347
348 void MainWindow::onPartLoadEnd(int index)
349 {
350     TRACE;
351     bool enablePrevious = false;
352     bool enableNext = false;
353     Book *book = Library::instance()->book(mCurrent);
354     if (book) {
355         if (index > 0) {
356             enablePrevious = true;
357         }
358         if (index < (book->parts.size() - 1)) {
359             enableNext = true;
360         }
361     }
362     Platform::showBusy(this, false);
363 }
364
365 void MainWindow::onAddBookmark(const QString &note)
366 {
367     TRACE;
368     view->addBookmark(note);
369     Platform::information(tr("Bookmarked current position"), this);
370 }
371
372 void MainWindow::onGoToBookmark(int index)
373 {
374     TRACE;
375     Book *book = Library::instance()->book(mCurrent);
376     view->goToBookmark(book->bookmarks()[index]);
377 }
378
379 void MainWindow::showChapters()
380 {
381     Book *book = Library::instance()->book(mCurrent);
382     if (book) {
383         ChaptersDialog *chapters = new ChaptersDialog(book, this);
384         chapters->setWindowModality(Qt::WindowModal);
385         connect(chapters, SIGNAL(goToChapter(int)),
386                 this, SLOT(onGoToChapter(int)));
387         chapters->show();
388     }
389 }
390
391 void MainWindow::onGoToChapter(int index)
392 {
393     TRACE;
394
395     Book *book = Library::instance()->book(mCurrent);
396     if (book) {
397         QString fragment;
398         int partIndex = book->partFromChapter(index, fragment);
399         if (partIndex != -1) {
400             view->goToPart(partIndex, fragment);
401         }
402     }
403 }
404
405 void MainWindow::timerEvent(QTimerEvent *event)
406 {
407     if (event->timerId() == preventBlankingTimer) {
408 #ifdef Q_WS_MAEMO_5
409         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
410                            MCE_REQUEST_IF, QDBusConnection::systemBus());
411         mce.call(MCE_PREVENT_BLANK_REQ);
412 #endif // Q_WS_MAEMO_5
413         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
414     }
415     AdopterWindow::timerEvent(event);
416 }
417
418 void MainWindow::resizeEvent(QResizeEvent *e)
419 {
420     TRACE;
421     progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
422 #if defined(Q_WS_MAEMO_5)
423     previousButton->setGeometry(0,
424         e->size().height() - toolBar->height() - TranslucentButton::pixels,
425         TranslucentButton::pixels, TranslucentButton::pixels);
426     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
427         TranslucentButton::pixels, TranslucentButton::pixels);
428 #elif defined(Q_OS_SYMBIAN)
429     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
430         TranslucentButton::pixels, TranslucentButton::pixels);
431     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
432         0, TranslucentButton::pixels, TranslucentButton::pixels);
433 #else
434     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
435         TranslucentButton::pixels, TranslucentButton::pixels);
436     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
437         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
438 #endif // Q_WS_MAEMO_5
439     qDebug() << "previousButton geometry" << previousButton->geometry();
440     previousButton->flash();
441     nextButton->flash();
442     QMainWindow::resizeEvent(e);
443 }
444
445 void MainWindow::about()
446 {
447     Dyalog *aboutDialog = new Dyalog(this, false);
448     aboutDialog->setWindowTitle(tr("About Dorian"));
449     QLabel *label = new QLabel(aboutDialog);
450     label->setTextFormat(Qt::RichText);
451     label->setOpenExternalLinks(true);
452     label->setWordWrap(true);
453     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
454         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
455         "Licensed under GNU General Public License, Version 3<br>"
456         "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
457         "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
458     aboutDialog->addWidget(label);
459     aboutDialog->addStretch();
460     aboutDialog->show();
461 }
462
463 void MainWindow::goToNextPage()
464 {
465     nextButton->flash();
466     previousButton->flash();
467     view->goNextPage();
468 }
469
470 void MainWindow::goToPreviousPage()
471 {
472     nextButton->flash();
473     previousButton->flash();
474     view->goPreviousPage();
475 }
476
477 void MainWindow::onBeginUpgrade(int total)
478 {
479     libraryProgress->setVisible(total > 0);
480     libraryProgress->setWindowTitle(tr("Upgrading library"));
481     libraryProgress->setMaximum(total);
482 }
483
484 void MainWindow::onUpgrading(const QString &path)
485 {
486     libraryProgress->setLabelText(tr("Upgrading %1").
487                                   arg(QFileInfo(path).fileName()));
488     libraryProgress->setValue(libraryProgress->value() + 1);
489 }
490
491 void MainWindow::onEndUpgrade()
492 {
493     libraryProgress->hide();
494     libraryProgress->reset();
495 }
496
497 void MainWindow::onBeginLoad(int total)
498 {
499     libraryProgress->setVisible(total > 0);
500     libraryProgress->setWindowTitle(tr("Loading library"));
501     libraryProgress->setMaximum(total);
502 }
503
504 void MainWindow::onLoading(const QString &path)
505 {
506     libraryProgress->setLabelText(tr("Loading %1").
507                                   arg(QFileInfo(path).fileName()));
508     libraryProgress->setValue(libraryProgress->value() + 1);
509 }
510
511 void MainWindow::onEndLoad()
512 {
513     libraryProgress->hide();
514     libraryProgress->reset();
515 }