Store library in database.
authorAkos Polster <akos@pipacs.com>
Fri, 8 Oct 2010 22:44:42 +0000 (00:44 +0200)
committerAkos Polster <akos@pipacs.com>
Fri, 8 Oct 2010 22:44:42 +0000 (00:44 +0200)
dorian.pro
model/book.cpp
model/bookdb.cpp [new file with mode: 0644]
model/bookdb.h [new file with mode: 0644]
model/library.cpp

index a131804..cab9378 100644 (file)
@@ -1,4 +1,4 @@
-QT += webkit xml
+QT += webkit xml sql
 
 INCLUDEPATH += $$PWD \
     $$PWD/model \
@@ -31,7 +31,8 @@ SOURCES += \
     widgets/listwindow.cpp \
     widgets/progress.cpp \
     widgets/adopterwindow.cpp \
-    platform.cpp
+    platform.cpp \
+    model/bookdb.cpp
 
 HEADERS += \
     mainwindow.h \
@@ -65,7 +66,8 @@ HEADERS += \
     widgets/adopterwindow.h \
     widgets/listview.h \
     model/xmlhandler.h \
-    platform.h
+    platform.h \
+    model/bookdb.h
 
 RESOURCES += \
     dorian.qrc
index bfaf451..a055dcf 100644 (file)
@@ -6,7 +6,7 @@
 #include <QDirIterator>
 #include <QFileInfo>
 #include <QtAlgorithms>
-#include <QCryptographicHash>
+#include <QVariantHash>
 
 #include "book.h"
 #include "opshandler.h"
@@ -16,6 +16,7 @@
 #include "containerhandler.h"
 #include "ncxhandler.h"
 #include "trace.h"
+#include "bookdb.h"
 
 const int COVER_WIDTH = 53;
 const int COVER_HEIGHT = 59;
@@ -277,6 +278,32 @@ void Book::load()
 {
     Trace t("Book::load");
     qDebug() << "path" << path();
+
+#if 1
+    QVariantHash data = BookDb::instance()->load(path());
+    title = data["title"].toString();
+    qDebug() << title;
+    creators = data["creators"].toStringList();
+    date = data["date"].toString();
+    publisher = data["publisher"].toString();
+    datePublished = data["datepublished"].toString();
+    subject = data["subject"].toString();
+    source = data["source"].toString();
+    rights = data["rights"].toString();
+    mLastBookmark.part = data["lastpart"].toInt();
+    mLastBookmark.pos = data["lastpos"].toReal();
+    cover = data["cover"].value<QImage>().scaled(COVER_WIDTH,
+        COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
+    if (cover.isNull()) {
+        cover = makeCover(":/icons/book.png");
+    }
+    int size = data["bookmarks"].toInt();
+    for (int i = 0; i < size; i++) {
+        int part = data[QString("bookmark%1part").arg(i)].toInt();
+        qreal pos = data[QString("bookmark%1pos").arg(i)].toReal();
+        mBookmarks.append(Bookmark(part, pos));
+    }
+#else
     QSettings settings;
     QString key = "book/" + path() + "/";
     qDebug() << "key" << key;
@@ -310,11 +337,33 @@ void Book::load()
                 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;
+    data["date"] = date;
+    data["publisher"] = publisher;
+    data["datepublished"] = datePublished;
+    data["subject"] = subject;
+    data["source"] = source;
+    data["rights"] = rights;
+    data["lastpart"] = mLastBookmark.part;
+    data["lastpos"] = mLastBookmark.pos;
+    data["cover"] = cover;
+    data["bookmarks"] = mBookmarks.size();
+    for (int i = 0; i < mBookmarks.size(); i++) {
+        data[QString("bookmark%1part").arg(i)] = mBookmarks[i].part;
+        data[QString("bookmark%1pos").arg(i)] = mBookmarks[i].pos;
+    }
+    BookDb::instance()->save(path(), data);
+#else
     QSettings settings;
     QString key = "book/" + path() + "/";
     qDebug() << "key" << key;
@@ -343,6 +392,7 @@ void Book::save()
         settings.setValue(key + "bookmark" + QString::number(i) + "/pos",
                           mBookmarks[i].pos);
     }
+#endif
 }
 
 void Book::setLastBookmark(int part, qreal position)
