.
[dorian] / foldersdialog.cpp
index 4f4966a..33666ef 100644 (file)
@@ -22,10 +22,14 @@ FoldersDialog::FoldersDialog(QWidget *parent): ListWindow(parent)
     list->setUniformItemSizes(true);
     addList(list);
     addAction(tr("Add folder"), this, SLOT(onAdd()));
-#ifndef Q_WS_MAEMO_5
-    addAction(tr("Delete folder"), this, SLOT(onDelete()));
-#endif
-    addAction(tr("Re-scan folders"), this, SLOT(onRefresh()));
+    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()
@@ -34,8 +38,10 @@ void FoldersDialog::onAdd()
 
     // Get folder name
     Settings *settings = Settings::instance();
-    QString last = settings->value("lastfolderadded", QDir::homePath()).toString();
-    QString path = QFileDialog::getExistingDirectory(this, tr("Add Folder"), last);
+    QString last =
+            settings->value("lastfolderadded", QDir::homePath()).toString();
+    QString path =
+            QFileDialog::getExistingDirectory(this, tr("Add Folder"), last);
     if (path == "") {
         return;
     }
@@ -47,12 +53,11 @@ 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,
-            tr("This folder is already in the library"),
-            QMaemo5InformationBox::DefaultTimeout);
+            tr("This folder is already in the library"));
 #else
         (void)QMessageBox::information(this, tr("Dorian"),
             tr("This folder is already in the library"), QMessageBox::Ok);
@@ -62,34 +67,63 @@ void FoldersDialog::onAdd()
 
 void FoldersDialog::onRemove()
 {
+    Trace t("FoldersDialog::onRemove");
+
+    QModelIndexList selection = list->selectionModel()->selectedIndexes();
+    if (selection.size() != 1) {
+        return;
+    }
+
+    QModelIndex selected = selection[0];
+    QString path = list->model()->data(selected).toString();
+    t.trace(path);
+
+    if (QMessageBox::Yes ==
+        QMessageBox::question(this, tr("Remove folder"),
+            tr("Remove folder \"%1\" from library?").arg(path),
+            QMessageBox::Yes | QMessageBox::No)) {
+        if (Library::instance()->removeFolder(path)) {
+            model->removeRow(selected.row());
+        }
+    }
 }
 
-void FoldersDialog::onRefresh()
+void FoldersDialog::refresh(const QStringList &dirs)
 {
-#ifdef Q_WS_MAEMO_5
-    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
-#endif
+    Trace t("FoldersDialog::refresh");
 
+    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->moveToThread(&bookFinderThread);
-    // bookFinderThread.start();
+    bookFinder->find(dirs, library->bookPaths());
+}
 
-    (void)QMetaObject::invokeMethod(
-        bookFinder,
-        "find",
-        Q_ARG(QStringList, model->stringList()),
-        Q_ARG(QStringList, library->bookPaths()));
+void FoldersDialog::onRefresh()
+{
+    Trace t("FoldersDialog::onRefresh");
 
-#ifdef Q_WS_MAEMO_5
-    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
-#endif
+    QModelIndexList selection = list->selectionModel()->selectedIndexes();
+    if (selection.size() != 1) {
+        return;
+    }
+    QModelIndex selected = selection[0];
+    QString path = list->model()->data(selected).toString();
+    refresh(QStringList(path));
+}
+
+void FoldersDialog::onRefreshAll()
+{
+    refresh(model->stringList());
 }
 
 void FoldersDialog::onRefreshDone(int added, int removed)
@@ -109,13 +143,19 @@ void FoldersDialog::onRefreshDone(int added, int removed)
     default: removedMsg = tr("%1 books removed").arg(removed);
     }
 
-    QString msg(tr("Scanning folders complete\n\n%1, %2.").
+    progress->reset();
+    QString msg(tr("Scanning complete\n\n%1, %2.").
                 arg(addedMsg).arg(removedMsg));
     Trace::trace(QString("FoldersDialog::onRefreshDone: " + msg));
 #ifdef Q_WS_MAEMO_5
-    QMaemo5InformationBox::
-            information(this, msg, QMaemo5InformationBox::NoTimeout);
+    QMaemo5InformationBox::information(this, msg);
 #else
     // FIXME
 #endif
 }
+
+void FoldersDialog::onAddBook(const QString &path)
+{
+    progress->setLabelText(QFileInfo(path).fileName());
+    progress->setValue(progress->value() + 1);
+}