Improve initialization order.
[dorian] / mainwindow.cpp
index 33a730a..42e7e77 100755 (executable)
@@ -59,7 +59,7 @@ MainWindow::MainWindow(QWidget *parent):
     layout->addWidget(view);
 
     // Progress
-    progress = new Progress(central);
+    progress = new Progress(this);
 
     // Settings dialog
     settings = new QDialog(this);
@@ -118,32 +118,6 @@ MainWindow::MainWindow(QWidget *parent):
     connect(library, SIGNAL(upgrading(const QString &)),
             this, SLOT(onUpgrading(const QString &)));
     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
-    connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
-    connect(library, SIGNAL(loading(const QString &)),
-            this, SLOT(onLoading(const QString &)));
-    connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
-    library->upgrade();
-    library->load();
-
-    // Load book on command line, or load last read book, or load default book
-    if (QCoreApplication::arguments().size() == 2) {
-        QString path = QCoreApplication::arguments()[1];
-        library->add(path);
-        QModelIndex index = library->find(path);
-        if (index.isValid()) {
-            library->setNowReading(index);
-        }
-    } else {
-        QModelIndex index = library->nowReading();
-        if (index.isValid()) {
-            library->setNowReading(index);
-        } else {
-            if (!library->rowCount()) {
-                library->add(":/books/2BR02B.epub");
-            }
-            library->setNowReading(library->index(0));
-        }
-    }
 
     // Handle loading book parts
     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
@@ -157,11 +131,8 @@ MainWindow::MainWindow(QWidget *parent):
     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
 
     // Handle settings changes
-    Settings *settings = Settings::instance();
-    connect(settings, SIGNAL(valueChanged(const QString &)),
+    connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
             this, SLOT(onSettingsChanged(const QString &)));
-    settings->setValue("orientation", settings->value("orientation"));
-    settings->setValue("lightson", settings->value("lightson"));
 
     // Handle book view buttons
     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
@@ -175,8 +146,39 @@ MainWindow::MainWindow(QWidget *parent):
 #endif
 }
 
+void MainWindow::initialize()
+{
+    TRACE;
+    Library *library = Library::instance();
+
+    // Upgrade library if needed, then load it
+    library->upgrade();
+    library->load();
+
+    // Load book on command line, or load last read book, or load default book
+    if (QCoreApplication::arguments().size() == 2) {
+        QString path = QCoreApplication::arguments()[1];
+        library->add(path);
+        QModelIndex index = library->find(path);
+        if (index.isValid()) {
+            library->setNowReading(index);
+        }
+    } else {
+        QModelIndex index = library->nowReading();
+        if (index.isValid()) {
+            library->setNowReading(index);
+        } else {
+            if (!library->rowCount()) {
+                library->add(":/books/2BR02B.epub");
+            }
+            library->setNowReading(library->index(0));
+        }
+    }
+}
+
 void MainWindow::onCurrentBookChanged()
 {
+    TRACE;
     setCurrentBook(Library::instance()->nowReading());
 }
 
@@ -217,9 +219,6 @@ void MainWindow::showRegular()
     show();
 #if defined(Q_OS_SYMBIAN)
     activateWindow();
-#elif defined(Q_WS_MAEMO_5)
-    // FIXME: This is ugly.
-    view->restoreLastBookmark();
 #endif
     progress->flash();
     nextButton->flash();
@@ -315,11 +314,11 @@ void MainWindow::closeEvent(QCloseEvent *event)
 
 void MainWindow::onSettingsChanged(const QString &key)
 {
-    TRACE;
-    qDebug() << key;
 #if defined(Q_WS_MAEMO_5)
     if (key == "orientation") {
-        QString value = Settings::instance()->value(key).toString();
+        QString value = Settings::instance()->value(key,
+            Platform::instance()->defaultOrientation()).toString();
+        qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
         if (value == "portrait") {
             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
@@ -329,6 +328,7 @@ void MainWindow::onSettingsChanged(const QString &key)
         }
     } else if (key == "lightson") {
         bool enable = Settings::instance()->value(key, false).toBool();
+        qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
         killTimer(preventBlankingTimer);
         if (enable) {
             preventBlankingTimer = startTimer(29 * 1000);
@@ -340,7 +340,7 @@ void MainWindow::onSettingsChanged(const QString &key)
 void MainWindow::onPartLoadStart()
 {
     TRACE;
-    Platform::showBusy(this, true);
+    Platform::instance()->showBusy(this, true);
 }
 
 void MainWindow::onPartLoadEnd(int index)
@@ -357,14 +357,14 @@ void MainWindow::onPartLoadEnd(int index)
             enableNext = true;
         }
     }
-    Platform::showBusy(this, false);
+    Platform::instance()->showBusy(this, false);
 }
 
 void MainWindow::onAddBookmark(const QString &note)
 {
     TRACE;
     view->addBookmark(note);
-    Platform::information(tr("Bookmarked current position"), this);
+    Platform::instance()->information(tr("Bookmarked current position"), this);
 }
 
 void MainWindow::onGoToBookmark(int index)
@@ -444,6 +444,7 @@ void MainWindow::about()
 {
     Dyalog *aboutDialog = new Dyalog(this, false);
     aboutDialog->setWindowTitle(tr("About Dorian"));
+    QString version = Platform::instance()->version();
     QLabel *label = new QLabel(aboutDialog);
     label->setTextFormat(Qt::RichText);
     label->setOpenExternalLinks(true);
@@ -452,7 +453,7 @@ void MainWindow::about()
         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
         "Licensed under GNU General Public License, Version 3<br>"
         "Source code:<br><a href='https://garage.maemo.org/projects/dorian/'>"
-        "garage.maemo.org/projects/dorian</a>").arg(Platform::version()));
+        "garage.maemo.org/projects/dorian</a>").arg(version));
     aboutDialog->addWidget(label);
     aboutDialog->addStretch();
     aboutDialog->show();