-QT += webkit xml
+QT += webkit xml sql
INCLUDEPATH += $$PWD \
$$PWD/model \
widgets/listwindow.cpp \
widgets/progress.cpp \
widgets/adopterwindow.cpp \
- platform.cpp
+ platform.cpp \
+ model/bookdb.cpp
HEADERS += \
mainwindow.h \
widgets/adopterwindow.h \
widgets/listview.h \
model/xmlhandler.h \
- platform.h
+ platform.h \
+ model/bookdb.h
RESOURCES += \
dorian.qrc
#include <QDirIterator>
#include <QFileInfo>
#include <QtAlgorithms>
-#include <QCryptographicHash>
+#include <QVariantHash>
#include "book.h"
#include "opshandler.h"
#include "containerhandler.h"
#include "ncxhandler.h"
#include "trace.h"
+#include "bookdb.h"
const int COVER_WIDTH = 53;
const int COVER_HEIGHT = 59;
{
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;
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;
settings.setValue(key + "bookmark" + QString::number(i) + "/pos",
mBookmarks[i].pos);
}
+#endif
}
void Book::setLastBookmark(int part, qreal position)
--- /dev/null
+#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;
+}
--- /dev/null
+#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
#include "library.h"
#include "book.h"
#include "trace.h"
+#include "bookdb.h"
static const char *DORIAN_VERSION =
#include "pkg/version.txt"
{
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);
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);
}
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());