Not much progress.
[dorian] / librarydialog.cpp
index cab4428..767667a 100644 (file)
@@ -2,6 +2,7 @@
 #include <QDebug>
 #include <QFileInfo>
 #include <QDir>
+#include <QModelIndex>
 
 #ifdef Q_WS_MAEMO_5
 #include <QtMaemo5/QMaemo5InformationBox>
 
 #include "librarydialog.h"
 #include "library.h"
+#include "sortedlibrary.h"
 #include "book.h"
 #include "infodialog.h"
+#include "settings.h"
+#include "listwindow.h"
+#include "listview.h"
+#include "trace.h"
+#include "bookfinder.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 QListWidget(this);
-    list->setSelectionMode(QAbstractItemView::SingleSelection);
-#ifndef Q_WS_MAEMO_5
-    setSizeGripEnabled(true);
-#endif
-
-    Library *library = Library::instance();
-    for (int i = 0; i < library->size(); i++) {
-        Book *book = library->at(i);
-        (void)new QListWidgetItem(book->cover, createItemText(book), list);
-    }
-    Book *current = library->current();
-    if (library->size() && current) {
-        list->setItemSelected(list->item(library->find(current)), true);
-    }
-
-    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
-    addButton = new QPushButton(tr("Add"), this);
+    // Add actions
 
 #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);
+    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()), ":/icons/add.png");
+    addAction(tr("Add books from folder"), this, SLOT(onAddFolder()),
+              ":/icons/folder.png");
+
+    // Create and add list view
+    list = new ListView(this);
+    sortedLibrary = new SortedLibrary(this);
+    list->setModel(sortedLibrary);
+    list->setSelectionMode(QAbstractItemView::SingleSelection);
+    list->setSpacing(1);
+    Library *library = Library::instance();
+    QModelIndex current = library->nowReading();
+    setSelected(current);
+    addList(list);
 
-    horizontalLayout->addWidget(buttonBox);
+    progress = new QProgressDialog(tr("Adding books"), "", 0, 0, this);
+    progress->reset();
+    progress->setMinimumDuration(0);
+    progress->setWindowModality(Qt::WindowModal);
+    progress->setCancelButton(0);
 
-    connect(library, SIGNAL(bookAdded()), this, SLOT(onBookAdded()));
-    connect(library, SIGNAL(currentBookChanged()),
+    connect(Library::instance(), SIGNAL(nowReadingChanged()),
             this, SLOT(onCurrentBookChanged()));
-#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()));
-#endif
-    connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
-#ifdef Q_WS_MAEMO_5
-    connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
-            this, SLOT(onItemActivated(QListWidgetItem *)));
-#endif
-
-#ifndef Q_WS_MAEMO_5
-    onItemSelectionChanged();
-#endif
+    connect(Library::instance(),
+            SIGNAL(rowsInserted(const QModelIndex &, int, int)),
+            this,
+            SLOT(onBookAdded()));
+    connect(list, SIGNAL(activated(const QModelIndex &)),
+            this, SLOT(onItemActivated(const QModelIndex &)));
 }
 
 void LibraryDialog::onAdd()
 {
     Library *library = Library::instance();
 
-    if (lastDir == "") {
-        if (library->size()) {
-            QFileInfo info(library->at(library->size() - 1)->path());
-            lastDir = info.absolutePath();
-        }
-    }
+    // Figure out directory to show
+    QString lastDir = Settings::instance()->value("lastdir").toString();
     if (lastDir == "") {
         lastDir = QDir::homePath();
     }
+
+    // Get book file name
     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
                                                 lastDir, "Books (*.epub)");
-    qDebug() << "LibraryDialog::onAdd" << path;
     if (path == "") {
         return;
     }
 
