X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=librarydialog.cpp;h=e45fe8f54b77fb414bc8efe45195c5c33d90d172;hb=30e85d759793aa2b367e4bacd730b733112a1a7b;hp=f861d2636dccb7dd0fdf434a2b14b2ffe510a9f6;hpb=51791f2941458065f5859d7dd8b838e651d76bcd;p=dorian diff --git a/librarydialog.cpp b/librarydialog.cpp index f861d26..e45fe8f 100644 --- a/librarydialog.cpp +++ b/librarydialog.cpp @@ -1,12 +1,5 @@ -#include -#include -#include #include -#include - -#ifdef Q_WS_MAEMO_5 -#include -#endif +#include #include "librarydialog.h" #include "library.h" @@ -15,43 +8,60 @@ #include "infodialog.h" #include "settings.h" #include "listwindow.h" -#include "foldersdialog.h" +#include "trace.h" +#include "bookfinder.h" +#include "searchdialog.h" +#include "platform.h" +#include "searchresultsdialog.h" +#include "progressdialog.h" +#include "settings.h" -LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent) +LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(tr("(No books)"), parent) { + TRACE; setWindowTitle(tr("Library")); + setAttribute(Qt::WA_DeleteOnClose, true); - // Create and add list view + // Add menu actions + sortByTitle = addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle())); + sortByAuthor = + addMenuAction(tr("Sort by author"), this, SLOT(onSortByAuthor())); - list = new QListView(this); + // Set model sortedLibrary = new SortedLibrary(this); - list->setModel(sortedLibrary); - list->setSelectionMode(QAbstractItemView::SingleSelection); - list->setSpacing(1); + setModel(sortedLibrary); + + // Add action buttons + addButton(tr("Add book"), this, SLOT(onAdd()), "add"); + addButton(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder"); + addButton(tr("Search the Web"), this, SLOT(onSearch()), "search"); + + // Set selected item 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())); - - connect(Library::instance(), SIGNAL(nowReadingChanged()), - this, SLOT(onCurrentBookChanged())); - connect(Library::instance(), - SIGNAL(rowsInserted(const QModelIndex &, int, int)), - this, - SLOT(onBookAdded())); - connect(list, SIGNAL(activated(const QModelIndex &)), + setCurrentItem(sortedLibrary->mapFromSource(current)); + + // Search dialog box + searchDialog = new SearchDialog(this); + connect(Search::instance(), SIGNAL(endSearch()), + this, SLOT(showSearchResults())); + + // Progress bar + progress = new ProgressDialog(tr("Adding books"), this); + + connect(library, SIGNAL(rowsInserted(const QModelIndex &, int, int)), + this, SLOT(onBookAdded())); + connect(this, SIGNAL(activated(const QModelIndex &)), this, SLOT(onItemActivated(const QModelIndex &))); + + // Retrieve default sort criteria + switch (Settings::instance()->value("lib/sortby").toInt()) { + case SortedLibrary::SortByAuthor: + onSortByAuthor(); + break; + default: + onSortByTitle(); + } } void LibraryDialog::onAdd() @@ -77,15 +87,9 @@ 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 - setSelected(index); + Platform::instance()->information( + tr("This book is already in the library"), this); + setSelected(sortedLibrary->mapFromSource(index)); } else { library->add(path); @@ -95,90 +99,146 @@ void LibraryDialog::onAdd() void LibraryDialog::onBookAdded() { Library *library = Library::instance(); - setSelected(library->index(library->rowCount() - 1)); + setSelected(sortedLibrary-> + mapFromSource(library->index(library->rowCount() - 1))); } -#ifndef Q_WS_MAEMO_5 - -void LibraryDialog::onRemove() +void LibraryDialog::onItemActivated(const QModelIndex &index) { - 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); - } + TRACE; + + QModelIndex libraryIndex = sortedLibrary->mapToSource(index); + Book *book = Library::instance()->book(libraryIndex); + int ret = (new InfoDialog(book, this))->exec(); + + switch (ret) { + case InfoDialog::Read: + Library::instance()->setNowReading(libraryIndex); + close(); + break; + case InfoDialog::Delete: + Library::instance()->remove(libraryIndex); + break; + default: + ; } } -void LibraryDialog::onRead() +QString LibraryDialog::createItemText(Book *book) { - qDebug() << "LibraryDialog::onRead"; - QModelIndex current = sortedLibrary->mapToSource(list->currentIndex()); - if (current.isValid()) { - Library::instance()->setNowReading(current); + Q_ASSERT(book); + QString text = book->shortName() + "\n"; + if (book->creators.size()) { + text += book->creators[0]; + for (int i = 1; i < book->creators.size(); i++) { + text += ", " + book->creators[i]; + } } + return text; } -void LibraryDialog::onDetails() +void LibraryDialog::setSelected(const QModelIndex &libraryIndex) { - onItemActivated(list->currentIndex()); + QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex); + if (sortedIndex.isValid()) { + setCurrentItem(sortedIndex); + } } -#endif // Q_WS_MAEMO_5 - -void LibraryDialog::onItemActivated(const QModelIndex &index) +void LibraryDialog::onAddFolder() { - qDebug() << "LibraryDialog::onItemActivated"; - QModelIndex libraryIndex = sortedLibrary->mapToSource(index); - Book *book = Library::instance()->book(libraryIndex); - (new InfoDialog(book, this))->exec(); + 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()); } -QString LibraryDialog::createItemText(const Book *book) +void LibraryDialog::onAddFromFolderDone(int added) { - QString text = book->title + "\n"; - if (book->creators.size()) { - text += book->creators[0]; - for (int i = 1; i < book->creators.size(); i++) { - text += ", " + book->creators[i]; - } + 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); } - return text; + + progress->reset(); + qDebug() << "LibraryDialog::onRefreshDone:" << msg; + Platform::instance()->information(msg, this); } -void LibraryDialog::onCurrentBookChanged() +void LibraryDialog::onAddFromFolder(const QString &path) { - close(); + progress->setLabelText(QFileInfo(path).fileName()); + progress->setValue(progress->value() + 1); } -void LibraryDialog::setSelected(const QModelIndex &libraryIndex) +void LibraryDialog::onSearch() { - QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex); - list->selectionModel()->clearSelection(); - if (sortedIndex.isValid()) { - list->selectionModel()->select(sortedIndex, - QItemSelectionModel::Select); - list->setCurrentIndex(sortedIndex); + TRACE; + int ret = searchDialog->exec(); + qDebug() << "Search dialog returned" << ret; + if (ret != QDialog::Accepted) { + return; } + progress->setLabelText(tr("Searching Project Gutenberg")); + progress->showWait(); + Search::instance()->start(searchDialog->query()); } -QModelIndex LibraryDialog::selected() const +void LibraryDialog::showSearchResults() { - QModelIndexList selectedItems = list->selectionModel()->selectedIndexes(); - if (selectedItems.size()) { - return sortedLibrary->mapToSource(selectedItems[0]); + progress->reset(); + QList results = Search::instance()->results(); + if (results.isEmpty()) { + QMessageBox::information(this, tr("Search results"), + tr("No books found")); + return; } - return QModelIndex(); + + SearchResultsDialog *dialog = new SearchResultsDialog(results, this); + connect(dialog, SIGNAL(add(const Search::Result &)), + this, SLOT(onAddSearchResult(const Search::Result &))); + dialog->show(); +} + +void LibraryDialog::onSortByAuthor() +{ + TRACE; + sortedLibrary->setSortBy(SortedLibrary::SortByAuthor); + Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor); + sortByAuthor->setChecked(true); + sortByTitle->setChecked(false); } -void LibraryDialog::onShowFolders() +void LibraryDialog::onSortByTitle() { - FoldersDialog *folders = new FoldersDialog(this); - folders->show(); + TRACE; + sortedLibrary->setSortBy(SortedLibrary::SortByTitle); + Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle); + sortByAuthor->setChecked(false); + sortByTitle->setChecked(true); }