4a5f266e8663d75488404af63afaa854885f89af
[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     // Dialogs
62     progress = new Progress(this);
63
64     // Tool bar actions
65
66 #ifdef Q_OS_SYMBIAN
67     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
68                                         "view-fullscreen", tr("Full screen"));
69 #endif
70
71     chaptersAction = addToolBarAction(this, SLOT(showChapters()),
72                                       "chapters", tr("Chapters"));
73     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
74                                        "bookmarks", tr("Bookmarks"));
75     infoAction = addToolBarAction(this, SLOT(showInfo()),
76                                   "info", tr("Book info"));
77     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
78                                      "library", tr("Library"));
79
80 #ifdef Q_WS_MAEMO_5
81     settingsAction = menuBar()->addAction(tr("Settings"));
82     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
83     devToolsAction = menuBar()->addAction(tr("Developer"));
84     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
85     QAction *aboutAction = menuBar()->addAction(tr("About"));
86     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
87 #else
88     settingsAction = addToolBarAction(this, SLOT(showSettings()),
89                                       "preferences-system", tr("Settings"));
90     devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
91                                       "developer", tr("Developer"));
92     addToolBarAction(this, SLOT(about()), "about", tr("About"));
93 #endif // Q_WS_MAEMO_5
94
95 #ifndef Q_OS_SYMBIAN
96     addToolBarSpace();
97     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
98                                         "view-fullscreen", tr("Full screen"));
99 #else
100     (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"));
101 #endif
102
103     // Buttons on top of the book view
104     previousButton = new TranslucentButton("back", this);
105     nextButton = new TranslucentButton("forward", this);
106
107     // Handle model changes
108     connect(Library::instance(), SIGNAL(nowReadingChanged()),
109             this, SLOT(onCurrentBookChanged()));
110
111     // Load library, upgrade it if needed
112     libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
113     Library *library = Library::instance();
114     connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
115     connect(library, SIGNAL(upgrading(const QString &)),
116             this, SLOT(onUpgrading(const QString &)));
117     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
118
119     // Handle loading book parts
120     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
121     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
122
123     // Handle progress
124     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
125
126     // Shadow window for full screen reading
127     fullScreenWindow = new FullScreenWindow(this);
128     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
129
130     // Handle settings changes
131     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
132             this, SLOT(onSettingsChanged(const QString &)));
133
134     // Handle book view buttons
135     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
136     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
137
138     // Adopt view, show window
139     showRegular();
140
141 #ifdef DORIAN_TEST_MODEL
142     (void)new ModelTest(Library::instance(), this);
143 #endif
144 }
145
146 void MainWindow::initialize()
147 {
148     TRACE;
149     Library *library = Library::instance();
150
151     // Upgrade library if needed, then load it
152     library->upgrade();
153     library->load();
154
155     // Load book on command line, or load last read book, or load default book
156     if (QCoreApplication::arguments().size() == 2) {
157         QString path = QCoreApplication::arguments()[1];
158         library->add(path);
159         QModelIndex index = library->find(path);
160         if (index.isValid()) {
161             library->setNowReading(index);
162         }
163     } else {
164         QModelIndex index = library->nowReading();
165         if (index.isValid()) {
166             library->setNowReading(index);
167         } else {
168             if (!library->rowCount()) {
169                 library->add(":/books/2BR02B.epub");
170             }
171             library->setNowReading(library->index(0));
172         }
173     }
174 }
175
176 void MainWindow::onCurrentBookChanged()
177 {
178     TRACE;
179     setCurrentBook(Library::instance()->nowReading());
180 }
181
182 void MainWindow::showRegular()
183 {
184     TRACE;
185
186     // Re-parent children
187     fullScreenWindow->leaveChildren();
188     QList<QWidget *> otherChildren;
189     otherChildren << progress << previousButton << nextButton;
190     takeChildren(view, otherChildren);
191
192     // Adjust geometry of decorations
193     QRect geo = geometry();
194     progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
195 #if defined(Q_WS_MAEMO_5)
196     previousButton->setGeometry(0,
197         geo.height() - toolBar->height() - TranslucentButton::pixels,
198         TranslucentButton::pixels, TranslucentButton::pixels);
199     nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
200         TranslucentButton::pixels, TranslucentButton::pixels);
201 #elif defined(Q_OS_SYMBIAN)
202     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
203         TranslucentButton::pixels, TranslucentButton::pixels);
204     nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
205         0, TranslucentButton::pixels, TranslucentButton::pixels);
206 #else
207     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
208         TranslucentButton::pixels, TranslucentButton::pixels);
209     nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
210         toolBar->height(), TranslucentButton::pixels,
211         TranslucentButton::pixels);
212 #endif // Q_WS_MAEMO_5
213     qDebug() << "previousButton geometry" << previousButton->geometry();
214
215     fullScreenWindow->hide();
216     show();
217 #if defined(Q_OS_SYMBIAN)
218     activateWindow();
219 #endif
220     progress->flash();
221     nextButton->flash();
222     previousButton->flash();
223 }
224
225 void MainWindow::showBig()
226 {
227     TRACE;
228
229     // Re-parent children
230     leaveChildren();
231     QList<QWidget *> otherChildren;
232     otherChildren << progress << nextButton << previousButton;
233     fullScreenWindow->takeChildren(view, otherChildren);
234
235     // Adjust geometry of decorations
236     QRect screen = QApplication::desktop()->screenGeometry();
237     progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
238 #if defined(Q_WS_MAEMO_5)
239     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
240         TranslucentButton::pixels, TranslucentButton::pixels);
241 #else
242     nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
243         TranslucentButton::pixels, TranslucentButton::pixels);
244 #endif // Q_WS_MAEMO_5
245     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
246         TranslucentButton::pixels, TranslucentButton::pixels);
247
248 #ifdef Q_OS_SYMBIAN
249     hide();
250 #endif
251     fullScreenWindow->showFullScreen();
252 #ifdef Q_OS_SYMBIAN
253     fullScreenWindow->activateWindow();
254 #endif
255     progress->flash();
256     nextButton->flash();
257     previousButton->flash();
258 }
259
260 void MainWindow::setCurrentBook(const QModelIndex &current)
261 {
262     mCurrent = current;
263     Book *book = Library::instance()->book(current);
264     view->setBook(book);
265     setWindowTitle(book? book->shortName(): tr("Dorian"));
266 }
267
268 void MainWindow::showLibrary()
269 {
270     (new LibraryDialog(this))->show();
271 }
272
273 void MainWindow::showSettings()
274 {
275     (new SettingsWindow(this))->show();
276 }
277
278 void MainWindow::showInfo()
279 {
280     if (mCurrent.isValid()) {
281         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
282                 exec();
283     }
284 }
285
286 void MainWindow::showDevTools()
287 {
288     (new DevTools())->exec();
289 }
290
291 void MainWindow::showBookmarks()
292 {
293     Book *book = Library::instance()->book(mCurrent);
294     if (book) {
295         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
296         bookmarks->setWindowModality(Qt::WindowModal);
297         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
298                 this, SLOT(onAddBookmark(const QString &)));
299         connect(bookmarks, SIGNAL(goToBookmark(int)),
300                 this, SLOT(onGoToBookmark(int)));
301         bookmarks->show();
302     }
303 }
304
305 void MainWindow::closeEvent(QCloseEvent *event)
306 {
307     TRACE;
308     view->setLastBookmark();
309     event->accept();
310 }
311
312 void MainWindow::onSettingsChanged(const QString &key)
313 {
314 #if defined(Q_WS_MAEMO_5)
315     if (key == "orientation") {
316         QString value = Settings::instance()->value(key,
317             Platform::instance()->defaultOrientation()).toString();
318         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
319         if (value == "portrait") {
320             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
321             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
322         } else {
323             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
324             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
325         }
326     } else if (key == "lightson") {
327         bool enable = Settings::instance()->value(key, false).toBool();
328         qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
329         killTimer(preventBlankingTimer);
330         if (enable) {
331             preventBlankingTimer = startTimer(29 * 1000);
332         }
333     }
334 #else
335     Q_UNUSED(key);
336 #endif // Q_WS_MAEMO_5
337 }
338
339 void MainWindow::onPartLoadStart()
340 {
341     TRACE;
342     Platform::instance()->showBusy(this, true);
343 }
344
345 void MainWindow::onPartLoadEnd(int index)
346 {
347     TRACE;
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     Platform::instance()->showBusy(this, false);
360 }
361
362 void MainWindow::onAddBookmark(const QString &note)
363 {
364     TRACE;
365     view->addBookmark(note);
366     Platform::instance()->information(tr("Bookmarked current position"), this);
367 }
368
369 void MainWindow::onGoToBookmark(int index)
370 {
371     TRACE;
372     Book *book = Library::instance()->book(mCurrent);
373     view->goToBookmark(book->bookmarks()[index]);
374 }
375
376 void MainWindow::showChapters()
377 {
378     Book *book = Library::instance()->book(mCurrent);
379     if (book) {
380         ChaptersDialog *chapters = new ChaptersDialog(book, this);
381         chapters->setWindowModality(Qt::WindowModal);
382         connect(chapters, SIGNAL(goToChapter(int)),
383                 this, SLOT(onGoToChapter(int)));
384         chapters->show();
385     }
386 }
387
388 void MainWindow::onGoToChapter(int index)
389 {
390     TRACE;
391
392     Book *book = Library::instance()->book(mCurrent);
393     if (book) {
394         QString fragment;
395         int partIndex = book->partFromChapter(index, fragment);
396         if (partIndex != -1) {
397             view->goToPart(partIndex, fragment);
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;
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
438 #ifdef Q_WS_MAEMO_5
439     // This is needed on Maemo, in order not to lose current reading position
440     // after orientation change
441     QTimer::singleShot(250, view, SLOT(restoreLastBookmark()));
442 #endif
443     previousButton->flash();
444     nextButton->flash();
445     QMainWindow::resizeEvent(e);
446 }
447
448 void MainWindow::about()
449 {
450     Dyalog *aboutDialog = new Dyalog(this, false);
451     aboutDialog->setWindowTitle(tr("About Dorian"));
452     QString version = Platform::instance()->version();
453     QLabel *label = new QLabel(aboutDialog);
454     label->setTextFormat(Qt::RichText);
455     label->setOpenExternalLinks(true);
456     label->setWordWrap(true);
457     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
458         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
459         "Licensed under GNU General Public License, Version 3<br>"
460         "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
461         "garage.maemo.org/projects/dorian</a>").arg(version));
462     aboutDialog->addWidget(label);
463     aboutDialog->addStretch();
464     aboutDialog->show();
465 }
466
467 void MainWindow::goToNextPage()
468 {
469     nextButton->flash();
470     previousButton->flash();
471     view->goNextPage();
472 }
473
474 void MainWindow::goToPreviousPage()
475 {
476     nextButton->flash();
477     previousButton->flash();
478     view->goPreviousPage();
479 }
480
481 void MainWindow::onBeginUpgrade(int total)
482 {
483     libraryProgress->setVisible(total > 0);
484     libraryProgress->setWindowTitle(tr("Upgrading library"));
485     libraryProgress->setMaximum(total);
486 }
487
488 void MainWindow::onUpgrading(const QString &path)
489 {
490     libraryProgress->setLabelText(tr("Upgrading %1").
491                                   arg(QFileInfo(path).fileName()));
492     libraryProgress->setValue(libraryProgress->value() + 1);
493 }
494
495 void MainWindow::onEndUpgrade()
496 {
497     libraryProgress->hide();
498     libraryProgress->reset();
499 }
500
501 void MainWindow::onBeginLoad(int total)
502 {
503     libraryProgress->setVisible(total > 0);
504     libraryProgress->setWindowTitle(tr("Loading library"));
505     libraryProgress->setMaximum(total);
506 }
507
508 void MainWindow::onLoading(const QString &path)
509 {
510     libraryProgress->setLabelText(tr("Loading %1").
511                                   arg(QFileInfo(path).fileName()));
512     libraryProgress->setValue(libraryProgress->value() + 1);
513 }
514
515 void MainWindow::onEndLoad()
516 {
517     libraryProgress->hide();
518     libraryProgress->reset();
519 }