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