Updated library dialog works.
[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         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 #if defined(Q_WS_MAEMO_5)
321     if (key == "orientation") {
322         QString value = Settings::instance()->value(key,
323             Platform::instance()->defaultOrientation()).toString();
324         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
325         if (value == "portrait") {
326             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
327             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
328             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
329                                            false);
330             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
331         } else {
332             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
333             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
334             fullScreenWindow->setAttribute(Qt::WA_Maemo5PortraitOrientation,
335                                            false);
336             fullScreenWindow->setAttribute(Qt::WA_Maemo5LandscapeOrientation,
337                                            true);
338         }
339     } else if (key == "lightson") {
340         bool enable = Settings::instance()->value(key, false).toBool();
341         qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
342         killTimer(preventBlankingTimer);
343         if (enable) {
344             preventBlankingTimer = startTimer(29 * 1000);
345         }
346     }
347 #else
348     Q_UNUSED(key);
349 #endif // Q_WS_MAEMO_5
350 }
351
352 void MainWindow::onPartLoadStart()
353 {
354     TRACE;
355     Platform::instance()->showBusy(this, true);
356 }
357
358 void MainWindow::onPartLoadEnd(int index)
359 {
360     TRACE;
361     bool enablePrevious = false;
362     bool enableNext = false;
363     Book *book = Library::instance()->book(mCurrent);
364     if (book) {
365         if (index > 0) {
366             enablePrevious = true;
367         }
368         if (index < (book->parts.size() - 1)) {
369             enableNext = true;
370         }
371     }
372     Platform::instance()->showBusy(this, false);
373 }
374
375 void MainWindow::onAddBookmark(const QString &note)
376 {
377     TRACE;
378     view->addBookmark(note);
379     Platform::instance()->information(tr("Bookmarked current position"), this);
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         connect(chapters, SIGNAL(goToChapter(int)),
395                 this, SLOT(onGoToChapter(int)));
396         chapters->show();
397     }
398 }
399
400 void MainWindow::onGoToChapter(int index)
401 {
402     TRACE;
403
404     Book *book = Library::instance()->book(mCurrent);
405     if (book) {
406         QString fragment;
407         int partIndex = book->partFromChapter(index, fragment);
408         if (partIndex != -1) {
409             view->goToPart(partIndex, fragment);
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
431     if (hasChild(progress)) {
432         qDebug() << "To" << e->size();
433         int y = e->size().height() - progress->thickness();
434 #if defined(Q_WS_MAEMO_5)
435         y -= toolBar->height();
436 #endif
437         progress->setGeometry(0, y, e->size().width(), y + progress->thickness());
438
439 #if defined(Q_WS_MAEMO_5)
440         previousButton->setGeometry(0,
441             e->size().height() - toolBar->height() - TranslucentButton::pixels,
442             TranslucentButton::pixels, TranslucentButton::pixels);
443         nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
444             TranslucentButton::pixels, TranslucentButton::pixels);
445 #elif defined(Q_OS_SYMBIAN)
446         previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
447             TranslucentButton::pixels, TranslucentButton::pixels);
448         nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
449             0, TranslucentButton::pixels, TranslucentButton::pixels);
450 #else
451         previousButton->setGeometry(0,
452             e->size().height() - TranslucentButton::pixels,
453             TranslucentButton::pixels, TranslucentButton::pixels);
454         nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
455             toolBar->height(), TranslucentButton::pixels,
456             TranslucentButton::pixels);
457 #endif // Q_WS_MAEMO_5
458
459 #ifdef Q_WS_MAEMO_5
460         // This is needed on Maemo, in order not to lose current reading position
461         // after orientation change
462         QTimer::singleShot(250, view, SLOT(restoreLastBookmark()));
463 #endif
464         previousButton->flash();
465         nextButton->flash();
466     }
467     QMainWindow::resizeEvent(e);
468 }
469
470 void MainWindow::about()
471 {
472     Dyalog *aboutDialog = new Dyalog(this, false);
473     aboutDialog->setWindowTitle(tr("About Dorian"));
474     QString version = Platform::instance()->version();
475     QLabel *label = new QLabel(aboutDialog);
476     label->setTextFormat(Qt::RichText);
477     label->setOpenExternalLinks(true);
478     label->setWordWrap(true);
479     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
480         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
481         "Licensed under GNU General Public License, Version 3<br>"
482         "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
483         "garage.maemo.org/projects/dorian</a>").arg(version));
484     aboutDialog->addWidget(label);
485     aboutDialog->addStretch();
486     aboutDialog->show();
487 }
488
489 void MainWindow::goToNextPage()
490 {
491     nextButton->flash();
492     previousButton->flash();
493     view->goNextPage();
494 }
495
496 void MainWindow::goToPreviousPage()
497 {
498     nextButton->flash();
499     previousButton->flash();
500     view->goPreviousPage();
501 }
502
503 void MainWindow::onBeginUpgrade(int total)
504 {
505     libraryProgress->setVisible(total > 0);
506     libraryProgress->setWindowTitle(tr("Upgrading library"));
507     libraryProgress->setMaximum(total);
508 }
509
510 void MainWindow::onUpgrading(const QString &path)
511 {
512     libraryProgress->setLabelText(tr("Upgrading %1").
513                                   arg(QFileInfo(path).fileName()));
514     libraryProgress->setValue(libraryProgress->value() + 1);
515 }
516
517 void MainWindow::onEndUpgrade()
518 {
519     libraryProgress->hide();
520     libraryProgress->reset();
521 }
522
523 void MainWindow::onBeginLoad(int total)
524 {
525     libraryProgress->setVisible(total > 0);
526     libraryProgress->setWindowTitle(tr("Loading library"));
527     libraryProgress->setMaximum(total);
528 }
529
530 void MainWindow::onLoading(const QString &path)
531 {
532     libraryProgress->setLabelText(tr("Loading %1").
533                                   arg(QFileInfo(path).fileName()));
534     libraryProgress->setValue(libraryProgress->value() + 1);
535 }
536
537 void MainWindow::onEndLoad()
538 {
539     libraryProgress->hide();
540     libraryProgress->reset();
541 }