Add some platform helpers.
[dorian] / mainwindow.cpp
index a872f28..1b653f2 100755 (executable)
@@ -4,6 +4,8 @@
 #include <QApplication>
 #include <QFileInfo>
 #include <QStringList>
+#include <QWebView>
+#include <QWebFrame>
 
 #ifdef Q_WS_MAEMO_5
 #   include <QtMaemo5/QMaemo5InformationBox>
 #include "translucentbutton.h"
 
 #ifdef DORIAN_TEST_MODEL
-#include "modeltest.h"
-#endif
-
-#ifdef Q_WS_MAC
-#   define ICON_PREFIX ":/icons/mac/"
-#else
-#   define ICON_PREFIX ":/icons/"
+#   include "modeltest.h"
 #endif
 
 const int PROGRESS_HEIGHT = 17;
@@ -102,10 +98,7 @@ MainWindow::MainWindow(QWidget *parent):
     addToolBarAction(this, SLOT(about()), "about", tr("About"));
 #endif // Q_WS_MAEMO_5
 
-    QFrame *frame = new QFrame(toolBar);
-    frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
-    toolBar->addWidget(frame);
-
+    addToolBarSpace();
     fullScreenAction = addToolBarAction(this, SLOT(showBig()),
                                         "view-fullscreen", tr("Full screen"));
 
@@ -117,8 +110,21 @@ MainWindow::MainWindow(QWidget *parent):
     connect(Library::instance(), SIGNAL(nowReadingChanged()),
             this, SLOT(onCurrentBookChanged()));
 
-    // Load book on command line, or load last read book, or load default book
+    // Load library, upgrade it if needed
+    upgradeProgress = new QProgressDialog(tr("Upgrading library"), "", 0, 0, this);
+    upgradeProgress->reset();
+    upgradeProgress->setMinimumDuration(0);
+    upgradeProgress->setWindowModality(Qt::WindowModal);
+    upgradeProgress->setCancelButton(0);
     Library *library = Library::instance();
+    connect(library, SIGNAL(beginUpgrade(int)), this, SLOT(onBeginUpgrade(int)));
+    connect(library, SIGNAL(upgrading(const QString &)),
+            this, SLOT(onUpgrading(const QString &)));
+    connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
+    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);
@@ -126,15 +132,13 @@ MainWindow::MainWindow(QWidget *parent):
         if (index.isValid()) {
             library->setNowReading(index);
         }
