Keep book metadata in Sqlite. Display progress while loading and upgrading library.
authorAkos Polster <akos@pipacs.com>
Sat, 9 Oct 2010 17:19:44 +0000 (19:19 +0200)
committerAkos Polster <akos@pipacs.com>
Sat, 9 Oct 2010 17:19:44 +0000 (19:19 +0200)
mainwindow.cpp
mainwindow.h
model/book.cpp
model/book.h
model/bookdb.cpp
model/library.cpp
model/library.h
pkg/changelog
pkg/maemo/control

index 1b653f2..9c47468 100755 (executable)
@@ -111,16 +111,20 @@ MainWindow::MainWindow(QWidget *parent):
             this, SLOT(onCurrentBookChanged()));
 
     // 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);
+    libraryProgress = new QProgressDialog(tr("Upgrading library"), "", 0, 0, this);
+    libraryProgress->reset();
+    libraryProgress->setMinimumDuration(0);
+    libraryProgress->setWindowModality(Qt::WindowModal);
+    libraryProgress->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()));
+    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();
 
@@ -463,20 +467,42 @@ void MainWindow::goToPreviousPage()
 
 void MainWindow::onBeginUpgrade(int total)
 {
-    upgradeProgress->setVisible(total > 0);
-    upgradeProgress->setMaximum(total);
+    libraryProgress->setVisible(total > 0);
+    libraryProgress->setWindowTitle(tr("Upgrading library"));
+    libraryProgress->setMaximum(total);
 }
 
 void MainWindow::onUpgrading(const QString &path)
 {
-    upgradeProgress->setLabelText(tr("Upgrading %1").
+    libraryProgress->setLabelText(tr("Upgrading %1").
                                   arg(QFileInfo(path).fileName()));
-    upgradeProgress->setValue(upgradeProgress->value() + 1);
+    libraryProgress->setValue(libraryProgress->value() + 1);
 }
 
 void MainWindow::onEndUpgrade()
 {
-    upgradeProgress->hide();
-    upgradeProgress->reset();
+    libraryProgress->hide();
+    libraryProgress->reset();
+}
+
+
+void MainWindow::onBeginLoad(int total)
+{
+    libraryProgress->setVisible(total > 0);
+    libraryProgress->setWindowTitle(tr("Loading library"));
+    libraryProgress->setMaximum(total);
+}
+
+void MainWindow::onLoading(const QString &path)
+{
+    libraryProgress->setLabelText(tr("Loading %1").
+                                  arg(QFileInfo(path).fileName()));
+    libraryProgress->setValue(libraryProgress->value() + 1);
+}
+
+void MainWindow::onEndLoad()
+{
+    libraryProgress->hide();
+    libraryProgress->reset();
 }
 
index 60f3d4e..416865f 100755 (executable)
@@ -44,6 +44,9 @@ public slots:
     void onBeginUpgrade(int total);
     void onUpgrading(const QString &book);
     void onEndUpgrade();
+    void onBeginLoad(int total);
+    void onLoading(const QString &book);
+    void onEndLoad();
 
 protected:
     void closeEvent(QCloseEvent *event);
@@ -70,7 +73,7 @@ private:
     Progress *progress;
     TranslucentButton *previousButton;
     TranslucentButton *nextButton;
-    QProgressDialog *upgradeProgress;
+    QProgressDialog *libraryProgress;
 };
 
 #endif // MAINWINDOW_H
index a055dcf..3b21603 100644 (file)
@@ -279,7 +279,6 @@ void Book::load()
     Trace t("Book::load");
     qDebug() << "path" << path();
 
-#if 1
     QVariantHash data = BookDb::instance()->load(path());
     title = data["title"].toString();
     qDebug() << title;
@@ -303,48 +302,12 @@ void Book::load()
         qreal pos = data[QString("bookmark%1pos").arg(i)].toReal();
         mBookmarks.append(Bookmark(part, pos));
     }
