Navigate with volume keys.
[dorian] / mainwindow.cpp
index 6392631..d327214 100755 (executable)
@@ -1,11 +1,19 @@
 #include <QtGui>
 #include <QtDebug>
 #include <QDir>
-#include <QCoreApplication>
+#include <QApplication>
 #include <QFileInfo>
+#include <QStringList>
+
 #ifdef Q_WS_MAEMO_5
 #   include <QtMaemo5/QMaemo5InformationBox>
-#endif
+#   include <QtDBus>
+#   include <QtGui/QX11Info>
+#   include <X11/Xlib.h>
+#   include <X11/Xatom.h>
+#   include <mce/mode-names.h>
+#   include <mce/dbus-names.h>
+#endif // Q_WS_MAEMO_5
 
 #include "bookview.h"
 #include "book.h"
@@ -19,6 +27,9 @@
 #include "settings.h"
 #include "chaptersdialog.h"
 #include "fullscreenwindow.h"
+#include "trace.h"
+#include "bookfinder.h"
+#include "progress.h"
 
 #ifdef DORIAN_TEST_MODEL
 #include "modeltest.h"
 #   define ICON_PREFIX ":/icons/"
 #endif
 
-const Qt::WindowFlags WIN_BIG_FLAGS =
-        Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint;
-const int WIN_BIG_TIMER = 3000;
+const int PROGRESS_HEIGHT = 17;
 
 MainWindow::MainWindow(QWidget *parent):
-        QMainWindow(parent), view(0), fullScreenWindow(0)
+    QMainWindow(parent), view(0), preventBlankingTimer(-1)
 {
+    Trace t("MainWindow::MainWindow");
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5StackedWindow, true);
+    grabZoomKeys();
 #endif
     setWindowTitle("Dorian");
 
+    // Central widget. Must be an intermediate, because the book view widget
+    // can be re-parented later
+    QFrame *central = new QFrame(this);
+    QVBoxLayout *layout = new QVBoxLayout(central);
+    layout->setMargin(0);
+    central->setLayout(layout);
+    setCentralWidget(central);
+
     // Book view
-    view = new BookView(this);
-    setCentralWidget(view);
+    view = new BookView(central);
+    view->show();
+    layout->addWidget(view);
+
+    // Progress
+    progress = new Progress(central);
 
     // Tool bar
     setUnifiedTitleAndToolBarOnMac(true);
@@ -84,8 +107,7 @@ MainWindow::MainWindow(QWidget *parent):
     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
     toolBar->addWidget(frame);
 
-    fullScreenAction = addToolBarAction(this, SLOT(showFullScreen()),
-                                        "view-fullscreen");
+    fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
 
     // Handle model changes
     connect(Library::instance(), SIGNAL(nowReadingChanged()),
@@ -115,10 +137,11 @@ MainWindow::MainWindow(QWidget *parent):
     }
 
     // Handle settings changes
-    connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
+    Settings *settings = Settings::instance();
+    connect(settings, SIGNAL(valueChanged(const QString &)),
             this, SLOT(onSettingsChanged(const QString &)));
-    Settings::instance()->setValue("orientation",
-                                   Settings::instance()->value("orientation"));
+    settings->setValue("orientation", settings->value("orientation"));
+    settings->setValue("lightson", settings->value("lightson"));
 
     // Handle loading chapters
     connect(view, SIGNAL(chapterLoadStart(int)),
@@ -126,32 +149,61 @@ MainWindow::MainWindow(QWidget *parent):
     connect(view, SIGNAL(chapterLoadEnd(int)),
             this, SLOT(onChapterLoadEnd(int)));
 
+    // Handle progress
+    connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
+
+    // Shadow window for full screen
+    fullScreenWindow = new FullScreenWindow(this);
+    connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
+
+    // Create thread for finding books in directories
+    bookFinder = new BookFinder();
+    connect(bookFinder, SIGNAL(add(const QString &)),
+            library, SLOT(add(const QString &)));
+    connect(bookFinder, SIGNAL(remove(const QString &)),
+            library, SLOT(remove(const QString &)));
+    bookFinder->moveToThread(&bookFinderThread);
+    bookFinderThread.start();
+
 #ifdef DORIAN_TEST_MODEL
     (void)new ModelTest(Library::instance(), this);
 #endif
 }
 