-    }
-    else {
+    } else {
         QModelIndex index = library->nowReading();
         if (index.isValid()) {
             library->setNowReading(index);
-        }
-        else {
+        } else {
             if (!library->rowCount()) {
-                library->add(":/books/2 B R 0 2 B.epub");
+                library->add(":/books/2BR02B.epub");
             }
             library->setNowReading(library->index(0));
         }
@@ -187,18 +191,22 @@ void MainWindow::showRegular()
     otherChildren << progress << previousButton << nextButton;
     takeChildren(view, otherChildren);
     QRect geo = geometry();
-    qDebug() << "Geometry" << geo << "toolbar" << toolBar->height();
     progress->setGeometry(0, 0, geo.width(), PROGRESS_HEIGHT);
-#ifdef Q_WS_MAEMO_5
+#if defined(Q_WS_MAEMO_5)
     previousButton->setGeometry(0,
         geo.height() - toolBar->height() - TranslucentButton::pixels,
         TranslucentButton::pixels, TranslucentButton::pixels);
     nextButton->setGeometry(geo.width() - TranslucentButton::pixels, 0,
         TranslucentButton::pixels, TranslucentButton::pixels);
-#else
+#elif defined(Q_OS_SYMBIAN)
     previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
         TranslucentButton::pixels, TranslucentButton::pixels);
     nextButton->setGeometry(geo.width() - TranslucentButton::pixels,
+        0, TranslucentButton::pixels, TranslucentButton::pixels);
+#else
+    previousButton->setGeometry(0, geo.height() - TranslucentButton::pixels,
+        TranslucentButton::pixels, TranslucentButton::pixels);
+    nextButton->setGeometry(geo.width() - TranslucentButton::pixels - 25,
         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
 #endif // Q_WS_MAEMO_5
     qDebug() << "previousButton geometry" << previousButton->geometry();
@@ -217,8 +225,13 @@ void MainWindow::showBig()
     otherChildren << progress << nextButton << previousButton;
     QRect screen = QApplication::desktop()->screenGeometry();
     progress->setGeometry(0, 0, screen.width(), PROGRESS_HEIGHT);
+#if defined(Q_WS_MAEMO_5)
     nextButton->setGeometry(screen.width() - TranslucentButton::pixels, 0,
         TranslucentButton::pixels, TranslucentButton::pixels);
+#else
+    nextButton->setGeometry(screen.width() - TranslucentButton::pixels - 25, 0,
+                            TranslucentButton::pixels, TranslucentButton::pixels);
+#endif // Q_WS_MAEMO_5
     previousButton->setGeometry(0, screen.height() - TranslucentButton::pixels,
         TranslucentButton::pixels, TranslucentButton::pixels);
 
@@ -250,7 +263,8 @@ void MainWindow::showSettings()
 void MainWindow::showInfo()
 {
     if (mCurrent.isValid()) {
-        (new InfoDialog(Library::instance()->book(mCurrent), this, false))->exec();
+        (new InfoDialog(Library::instance()->book(mCurrent), this, false))->
+                exec();
     }
 }
 
@@ -288,8 +302,7 @@ void MainWindow::onSettingsChanged(const QString &key)
         if (value == "portrait") {
             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
-        }
-        else {
+        } else {
             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
         }
@@ -392,17 +405,21 @@ void MainWindow::resizeEvent(QResizeEvent *e)
 {
     Trace t("MainWindow::resizeEvent");
     progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
-    qDebug() << "Toolbar height" << toolBar->height();
-#ifdef Q_WS_MAEMO_5
+#if defined(Q_WS_MAEMO_5)
     previousButton->setGeometry(0,
         e->size().height() - toolBar->height() - TranslucentButton::pixels,
         TranslucentButton::pixels, TranslucentButton::pixels);
     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels, 0,
         TranslucentButton::pixels, TranslucentButton::pixels);
-#else
+#elif defined(Q_OS_SYMBIAN)
     previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
         TranslucentButton::pixels, TranslucentButton::pixels);
     nextButton->setGeometry(e->size().width() - TranslucentButton::pixels,
+        0, TranslucentButton::pixels, TranslucentButton::pixels);
+#else
+    previousButton->setGeometry(0, e->size().height() - TranslucentButton::pixels,
+        TranslucentButton::pixels, TranslucentButton::pixels);
+    nextButton->setGeometry(e->size().width() - TranslucentButton::pixels - 25,
         toolBar->height(), TranslucentButton::pixels, TranslucentButton::pixels);
 #endif // Q_WS_MAEMO_5
     qDebug() << "previousButton geometry" << previousButton->geometry();
@@ -418,12 +435,14 @@ void MainWindow::about()
     QLabel *label = new QLabel(aboutDialog);
     label->setTextFormat(Qt::RichText);
     label->setOpenExternalLinks(true);
-    label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 by "
+    label->setWordWrap(true);
+    label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 "
         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
         "Licensed under GNU General Public License, Version 3<br>"
         "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
         "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
     aboutDialog->addWidget(label);
+    aboutDialog->addStretch();
     aboutDialog->show();
 }
 
@@ -441,3 +460,23 @@ void MainWindow::goToPreviousPage()
     previousButton->flash(1500);
     view->goPreviousPage();
 }
+
+void MainWindow::onBeginUpgrade(int total)
+{
+    upgradeProgress->setVisible(total > 0);
+    upgradeProgress->setMaximum(total);
+}
+
+void MainWindow::onUpgrading(const QString &path)
+{
+    upgradeProgress->setLabelText(tr("Upgrading %1").
+                                  arg(QFileInfo(path).fileName()));
+    upgradeProgress->setValue(upgradeProgress->value() + 1);
+}
+
+void MainWindow::onEndUpgrade()
+{
+    upgradeProgress->hide();
+    upgradeProgress->reset();
+}
+