Retrieve metadata when adding book to library. Show progress while scanning folders...
authorAkos Polster <polster@marzipan.local>
Sat, 21 Aug 2010 13:58:26 +0000 (15:58 +0200)
committerAkos Polster <polster@marzipan.local>
Sat, 21 Aug 2010 13:58:26 +0000 (15:58 +0200)
foldersdialog.cpp
foldersdialog.h
model/book.cpp
model/book.h
model/bookfinder.cpp
model/bookfinder.h
model/library.cpp
pkg/changelog
widgets/progress.cpp

index 3582471..33666ef 100644 (file)
@@ -25,6 +25,11 @@ FoldersDialog::FoldersDialog(QWidget *parent): ListWindow(parent)
     addItemAction(tr("Re-scan"), this, SLOT(onRefresh()));
     addItemAction(tr("Remove"), this, SLOT(onRemove()));
     addAction(tr("Re-scan all folders"), this, SLOT(onRefreshAll()));
+    progress = new QProgressDialog(tr("Scanning for books"), "", 0, 0, this);
+    progress->reset();
+    progress->setMinimumDuration(0);
+    progress->setWindowModality(Qt::WindowModal);
+    progress->setCancelButton(0);
 }
 
 void FoldersDialog::onAdd()
@@ -48,7 +53,7 @@ void FoldersDialog::onAdd()
         int rows = model->rowCount();
         model->insertRows(rows, 1);
         model->setData(model->index(rows), path);
-        onRefresh();
+        refresh(QStringList(path));
     } else {
 #ifdef Q_WS_MAEMO_5
         QMaemo5InformationBox::information(this,
@@ -83,54 +88,42 @@ void FoldersDialog::onRemove()
     }
 }
 
-void FoldersDialog::onRefresh()
+void FoldersDialog::refresh(const QStringList &dirs)
 {
-    Trace t("FoldersDialog::onRefresh");
+    Trace t("FoldersDialog::refresh");
 
-    QModelIndexList selection = list->selectionModel()->selectedIndexes();
-    if (selection.size() != 1) {
-        return;
-    }
-
-#ifdef Q_WS_MAEMO_5
-    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
-#endif
-    QModelIndex selected = selection[0];
-    QString path = list->model()->data(selected).toString();
-    t.trace(path);
+    progress->setWindowTitle((dirs.length() > 1)?
+                             tr("Scanning all folders"): tr("Scanning folder"));
     BookFinder *bookFinder = new BookFinder(this);
     Library *library = Library::instance();
+    connect(bookFinder, SIGNAL(beginAdd(int)), progress, SLOT(setMaximum(int)));
+    connect(bookFinder, SIGNAL(add(const QString &)),
+            this, SLOT(onAddBook(const QString &)));
     connect(bookFinder, SIGNAL(add(const QString &)),
             library, SLOT(add(const QString &)));
     connect(bookFinder, SIGNAL(remove(const QString &)),
             library, SLOT(remove(const QString &)));
     connect(bookFinder, SIGNAL(done(int,int)),
             this, SLOT(onRefreshDone(int, int)));
-    bookFinder->find(QStringList(path), library->bookPaths());
-#ifdef Q_WS_MAEMO_5
-    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
-#endif
+    bookFinder->find(dirs, library->bookPaths());
 }
 
-void FoldersDialog::onRefreshAll()
+void FoldersDialog::onRefresh()
 {
-#ifdef Q_WS_MAEMO_5
-    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
-#endif
+    Trace t("FoldersDialog::onRefresh");
 
-    BookFinder *bookFinder = new BookFinder(this);
-    Library *library = Library::instance();
-    connect(bookFinder, SIGNAL(add(const QString &)),
-            library, SLOT(add(const QString &)));
-    connect(bookFinder, SIGNAL(remove(const QString &)),
-            library, SLOT(remove(const QString &)));
-    connect(bookFinder, SIGNAL(done(int,int)),
-            this, SLOT(onRefreshDone(int, int)));
-    bookFinder->find(model->stringList(), library->bookPaths());
+    QModelIndexList selection = list->selectionModel()->selectedIndexes();
+    if (selection.size() != 1) {
+        return;
+    }
+    QModelIndex selected = selection[0];
+    QString path = list->model()->data(selected).toString();
+    refresh(QStringList(path));
+}
 
-#ifdef Q_WS_MAEMO_5
-    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
-#endif
+void FoldersDialog::onRefreshAll()
+{
+    refresh(model->stringList());
 }
 
 void FoldersDialog::onRefreshDone(int added, int removed)
@@ -150,6 +143,7 @@ void FoldersDialog::onRefreshDone(int added, int removed)
     default: removedMsg = tr("%1 books removed").arg(removed);
     }
 
+    progress->reset();
     QString msg(tr("Scanning complete\n\n%1, %2.").
                 arg(addedMsg).arg(removedMsg));
     Trace::trace(QString("FoldersDialog::onRefreshDone: " + msg));
@@ -159,3 +153,9 @@ void FoldersDialog::onRefreshDone(int added, int removed)
     // FIXME
 #endif
 }
+
+void FoldersDialog::onAddBook(const QString &path)
+{
+    progress->setLabelText(QFileInfo(path).fileName());
+    progress->setValue(progress->value() + 1);
+}
index 45adba1..a655bbc 100644 (file)
@@ -3,6 +3,7 @@
 
 class QStringListModel;
 class QWidget;
