.
[dorian] / librarydialog.cpp
index d11c6a4..416e05a 100644 (file)
@@ -1,12 +1,5 @@
-#include <QtGui>
-#include <QDebug>
-#include <QFileInfo>
 #include <QDir>
-#include <QModelIndex>
-
-#ifdef Q_WS_MAEMO_5
-#include <QtMaemo5/QMaemo5InformationBox>
-#endif
+#include <QtGui>
 
 #include "librarydialog.h"
 #include "library.h"
 #include "infodialog.h"
 #include "settings.h"
 #include "listwindow.h"
-#include "foldersdialog.h"
+#include "listview.h"
+#include "trace.h"
+#include "bookfinder.h"
+#include "searchdialog.h"
+#include "platform.h"
+#include "searchresultsdialog.h"
+#include "progressdialog.h"
 
 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
 {
     setWindowTitle(tr("Library"));
 
-    // Create and add list view
+    // Add actions
+
+#ifndef Q_WS_MAEMO_5
+    addItemAction(tr("Details"), this, SLOT(onDetails()));
+    addItemAction(tr("Read"), this, SLOT(onRead()));
+    addItemAction(tr("Delete"), this, SLOT(onRemove()));
+#endif // ! Q_WS_MAEMO_5
+
+    addAction(tr("Add book"), this, SLOT(onAdd()), "add");
+    addAction(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
+    addAction(tr("Search the Web"), this, SLOT(onSearch()), "search");
 
-    list = new QListView(this);
+    // Create and add list view
+    list = new ListView(this);
     sortedLibrary = new SortedLibrary(this);
     list->setModel(sortedLibrary);
     list->setSelectionMode(QAbstractItemView::SingleSelection);
     list->setSpacing(1);
-    list->setUniformItemSizes(true);
     Library *library = Library::instance();
     QModelIndex current = library->nowReading();
     setSelected(current);
     addList(list);
 
-    // Add actions
-
-#ifndef Q_WS_MAEMO_5
-    addItemAction(tr("Details"), this, SLOT(onDetails()));
-    addItemAction(tr("Read"), this, SLOT(onRead()));
-    addItemAction(tr("Delete"), this, SLOT(onRemove()));
-#endif // ! Q_WS_MAEMO_5
-
-    addAction(tr("Add book"), this, SLOT(onAdd()));
-    addAction(tr("Manage folders"), this, SLOT(onShowFolders()));
+    progress = new ProgressDialog(tr("Adding books"), this);
 
     connect(Library::instance(), SIGNAL(nowReadingChanged()),
             this, SLOT(onCurrentBookChanged()));
@@ -53,6 +53,11 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
             SLOT(onBookAdded()));
     connect(list, SIGNAL(activated(const QModelIndex &)),
             this, SLOT(onItemActivated(const QModelIndex &)));
+
+    // Create search dialog
+    searchDialog = new SearchDialog(this);
+    connect(Search::instance(), SIGNAL(endSearch()),
+            this, SLOT(showSearchResults()));
 }
 
 void LibraryDialog::onAdd()
@@ -78,14 +83,7 @@ void LibraryDialog::onAdd()
     // Add book to library
     QModelIndex index = library->find(path);
     if (index.isValid()) {
-#ifdef Q_WS_MAEMO_5
-        QMaemo5InformationBox::information(this,
-            tr("This book is already in the library"),
-            QMaemo5InformationBox::DefaultTimeout);
-#else
-        (void)QMessageBox::information(this, tr("Dorian"),
-            tr("This book is already in the library"), QMessageBox::Ok);
-#endif // Q_WS_MAEMO_5
+        Platform::information(tr("This book is already in the library"), this);
         setSelected(index);
     }
     else {
@@ -119,7 +117,6 @@ void LibraryDialog::onRemove()
 
 void LibraryDialog::onRead()
 {
-    qDebug() << "LibraryDialog::onRead";
     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
     if (current.isValid()) {
         Library::instance()->setNowReading(current);
@@ -135,7 +132,7 @@ void LibraryDialog::onDetails()
 
 void LibraryDialog::onItemActivated(const QModelIndex &index)
 {
-    qDebug() << "LibraryDialog::onItemActivated";
+    TRACE;
     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
     Book *book = Library::instance()->book(libraryIndex);
     (new InfoDialog(book, this))->exec();
@@ -178,8 +175,80 @@ QModelIndex LibraryDialog::selected() const
     return QModelIndex();
 }
 
-void LibraryDialog::onShowFolders()
+void LibraryDialog::onAddFolder()
+{
+    TRACE;
+
+    // Get folder name
+    Settings *settings = Settings::instance();
+    QString last =
+            settings->value("lastfolderadded", QDir::homePath()).toString();
+    QString path =
+            QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
+    if (path == "") {
+        return;
+    }
+    settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
+    qDebug() << path;
+
+    // Add books from folder
+    progress->setWindowTitle(tr("Adding books"));
+    BookFinder *bookFinder = new BookFinder(this);
+    Library *library = Library::instance();
+    connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
+    connect(bookFinder, SIGNAL(add(const QString &)),
+            this, SLOT(onAddFromFolder(const QString &)));
+    connect(bookFinder, SIGNAL(add(const QString &)),
+            library, SLOT(add(const QString &)));
+    connect(bookFinder, SIGNAL(done(int)),
+            this, SLOT(onAddFromFolderDone(int)));
+    bookFinder->find(path, Library::instance()->bookPaths());
+}
+
+void LibraryDialog::onAddFromFolderDone(int added)
+{
+    QString msg;
+
+    switch (added) {
+    case 0: msg = tr("No new books found"); break;
+    case 1: msg = tr("One book added"); break;
+    default: msg = tr("%1 books added").arg(added);
+    }
+
+    progress->reset();
+    qDebug() << "LibraryDialog::onRefreshDone:" << msg;
+    Platform::information(msg, this);
+}
+
+void LibraryDialog::onAddFromFolder(const QString &path)
+{
+    progress->setLabelText(QFileInfo(path).fileName());
+    progress->setValue(progress->value() + 1);
+}
+
+void LibraryDialog::onSearch()
+{
+    int ret = searchDialog->exec();
+    if (ret != QDialog::Accepted) {
+        return;
+    }
+    progress->setLabelText(tr("Searching Project Gutenberg"));
+    progress->showWait();
+    Search::instance()->start(searchDialog->query());
+}
+
+void LibraryDialog::showSearchResults()
 {
-    FoldersDialog *folders = new FoldersDialog(this);
-    folders->show();
+    progress->reset();
+    QList<Search::Result> results = Search::instance()->results();
+    if (results.count() == 0) {
+        QMessageBox::information(this, tr("Search results"),
+                                 tr("No books found"));
+        return;
+    }
+
+    SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
+    connect(dialog, SIGNAL(add(const Search::Result &)),
+            this, SLOT(onAddSearchResult(const Search::Result &)));
+    dialog->show();
 }