Initial steps to keep library in a database.
[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 book on command line, or load last read book, or load default book
114     Library *library = Library::instance();
115     if (QCoreApplication::arguments().size() == 2) {
116         QString path = QCoreApplication::arguments()[1];
117         library->add(path);
118         QModelIndex index = library->find(path);
119         if (index.isValid()) {
120             library->setNowReading(index);
121         }
122     }
123     else {
124         QModelIndex index = library->nowReading();
125         if (index.isValid()) {
126             library->setNowReading(index);
127         }
128         else {
129             if (!library->rowCount()) {
130                 library->add(":/books/2BR02B.epub");
131             }
132             library->setNowReading(library->index(0));
133         }
134     }
135
136     // Handle loading book parts
137     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
138     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
139
140     // Handle progress
141     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
142
143     // Shadow window for full screen reading
144     fullScreenWindow = new FullScreenWindow(this);
145     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
146
147     // Handle settings changes
148     Settings *settings = Settings::instance();
149     connect(settings, SIGNAL(valueChanged(const QString &)),
150             this, SLOT(onSettingsChanged(const QString &)));
151     settings->setValue("orientation", settings->value("orientation"));
152     settings->setValue("lightson", settings->value("lightson"));
153     settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
154
155     // Handle book view buttons
156     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
157     connect(previousButton, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
158
159 #ifdef DORIAN_TEST_MODEL
160     (void)new ModelTest(Library::instance(), this);
161 #endif
162 }
163
164 MainWindow::~MainWindow()
165 {
166 }
167
168 void MainWindow::onCurrentBookChanged()
169 {
170     setCurrentBook(Library::instance()->nowReading());
171 }
172
173 void MainWindow::showRegular()
174 {
175     Trace t("MainWindow::showRegular");
176     fullScreenWindow->hide();
177     fullScreenWindow->leaveChildren();
178
179     QList<QWidget *> otherChildren;
180     otherChildren << progress << previousButton << nextButton;
181     takeChildren(view, otherChildren);
182     QRect geo = geometry();
183     progress->setGeometry(0, 0, geo.width(), PROGRESS_HEIGHT);
184 #if defined(Q_WS_MAEMO_5)
185     previousButton->setGeometry(0,
186         geo.height() - toolBar->height() - TranslucentButton::pixels,
187         TranslucentButton::pixels, TranslucentButton::pixels);
188     nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
189         TranslucentButton::pixels, TranslucentButton::pixels);
190 #elif defined(Q_OS_SYMBIAN)
191     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
192         TranslucentButton::pixels, TranslucentButton::pixels);
193     nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
194         0, TranslucentButton::pixels, TranslucentButton::pixels);
195 #else
196     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
197         TranslucentButton::pixels, TranslucentButton::pixels);
198     nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
199         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
200 #endif // Q_WS_MAEMO_5
201     qDebug() << "previousButton geometry" << previousButton->geometry();
202     progress->flash();
203     nextButton->show();
204     previousButton->show();
205     nextButton->flash(1500);
206     previousButton->flash(1500);
207 }
208
209 void MainWindow::showBig()
210 {
211     Trace t("MainWindow::showBig");
212     leaveChildren();
213     QList<QWidget *> otherChildren;
214     otherChildren << progress << nextButton << previousButton;
215     QRect screen = QApplication::desktop()->screenGeometry();
216     progress->setGeometry(0, 0, screen.width(), PROGRESS_HEIGHT);
217 #if defined(Q_WS_MAEMO_5)
218     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
219         TranslucentButton::pixels, TranslucentButton::pixels);
220 #else
221     nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
222                             TranslucentButton::pixels, TranslucentButton::pixels);
223 #endif // Q_WS_MAEMO_5
224     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
225         TranslucentButton::pixels, TranslucentButton::pixels);
226
227     fullScreenWindow->takeChildren(view, otherChildren);
228     fullScreenWindow->showFullScreen();
229     progress->flash();
230     nextButton->flash(1500);
231     previousButton->flash(1500);
232 }
233
234 void MainWindow::setCurrentBook(const QModelIndex &current)
235 {
236     mCurrent = current;
237     Book *book = Library::instance()->book(current);
238     view->setBook(book);
239     setWindowTitle(book? book->shortName(): tr("Dorian"));
240 }
241
242 void MainWindow::showLibrary()
243 {
244     (new LibraryDialog(this))->show();
245 }
246
247 void MainWindow::showSettings()
248 {
249     (new SettingsWindow(this))->show();
250 }
251
252 void MainWindow::showInfo()
253 {
254     if (mCurrent.isValid()) {
255         (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
256                 exec();
257     }
258 }
259
260 void MainWindow::showDevTools()
261 {
262     (new DevTools())->exec();
263 }
264
265 void MainWindow::showBookmarks()
266 {
267     Book *book = Library::instance()->book(mCurrent);
268     if (book) {
269         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
270         bookmarks->setWindowModality(Qt::WindowModal);
271         connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
272         connect(bookmarks, SIGNAL(goToBookmark(int)),
273                 this, SLOT(onGoToBookmark(int)));
274         bookmarks->show();
275     }
276 }
277
278 void MainWindow::closeEvent(QCloseEvent *event)
279 {
280     Trace t("MainWindow::closeEvent");
281     view->setLastBookmark();
282     event->accept();
283 }
284
285 void MainWindow::onSettingsChanged(const QString &key)
286 {
287 #ifdef Q_WS_MAEMO_5
288     if (key == "orientation") {
289         QString value = Settings::instance()->value(key).toString();
290         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
291         if (value == "portrait") {
292             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
293             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
294         }
295         else {
296             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
297             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
298         }
299     } else if (key == "lightson") {
300         bool enable = Settings::instance()->value(key, false).toBool();
301         qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
302         killTimer(preventBlankingTimer);
303         if (enable) {
304             preventBlankingTimer = startTimer(29 * 1000);
305         }
306     } else if (key == "usevolumekeys") {
307         bool value = Settings::instance()->value(key).toBool();
308         qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
309         grabZoomKeys(value);
310         fullScreenWindow->grabZoomKeys(value);
311     }
312 #else
313     Q_UNUSED(key);
314 #endif // Q_WS_MAEMO_5
315 }
316
317 void MainWindow::onPartLoadStart()
318 {
319     Trace t("MainWindow::onPartLoadStart");
320 #ifdef Q_WS_MAEMO_5
321     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
322 #endif
323 }
324
325 void MainWindow::onPartLoadEnd(int index)
326 {
327     Trace t("MainWindow::onPartLoadEnd");
328     bool enablePrevious = false;
329     bool enableNext = false;
330     Book *book = Library::instance()->book(mCurrent);
331     if (book) {
332         if (index > 0) {
333             enablePrevious = true;
334         }
335         if (index < (book->parts.size() - 1)) {
336             enableNext = true;
337         }
338     }
339 #ifdef Q_WS_MAEMO_5
340     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
341 #endif // Q_WS_MAEMO_5
342 }
343
344 void MainWindow::onAddBookmark()
345 {
346     Trace t("MainWindow:onAddBookmark");
347     view->addBookmark();
348 }
349
350 void MainWindow::onGoToBookmark(int index)
351 {
352     Trace t("MainWindow::onGoToBookmark");
353     Book *book = Library::instance()->book(mCurrent);
354     view->goToBookmark(book->bookmarks()[index]);
355 }
356
357 void MainWindow::showChapters()
358 {
359     Book *book = Library::instance()->book(mCurrent);
360     if (book) {
361         ChaptersDialog *chapters = new ChaptersDialog(book, this);
362         chapters->setWindowModality(Qt::WindowModal);
363         connect(chapters, SIGNAL(goToChapter(int)),
364                 this, SLOT(onGoToChapter(int)));
365         chapters->show();
366     }
367 }
368
369 void MainWindow::onGoToChapter(int index)
370 {
371     Trace t("MainWindow::onGoToChapter");
372
373     Book *book = Library::instance()->book(mCurrent);
374     if (book) {
375         int partIndex = book->partFromChapter(index);
376         if (partIndex != -1) {
377             view->goToBookmark(Book::Bookmark(partIndex, 0));
378         }
379     }
380 }
381
382 void MainWindow::timerEvent(QTimerEvent *event)
383 {
384     if (event->timerId() == preventBlankingTimer) {
385 #ifdef Q_WS_MAEMO_5
386         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
387                            MCE_REQUEST_IF, QDBusConnection::systemBus());
388         mce.call(MCE_PREVENT_BLANK_REQ);
389 #endif // Q_WS_MAEMO_5
390         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
391     }
392 }
393
394 void MainWindow::resizeEvent(QResizeEvent *e)
395 {
396     Trace t("MainWindow::resizeEvent");
397     progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
398 #if defined(Q_WS_MAEMO_5)
399     previousButton->setGeometry(0,
400         e->size().height() - toolBar->height() - TranslucentButton::pixels,
401         TranslucentButton::pixels, TranslucentButton::pixels);
402     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
403         TranslucentButton::pixels, TranslucentButton::pixels);
404 #elif defined(Q_OS_SYMBIAN)
405     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
406         TranslucentButton::pixels, TranslucentButton::pixels);
407     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
408         0, TranslucentButton::pixels, TranslucentButton::pixels);
409 #else
410     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
411         TranslucentButton::pixels, TranslucentButton::pixels);
412     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
413         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
414 #endif // Q_WS_MAEMO_5
415     qDebug() << "previousButton geometry" << previousButton->geometry();
416     previousButton->flash(1500);
417     nextButton->flash(1500);
418     QMainWindow::resizeEvent(e);
419 }
420
421 void MainWindow::about()
422 {
423     Dyalog *aboutDialog = new Dyalog(this);
424     aboutDialog->setWindowTitle(tr("About Dorian"));
425     QLabel *label = new QLabel(aboutDialog);
426     label->setTextFormat(Qt::RichText);
427     label->setOpenExternalLinks(true);
428     label->setWordWrap(true);
429     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
430         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
431         "Licensed under GNU General Public License, Version 3<br>"
432         "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
433         "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
434     aboutDialog->addWidget(label);
435     aboutDialog->addStretch();
436     aboutDialog->show();
437 }
438
439
440 void MainWindow::goToNextPage()
441 {
442     nextButton->flash(1500);
443     previousButton->flash(1500);
444     view->goNextPage();
445 }
446
447 void MainWindow::goToPreviousPage()
448 {
449     nextButton->flash(1500);
450     previousButton->flash(1500);
451     view->goPreviousPage();
452 }