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