Nothing, really.
[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     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
166     // Handle book view buttons
167     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
168     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
169
170     // Adopt view, show window
171     showRegular();
172
173 #ifdef DORIAN_TEST_MODEL
174     (void)new ModelTest(Library::instance(), this);
175 #endif
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();
226     previousButton->flash();
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();
261     previousButton->flash();
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     TRACE;
319     qDebug() << key;
320 #if defined(Q_WS_MAEMO_5)
321     if (key == "orientation") {
322         QString value = Settings::instance()->value(key).toString();
323         if (value == "portrait") {
324             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
325             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
326         } else {
327             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
328             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
329         }
330     } else if (key == "lightson") {
331         bool enable = Settings::instance()->value(key, false).toBool();
332         killTimer(preventBlankingTimer);
333         if (enable) {
334             preventBlankingTimer = startTimer(29 * 1000);
335         }
336     }
337 #endif // Q_WS_MAEMO_5
338 }
339
340 void MainWindow::onPartLoadStart()
341 {
342     TRACE;
343     Platform::showBusy(this, true);
344 }
345
346 void MainWindow::onPartLoadEnd(int index)
347 {
348     TRACE;
349     bool enablePrevious = false;
350     bool enableNext = false;
351     Book *book = Library::instance()->book(mCurrent);
352     if (book) {
353         if (index > 0) {
354             enablePrevious = true;
355         }
356         if (index < (book->parts.size() - 1)) {
357             enableNext = true;
358         }
359     }
360     Platform::showBusy(this, false);
361 }
362
363 void MainWindow::onAddBookmark(const QString &note)
364 {
365     TRACE;
366     view->addBookmark(note);
367     Platform::information(tr("Bookmarked current position"), this);
368 }
369
370 void MainWindow::onGoToBookmark(int index)
371 {
372     TRACE;
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;
392
393     Book *book = Library::instance()->book(mCurrent);
394     if (book) {
395         QString fragment;
396         int partIndex = book->partFromChapter(index, fragment);
397         if (partIndex != -1) {
398             view->goToPart(partIndex, fragment);
399         }
400     }
401 }
402
403 void MainWindow::timerEvent(QTimerEvent *event)
404 {
405     if (event->timerId() == preventBlankingTimer) {
406 #ifdef Q_WS_MAEMO_5
407         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
408                            MCE_REQUEST_IF, QDBusConnection::systemBus());
409         mce.call(MCE_PREVENT_BLANK_REQ);
410 #endif // Q_WS_MAEMO_5
411         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
412     }
413     AdopterWindow::timerEvent(event);
414 }
415
416 void MainWindow::resizeEvent(QResizeEvent *e)
417 {
418     TRACE;
419     progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
420 #if defined(Q_WS_MAEMO_5)
421     previousButton->setGeometry(0,
422         e->size().height() - toolBar->height() - TranslucentButton::pixels,
423         TranslucentButton::pixels, TranslucentButton::pixels);
424     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
425         TranslucentButton::pixels, TranslucentButton::pixels);
426 #elif defined(Q_OS_SYMBIAN)
427     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
428         TranslucentButton::pixels, TranslucentButton::pixels);
429     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
430         0, TranslucentButton::pixels, TranslucentButton::pixels);
431 #else
432     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
433         TranslucentButton::pixels, TranslucentButton::pixels);
434     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
435         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
436 #endif // Q_WS_MAEMO_5
437     qDebug() << "previousButton geometry" << previousButton->geometry();
438     previousButton->flash();
439     nextButton->flash();
440     QMainWindow::resizeEvent(e);
441 }
442
443 void MainWindow::about()
444 {
445     Dyalog *aboutDialog = new Dyalog(this, false);
446     aboutDialog->setWindowTitle(tr("About Dorian"));
447     QLabel *label = new QLabel(aboutDialog);
448     label->setTextFormat(Qt::RichText);
449     label->setOpenExternalLinks(true);
450     label->setWordWrap(true);
451     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
452         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
453         "Licensed under GNU General Public License, Version 3<br>"
454         "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
455         "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
456     aboutDialog->addWidget(label);
457     aboutDialog->addStretch();
458     aboutDialog->show();
459 }
460
461 void MainWindow::goToNextPage()
462 {
463     nextButton->flash();
464     previousButton->flash();
465     view->goNextPage();
466 }
467
468 void MainWindow::goToPreviousPage()
469 {
470     nextButton->flash();
471     previousButton->flash();
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 void MainWindow::onBeginLoad(int total)
496 {
497     libraryProgress->setVisible(total > 0);
498     libraryProgress->setWindowTitle(tr("Loading library"));
499     libraryProgress->setMaximum(total);
500 }
501
502 void MainWindow::onLoading(const QString &path)
503 {
504     libraryProgress->setLabelText(tr("Loading %1").
505                                   arg(QFileInfo(path).fileName()));
506     libraryProgress->setValue(libraryProgress->value() + 1);
507 }
508
509 void MainWindow::onEndLoad()
510 {
511     libraryProgress->hide();
512     libraryProgress->reset();
513 }