Add Flickable.
[dorian] / librarydialog.cpp
index fd7777b..0d7eeb4 100644 (file)
 #include "book.h"
 #include "infodialog.h"
 #include "settings.h"
+#include "listwindow.h"
+#include "listview.h"
+#include "trace.h"
+#include "bookfinder.h"
+#include "searchdialog.h"
+#include "platform.h"
 
-LibraryDialog::LibraryDialog(QWidget *parent):
-        QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
-                Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
+LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
 {
     setWindowTitle(tr("Library"));
-    list = new QListView(this);
+
+    // 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");
+
+    // Create and add list view
+    list = new ListView(this);
     sortedLibrary = new SortedLibrary(this);
     list->setModel(sortedLibrary);
     list->setSelectionMode(QAbstractItemView::SingleSelection);
-    list->setUniformItemSizes(true);
-#ifndef Q_WS_MAEMO_5
-    setSizeGripEnabled(true);
-#endif
-
+    list->setSpacing(1);
     Library *library = Library::instance();
     QModelIndex current = library->nowReading();
     setSelected(current);
+    addList(list);
 
-    QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
-    horizontalLayout->addWidget(list);
-
-    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
-#ifndef Q_WS_MAEMO_5
-    detailsButton = new QPushButton(tr("Details"), this);
-    readButton = new QPushButton(tr("Read"), this);
-    removeButton = new QPushButton(tr("Delete"), this);
-#endif // Q_WS_MAEMO_5
-    addButton = new QPushButton(tr("Add"), this);
-
-#ifndef Q_WS_MAEMO_5
-    buttonBox->addButton(detailsButton, QDialogButtonBox::ActionRole);
-    buttonBox->addButton(readButton, QDialogButtonBox::AcceptRole);
-    buttonBox->addButton(removeButton, QDialogButtonBox::ActionRole);
-#endif // Q_WS_MAEMO_5
-    buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
-
-    horizontalLayout->addWidget(buttonBox);
+    progress = new QProgressDialog(tr("Adding books"), "", 0, 0, this);
+    progress->reset();
+    progress->setMinimumDuration(0);
+    progress->setWindowModality(Qt::WindowModal);
+    progress->setCancelButton(0);
+#ifdef Q_WS_S60
+    progress->setFixedWidth(
+            QApplication::desktop()->availableGeometry().width());
+#endif
 
     connect(Library::instance(), SIGNAL(nowReadingChanged()),
             this, SLOT(onCurrentBookChanged()));
@@ -59,21 +64,11 @@ LibraryDialog::LibraryDialog(QWidget *parent):
             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
             this,
             SLOT(onBookAdded()));
-    connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
     connect(list, SIGNAL(activated(const QModelIndex &)),
             this, SLOT(onItemActivated(const QModelIndex &)));
-#ifndef Q_WS_MAEMO_5
-    connect(list, SIGNAL(itemSelectionChanged()),
-            this, SLOT(onItemSelectionChanged()));
-    connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
-    connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
-    connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
-    connect(list->selectionModel(),
-            SIGNAL(selectionChanged(const QItemSelection &,
-                                    const QItemSelection &)),
-            this, SLOT(onItemSelectionChanged()));
-    onItemSelectionChanged();
-#endif // !Q_WS_MAEMO_5
+
+    // Create search dialog
+    searchDialog = new SearchDialog(this);
 }
 
 void LibraryDialog::onAdd()
@@ -124,15 +119,15 @@ void LibraryDialog::onBookAdded()
 
 void LibraryDialog::onRemove()
 {
-    qDebug() << "LibraryDialog::onRemove";
     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
     if (current.isValid()) {
         Book *currentBook = Library::instance()->book(current);
         QString title = currentBook->name();
         if (QMessageBox::Yes ==
-            QMessageBox::question(this, "Delete book",
-                                  "Delete book \"" + title + "\"?",
-                                  QMessageBox::Yes, QMessageBox::No)) {
+            QMessageBox::question(this, tr("Delete book"),
+                tr("Delete book \"%1\" from library?").
+                    arg(currentBook->shortName()),
+                QMessageBox::Yes | QMessageBox::No)) {
             Library::instance()->remove(current);
         }
     }
@@ -174,22 +169,6 @@ QString LibraryDialog::createItemText(const Book *book)
     return text;
 }
 
-#ifndef Q_WS_MAEMO_5
-
-void LibraryDialog::onItemSelectionChanged()
-{
-    bool enable = selected().isValid();
-    qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
-    readButton->setEnabled(enable);
-    qDebug() << " readButton";
-    detailsButton->setEnabled(enable);
-    qDebug() << " detailsButton";
-    removeButton->setEnabled(enable);
-    qDebug() << " removeButton";
-}
-
-#endif // Q_WS_MAEMO_5
-
 void LibraryDialog::onCurrentBookChanged()
 {
     close();
@@ -214,3 +193,67 @@ QModelIndex LibraryDialog::selected() const
     }
     return QModelIndex();
 }
+
+void LibraryDialog::onAddFolder()
+{
+    Trace t("LibraryDialog::onAddFolder");
+
+    // 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;
+#ifdef Q_WS_MAEMO_5
+    QMaemo5InformationBox::information(this, msg);
+#else
+    // FIXME
+#endif
+}
+
+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;
+    }
+    Search::instance()->start(searchDialog->query());
+}