Bookmarks with notes, first attempt.
[dorian] / model / book.cpp
index 8b7bd4a..f403769 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;
@@ -54,7 +55,7 @@ bool Book::open()
         title = "No book";
         return false;
     }
-    if (!extract()) {
+    if (!extract(QStringList())) {
         return false;
     }
     if (!parse()) {
@@ -97,10 +98,11 @@ void Book::close()
 QString Book::tmpDir() const
 {
     QString tmpName = QFileInfo(mTempFile.fileName()).fileName();
-    return QDir::tempPath() + "/dorian/" + tmpName;
+    return QDir(QDir::temp().absoluteFilePath("dorian")).
+            absoluteFilePath(tmpName);
 }
 
-bool Book::extract()
+bool Book::extract(const QStringList &excludedExtensions)
 {
     Trace t("Book::extract");
     bool ret = false;
@@ -112,20 +114,22 @@ bool Book::extract()
         qCritical() << "Book::extract: Failed to remove" << tmp;
         return false;
     }
-    QDir d;
-    if (!d.mkpath(tmp)) {
-        qCritical() << "Book::extract: Could not create" << tmp;
-        return false;
+    QDir d(tmp);
+    if (!d.exists()) {
+        if (!d.mkpath(tmp)) {
+            qCritical() << "Book::extract: Could not create" << tmp;
+            return false;
+        }
     }
 
     // If book comes from resource, copy it to the temporary directory first
     QString bookPath = path();
     if (bookPath.startsWith(":/books/")) {
         QFile src(bookPath);
-        QString dst(tmp + "/book.epub");
+        QString dst(QDir(tmp).absoluteFilePath("book.epub"));
         if (!src.copy(dst)) {
-            qCritical() << "Book::extract: Failed to copy built-in book to"
-                    << dst;
+            qCritical() << "Book::extract: Failed to copy built-in book"
+                    << bookPath << "to" << dst;
             return false;
         }
         bookPath = dst;
@@ -136,7 +140,7 @@ bool Book::extract()
         qCritical() << "Book::extract: Could not change to" << tmp;
         return false;
     }
-    ret = extractZip(bookPath);
+    ret = extractZip(bookPath, excludedExtensions);
     if (!ret) {
         qCritical() << "Book::extract: Extracting ZIP failed";
     }
@@ -169,15 +173,26 @@ bool Book::parse()
     chapters = parts;
 
     // Load cover image
+    QString coverPath;
     QStringList coverKeys;
     coverKeys << "cover-image" << "img-cover-jpeg" << "cover";
     foreach (QString key, coverKeys) {
         if (content.contains(key)) {
-            qDebug() << "Loading cover image from" << content[key].href;
-            cover = makeCover(rootPath() + "/" + content[key].href);
+            coverPath = QDir(rootPath()).absoluteFilePath(content[key].href);
             break;
         }
     }
+    if (coverPath.isEmpty()) {
+        // Last resort
+        QString coverJpeg = QDir(rootPath()).absoluteFilePath("cover.jpg");
+        if (QFileInfo(coverJpeg).exists()) {
+            coverPath = coverJpeg;
+        }
+    }
+    if (!coverPath.isEmpty()) {
+        qDebug() << "Loading cover image from" << coverPath;
+        cover = makeCover(coverPath);
+    }
 
     // If there is an "ncx" item in content, parse it: That's the real table of
     // contents
@@ -193,7 +208,7 @@ bool Book::parse()
     }
     if (!ncxFileName.isEmpty()) {
         qDebug() << "Parsing NCX file" << ncxFileName;
-        QFile ncxFile(rootPath() + "/" + ncxFileName);
+        QFile ncxFile(QDir(rootPath()).absoluteFilePath(ncxFileName));
         source = new QXmlInputSource(&ncxFile);
         NcxHandler *ncxHandler = new NcxHandler(*this);
         errorHandler = new XmlErrorHandler();
@@ -208,7 +223,7 @@ bool Book::parse()
     // Calculate book part sizes
     size = 0;
     foreach (QString part, parts) {
-        QFileInfo info(content[part].href);
+        QFileInfo info(QDir(rootPath()).absoluteFilePath(content[part].href));
         content[part].size = info.size();
         size += content[part].size;
     }
@@ -263,76 +278,62 @@ void Book::load()
 {
     Trace t("Book::load");
     qDebug() << "path" << path();
-    QSettings settings;
-    QString key = "book/" + path() + "/";
-    qDebug() << "key" << key;
 
-    // Load book info
-    title = settings.value(key + "title").toString();
+    QVariantHash data = BookDb::instance()->load(path());
+    title = data["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,
+    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");
     }
-
-    // Load bookmarks
-    int size = settings.value(key + "bookmarks").toInt();
+    int size = data["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));
+        int part = data[QString("bookmark%1part").arg(i)].toInt();
+        qreal pos = data[QString("bookmark%1pos").arg(i)].toReal();
+        QString note = data[QString("bookmark%1note").arg(i)].toString();
+        mBookmarks.append(Bookmark(part, pos, note));
     }
 }
 
 void Book::save()
 {
     Trace t("Book::save");
-    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());
+
+    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++) {
-        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);
+        data[QString("bookmark%1part").arg(i)] = mBookmarks[i].part;
+        data[QString("bookmark%1pos").arg(i)] = mBookmarks[i].pos;
+        data[QString("bookmark%1note").arg(i)] = mBookmarks[i].note;
     }
+    BookDb::instance()->save(path(), data);
 }
 
 void Book::setLastBookmark(int part, qreal position)
 {
+    Trace t("Book:setLastBookmark");
+    qDebug() << "part" << part << "position" << position;
     mLastBookmark.part = part;
     mLastBookmark.pos = position;
     save();
@@ -343,9 +344,9 @@ Book::Bookmark Book::lastBookmark() const
     return Book::Bookmark(mLastBookmark);
 }
 
