Fix warning.
[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
122     // Handle loading book parts
123     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
124     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
125
126     // Handle progress
127     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
128
129     // Shadow window for full screen reading
130     fullScreenWindow = new FullScreenWindow(this);
131     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
132
133     // Handle settings changes
134     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
135             this, SLOT(onSettingsChanged(const QString &)));
136
137     // Handle book view buttons
138     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
139     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
140
141     // Adopt view, show window
142     showRegular();
143
144 #ifdef DORIAN_TEST_MODEL
145     (void)new ModelTest(Library::instance(), this);
146 #endif
147 }
148
149 void MainWindow::initialize()
150 {
151     TRACE;
152     Library *library = Library::instance();
153
154     // Upgrade library if needed, then load it
155     library->upgrade();
156     library->load();
157
158     // Load book on command line, or load last read book, or load default book
159     if (QCoreApplication::arguments().size() == 2) {
160         QString path = QCoreApplication::arguments()[1];
161         library->add(path);
162         QModelIndex index = library->find(path);
163         if (index.isValid()) {
164             library->setNowReading(index);
165         }
166     } else {
167         QModelIndex index = library->nowReading();
168         if (index.isValid()) {
169             library->setNowReading(index);
170         } else {
171             if (!library->rowCount()) {
172                 library->add(":/books/2BR02B.epub");
173             }
174             library->setNowReading(library->index(0));
175         }
176     }
177 }
178
179 void MainWindow::onCurrentBookChanged()
180 {
181     TRACE;
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 #endif
223     progress->flash();
224     nextButton->flash();
225     previousButton->flash();
226 }
227
228 void MainWindow::showBig()
229 {
230     TRACE;
231
232     // Re-parent children
233     leaveChildren();
234     QList<QWidget *> otherChildren;
235     otherChildren << progress << nextButton << previousButton;
236     fullScreenWindow->takeChildren(view, otherChildren);
237
238     // Adjust geometry of decorations
239     QRect screen = QApplication::desktop()->screenGeometry();
240     progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
241 #if defined(Q_WS_MAEMO_5)
242     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
243         TranslucentButton::pixels, TranslucentButton::pixels);
244 #else
245     nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
246         TranslucentButton::pixels, TranslucentButton::pixels);
247 #endif // Q_WS_MAEMO_5
248     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
249         TranslucentButton::pixels, TranslucentButton::pixels);
250
251 #ifdef Q_OS_SYMBIAN
252     hide();
253 #endif
254     fullScreenWindow->showFullScreen();
255 #ifdef Q_OS_SYMBIAN
256     fullScreenWindow->activateWindow();
257 #endif
258     progress->flash();
259     nextButton->flash();
260     previousButton->flash();
261 }
262
263 void MainWindow::setCurrentBook(const QModelIndex &current)
264 {
265     mCurrent = current;
266     Book *book = Library::instance()->book(current);
267     view->setBook(book);
268     setWindowTitle(book? book->shortName(): tr("Dorian"));
269 }
270
271 void MainWindow::showLibrary()
272 {
273     (new LibraryDialog(this))->show();
274 }
275
276 void MainWindow::showSettings()
277 {
278     (new SettingsWindow(this))->show();
279 }
280
281 void MainWindow::showInfo()
282 {
283     if (mCurrent.isValid()) {
284         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
285                 exec();
286     }
287 }
288
289 void MainWindow::showDevTools()
290 {
291     (new DevTools())->exec();
292 }
293
294 void MainWindow::showBookmarks()
295 {
296     Book *book = Library::instance()->book(mCurrent);
297     if (book) {
298         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
299         bookmarks->setWindowModality(Qt::WindowModal);
300         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
301                 this, SLOT(onAddBookmark(const QString &)));
302         connect(bookmarks, SIGNAL(goToBookmark(int)),
303                 this, SLOT(onGoToBookmark(int)));
304         bookmarks->show();
305     }
306 }
307
308 void MainWindow::closeEvent(QCloseEvent *event)
309 {
310     TRACE;
311     view->setLastBookmark();
312     event->accept();
313 }
314
315 void MainWindow::onSettingsChanged(const QString &key)
316 {
317 #if defined(Q_WS_MAEMO_5)
318     if (key == "orientation") {
319         QString value = Settings::instance()->value(key,
320             Platform::instance()->defaultOrientation()).toString();
321         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
322         if (value == "portrait") {
323             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
324             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
325         } else {
326             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
327             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
328         }
329     } else if (key == "lightson") {
330         bool enable = Settings::instance()->value(key, false).toBool();
331         qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
332         killTimer(preventBlankingTimer);
333         if (enable) {
334             preventBlankingTimer = startTimer(29 * 1000);
335         }
336     }
337 #else
338     Q_UNUSED(key);
339 #endif // Q_WS_MAEMO_5
340 }
341
342 void MainWindow::onPartLoadStart()
343 {
344     TRACE;
345     Platform::instance()->showBusy(this, true);
346 }
347
348 void MainWindow::onPartLoadEnd(int index)
349 {
350     TRACE;
351     bool enablePrevious = false;
352     bool enableNext = false;
353     Book *book = Library::instance()->book(mCurrent);
354     if (book) {
355         if (index > 0) {
356             enablePrevious = true;
357         }
358         if (index < (book->parts.size() - 1)) {
359             enableNext = true;
360         }
361     }
362     Platform::instance()->showBusy(this, false);
363 }
364
365 void MainWindow::onAddBookmark(const QString &note)
366 {
367     TRACE;
368     view->addBookmark(note);
369     Platform::instance()->information(tr("Bookmarked current position"), this);
370 }
371
372 void MainWindow::onGoToBookmark(int index)
373 {
374     TRACE;
375     Book *book = Library::instance()->book(mCurrent);
376     view->goToBookmark(book->bookmarks()[index]);
377 }
378
379 void MainWindow::showChapters()
380 {
381     Book *book = Library::instance()->book(mCurrent);
382     if (book) {
383         ChaptersDialog *chapters = new ChaptersDialog(book, this);
384         chapters->setWindowModality(Qt::WindowModal);
385         connect(chapters, SIGNAL(goToChapter(int)),
386                 this, SLOT(onGoToChapter(int)));
387         chapters->show();
388     }
389 }
390
391 void MainWindow::onGoToChapter(int index)
392 {
393     TRACE;
394
395     Book *book = Library::instance()->book(mCurrent);
396     if (book) {
397         QString fragment;
398         int partIndex = book->partFromChapter(index, fragment);
399         if (partIndex != -1) {
400             view->goToPart(partIndex, fragment);
401         }
402     }
403 }
404
405 void MainWindow::timerEvent(QTimerEvent *event)
406 {
407     if (event->timerId() == preventBlankingTimer) {
408 #ifdef Q_WS_MAEMO_5
409         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
410                            MCE_REQUEST_IF, QDBusConnection::systemBus());
411         mce.call(MCE_PREVENT_BLANK_REQ);
412 #endif // Q_WS_MAEMO_5
413         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
414     }
415     AdopterWindow::timerEvent(event);
416 }
417
418 void MainWindow::resizeEvent(QResizeEvent *e)
419 {
420     TRACE;
421     progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
422 #if defined(Q_WS_MAEMO_5)
423     previousButton->setGeometry(0,
424         e->size().height() - toolBar->height() - TranslucentButton::pixels,
425         TranslucentButton::pixels, TranslucentButton::pixels);
426     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
427         TranslucentButton::pixels, TranslucentButton::pixels);
428 #elif defined(Q_OS_SYMBIAN)
429     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
430         TranslucentButton::pixels, TranslucentButton::pixels);
431     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
432         0, TranslucentButton::pixels, TranslucentButton::pixels);
433 #else
434     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
435         TranslucentButton::pixels, TranslucentButton::pixels);
436     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
437         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
438 #endif // Q_WS_MAEMO_5
439     qDebug() << "previousButton geometry" << previousButton->geometry();
440
441 #ifdef Q_WS_MAEMO_5
442     // This is needed on Maemo, in order not to lose current reading position
443     // after orientation change
444     QTimer::singleShot(250, view, SLOT(restoreLastBookmark()));
445 #endif
446     previousButton->flash();
447     nextButton->flash();
448     QMainWindow::resizeEvent(e);
449 }
450
451 void MainWindow::about()
452 {
453     Dyalog *aboutDialog = new Dyalog(this, false);
454     aboutDialog->setWindowTitle(tr("About Dorian"));
455     QString version = Platform::instance()->version();
456     QLabel *label = new QLabel(aboutDialog);
457     label->setTextFormat(Qt::RichText);
458     label->setOpenExternalLinks(true);
459     label->setWordWrap(true);
460     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
461         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
462         "Licensed under GNU General Public License, Version 3<br>"
463         "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
464         "garage.maemo.org/projects/dorian</a>").arg(version));
465     aboutDialog->addWidget(label);
466     aboutDialog->addStretch();
467     aboutDialog->show();
468 }
469
470 void MainWindow::goToNextPage()
471 {
472     nextButton->flash();
473     previousButton->flash();
474     view->goNextPage();
475 }
476
477 void MainWindow::goToPreviousPage()
478 {
479     nextButton->flash();
480     previousButton->flash();
481     view->goPreviousPage();
482 }
483
484 void MainWindow::onBeginUpgrade(int total)
485 {
486     libraryProgress->setVisible(total > 0);
487     libraryProgress->setWindowTitle(tr("Upgrading library"));
488     libraryProgress->setMaximum(total);
489 }
490
491 void MainWindow::onUpgrading(const QString &path)
492 {
493     libraryProgress->setLabelText(tr("Upgrading %1").
494                                   arg(QFileInfo(path).fileName()));
495     libraryProgress->setValue(libraryProgress->value() + 1);
496 }
497
498 void MainWindow::onEndUpgrade()
499 {
500     libraryProgress->hide();
501     libraryProgress->reset();
502 }
503
504 void MainWindow::onBeginLoad(int total)
505 {
506     libraryProgress->setVisible(total > 0);
507     libraryProgress->setWindowTitle(tr("Loading library"));
508     libraryProgress->setMaximum(total);
509 }
510
511 void MainWindow::onLoading(const QString &path)
512 {
513     libraryProgress->setLabelText(tr("Loading %1").
514                                   arg(QFileInfo(path).fileName()));
515     libraryProgress->setValue(libraryProgress->value() + 1);
516 }
517
518 void MainWindow::onEndLoad()
519 {
520     libraryProgress->hide();
521     libraryProgress->reset();
522 }