Fix author name in library and book info dialogs.
[dorian] / model / book.cpp
index cd1ba6a..874ff2b 100644 (file)
@@ -1,4 +1,5 @@
 #include <qtextdocument.h>  // Qt::escape is currently defined here...
+#include <QtGui>
 
 #include "book.h"
 #include "opshandler.h"
 
 const int COVER_WIDTH = 53;
 const int COVER_HEIGHT = 59;
-
-static QImage makeCover(const QString &path)
-{
-    return QImage(path).scaled(COVER_WIDTH, COVER_HEIGHT,
-        Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation).
-        scaled(COVER_WIDTH, COVER_HEIGHT, Qt::KeepAspectRatio);
-}
+const int COVER_MAX = 512 * 1024;
 
 Book::Book(const QString &p, QObject *parent): QObject(parent), loaded(false)
 {
@@ -27,11 +22,15 @@ Book::Book(const QString &p, QObject *parent): QObject(parent), loaded(false)
         QFileInfo info(p);
         mPath = info.absoluteFilePath();
         title = info.baseName();
-        cover = makeCover(":/icons/book.png");
         mTempFile.open();
     }
 }
 
+Book::~Book()
+{
+    close();
+}
+
 QString Book::path()
 {
     return mPath;
@@ -293,8 +292,7 @@ void Book::load()
     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);
+    cover = data["cover"].value<QImage>();
     if (cover.isNull()) {
         cover = makeCover(":/icons/book.png");
     }
@@ -406,15 +404,11 @@ QString Book::name()
     if (title.size()) {
         QString ret = title;
         if (creators.length()) {
-            ret += "\nBy " + creators[0];
-            for (int i = 1; i < creators.length(); i++) {
-                ret += ", " + creators[i];
-            }
+            ret += "\nBy " + creators.join(", ");
         }
         return ret;
-    } else {
-        return path();
     }
+    return path();
 }
 
 QString Book::shortName()
@@ -423,6 +417,12 @@ QString Book::shortName()
     return (title.isEmpty())? QFileInfo(path()).baseName(): title;
 }
 
+QImage Book::coverImage()
+{
+    load();
+    return cover;
+}
+
 int Book::chapterFromPart(int index)
 {
     TRACE;
@@ -499,7 +499,8 @@ qreal Book::getProgress(int part, qreal position)
 bool Book::extractMetaData()
 {
     QStringList excludedExtensions;
-    excludedExtensions << ".html" << ".xhtml" << ".xht" << ".htm";
+    excludedExtensions << ".html" << ".xhtml" << ".xht" << ".htm" << ".gif"
+            << ".css" << "*.ttf" << "mimetype";
     return extract(excludedExtensions);
 }
 
@@ -508,7 +509,6 @@ void Book::upgrade()
     TRACE;
 
     // Load book from old database (QSettings)
-
     QSettings settings;
     QString key = "book/" + path() + "/";
     title = settings.value(key + "title").toString();
@@ -522,10 +522,11 @@ void Book::upgrade()
     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);
+    cover = settings.value(key + "cover").value<QImage>();
     if (cover.isNull()) {
         cover = makeCover(":/icons/book.png");
+    } else {
+        cover = makeCover(QPixmap::fromImage(cover));
     }
     int size = settings.value(key + "bookmarks").toInt();
     for (int i = 0; i < size; i++) {
@@ -538,14 +539,47 @@ void Book::upgrade()
         mBookmarks.append(Bookmark(part, pos));
     }
 
-    // Save book to new database
+    // Remove QSettings
+    settings.remove("book/" + path());
 
+    // Save book to new database
     save();
 }
 
 void Book::remove()
 {
     TRACE;
-    load();
+    close();
     BookDb::instance()->remove(path());
 }
+
+QImage Book::makeCover(const QString &fileName)
+{
+    TRACE;
+    qDebug() << fileName;
+    QFileInfo info(fileName);
+    if (info.isReadable() && (info.size() < COVER_MAX)) {
+        return makeCover(QPixmap(fileName));
+    }
+    return makeCover(QPixmap(":/icons/book.png"));
+}
+
+QImage Book::makeCover(const QPixmap &pixmap)
+{
+    TRACE;
+    QPixmap src = pixmap.scaled(COVER_WIDTH, COVER_HEIGHT,
+        Qt::KeepAspectRatio, Qt::SmoothTransformation);
+    QPixmap transparent(COVER_WIDTH, COVER_HEIGHT);
+    transparent.fill(Qt::transparent);
+
+    QPainter p;
+    p.begin(&transparent);
+    p.setCompositionMode(QPainter::CompositionMode_Source);
+    p.drawPixmap((COVER_WIDTH - src.width()) / 2,
+                 (COVER_HEIGHT - src.height()) / 2, src);
+    p.end();
+
+    return transparent.toImage();
+}
+
+