Fix forward navigation control on Linux.
[dorian] / model / library.cpp
index f28181f..14082e5 100644 (file)
@@ -1,16 +1,16 @@
-#include <QSettings>
-#include <QDebug>
-#include <QFileInfo>
-
 #include "library.h"
 #include "book.h"
 #include "trace.h"
+#include "bookdb.h"
+
+static const char *DORIAN_VERSION =
+#include "pkg/version.txt"
+;
 
-Library *Library::mInstance = 0;
+static Library *theInstance = 0;
 
 Library::Library(QObject *parent): QAbstractListModel(parent)
 {
-    load();
 }
 
 Library::~Library()
@@ -20,10 +20,10 @@ Library::~Library()
 
 Library *Library::instance()
 {
-    if (!mInstance) {
-        mInstance = new Library();
+    if (!theInstance) {
+        theInstance = new Library();
     }
-    return mInstance;
+    return theInstance;
 }
 
 int Library::rowCount(const QModelIndex &parent) const
@@ -37,18 +37,23 @@ int Library::rowCount(const QModelIndex &parent) const
 
 QVariant Library::data(const QModelIndex &index, int role) const
 {
+    QVariant ret;
     if (!index.isValid()) {
-        return QVariant();
+        return ret;
     }
 
     switch (role) {
     case Qt::DisplayRole:
-        return mBooks[index.row()]->name();
+        ret = mBooks[index.row()]->name();
+        break;
     case Qt::DecorationRole:
-        return QPixmap::fromImage(mBooks[index.row()]->cover);
+        ret.setValue(mBooks[index.row()]->coverImage());
+        break;
     default:
-        return QVariant();
+        ;
     }
+
+    return ret;
 }
 
 Book *Library::book(const QModelIndex &index)
@@ -65,36 +70,37 @@ Book *Library::book(const QModelIndex &index)
 
 void Library::close()
 {
-    delete mInstance;
-    mInstance = 0;
+    delete theInstance;
+    theInstance = 0;
 }
 
 void Library::load()
 {
-    QSettings settings;
+    TRACE;
+
     clear();
-    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();
+    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();
+        // book->load();
         mBooks.append(book);
     }
+
+    QSettings settings;
     QString currentPath = settings.value("lib/nowreading").toString();
     mNowReading = find(currentPath);
+    emit endLoad();
 }
 
 void Library::save()
 {
+    TRACE;
     QSettings settings;
-    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());
-    }
     Book *currentBook = book(mNowReading);
     settings.setValue("lib/nowreading",
                       currentBook? currentBook->path(): QString());
@@ -102,7 +108,7 @@ void Library::save()
 
 bool Library::add(const QString &path)
 {
-    Trace t("Library::add " + path);
+    TRACE;
     if (path == "") {
         qCritical() << "Library::add: Empty path";
         return false;
@@ -115,6 +121,7 @@ bool Library::add(const QString &path)
     beginInsertRows(QModelIndex(), size, size);
     Book *book = new Book(path);
     book->peek();
+    book->dateAdded = QDateTime::currentDateTime().toUTC();
     mBooks.append(book);
     save();
     endInsertRows();
@@ -123,24 +130,27 @@ bool Library::add(const QString &path)
 
 void Library::remove(const QModelIndex &index)
 {
+    TRACE;
     Book *toRemove = book(index);
     if (!toRemove) {
         return;
     }
+    if (index == mNowReading) {
+        mNowReading = QModelIndex();
+        emit nowReadingChanged();
+    }
+    toRemove->remove();
     int row = index.row();
     beginRemoveRows(QModelIndex(), row, row);
     mBooks.removeAt(row);
     save();
     endRemoveRows();
-    if (index == mNowReading) {
-        mNowReading = QModelIndex();
-        emit nowReadingChanged();
-    }
     delete toRemove;
 }
 
 void Library::remove(const QString &path)
 {
+    TRACE;
     remove(find(path));
 }
 
@@ -158,6 +168,7 @@ void Library::setNowReading(const QModelIndex &index)
 
 void Library::clear()
 {
+    TRACE;
     for (int i = 0; i < mBooks.size(); i++) {
         delete mBooks[i];
     }
@@ -167,6 +178,7 @@ void Library::clear()
 
 QModelIndex Library::find(QString path) const
 {
+    TRACE;
     if (path != "") {
         QString absolutePath = QFileInfo(path).absoluteFilePath();
         for (int i = 0; i < mBooks.size(); i++) {
@@ -180,6 +192,7 @@ QModelIndex Library::find(QString path) const
 
 QModelIndex Library::find(const Book *book) const
 {
+    TRACE;
     if (book) {
         for (int i = 0; i < mBooks.size(); i++) {
             if (book == mBooks[i]) {
@@ -192,7 +205,7 @@ QModelIndex Library::find(const Book *book) const
 
 void Library::onBookOpened(const QString &path)
 {
-    Trace t("Library::onBookOpened " + path);
+    TRACE;
     QModelIndex index = find(path);
     if (index.isValid()) {
         emit dataChanged(index, index);
@@ -207,3 +220,25 @@ QStringList Library::bookPaths()
     }
     return ret;
 }
+
+void Library::upgrade()
+{
+    TRACE;
+    QSettings settings;
+    QString oldVersion = settings.value("lib/version").toString();
+    if (oldVersion.isEmpty()) {
+        int size = settings.value("lib/size").toInt();
+        emit beginUpgrade(size);
+        for (int i = 0; i < size; i++) {
+            QString key = "lib/book" + QString::number(i);
+            QString path = settings.value(key).toString();
+            emit upgrading(path);
+            Book *book = new Book(path);
+            book->upgrade();
+        }
+    } else {
+        emit beginUpgrade(0);
+    }
+    settings.setValue("lib/version", QString(DORIAN_VERSION));
+    emit endUpgrade();
+}