-#else
-    QSettings settings;
-    QString key = "book/" + path() + "/";
-    qDebug() << "key" << key;
-
-    // Load book info
-    title = settings.value(key + "title").toString();
-    qDebug() << title;
-    creators = settings.value(key + "creators").toStringList();
-    date = settings.value(key + "date").toString();
-    publisher = settings.value(key + "publisher").toString();
-    datePublished = settings.value(key + "datepublished").toString();
-    subject = settings.value(key + "subject").toString();
-    source = settings.value(key + "source").toString();
-    rights = settings.value(key + "rights").toString();
-    mLastBookmark.part = settings.value(key + "lastpart").toInt();
-    mLastBookmark.pos = settings.value(key + "lastpos").toReal();
-    cover = settings.value(key + "cover").value<QImage>().scaled(COVER_WIDTH,
-        COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
-    if (cover.isNull()) {
-        cover = makeCover(":/icons/book.png");
-    }
-
-    // Load bookmarks
-    int size = settings.value(key + "bookmarks").toInt();
-    for (int i = 0; i < size; i++) {
-        int part = settings.value(key + "bookmark" + QString::number(i) +
-                                     "/part").toInt();
-        qreal pos = settings.value(key + "bookmark" + QString::number(i) +
-                                   "/pos").toReal();
-        qDebug() << QString("Bookmark %1 at part %2, %3").
-                arg(i).arg(part).arg(pos);
-        mBookmarks.append(Bookmark(part, pos));
-    }
-#endif
 }
 
 void Book::save()
 {
     Trace t("Book::save");
 
-#if 1
     QVariantHash data;
     data["title"] = title;
     data["creators"] = creators;
@@ -363,40 +326,12 @@ void Book::save()
         data[QString("bookmark%1pos").arg(i)] = mBookmarks[i].pos;
     }
     BookDb::instance()->save(path(), data);