+class QProgressDialog;
 
 #include "listwindow.h"
 
@@ -14,17 +15,18 @@ class FoldersDialog: public ListWindow
 public:
     explicit FoldersDialog(QWidget *parent = 0);
 
-signals:
-
 public slots:
     void onAdd();
     void onRemove();
     void onRefresh();
     void onRefreshAll();
     void onRefreshDone(int added, int removed);
+    void onAddBook(const QString &path);
 
 protected:
+    void refresh(const QStringList &folders);
     QStringListModel *model;
+    QProgressDialog *progress;
 };
 
 #endif // FOLDERSDIALOG_H
index 66e2392..6291419 100644 (file)
@@ -6,6 +6,7 @@
 #include <QDirIterator>
 #include <QFileInfo>
 #include <QtAlgorithms>
+#include <QCryptographicHash>
 
 #include "book.h"
 #include "opshandler.h"
@@ -34,6 +35,7 @@ Book::Book(const QString &p, QObject *parent): QObject(parent)
         mPath = info.absoluteFilePath();
         title = info.baseName();
         cover = makeCover(":/icons/book.png");
+        mTempFile.open();
     }
 }
 
@@ -63,6 +65,26 @@ bool Book::open()
     return true;
 }
 
+void Book::peek()
+{
+    Trace t("Book::peek");
+    t.trace(path());
+    close();
+    clear();
+    if (path().isEmpty()) {
+        title = "No book";
+        return;
+    }
+    if (!extractMetaData()) {
+        return;
+    }
+    if (!parse()) {
+        return;
+    }
+    save();
+    close();
+}
+
 void Book::close()
 {
     Trace t("Book::close");
@@ -74,7 +96,8 @@ void Book::close()
 
 QString Book::tmpDir() const
 {
-    return QDir::tempPath() + "/dorian/book";
+    QString tmpName = QFileInfo(mTempFile.fileName()).fileName();
+    return QDir::tempPath() + "/dorian/" + tmpName;
 }
 
 bool Book::extract()
@@ -441,3 +464,9 @@ qreal Book::getProgress(int part, qreal position)
     partSize += content[key].size * position;
     return partSize / (qreal)size;
 }
+
+bool Book::extractMetaData()
+{
+    // FIXME
+    return extract();
+}
index c60ae1c..6e53829 100644 (file)
@@ -7,6 +7,7 @@
 #include <QIcon>
 #include <QMetaType>
 #include <QObject>
+#include <QTemporaryFile>
 
 /** A book. */
 class Book: public QObject
@@ -50,6 +51,9 @@ public:
     /** Extract and parse EPUB contents, fill in all members except mPath. */
     bool open();
 
+    /** Extract and parse EPUB metadata only, fill in all members except mPath. */
+    void peek();
+
     /** Clear toc and content members, remove extracted content files. */
     void close();
 
@@ -125,6 +129,9 @@ protected:
     /** Extract EPUB as ZIP. */
     bool extract();
 
+    /** Extract metadata from EPUB. */
+    bool extractMetaData();
+
     /** Parse extracted EPUB. */
     bool parse();
 
@@ -138,6 +145,7 @@ protected:
     Bookmark mLastBookmark;                 //< Last position read.
     QList<Bookmark> mBookmarks;             //< List of bookmarks.
     QString mRootPath;                      //< Path to root item in EPUB dir.
+    QTemporaryFile mTempFile;               //< Guards extracting books.
 };
 
 #endif // BOOK_H
index 507a7e5..f02045f 100644 (file)
@@ -25,6 +25,14 @@ void BookFinder::find(const QStringList &directories, const QStringList &books)
         }
     }
 
+    int toAdd = 0;
+    foreach (QString found, booksFound) {
+        if (!books.contains(found)) {
+            toAdd++;
+        }
+    }
+    emit beginAdd(toAdd);
+
     foreach (QString found, booksFound) {
         if (!books.contains(found)) {
             t.trace(QString("New book ") + found);
index 83ddf46..a3d5d91 100644 (file)
@@ -18,6 +18,7 @@ public slots:
     void find(const QStringList &directories, const QStringList &books);
 
 signals:
+    void beginAdd(int count);
     void add(const QString &book);
     void remove(const QString &book);
     void done(int added, int removed);
index 23a3dea..c210555 100644 (file)
@@ -114,8 +114,9 @@ bool Library::add(const QString &path)
         return false;
     }
     int size = mBooks.size();
-    Book *book = new Book(path);
     beginInsertRows(QModelIndex(), size, size);
+    Book *book = new Book(path);
+    book->peek();
     mBooks.append(book);
     save();
     endInsertRows();
index 1be16b8..ca37058 100644 (file)
@@ -1,5 +1,8 @@
 dorian (0.1.3-1) unstable; urgency=low
 
+  * Retrieve book metadata when adding it to library
+  * Show progress while scanning folders for books
+
  -- Akos Polster <akos@pipacs.com>  Sun, 15 Aug 2010 20:00:00 +0200
 
 dorian (0.1.2-1) unstable; urgency=low
index fa0d41d..db1733a 100644 (file)
@@ -34,7 +34,7 @@ void Progress::flash()
 {
     killTimer(timer);
     show();
-    timer = startTimer(400);
+    timer = startTimer(700);
 }
 
 void Progress::timerEvent(QTimerEvent *e)