+MainWindow::~MainWindow()
+{
+    bookFinderThread.quit();
+    bookFinderThread.wait();
+    delete bookFinder;
+}
+
 void MainWindow::onCurrentBookChanged()
 {
     setCurrentBook(Library::instance()->nowReading());
 }
 
-void MainWindow::showNormal()
+void MainWindow::showRegular()
 {
-    qDebug() << "MainWindow::showNormal";
-    view->setParent(this);
-    setCentralWidget(view);
-    show();
-    delete fullScreenWindow;
-    fullScreenWindow = 0;
+    Trace t("MainWindow::showRegular");
+    fullScreenWindow->hide();
+    fullScreenWindow->leaveChild();
+    view->setParent(centralWidget());
+    progress->setParent(centralWidget());
+    progress->setGeometry(0, 0, geometry().width(), PROGRESS_HEIGHT);
+    centralWidget()->layout()->addWidget(view);
+    progress->flash();
 }
 
-void MainWindow::showFullScreen()
+void MainWindow::showBig()
 {
-    qDebug() << "MainWindow::showFullscreen";
-    fullScreenWindow = new FullScreenWindow(view, this);
+    Trace t("MainWindow::showBig");
+    centralWidget()->layout()->removeWidget(view);
+    progress->setParent(fullScreenWindow);
+    progress->setGeometry(0, 0, QApplication::desktop()->screenGeometry().width(),
+                          PROGRESS_HEIGHT);
+    fullScreenWindow->takeChild(view);
     fullScreenWindow->showFullScreen();
-    connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showNormal()));
+    progress->flash();
 }
 
 void MainWindow::setCurrentBook(const QModelIndex &current)
@@ -159,7 +211,7 @@ void MainWindow::setCurrentBook(const QModelIndex &current)
     mCurrent = current;
     Book *book = Library::instance()->book(current);
     view->setBook(book);
