From 6ddc8e668c46c2ae5920992637e9922285f92c20 Mon Sep 17 00:00:00 2001 From: Akos Polster Date: Sat, 9 Oct 2010 19:19:44 +0200 Subject: [PATCH] Keep book metadata in Sqlite. Display progress while loading and upgrading library. --- mainwindow.cpp | 48 +++++++++++++++++------ mainwindow.h | 5 ++- model/book.cpp | 111 +++++++++++++++++++++-------------------------------- model/book.h | 3 ++ model/bookdb.cpp | 23 +++++++++-- model/library.cpp | 36 +++++++---------- model/library.h | 3 ++ pkg/changelog | 4 +- pkg/maemo/control | 2 +- 9 files changed, 128 insertions(+), 107 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 1b653f2..9c47468 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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(); } diff --git a/mainwindow.h b/mainwindow.h index 60f3d4e..416865f 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -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 diff --git a/model/book.cpp b/model/book.cpp index a055dcf..3b21603 100644 --- a/model/book.cpp +++ b/model/book.cpp @@ -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().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().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()); } diff --git a/model/book.h b/model/book.h index a5d5175..8639b07 100644 --- a/model/book.h +++ b/model/book.h @@ -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(); diff --git a/model/bookdb.cpp b/model/bookdb.cpp index c5b5107..c2987c0 100644 --- a/model/bookdb.cpp +++ b/model/bookdb.cpp @@ -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()); } diff --git a/model/library.cpp b/model/library.cpp index 83f3870..f4a5ef9 100644 --- a/model/library.cpp +++ b/model/library.cpp @@ -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++) { diff --git a/model/library.h b/model/library.h index ed5582f..0a4a7f3 100644 --- a/model/library.h +++ b/model/library.h @@ -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); diff --git a/pkg/changelog b/pkg/changelog index b0eb939..f891689 100644 --- a/pkg/changelog +++ b/pkg/changelog @@ -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 Thu, 8 Oct 2010 02:00:00 +0200 diff --git a/pkg/maemo/control b/pkg/maemo/control index 6b176ff..3848670 100644 --- a/pkg/maemo/control +++ b/pkg/maemo/control @@ -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 -- 1.7.9.5