Fix naming: book parts vs. chapters.
[dorian] / mainwindow.cpp
index df4729d..b43f326 100755 (executable)
@@ -3,9 +3,14 @@
 #include <QDir>
 #include <QApplication>
 #include <QFileInfo>
+#include <QStringList>
+
 #ifdef Q_WS_MAEMO_5
 #   include <QtMaemo5/QMaemo5InformationBox>
-#endif
+#   include <QtDBus>
+#   include <mce/mode-names.h>
+#   include <mce/dbus-names.h>
+#endif // Q_WS_MAEMO_5
 
 #include "bookview.h"
 #include "book.h"
@@ -20,6 +25,7 @@
 #include "chaptersdialog.h"
 #include "fullscreenwindow.h"
 #include "trace.h"
+#include "bookfinder.h"
 
 #ifdef DORIAN_TEST_MODEL
 #include "modeltest.h"
 #   define ICON_PREFIX ":/icons/"
 #endif
 
-MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), view(0)
+MainWindow::MainWindow(QWidget *parent):
+    QMainWindow(parent), view(0), preventBlankingTimer(-1)
 {
+    Trace t("MainWindow::MainWindow");
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5StackedWindow, true);
 #endif
     setWindowTitle("Dorian");
 
-    // Central widget. Must be an intermediate because of reparenting the book view
+    // 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);
@@ -118,10 +127,11 @@ MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), view(0)
     }
 
     // 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)),
@@ -133,11 +143,36 @@ MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), view(0)
     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();
+
+#if 0
+    bool ret = QMetaObject::invokeMethod(
+        bookFinder,
+        "find",
+        Q_ARG(QStringList, QStringList(QString("/Users/polster/Books"))),
+        Q_ARG(QStringList, library->bookPaths()));
+    t.trace(QString("Invoking BookFinder::find ") + (ret?"succeeded":"failed"));
+#endif
+
 #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());
@@ -165,7 +200,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,
@@ -178,29 +213,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()
@@ -225,10 +255,11 @@ void MainWindow::closeEvent(QCloseEvent *event)
 
 void MainWindow::onSettingsChanged(const QString &key)
 {
-    Trace t("MainWindow::onSettingsChanged");
 #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_Maemo5LandscapeOrientation, false);
             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
@@ -237,42 +268,31 @@ void MainWindow::onSettingsChanged(const QString &key)
             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
         }
-
-        // FIXME: Orientation change should re-activate the window but it doesn't.
-        // And I have no idea how to force it
-
-        // view->restoreLastBookmark();
-        // view->setFocus();
-        // raise();
-
-        // QApplication::setActiveWindow(this);
-        // activateWindow();
-        // QEvent *enter = new QEvent(QEvent::Enter);
-        // QApplication::postEvent(view, enter);
-
-        // view->grabKeyboard();
-        // showNormal();
-
-        // QTestEventList events;
-        // events.addMouseClick(Qt::LeftButton);
-        // events.simulate(view);
+    } 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::onChapterLoadStart");
+    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::onChapterLoadEnd");
+    Trace t("MainWindow::onPartLoadEnd");
     bool enablePrevious = false;
     bool enableNext = false;
     Book *book = Library::instance()->book(mCurrent);
@@ -322,5 +342,25 @@ 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->tocFromChapter(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");
+    }
 }