-    setWindowTitle(book? book->name(): tr("Dorian"));
+    setWindowTitle(book? book->shortName(): tr("Dorian"));
 }
 
 QAction *MainWindow::addToolBarAction(const QObject *receiver,
@@ -172,29 +224,24 @@ QAction *MainWindow::addToolBarAction(const QObject *receiver,
 
 void MainWindow::showLibrary()
 {
-    LibraryDialog *dialog = new LibraryDialog(this);
-    dialog->show();
+    (new LibraryDialog(this))->show();
 }
 
 void MainWindow::showSettings()
 {
-    SettingsWindow *settings = new SettingsWindow(this);
-    settings->show();
+    (new SettingsWindow(this))->show();
 }
 
 void MainWindow::showInfo()
 {
     if (mCurrent.isValid()) {
-        InfoDialog *info =
-            new InfoDialog(Library::instance()->book(mCurrent), this);
-        info->exec();
+        (new InfoDialog(Library::instance()->book(mCurrent), this))->exec();
     }
 }
 
 void MainWindow::showDevTools()
 {
-    DevTools *devTools = new DevTools();
-    devTools->exec();
+    (new DevTools())->exec();
 }
 
 void MainWindow::showBookmarks()
@@ -212,7 +259,7 @@ void MainWindow::showBookmarks()
 
 void MainWindow::closeEvent(QCloseEvent *event)
 {
-    qDebug() << "MainWindow::closeEvent";
+    Trace t("MainWindow::closeEvent");
     view->setLastBookmark();
     event->accept();
 }
@@ -222,29 +269,41 @@ void MainWindow::onSettingsChanged(const QString &key)
 #ifdef Q_WS_MAEMO_5
     if (key == "orientation") {
         QString value = Settings::instance()->value(key).toString();
+        Trace::trace(QString("MainWindow::onSettingsChanged: orientation %1").
+                     arg(value));
         if (value == "portrait") {
-            setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
+            setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
         }
         else {
             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
         }
+    } else if (key == "lightson") {
+        bool enable = Settings::instance()->value(key, false).toBool();
+        Trace::trace(QString("MainWindow::onSettingsChanged: lightson: %1").
+                     arg(enable));
+        killTimer(preventBlankingTimer);
+        if (enable) {
+            preventBlankingTimer = startTimer(29 * 1000);
+        }
     }
 #else
     Q_UNUSED(key);
 #endif // Q_WS_MAEMO_5
 }
 
-void MainWindow::onChapterLoadStart()
+void MainWindow::onPartLoadStart()
 {
+    Trace t("MainWindow::onPartLoadStart");
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
 #endif
 }
 
-void MainWindow::onChapterLoadEnd(int index)
+void MainWindow::onPartLoadEnd(int index)
 {
+    Trace t("MainWindow::onPartLoadEnd");
     bool enablePrevious = false;
     bool enableNext = false;
     Book *book = Library::instance()->book(mCurrent);
@@ -252,7 +311,7 @@ void MainWindow::onChapterLoadEnd(int index)
         if (index > 0) {
             enablePrevious = true;
         }
-        if (index < (book->toc.size() - 1)) {
+        if (index < (book->parts.size() - 1)) {
             enableNext = true;
         }
     }
@@ -269,11 +328,13 @@ void MainWindow::onChapterLoadEnd(int index)
 
 void MainWindow::onAddBookmark()
 {
+    Trace t("MainWindow:onAddBookmark");
     view->addBookmark();
 }
 
 void MainWindow::onGoToBookmark(int index)
 {
+    Trace t("MainWindow::onGoToBookmark");
     Book *book = Library::instance()->book(mCurrent);
     view->goToBookmark(book->bookmarks()[index]);
 }
@@ -292,6 +353,55 @@ void MainWindow::showChapters()
 
 void MainWindow::onGoToChapter(int index)
 {
-    view->goToBookmark(Book::Bookmark(index, 0));
+    Trace t("MainWindow::onGoToChapter");
+
+    Book *book = Library::instance()->book(mCurrent);
+    if (book) {
+        int partIndex = book->partFromChapter(index);
+        if (partIndex != -1) {
+            view->goToBookmark(Book::Bookmark(partIndex, 0));
+        }
+    }
+}
+
+void MainWindow::timerEvent(QTimerEvent *event)
+{
+    if (event->timerId() == preventBlankingTimer) {
+#ifdef Q_WS_MAEMO_5
+        QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
+                           MCE_REQUEST_IF, QDBusConnection::systemBus());
+        mce.call(MCE_PREVENT_BLANK_REQ);
+#endif // Q_WS_MAEMO_5
+        Trace::trace("MainWindow::timerEvent: Prevent display blanking");
+    }
 }
 
+void MainWindow::resizeEvent(QResizeEvent *e)
+{
+    progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
+    QMainWindow::resizeEvent(e);
+}
+
+void MainWindow::grabZoomKeys()
+{
+#ifdef Q_WS_MAEMO_5
+    if (!winId()) {
+        qCritical() << "Can't grab keys unless we have a window id";
+        return;
+    }
+    unsigned long val = 1;
+    Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
+    if (!atom) {
+        qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
+        return;
+    }
+    XChangeProperty(QX11Info::display(),
+        winId(),
+        atom,
+        XA_INTEGER,
+        32,
+        PropModeReplace,
+        reinterpret_cast<unsigned char *>(&val),
+        1);
+#endif // Q_WS_MAEMO_5
+}