Show progress while searching and downloading.
[dorian] / mainwindow.cpp
1 #include <QtGui>
2 #include <QtDebug>
3 #include <QDir>
4 #include <QApplication>
5 #include <QFileInfo>
6 #include <QStringList>
7 #include <QWebView>
8 #include <QWebFrame>
9
10 #ifdef Q_WS_MAEMO_5
11 #   include <QtMaemo5/QMaemo5InformationBox>
12 #   include <QtDBus>
13 #   include <QtGui/QX11Info>
14 #   include <X11/Xlib.h>
15 #   include <X11/Xatom.h>
16 #   include <mce/mode-names.h>
17 #   include <mce/dbus-names.h>
18 #endif // Q_WS_MAEMO_5
19
20 #include "bookview.h"
21 #include "book.h"
22 #include "library.h"
23 #include "infodialog.h"
24 #include "librarydialog.h"
25 #include "devtools.h"
26 #include "mainwindow.h"
27 #include "settingswindow.h"
28 #include "bookmarksdialog.h"
29 #include "settings.h"
30 #include "chaptersdialog.h"
31 #include "fullscreenwindow.h"
32 #include "trace.h"
33 #include "bookfinder.h"
34 #include "progress.h"
35 #include "dyalog.h"
36 #include "translucentbutton.h"
37 #include "platform.h"
38 #include "progressdialog.h"
39
40 #ifdef DORIAN_TEST_MODEL
41 #   include "modeltest.h"
42 #endif
43
44 const int DORIAN_PROGRESS_HEIGHT = 17;
45
46 MainWindow::MainWindow(QWidget *parent):
47     AdopterWindow(parent), view(0), preventBlankingTimer(-1)
48 {
49     Trace t("MainWindow::MainWindow");
50 #ifdef Q_WS_MAEMO_5
51     setAttribute(Qt::WA_Maemo5StackedWindow, true);
52 #endif
53     setWindowTitle("Dorian");
54
55     // Central widget. Must be an intermediate, because the book view widget
56     // can be re-parented later
57     QFrame *central = new QFrame(this);
58     QVBoxLayout *layout = new QVBoxLayout(central);
59     layout->setMargin(0);
60     central->setLayout(layout);
61     setCentralWidget(central);
62
63     // Book view
64     view = new BookView(central);
65     view->show();
66     layout->addWidget(view);
67
68     // Progress
69     progress = new Progress(central);
70
71     // Settings dialog
72     settings = new QDialog(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"));
83     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
84                                        "bookmarks", tr("Bookmarks"));
85     infoAction = addToolBarAction(this, SLOT(showInfo()),
86                                   "info", tr("Book info"));
87     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
88                                      "library", tr("Library"));
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 #endif
110
111     // Buttons on top of the book view
112     previousButton = new TranslucentButton("back", this);
113     nextButton = new TranslucentButton("forward", this);
114
115     // Handle model changes
116     connect(Library::instance(), SIGNAL(nowReadingChanged()),
117             this, SLOT(onCurrentBookChanged()));
118
119     // Load library, upgrade it if needed
120     libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
121     Library *library = Library::instance();
122     connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
123     connect(library, SIGNAL(upgrading(const QString &)),
124             this, SLOT(onUpgrading(const QString &)));
125     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
126     connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
127     connect(library, SIGNAL(loading(const QString &)),
128             this, SLOT(onLoading(const QString &)));
129     connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
130     library->upgrade();
131     library->load();
132
133     // Load book on command line, or load last read book, or load default book
134     if (QCoreApplication::arguments().size() == 2) {
135         QString path = QCoreApplication::arguments()[1];
136         library->add(path);
137         QModelIndex index = library->find(path);
138         if (index.isValid()) {
139             library->setNowReading(index);
140         }
141     } else {
142         QModelIndex index = library->nowReading();
143         if (index.isValid()) {
144             library->setNowReading(index);
145         } else {
146             if (!library->rowCount()) {
147                 library->add(":/books/2BR02B.epub");
148             }
149             library->setNowReading(library->index(0));
150         }
151     }
152
153     // Handle loading book parts
154     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
155     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
156
157     // Handle progress
158     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
159
160     // Shadow window for full screen reading
161     fullScreenWindow = new FullScreenWindow(this);
162     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
163
164     // Handle settings changes
165     Settings *settings = Settings::instance();
166     connect(settings, SIGNAL(valueChanged(const QString &)),
167             this, SLOT(onSettingsChanged(const QString &)));
168     settings->setValue("orientation", settings->value("orientation"));
169     settings->setValue("lightson", settings->value("lightson"));
170     settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
171
172     // Handle book view buttons
173     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
174     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
175
176 #ifdef DORIAN_TEST_MODEL
177     (void)new ModelTest(Library::instance(), this);
178 #endif
179 }
180
181 MainWindow::~MainWindow()
182 {
183 }
184
185 void MainWindow::onCurrentBookChanged()
186 {
187     setCurrentBook(Library::instance()->nowReading());
188 }
189
190 void MainWindow::showRegular()
191 {
192     Trace t("MainWindow::showRegular");
193     fullScreenWindow->hide();
194     fullScreenWindow->leaveChildren();
195
196     QList<QWidget *> otherChildren;
197     otherChildren << progress << previousButton << nextButton;
198     takeChildren(view, otherChildren);
199     QRect geo = geometry();
200     progress->setGeometry(0, 0, geo.width(), DORIAN_PROGRESS_HEIGHT);
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, TranslucentButton::pixels);
217 #endif // Q_WS_MAEMO_5
218     qDebug() << "previousButton geometry" << previousButton->geometry();
219     progress->flash();
220     nextButton->show();
221     previousButton->show();
222     nextButton->flash(1500);
223     previousButton->flash(1500);
224 }
225
226 void MainWindow::showBig()
227 {
228     Trace t("MainWindow::showBig");
229     leaveChildren();
230     QList<QWidget *> otherChildren;
231     otherChildren << progress << nextButton << previousButton;
232     QRect screen = QApplication::desktop()->screenGeometry();
233     progress->setGeometry(0, 0, screen.width(), DORIAN_PROGRESS_HEIGHT);
234 #if defined(Q_WS_MAEMO_5)
235     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
236         TranslucentButton::pixels, TranslucentButton::pixels);
237 #else
238     nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
239                             TranslucentButton::pixels, TranslucentButton::pixels);
240 #endif // Q_WS_MAEMO_5
241     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
242         TranslucentButton::pixels, TranslucentButton::pixels);
243
244     fullScreenWindow->takeChildren(view, otherChildren);
245     fullScreenWindow->showFullScreen();
246     progress->flash();
247     nextButton->flash(1500);
248     previousButton->flash(1500);
249 }
250
251 void MainWindow::setCurrentBook(const QModelIndex &current)
252 {
253     mCurrent = current;
254     Book *book = Library::instance()->book(current);
255     view->setBook(book);
256     setWindowTitle(book? book->shortName(): tr("Dorian"));
257 }
258
259 void MainWindow::showLibrary()
260 {
261     (new LibraryDialog(this))->show();
262 }
263
264 void MainWindow::showSettings()
265 {
266     (new SettingsWindow(this))->show();
267 }
268
269 void MainWindow::showInfo()
270 {
271     if (mCurrent.isValid()) {
272         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
273                 exec();
274     }
275 }
276
277 void MainWindow::showDevTools()
278 {
279     (new DevTools())->exec();
280 }
281
282 void MainWindow::showBookmarks()
283 {
284     Book *book = Library::instance()->book(mCurrent);
285     if (book) {
286         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
287         bookmarks->setWindowModality(Qt::WindowModal);
288         connect(bookmarks, SIGNAL(addBookmark(const QString &)),
289                 this, SLOT(onAddBookmark(const QString &)));
290         connect(bookmarks, SIGNAL(goToBookmark(int)),
291                 this, SLOT(onGoToBookmark(int)));
292         bookmarks->show();
293     }
294 }
295
296 void MainWindow::closeEvent(QCloseEvent *event)
297 {
298     Trace t("MainWindow::closeEvent");
299     view->setLastBookmark();
300     event->accept();
301 }
302
303 void MainWindow::onSettingsChanged(const QString &key)
304 {
305 #ifdef Q_WS_MAEMO_5
306     if (key == "orientation") {
307         QString value = Settings::instance()->value(key).toString();
308         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
309         if (value == "portrait") {
310             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
311             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
312         } else {
313             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
314             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
315         }
316     } else if (key == "lightson") {
317         bool enable = Settings::instance()->value(key, false).toBool();
318         qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
319         killTimer(preventBlankingTimer);
320         if (enable) {
321             preventBlankingTimer = startTimer(29 * 1000);
322         }
323     } else if (key == "usevolumekeys") {
324         bool value = Settings::instance()->value(key).toBool();
325         qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
326         grabZoomKeys(value);
327         fullScreenWindow->grabZoomKeys(value);
328     }
329 #else
330     Q_UNUSED(key);
331 #endif // Q_WS_MAEMO_5
332 }
333
334 void MainWindow::onPartLoadStart()
335 {
336     Trace t("MainWindow::onPartLoadStart");
337 #ifdef Q_WS_MAEMO_5
338     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
339 #endif
340 }
341
342 void MainWindow::onPartLoadEnd(int index)
343 {
344     Trace t("MainWindow::onPartLoadEnd");
345     bool enablePrevious = false;
346     bool enableNext = false;
347     Book *book = Library::instance()->book(mCurrent);
348     if (book) {
349         if (index > 0) {
350             enablePrevious = true;
351         }
352         if (index < (book->parts.size() - 1)) {
353             enableNext = true;
354         }
355     }
356 #ifdef Q_WS_MAEMO_5
357     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
358 #endif // Q_WS_MAEMO_5
359 }
360
361 void MainWindow::onAddBookmark(const QString &note)
362 {
363     Trace t("MainWindow:onAddBookmark");
364     view->addBookmark(note);
365 }
366
367 void MainWindow::onGoToBookmark(int index)
368 {
369     Trace t("MainWindow::onGoToBookmark");
370     Book *book = Library::instance()->book(mCurrent);
371     view->goToBookmark(book->bookmarks()[index]);
372 }
373
374 void MainWindow::showChapters()
375 {
376     Book *book = Library::instance()->book(mCurrent);
377     if (book) {
378         ChaptersDialog *chapters = new ChaptersDialog(book, this);
379         chapters->setWindowModality(Qt::WindowModal);
380         connect(chapters, SIGNAL(goToChapter(int)),
381                 this, SLOT(onGoToChapter(int)));
382         chapters->show();
383     }
384 }
385
386 void MainWindow::onGoToChapter(int index)
387 {
388     Trace t("MainWindow::onGoToChapter");
389
390     Book *book = Library::instance()->book(mCurrent);
391     if (book) {
392         int partIndex = book->partFromChapter(index);
393         if (partIndex != -1) {
394             view->goToBookmark(Book::Bookmark(partIndex, 0));
395         }
396     }
397 }
398
399 void MainWindow::timerEvent(QTimerEvent *event)
400 {
401     if (event->timerId() == preventBlankingTimer) {
402 #ifdef Q_WS_MAEMO_5
403         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
404                            MCE_REQUEST_IF, QDBusConnection::systemBus());
405         mce.call(MCE_PREVENT_BLANK_REQ);
406 #endif // Q_WS_MAEMO_5
407         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
408     }
409     AdopterWindow::timerEvent(event);
410 }
411
412 void MainWindow::resizeEvent(QResizeEvent *e)
413 {
414     Trace t("MainWindow::resizeEvent");
415     progress->setGeometry(QRect(0, 0, e->size().width(), DORIAN_PROGRESS_HEIGHT));
416 #if defined(Q_WS_MAEMO_5)
417     previousButton->setGeometry(0,
418         e->size().height() - toolBar->height() - TranslucentButton::pixels,
419         TranslucentButton::pixels, TranslucentButton::pixels);
420     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
421         TranslucentButton::pixels, TranslucentButton::pixels);
422 #elif defined(Q_OS_SYMBIAN)
423     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
424         TranslucentButton::pixels, TranslucentButton::pixels);
425     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
426         0, TranslucentButton::pixels, TranslucentButton::pixels);
427 #else
428     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
429         TranslucentButton::pixels, TranslucentButton::pixels);
430     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
431         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
432 #endif // Q_WS_MAEMO_5
433     qDebug() << "previousButton geometry" << previousButton->geometry();
434     previousButton->flash(1500);
435     nextButton->flash(1500);
436     QMainWindow::resizeEvent(e);
437 }
438
439 void MainWindow::about()
440 {
441     Dyalog *aboutDialog = new Dyalog(this, false);
442     aboutDialog->setWindowTitle(tr("About Dorian"));
443     QLabel *label = new QLabel(aboutDialog);
444     label->setTextFormat(Qt::RichText);
445     label->setOpenExternalLinks(true);
446     label->setWordWrap(true);
447     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
448         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
449         "Licensed under GNU General Public License, Version 3<br>"
450         "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
451         "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
452     aboutDialog->addWidget(label);
453     aboutDialog->addStretch();
454     aboutDialog->show();
455 }
456
457
458 void MainWindow::goToNextPage()
459 {
460     nextButton->flash(1500);
461     previousButton->flash(1500);
462     view->goNextPage();
463 }
464
465 void MainWindow::goToPreviousPage()
466 {
467     nextButton->flash(1500);
468     previousButton->flash(1500);
469     view->goPreviousPage();
470 }
471
472 void MainWindow::onBeginUpgrade(int total)
473 {
474     libraryProgress->setVisible(total > 0);
475     libraryProgress->setWindowTitle(tr("Upgrading library"));
476     libraryProgress->setMaximum(total);
477 }
478
479 void MainWindow::onUpgrading(const QString &path)
480 {
481     libraryProgress->setLabelText(tr("Upgrading %1").
482                                   arg(QFileInfo(path).fileName()));
483     libraryProgress->setValue(libraryProgress->value() + 1);
484 }
485
486 void MainWindow::onEndUpgrade()
487 {
488     libraryProgress->hide();
489     libraryProgress->reset();
490 }
491
492
493 void MainWindow::onBeginLoad(int total)
494 {
495     libraryProgress->setVisible(total > 0);
496     libraryProgress->setWindowTitle(tr("Loading library"));
497     libraryProgress->setMaximum(total);
498 }
499
500 void MainWindow::onLoading(const QString &path)
501 {
502     libraryProgress->setLabelText(tr("Loading %1").
503                                   arg(QFileInfo(path).fileName()));
504     libraryProgress->setValue(libraryProgress->value() + 1);
505 }
506
507 void MainWindow::onEndLoad()
508 {
509     libraryProgress->hide();
510     libraryProgress->reset();
511 }
512