-void Book::addBookmark(int part, qreal position)
+void Book::addBookmark(int part, qreal position, const QString &note)
 {
-    mBookmarks.append(Bookmark(part, position));
+    mBookmarks.append(Bookmark(part, position, note));
     qSort(mBookmarks.begin(), mBookmarks.end());
     save();
 }
@@ -441,27 +442,25 @@ int Book::partFromChapter(int index)
     Trace t("Book::partFromChapter");
     QString id = chapters[index];
     QString href = content[id].href;
-    QString baseRef(href);
-    QUrl url(QString("file://") + href);
-    if (url.hasFragment()) {
-        QString fragment = url.fragment();
-        baseRef.chop(fragment.length() + 1);
+    int hashPos = href.indexOf("#");
+    if (hashPos != -1) {
+        href = href.left(hashPos);
     }
+
     qDebug() << "Chapter" << index;
     qDebug() << " id" << id;
     qDebug() << " href" << href;
-    qDebug() << " base href" << baseRef;
 
     for (int i = 0; i < parts.size(); i++) {
         QString partId = parts[i];
-        if (content[partId].href == baseRef) {
-            qDebug() << "Part index for" << baseRef << "is" << i;
+        if (content[partId].href == href) {
+            qDebug() << "Part index for" << href << "is" << i;
             return i;
         }
     }
 
     qWarning() << "Book::partFromChapter: Could not find part index for"
-            << baseRef;
+            << href;
     return -1;
 }
 
@@ -481,6 +480,55 @@ qreal Book::getProgress(int part, qreal position)
 
 bool Book::extractMetaData()
 {
-    // FIXME
-    return extract();
+    QStringList excludedExtensions;
+    excludedExtensions << ".html" << ".xhtml" << ".xht" << ".htm";
+    return extract(excludedExtensions);
+}
+
+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());
 }