diff --git a/model/bookdb.cpp b/model/bookdb.cpp
new file mode 100644 (file)
index 0000000..c5b5107
--- /dev/null
@@ -0,0 +1,102 @@
+#include <QFileInfo>
+
+#include "bookdb.h"
+#include "platform.h"
+#include "trace.h"
+
+BookDb *theInstance;
+
+BookDb *BookDb::instance()
+{
+    if (!theInstance) {
+        theInstance = new BookDb;
+    }
+    return theInstance;
+}
+
+void BookDb::close()
+{
+    if (theInstance) {
+        delete theInstance;
+        theInstance = 0;
+    }
+}
+
+BookDb::BookDb()
+{
+    Trace t("BookDb::BookDb");
+    bool shouldCreate = false;
+    QFileInfo info(Platform::dbPath());
+    if (!info.exists()) {
+        QDir dbDir;
+        dbDir.mkpath(info.absolutePath());
+        shouldCreate = true;
+    }
+    db = QSqlDatabase::addDatabase("QSQLITE");
+    db.setDatabaseName(Platform::dbPath());
+    if (!db.open()) {
+        qCritical() << "Could not open" << Platform::dbPath();
+    }
+    if (shouldCreate) {
+        create();
+    }
+}
+
+BookDb::~BookDb()
+{
+    db.close();
+}
+
+void BookDb::create()
+{
+    Trace t("BookDb::create");
+    QSqlQuery query;
+    if (!query.exec("create table book (name text primary key, content blob)")) {
+        qCritical() << "Failed to create database";
+    }
+}
+
+QVariantHash BookDb::load(const QString &book)
+{
+    Trace t("BookDb::load");
+    qDebug() << book;
+    QVariantHash ret;
+    QSqlQuery query("select content from book where name = ?");
+    query.bindValue(0, book);
+    while (query.next()) {
+        ret = query.value(0).toHash();
+        break;
+    }
+    qDebug() << ret;
+    return ret;
+}
+
+void BookDb::save(const QString &book, const QVariantHash &data)
+{
+    Trace t("BookDb::save");
+    qDebug() << book;
+    QSqlQuery query("insert or replace into book values (?, ?)");
+    query.bindValue(0, book);
+    query.bindValue(1, data);
+    if (!query.exec()) {
+        qCritical() << "Failed to insert or replace";
+    }
+}
+
+void BookDb::remove(const QString &book)
+{
+    Q_UNUSED(book);
+}
+
+QStringList BookDb::books()
+{
+    Trace t("BookDb::books");
+    QStringList ret;
+    QSqlQuery query("select name from book");
+    query.setForwardOnly(true);
+    while (query.next()) {
+        ret.append(query.value(0).toString());
+    }
+    qDebug() << ret;
+    return ret;
+}
diff --git a/model/bookdb.h b/model/bookdb.h
new file mode 100644 (file)
index 0000000..3bfa5bb
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef BOOKDB_H
+#define BOOKDB_H
+
+#include <QVariantHash>
+#include <QtSql>
+
+class QString;
+
+class BookDb
+{
+public:
+    static BookDb *instance();
+    static void close();
+    QVariantHash load(const QString &book);
+    void save(const QString &book, const QVariantHash &data);
+    void remove(const QString &book);
+    QStringList books();
+
+private:
+    BookDb();
+    ~BookDb();
+    void create();
+    QSqlDatabase db;
+};
+
+#endif // BOOKDB_H
index 84d51e9..83f3870 100644 (file)
@@ -5,6 +5,7 @@
 #include "library.h"
 #include "book.h"
 #include "trace.h"
+#include "bookdb.h"
 
 static const char *DORIAN_VERSION =
 #include "pkg/version.txt"
@@ -76,6 +77,7 @@ void Library::load()
 {
     QSettings settings;
     clear();
+#if 0
     int size = settings.value("lib/size").toInt();
     for (int i = 0; i < size; i++) {
         QString key = "lib/book" + QString::number(i);
@@ -86,6 +88,16 @@ void Library::load()
         book->load();
         mBooks.append(book);
     }
+#else
+    foreach(QString path, BookDb::instance()->books()) {
+        Book *book = new Book(path);
+        connect(book, SIGNAL(opened(const QString &)),
+                this, SLOT(onBookOpened(const QString &)));
+        book->load();
+        mBooks.append(book);
+    }
+#endif
+
     QString currentPath = settings.value("lib/nowreading").toString();
     mNowReading = find(currentPath);
 }
@@ -93,11 +105,13 @@ void Library::load()
 void 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());