-#else
-    QSettings settings;
-    QString key = "book/" + path() + "/";
-    qDebug() << "key" << key;
-
-    // Save book info
-    settings.setValue(key + "title", title);
-    qDebug() << "title" << title;
-    settings.setValue(key + "creators", creators);
-    settings.setValue(key + "date", date);
-    settings.setValue(key + "publisher", publisher);
-    settings.setValue(key + "datepublished", datePublished);
-    settings.setValue(key + "subject", subject);
-    settings.setValue(key + "source", source);
-    settings.setValue(key + "rights", rights);
-    settings.setValue(key + "lastpart", mLastBookmark.part);
-    settings.setValue(key + "lastpos", mLastBookmark.pos);
-    settings.setValue(key + "cover", cover);
-
-    // Save bookmarks
-    settings.setValue(key + "bookmarks", mBookmarks.size());
-    for (int i = 0; i < mBookmarks.size(); i++) {
-        qDebug() << QString("Bookmark %1 at %2, %3").
-                arg(i).arg(mBookmarks[i].part).arg(mBookmarks[i].pos);
-        settings.setValue(key + "bookmark" + QString::number(i) + "/part",
-                          mBookmarks[i].part);
-        settings.setValue(key + "bookmark" + QString::number(i) + "/pos",
-                          mBookmarks[i].pos);
-    }
-#endif
 }
 
 void Book::setLastBookmark(int part, qreal position)
 {
+    Trace t("Book:setLastBookmark");
+    qDebug() << "part" << part << "position" << position;
     mLastBookmark.part = part;
     mLastBookmark.pos = position;
     save();
@@ -551,5 +486,47 @@ bool Book::extractMetaData()
 void Book::upgrade()
 {
     Trace t("Book::upgrade");
+
     qDebug() << path();
+
+    // Load book from old database (QSettings)
+
+    QSettings settings;
+    QString key = "book/" + path() + "/";
+    title = settings.value(key + "title").toString();
+    qDebug() << title;
+    creators = settings.value(key + "creators").toStringList();
+    date = settings.value(key + "date").toString();
+    publisher = settings.value(key + "publisher").toString();
+    datePublished = settings.value(key + "datepublished").toString();
+    subject = settings.value(key + "subject").toString();
+    source = settings.value(key + "source").toString();
+    rights = settings.value(key + "rights").toString();
+    mLastBookmark.part = settings.value(key + "lastpart").toInt();
+    mLastBookmark.pos = settings.value(key + "lastpos").toReal();
+    cover = settings.value(key + "cover").value<QImage>().scaled(COVER_WIDTH,
+        COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
+    if (cover.isNull()) {
+        cover = makeCover(":/icons/book.png");
+    }
+    int size = settings.value(key + "bookmarks").toInt();
+    for (int i = 0; i < size; i++) {
+        int part = settings.value(key + "bookmark" + QString::number(i) +
+                                     "/part").toInt();
+        qreal pos = settings.value(key + "bookmark" + QString::number(i) +
+                                   "/pos").toReal();
+        qDebug() << QString("Bookmark %1 at part %2, %3").
+                arg(i).arg(part).arg(pos);
+        mBookmarks.append(Bookmark(part, pos));
+    }
+
+    // Save book to new database
+
+    save();
+}
+
+void Book::remove()
+{
+    Trace t("Book::remove");
+    BookDb::instance()->remove(path());
 }
index a5d5175..8639b07 100644 (file)
@@ -51,6 +51,9 @@ public:
     /** Upgrade persistent storage of book meta-data. */
     void upgrade();
 
+    /** Delete book meta-data from persistent storage. */
+    void remove();
+
     /** Extract and parse EPUB contents, fill in all members except mPath. */
     bool open();
 
index c5b5107..c2987c0 100644 (file)
@@ -61,10 +61,18 @@ QVariantHash BookDb::load(const QString &book)
     Trace t("BookDb::load");
     qDebug() << book;
     QVariantHash ret;
+    QByteArray bytes;
     QSqlQuery query("select content from book where name = ?");
     query.bindValue(0, book);
+    query.setForwardOnly(true);
+    if (!query.exec()) {
+        qCritical() << "Query failed";
+        return ret;
+    }
     while (query.next()) {
-        ret = query.value(0).toHash();
+        bytes = query.value(0).toByteArray();
+        QDataStream in(bytes);
+        in >> ret;
         break;
     }
     qDebug() << ret;
@@ -75,16 +83,21 @@ void BookDb::save(const QString &book, const QVariantHash &data)
 {
     Trace t("BookDb::save");
     qDebug() << book;
+    qDebug() << data;
+    QByteArray bytes;
+    QDataStream out(&bytes, QIODevice::WriteOnly);
+    out << data;
     QSqlQuery query("insert or replace into book values (?, ?)");
     query.bindValue(0, book);
-    query.bindValue(1, data);
+    query.bindValue(1, bytes);
     if (!query.exec()) {
-        qCritical() << "Failed to insert or replace";
+        qCritical() << "Query failed";
     }
 }
 
 void BookDb::remove(const QString &book)
 {
+    // FIXME
     Q_UNUSED(book);
 }
 
@@ -94,6 +107,10 @@ QStringList BookDb::books()
     QStringList ret;
     QSqlQuery query("select name from book");
     query.setForwardOnly(true);
+    if (!query.exec()) {
+        qCritical() << "Query failed";
+        return ret;
+    }
     while (query.next()) {
         ret.append(query.value(0).toString());
     }
index 83f3870..f4a5ef9 100644 (file)
@@ -75,43 +75,31 @@ void Library::close()
 
 void Library::load()
 {
-    QSettings settings;
+    Trace t("Library::load");
+
     clear();
-#if 0
-    int size = settings.value("lib/size").toInt();
-    for (int i = 0; i < size; i++) {
-        QString key = "lib/book" + QString::number(i);
-        QString path = settings.value(key).toString();
-        Book *book = new Book(path);
-        connect(book, SIGNAL(opened(const QString &)),
-                this, SLOT(onBookOpened(const QString &)));
-        book->load();
-        mBooks.append(book);
-    }
-#else
-    foreach(QString path, BookDb::instance()->books()) {
+    QStringList books = BookDb::instance()->books();
+    emit beginLoad(books.size());
+
+    foreach(QString path, books) {
+        emit loading(path);
         Book *book = new Book(path);
         connect(book, SIGNAL(opened(const QString &)),
                 this, SLOT(onBookOpened(const QString &)));
         book->load();
         mBooks.append(book);
     }
-#endif
 
+    QSettings settings;
     QString currentPath = settings.value("lib/nowreading").toString();
     mNowReading = find(currentPath);
+    emit endLoad();
 }
 
 void Library::save()
 {
+    Trace t("Library::save");
     QSettings settings;
-#if 0
-    settings.setValue("lib/size", mBooks.size());
-    for (int i = 0; i < mBooks.size(); i++) {
-        QString key = "lib/book" + QString::number(i);
-        settings.setValue(key, mBooks[i]->path());
-    }
-#endif
     Book *currentBook = book(mNowReading);
     settings.setValue("lib/nowreading",
                       currentBook? currentBook->path(): QString());
@@ -140,10 +128,12 @@ bool Library::add(const QString &path)
 
 void Library::remove(const QModelIndex &index)
 {
+    Trace t("Library::remove");
     Book *toRemove = book(index);
     if (!toRemove) {
         return;
     }
+    toRemove->remove();
     int row = index.row();
     beginRemoveRows(QModelIndex(), row, row);
     mBooks.removeAt(row);
@@ -230,7 +220,7 @@ void Library::upgrade()
     Trace t("Library::upgrade");
     QSettings settings;
     QString oldVersion = settings.value("lib/version").toString();
-    if (true /* oldVersion.isEmpty() */) {
+    if (/* true */ oldVersion.isEmpty()) {
         int size = settings.value("lib/size").toInt();
         emit beginUpgrade(size);
         for (int i = 0; i < size; i++) {
index ed5582f..0a4a7f3 100644 (file)
@@ -35,6 +35,9 @@ signals:
     void beginUpgrade(int total);
     void upgrading(const QString &book);
     void endUpgrade();
+    void beginLoad(int total);
+    void loading(const QString &book);
+    void endLoad();
 
 public slots:
     bool add(const QString &path);
index b0eb939..f891689 100644 (file)
@@ -1,6 +1,8 @@
-dorian (0.2.3-1) unstable; urgency=low
+dorian (0.3.0-1) unstable; urgency=low
 
   * Keep library data in database
+  * Upgrade from old data format if needed
+  * Show progress during library load and upgrade
 
  -- Akos Polster <akos@pipacs.com>  Thu,  8 Oct 2010 02:00:00 +0200
 
index 6b176ff..3848670 100644 (file)
@@ -8,7 +8,7 @@ XSBC-Bugtracker: https://garage.maemo.org/tracker/?atid=6138&group_id=1757
 
 Package: dorian
 Architecture: armel
-Depends: zlib1g, libqt4-core (>= 4.6.1), libqt4-gui (>= 4.6.1), libqt4-webkit (>= 4.6.1), libqt4-xml (>= 4.6.1), libqt4-dbus (>= 4.6.1), mce
+Depends: zlib1g, libqt4-core (>= 4.6.1), libqt4-gui (>= 4.6.1), libqt4-webkit (>= 4.6.1), libqt4-xml (>= 4.6.1), libqt4-dbus (>= 4.6.1), libqt4-sql (>= 4.6.1), libqt4-sql-sqlite (>= 4.6.1), mce
 Description: E-book reader
  Read books published in DRM-free EPUB format
 XB-Maemo-Display-Name: Dorian