-    lastDir = QFileInfo(path).absolutePath();
-    if (library->find(path) != -1) {
+    // Save directory selected
+    Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
+
+    // Add book to library
+    QModelIndex index = library->find(path);
+    if (index.isValid()) {
 #ifdef Q_WS_MAEMO_5
         QMaemo5InformationBox::information(this,
-            tr("Book is alreadButtony in the library"),
+            tr("This book is already in the library"),
             QMaemo5InformationBox::DefaultTimeout);
-#endif
-        // FIXME: Select existing book
+#else
+        (void)QMessageBox::information(this, tr("Dorian"),
+            tr("This book is already in the library"), QMessageBox::Ok);
+#endif // Q_WS_MAEMO_5
+        setSelected(index);
     }
     else {
         library->add(path);
@@ -111,62 +103,49 @@ void LibraryDialog::onAdd()
 void LibraryDialog::onBookAdded()
 {
     Library *library = Library::instance();
-    int index = library->size() - 1;
-    Book *book = library->at(index);
-    QListWidgetItem *item = new QListWidgetItem(book->cover,
-                                                createItemText(book));
-    list->addItem(item);
-    list->setCurrentItem(item);
+    setSelected(library->index(library->rowCount() - 1));
 }
 
 #ifndef Q_WS_MAEMO_5
 
 void LibraryDialog::onRemove()
 {
-    qDebug() << "LibraryDialog::onRemove";
-    if (list->selectedItems().isEmpty()) {
-        return;
-    }
-    QListWidgetItem *item = list->selectedItems()[0];
-    int row = list->row(item);
-    QString title = Library::instance()->at(row)->title;
-    if (QMessageBox::Yes ==
-        QMessageBox::question(this, "Delete book",
-                              "Delete book \"" + title + "\"?", QMessageBox::Yes
-#ifndef Q_WS_MAEMO_5
-                              , QMessageBox::No
-#endif
-                              )) {
-        list->takeItem(row);
-        Library::instance()->remove(row);
+    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, tr("Delete book"),
+                tr("Delete book \"%1\" from library?").
+                    arg(currentBook->shortName()),
+                QMessageBox::Yes | QMessageBox::No)) {
+            Library::instance()->remove(current);
+        }
     }
 }
 
 void LibraryDialog::onRead()
 {
     qDebug() << "LibraryDialog::onRead";
-    if (list->selectedItems().isEmpty()) {
-        return;
+    QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
+    if (current.isValid()) {
+        Library::instance()->setNowReading(current);
     }
-    QListWidgetItem *item = list->selectedItems()[0];
-    int row = list->row(item);
-    Library::instance()->setCurrent(row);
 }
 
 void LibraryDialog::onDetails()
 {
-    onItemActivated(list->selectedItems()[0]);
+    onItemActivated(list->currentIndex());
 }
 
 #endif // Q_WS_MAEMO_5
 
-void LibraryDialog::onItemActivated(QListWidgetItem *item)
+void LibraryDialog::onItemActivated(const QModelIndex &index)
 {
     qDebug() << "LibraryDialog::onItemActivated";
-    int row = list->row(item);
-    Book *book = Library::instance()->at(row);
-    InfoDialog *info = new InfoDialog(book, this);
-    info->exec();
+    QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
+    Book *book = Library::instance()->book(libraryIndex);
+    (new InfoDialog(book, this))->exec();
 }
 
 QString LibraryDialog::createItemText(const Book *book)
@@ -181,23 +160,82 @@ QString LibraryDialog::createItemText(const Book *book)
     return text;
 }
 
-#ifndef Q_WS_MAEMO_5
+void LibraryDialog::onCurrentBookChanged()
+{
+    close();
+}
 
-void LibraryDialog::onItemSelectionChanged()
+void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
 {
-    bool enable = list->selectedItems().size();
-    qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
-    readButton->setEnabled(enable);
-    qDebug() << " readButton";
-    detailsButton->setEnabled(enable);
-    qDebug() << " detailsButton";
-    removeButton->setEnabled(enable);
-    qDebug() << " removeButton";
+    QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
+    list->selectionModel()->clearSelection();
+    if (sortedIndex.isValid()) {
+        list->selectionModel()->select(sortedIndex,
+                                       QItemSelectionModel::Select);
+        list->setCurrentIndex(sortedIndex);
+    }
 }
 
-#endif // Q_WS_MAEMO_5
+QModelIndex LibraryDialog::selected() const
+{
+    QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
+    if (selectedItems.size()) {
+        return sortedLibrary->mapToSource(selectedItems[0]);
+    }
+    return QModelIndex();
+}
 
-void LibraryDialog::onCurrentBookChanged()
+void LibraryDialog::onAddFolder()
 {
-    close();
+    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 new book added"); break;
+    default: msg = tr("%1 new 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);
 }