Fix cover images.
[dorian] / model / library.cpp
index f4a5ef9..1a66639 100644 (file)
@@ -1,7 +1,3 @@
-#include <QSettings>
-#include <QDebug>
-#include <QFileInfo>
-
 #include "library.h"
 #include "book.h"
 #include "trace.h"
@@ -11,7 +7,7 @@ static const char *DORIAN_VERSION =
 #include "pkg/version.txt"
 ;
 
-Library *Library::mInstance = 0;
+static Library *theInstance = 0;
 
 Library::Library(QObject *parent): QAbstractListModel(parent)
 {
@@ -24,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
@@ -41,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)
@@ -69,13 +70,13 @@ Book *Library::book(const QModelIndex &index)
 
 void Library::close()
 {
-    delete mInstance;
-    mInstance = 0;
+    delete theInstance;
+    theInstance = 0;
 }
 
 void Library::load()
 {
-    Trace t("Library::load");
+    TRACE;
 
     clear();
     QStringList books = BookDb::instance()->books();
@@ -86,7 +87,7 @@ void Library::load()
         Book *book = new Book(path);
         connect(book, SIGNAL(opened(const QString &)),
                 this, SLOT(onBookOpened(const QString &)));
-        book->load();
+        // book->load();
         mBooks.append(book);
     }
 
@@ -98,7 +99,7 @@ void Library::load()
 
 void Library::save()
 {
-    Trace t("Library::save");
+    TRACE;
     QSettings settings;
     Book *currentBook = book(mNowReading);
     settings.setValue("lib/nowreading",
@@ -107,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;
@@ -128,26 +129,27 @@ bool Library::add(const QString &path)
 
 void Library::remove(const QModelIndex &index)
 {
-    Trace t("Library::remove");
+    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));
 }
 
@@ -165,6 +167,7 @@ void Library::setNowReading(const QModelIndex &index)
 
 void Library::clear()
 {
+    TRACE;
     for (int i = 0; i < mBooks.size(); i++) {
         delete mBooks[i];
     }
@@ -174,6 +177,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++) {
@@ -187,6 +191,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]) {
@@ -199,7 +204,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);
@@ -217,10 +222,10 @@ QStringList Library::bookPaths()
 
 void Library::upgrade()
 {
-    Trace t("Library::upgrade");
+    TRACE;
     QSettings settings;
     QString oldVersion = settings.value("lib/version").toString();
-    if (/* true */ oldVersion.isEmpty()) {
+    if (oldVersion.isEmpty()) {
         int size = settings.value("lib/size").toInt();
         emit beginUpgrade(size);
         for (int i = 0; i < size; i++) {