Fix search dialog title. Add "Exit" to the Symbian menu. Attempt to fix
[dorian] / mainwindow.cpp
1 #include <QtGui>
2
3 #ifdef Q_WS_MAEMO_5
4 #   include <QtMaemo5/QMaemo5InformationBox>
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(central);
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     settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
166
167     // Handle book view buttons
168     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
169     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
170
171 #ifdef DORIAN_TEST_MODEL
172     (void)new ModelTest(Library::instance(), this);
173 #endif
174 }
175
176 MainWindow::~MainWindow()
177 {
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(1500);
228     previousButton->flash(1500);
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(1500);
263     previousButton->flash(1500);
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 #ifdef Q_WS_MAEMO_5
321     if (key == "orientation") {
322         QString value = Settings::instance()->value(key).toString();
323         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
324         if (value == "portrait") {
325             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
326             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
327         } else {
328             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
329             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
330         }
331     } else if (key == "lightson") {
332         bool enable = Settings::instance()->value(key, false).toBool();
333         qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
334         killTimer(preventBlankingTimer);
335         if (enable) {
336             preventBlankingTimer = startTimer(29 * 1000);
337         }
338     } else if (key == "usevolumekeys") {
339         bool value = Settings::instance()->value(key).toBool();
340         qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
341         grabZoomKeys(value);
342         fullScreenWindow->grabZoomKeys(value);
343     }
344 #else
345     Q_UNUSED(key);
346 #endif // Q_WS_MAEMO_5
347 }
348
349 void MainWindow::onPartLoadStart()
350 {
351     TRACE;
352 #ifdef Q_WS_MAEMO_5
353     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
354 #endif
355 }
356
357 void MainWindow::onPartLoadEnd(int index)
358 {
359     TRACE;
360     bool enablePrevious = false;
361     bool enableNext = false;
362     Book *book = Library::instance()->book(mCurrent);
363     if (book) {
364         if (index > 0) {
365             enablePrevious = true;
366         }
367         if (index < (book->parts.size() - 1)) {
368             enableNext = true;
369         }
370     }
371 #ifdef Q_WS_MAEMO_5
372     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
373 #endif // Q_WS_MAEMO_5
374 }
375
376 void MainWindow::onAddBookmark(const QString &note)
377 {
378     TRACE;
379     view->addBookmark(note);
380 }
381
382 void MainWindow::onGoToBookmark(int index)
383 {
384     TRACE;
385     Book *book = Library::instance()->book(mCurrent);
386     view->goToBookmark(book->bookmarks()[index]);
387 }
388
389 void MainWindow::showChapters()
390 {
391     Book *book = Library::instance()->book(mCurrent);
392     if (book) {
393         ChaptersDialog *chapters = new ChaptersDialog(book, this);
394         chapters->setWindowModality(Qt::WindowModal);
395         connect(chapters, SIGNAL(goToChapter(int)),
396                 this, SLOT(onGoToChapter(int)));
397         chapters->show();
398     }
399 }
400
401 void MainWindow::onGoToChapter(int index)
402 {
403     TRACE;
404
405     Book *book = Library::instance()->book(mCurrent);
406     if (book) {
407         int partIndex = book->partFromChapter(index);
408         if (partIndex != -1) {
409             view->goToBookmark(Book::Bookmark(partIndex, 0));
410         }
411     }
412 }
413
414 void MainWindow::timerEvent(QTimerEvent *event)
415 {
416     if (event->timerId() == preventBlankingTimer) {
417 #ifdef Q_WS_MAEMO_5
418         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
419                            MCE_REQUEST_IF, QDBusConnection::systemBus());
420         mce.call(MCE_PREVENT_BLANK_REQ);
421 #endif // Q_WS_MAEMO_5
422         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
423     }
424     AdopterWindow::timerEvent(event);
425 }
426
427 void MainWindow::resizeEvent(QResizeEvent *e)
428 {
429     TRACE;
430     progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
431 #if defined(Q_WS_MAEMO_5)
432     previousButton->setGeometry(0,
433         e->size().height() - toolBar->height() - TranslucentButton::pixels,
434         TranslucentButton::pixels, TranslucentButton::pixels);
435     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
436         TranslucentButton::pixels, TranslucentButton::pixels);
437 #elif defined(Q_OS_SYMBIAN)
438     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
439         TranslucentButton::pixels, TranslucentButton::pixels);
440     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
441         0, TranslucentButton::pixels, TranslucentButton::pixels);
442 #else
443     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
444         TranslucentButton::pixels, TranslucentButton::pixels);
445     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
446         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
447 #endif // Q_WS_MAEMO_5
448     qDebug() << "previousButton geometry" << previousButton->geometry();
449     previousButton->flash(1500);
450     nextButton->flash(1500);
451     QMainWindow::resizeEvent(e);
452 }
453
454 void MainWindow::about()
455 {
456     Dyalog *aboutDialog = new Dyalog(this, false);
457     aboutDialog->setWindowTitle(tr("About Dorian"));
458     QLabel *label = new QLabel(aboutDialog);
459     label->setTextFormat(Qt::RichText);
460     label->setOpenExternalLinks(true);
461     label->setWordWrap(true);
462     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
463         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
464         "Licensed under GNU General Public License, Version 3<br>"
465         "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
466         "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
467     aboutDialog->addWidget(label);
468     aboutDialog->addStretch();
469     aboutDialog->show();
470 }
471
472
473 void MainWindow::goToNextPage()
474 {
475     nextButton->flash(1500);
476     previousButton->flash(1500);
477     view->goNextPage();
478 }
479
480 void MainWindow::goToPreviousPage()
481 {
482     nextButton->flash(1500);
483     previousButton->flash(1500);
484     view->goPreviousPage();
485 }
486
487 void MainWindow::onBeginUpgrade(int total)
488 {
489     libraryProgress->setVisible(total > 0);
490     libraryProgress->setWindowTitle(tr("Upgrading library"));
491     libraryProgress->setMaximum(total);
492 }
493
494 void MainWindow::onUpgrading(const QString &path)
495 {
496     libraryProgress->setLabelText(tr("Upgrading %1").
497                                   arg(QFileInfo(path).fileName()));
498     libraryProgress->setValue(libraryProgress->value() + 1);
499 }
500
501 void MainWindow::onEndUpgrade()
502 {
503     libraryProgress->hide();
504     libraryProgress->reset();
505 }
506
507
508 void MainWindow::onBeginLoad(int total)
509 {
510     libraryProgress->setVisible(total > 0);
511     libraryProgress->setWindowTitle(tr("Loading library"));
512     libraryProgress->setMaximum(total);
513 }
514
515 void MainWindow::onLoading(const QString &path)
516 {
517     libraryProgress->setLabelText(tr("Loading %1").
518                                   arg(QFileInfo(path).fileName()));
519     libraryProgress->setValue(libraryProgress->value() + 1);
520 }
521
522 void MainWindow::onEndLoad()
523 {
524     libraryProgress->hide();
525     libraryProgress->reset();
526 }
527