New minor version. Database upgrade preparations.
[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
38 #ifdef DORIAN_TEST_MODEL
39 #   include "modeltest.h"
40 #endif
41
42 const int PROGRESS_HEIGHT = 17;
43 static const char *DORIAN_VERSION =
44 #include "pkg/version.txt"
45 ;
46
47 MainWindow::MainWindow(QWidget *parent):
48     AdopterWindow(parent), view(0), preventBlankingTimer(-1)
49 {
50     Trace t("MainWindow::MainWindow");
51 #ifdef Q_WS_MAEMO_5
52     setAttribute(Qt::WA_Maemo5StackedWindow, true);
53 #endif
54     setWindowTitle("Dorian");
55
56     // Central widget. Must be an intermediate, because the book view widget
57     // can be re-parented later
58     QFrame *central = new QFrame(this);
59     QVBoxLayout *layout = new QVBoxLayout(central);
60     layout->setMargin(0);
61     central->setLayout(layout);
62     setCentralWidget(central);
63
64     // Book view
65     view = new BookView(central);
66     view->show();
67     layout->addWidget(view);
68
69     // Progress
70     progress = new Progress(central);
71
72     // Settings dialog
73     settings = new QDialog(this);
74
75     // Tool bar actions
76
77     chaptersAction = addToolBarAction(this, SLOT(showChapters()),
78                                       "chapters", tr("Chapters"));
79     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
80                                        "bookmarks", tr("Bookmarks"));
81     infoAction = addToolBarAction(this, SLOT(showInfo()),
82                                   "info", tr("Book info"));
83     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
84                                      "library", tr("Library"));
85
86 #ifdef Q_WS_MAEMO_5
87     settingsAction = menuBar()->addAction(tr("Settings"));
88     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
89     devToolsAction = menuBar()->addAction(tr("Developer"));
90     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
91     QAction *aboutAction = menuBar()->addAction(tr("About"));
92     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
93 #else
94     settingsAction = addToolBarAction(this, SLOT(showSettings()),
95                                       "preferences-system", tr("Settings"));
96     devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
97                                       "developer", tr("Developer"));
98     addToolBarAction(this, SLOT(about()), "about", tr("About"));
99 #endif // Q_WS_MAEMO_5
100
101     addToolBarSpace();
102     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
103                                         "view-fullscreen", tr("Full screen"));
104
105     // Buttons on top of the book view
106     previousButton = new TranslucentButton("back", this);
107     nextButton = new TranslucentButton("forward", this);
108
109     // Handle model changes
110     connect(Library::instance(), SIGNAL(nowReadingChanged()),
111             this, SLOT(onCurrentBookChanged()));
112
113     // Load library, upgrade it if needed
114     upgradeProgress = new QProgressDialog(tr("Upgrading library"), "", 0, 0, this);
115     upgradeProgress->reset();
116     upgradeProgress->setMinimumDuration(0);
117     upgradeProgress->setWindowModality(Qt::WindowModal);
118     upgradeProgress->setCancelButton(0);
119     Library *library = Library::instance();
120     connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
121     connect(library, SIGNAL(upgrading(const QString &)),
122             this, SLOT(onUpgrading(const QString &)));
123     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
124     library->upgrade();
125     library->load();
126
127     // Load book on command line, or load last read book, or load default book
128     if (QCoreApplication::arguments().size() == 2) {
129         QString path = QCoreApplication::arguments()[1];
130         library->add(path);
131         QModelIndex index = library->find(path);
132         if (index.isValid()) {
133             library->setNowReading(index);
134         }
135     } else {
136         QModelIndex index = library->nowReading();
137         if (index.isValid()) {
138             library->setNowReading(index);
139         } else {
140             if (!library->rowCount()) {
141                 library->add(":/books/2BR02B.epub");
142             }
143             library->setNowReading(library->index(0));
144         }
145     }
146
147     // Handle loading book parts
148     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
149     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
150
151     // Handle progress
152     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
153
154     // Shadow window for full screen reading
155     fullScreenWindow = new FullScreenWindow(this);
156     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
157
158     // Handle settings changes
159     Settings *settings = Settings::instance();
160     connect(settings, SIGNAL(valueChanged(const QString &)),
161             this, SLOT(onSettingsChanged(const QString &)));
162     settings->setValue("orientation", settings->value("orientation"));
163     settings->setValue("lightson", settings->value("lightson"));
164     settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
165
166     // Handle book view buttons
167     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
168     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
169
170 #ifdef DORIAN_TEST_MODEL
171     (void)new ModelTest(Library::instance(), this);
172 #endif
173 }
174
175 MainWindow::~MainWindow()
176 {
177 }
178
179 void MainWindow::onCurrentBookChanged()
180 {
181     setCurrentBook(Library::instance()->nowReading());
182 }
183
184 void MainWindow::showRegular()
185 {
186     Trace t("MainWindow::showRegular");
187     fullScreenWindow->hide();
188     fullScreenWindow->leaveChildren();
189
190     QList<QWidget *> otherChildren;
191     otherChildren << progress << previousButton << nextButton;
192     takeChildren(view, otherChildren);
193     QRect geo = geometry();
194     progress->setGeometry(0, 0, geo.width(), PROGRESS_HEIGHT);
195 #if defined(Q_WS_MAEMO_5)
196     previousButton->setGeometry(0,
197         geo.height() - toolBar->height() - TranslucentButton::pixels,
198         TranslucentButton::pixels, TranslucentButton::pixels);
199     nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
200         TranslucentButton::pixels, TranslucentButton::pixels);
201 #elif defined(Q_OS_SYMBIAN)
202     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
203         TranslucentButton::pixels, TranslucentButton::pixels);
204     nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
205         0, TranslucentButton::pixels, TranslucentButton::pixels);
206 #else
207     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
208         TranslucentButton::pixels, TranslucentButton::pixels);
209     nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
210         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
211 #endif // Q_WS_MAEMO_5
212     qDebug() << "previousButton geometry" << previousButton->geometry();
213     progress->flash();
214     nextButton->show();
215     previousButton->show();
216     nextButton->flash(1500);
217     previousButton->flash(1500);
218 }
219
220 void MainWindow::showBig()
221 {
222     Trace t("MainWindow::showBig");
223     leaveChildren();
224     QList<QWidget *> otherChildren;
225     otherChildren << progress << nextButton << previousButton;
226     QRect screen = QApplication::desktop()->screenGeometry();
227     progress->setGeometry(0, 0, screen.width(), PROGRESS_HEIGHT);
228 #if defined(Q_WS_MAEMO_5)
229     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
230         TranslucentButton::pixels, TranslucentButton::pixels);
231 #else
232     nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
233                             TranslucentButton::pixels, TranslucentButton::pixels);
234 #endif // Q_WS_MAEMO_5
235     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
236         TranslucentButton::pixels, TranslucentButton::pixels);
237
238     fullScreenWindow->takeChildren(view, otherChildren);
239     fullScreenWindow->showFullScreen();
240     progress->flash();
241     nextButton->flash(1500);
242     previousButton->flash(1500);
243 }
244
245 void MainWindow::setCurrentBook(const QModelIndex &current)
246 {
247     mCurrent = current;
248     Book *book = Library::instance()->book(current);
249     view->setBook(book);
250     setWindowTitle(book? book->shortName(): tr("Dorian"));
251 }
252
253 void MainWindow::showLibrary()
254 {
255     (new LibraryDialog(this))->show();
256 }
257
258 void MainWindow::showSettings()
259 {
260     (new SettingsWindow(this))->show();
261 }
262
263 void MainWindow::showInfo()
264 {
265     if (mCurrent.isValid()) {
266         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
267                 exec();
268     }
269 }
270
271 void MainWindow::showDevTools()
272 {
273     (new DevTools())->exec();
274 }
275
276 void MainWindow::showBookmarks()
277 {
278     Book *book = Library::instance()->book(mCurrent);
279     if (book) {
280         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
281         bookmarks->setWindowModality(Qt::WindowModal);
282         connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
283         connect(bookmarks, SIGNAL(goToBookmark(int)),
284                 this, SLOT(onGoToBookmark(int)));
285         bookmarks->show();
286     }
287 }
288
289 void MainWindow::closeEvent(QCloseEvent *event)
290 {
291     Trace t("MainWindow::closeEvent");
292     view->setLastBookmark();
293     event->accept();
294 }
295
296 void MainWindow::onSettingsChanged(const QString &key)
297 {
298 #ifdef Q_WS_MAEMO_5
299     if (key == "orientation") {
300         QString value = Settings::instance()->value(key).toString();
301         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
302         if (value == "portrait") {
303             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
304             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
305         } else {
306             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
307             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
308         }
309     } else if (key == "lightson") {
310         bool enable = Settings::instance()->value(key, false).toBool();
311         qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
312         killTimer(preventBlankingTimer);
313         if (enable) {
314             preventBlankingTimer = startTimer(29 * 1000);
315         }
316     } else if (key == "usevolumekeys") {
317         bool value = Settings::instance()->value(key).toBool();
318         qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
319         grabZoomKeys(value);
320         fullScreenWindow->grabZoomKeys(value);
321     }
322 #else
323     Q_UNUSED(key);
324 #endif // Q_WS_MAEMO_5
325 }
326
327 void MainWindow::onPartLoadStart()
328 {
329     Trace t("MainWindow::onPartLoadStart");
330 #ifdef Q_WS_MAEMO_5
331     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
332 #endif
333 }
334
335 void MainWindow::onPartLoadEnd(int index)
336 {
337     Trace t("MainWindow::onPartLoadEnd");
338     bool enablePrevious = false;
339     bool enableNext = false;
340     Book *book = Library::instance()->book(mCurrent);
341     if (book) {
342         if (index > 0) {
343             enablePrevious = true;
344         }
345         if (index < (book->parts.size() - 1)) {
346             enableNext = true;
347         }
348     }
349 #ifdef Q_WS_MAEMO_5
350     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
351 #endif // Q_WS_MAEMO_5
352 }
353
354 void MainWindow::onAddBookmark()
355 {
356     Trace t("MainWindow:onAddBookmark");
357     view->addBookmark();
358 }
359
360 void MainWindow::onGoToBookmark(int index)
361 {
362     Trace t("MainWindow::onGoToBookmark");
363     Book *book = Library::instance()->book(mCurrent);
364     view->goToBookmark(book->bookmarks()[index]);
365 }
366
367 void MainWindow::showChapters()
368 {
369     Book *book = Library::instance()->book(mCurrent);
370     if (book) {
371         ChaptersDialog *chapters = new ChaptersDialog(book, this);
372         chapters->setWindowModality(Qt::WindowModal);
373         connect(chapters, SIGNAL(goToChapter(int)),
374                 this, SLOT(onGoToChapter(int)));
375         chapters->show();
376     }
377 }
378
379 void MainWindow::onGoToChapter(int index)
380 {
381     Trace t("MainWindow::onGoToChapter");
382
383     Book *book = Library::instance()->book(mCurrent);
384     if (book) {
385         int partIndex = book->partFromChapter(index);
386         if (partIndex != -1) {
387             view->goToBookmark(Book::Bookmark(partIndex, 0));
388         }
389     }
390 }
391
392 void MainWindow::timerEvent(QTimerEvent *event)
393 {
394     if (event->timerId() == preventBlankingTimer) {
395 #ifdef Q_WS_MAEMO_5
396         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
397                            MCE_REQUEST_IF, QDBusConnection::systemBus());
398         mce.call(MCE_PREVENT_BLANK_REQ);
399 #endif // Q_WS_MAEMO_5
400         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
401     }
402 }
403
404 void MainWindow::resizeEvent(QResizeEvent *e)
405 {
406     Trace t("MainWindow::resizeEvent");
407     progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
408 #if defined(Q_WS_MAEMO_5)
409     previousButton->setGeometry(0,
410         e->size().height() - toolBar->height() - TranslucentButton::pixels,
411         TranslucentButton::pixels, TranslucentButton::pixels);
412     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
413         TranslucentButton::pixels, TranslucentButton::pixels);
414 #elif defined(Q_OS_SYMBIAN)
415     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
416         TranslucentButton::pixels, TranslucentButton::pixels);
417     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
418         0, TranslucentButton::pixels, TranslucentButton::pixels);
419 #else
420     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
421         TranslucentButton::pixels, TranslucentButton::pixels);
422     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
423         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
424 #endif // Q_WS_MAEMO_5
425     qDebug() << "previousButton geometry" << previousButton->geometry();
426     previousButton->flash(1500);
427     nextButton->flash(1500);
428     QMainWindow::resizeEvent(e);
429 }
430
431 void MainWindow::about()
432 {
433     Dyalog *aboutDialog = new Dyalog(this);
434     aboutDialog->setWindowTitle(tr("About Dorian"));
435     QLabel *label = new QLabel(aboutDialog);
436     label->setTextFormat(Qt::RichText);
437     label->setOpenExternalLinks(true);
438     label->setWordWrap(true);
439     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
440         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
441         "Licensed under GNU General Public License, Version 3<br>"
442         "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
443         "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
444     aboutDialog->addWidget(label);
445     aboutDialog->addStretch();
446     aboutDialog->show();
447 }
448
449
450 void MainWindow::goToNextPage()
451 {
452     nextButton->flash(1500);
453     previousButton->flash(1500);
454     view->goNextPage();
455 }
456
457 void MainWindow::goToPreviousPage()
458 {
459     nextButton->flash(1500);
460     previousButton->flash(1500);
461     view->goPreviousPage();
462 }
463
464 void MainWindow::onBeginUpgrade(int total)
465 {
466     upgradeProgress->setVisible(total > 0);
467     upgradeProgress->setMaximum(total);
468 }
469
470 void MainWindow::onUpgrading(const QString &path)
471 {
472     upgradeProgress->setLabelText(tr("Upgrading %1").
473                                   arg(QFileInfo(path).fileName()));
474     upgradeProgress->setValue(upgradeProgress->value() + 1);
475 }
476
477 void MainWindow::onEndUpgrade()
478 {
479     upgradeProgress->hide();
480     upgradeProgress->reset();
481 }
482