-#include <QtGui>
-#include <QDebug>
-#include <QFileInfo>
#include <QDir>
-
-#ifdef Q_WS_MAEMO_5
-#include <QtMaemo5/QMaemo5InformationBox>
-#endif
+#include <QtGui>
#include "librarydialog.h"
#include "library.h"
+#include "sortedlibrary.h"
#include "book.h"
#include "infodialog.h"
+#include "settings.h"
+#include "listwindow.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):
- QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
- Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
+ ListWindow(tr("(No books)"), parent)
{
+ TRACE;
setWindowTitle(tr("Library"));
- list = new QListWidget(this);
- list->setSelectionMode(QAbstractItemView::SingleSelection);
-#ifndef Q_WS_MAEMO_5
- setSizeGripEnabled(true);
-#endif
+ setAttribute(Qt::WA_DeleteOnClose, true);
+
+ // Add menu actions
+ sortByTitle =
+ addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle()));
+ sortByAuthor =
+ addMenuAction(tr("Sort by author"), this, SLOT(onSortByAuthor()));
+ // Set model
+ sortedLibrary = new SortedLibrary(this);
+ 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");
+ addItemButton(tr("Delete"), this, SLOT(onDelete()), "delete");
+
+ // Set selected item
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);
- }
+ QModelIndex current = library->nowReading();
+ setCurrentItem(sortedLibrary->mapFromSource(current));
- 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);
-
-#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);
-
- connect(library, SIGNAL(bookAdded()), this, SLOT(onBookAdded()));
- connect(library, SIGNAL(currentBookChanged()),
- 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
+ // 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()
{
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();
}
- QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
+
+ // 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) {
-#ifdef Q_WS_MAEMO_5
- QMaemo5InformationBox::information(this,
- tr("Book is alreadButtony in the library"),
- QMaemo5InformationBox::DefaultTimeout);
-#endif
- // FIXME: Select existing book
+ // Save directory selected
+ Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
+
+ // Add book to library
+ QModelIndex index = library->find(path);
+ if (index.isValid()) {
+ Platform::instance()->information(
+ tr("This book is already in the library"), this);
+ setSelected(sortedLibrary->mapFromSource(index));
}
else {
library->add(path);
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(sortedLibrary->
+ mapFromSource(library->index(library->rowCount() - 1)));
}
-#ifndef Q_WS_MAEMO_5
+void LibraryDialog::onItemActivated(const QModelIndex &index)
+{
+ 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::onRemove()
+void LibraryDialog::onDelete()
{
- qDebug() << "LibraryDialog::onRemove";
- if (list->selectedItems().isEmpty()) {
+ QModelIndex current = currentItem();
+ if (!current.isValid()) {
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, QMessageBox::No)) {
- list->takeItem(row);
- Library::instance()->remove(row);
+ QModelIndex libraryIndex = sortedLibrary->mapToSource(current);
+ Book *book = Library::instance()->book(libraryIndex);
+ if (QMessageBox::Yes !=
+ QMessageBox::question(this, tr("Delete book"),
+ tr("Delete book \"%1\"?").arg(book->shortName()),
+ QMessageBox::Yes | QMessageBox::No)) {
+ return;
}
+ Library::instance()->remove(libraryIndex);
}
-void LibraryDialog::onRead()
+QString LibraryDialog::createItemText(Book *book)
{
- qDebug() << "LibraryDialog::onRead";
- if (list->selectedItems().isEmpty()) {
- return;
+ 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];
+ }
}
- QListWidgetItem *item = list->selectedItems()[0];
- int row = list->row(item);
- Library::instance()->setCurrent(row);
+ return text;
}
-void LibraryDialog::onDetails()
+void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
{
- onItemActivated(list->selectedItems()[0]);
+ QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
+ if (sortedIndex.isValid()) {
+ setCurrentItem(sortedIndex);
+ }
}
-#endif // Q_WS_MAEMO_5
-
-void LibraryDialog::onItemActivated(QListWidgetItem *item)
+void LibraryDialog::onAddFolder()
{
- qDebug() << "LibraryDialog::onItemActivated";
- int row = list->row(item);
- Book *book = Library::instance()->at(row);
- InfoDialog *info = new InfoDialog(book, this);
- info->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);
}
-#ifndef Q_WS_MAEMO_5
+void LibraryDialog::onAddFromFolder(const QString &path)
+{
+ progress->setLabelText(QFileInfo(path).fileName());
+ progress->setValue(progress->value() + 1);
+}
+
+void LibraryDialog::onSearch()
+{
+ 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());
+}
-void LibraryDialog::onItemSelectionChanged()
+void LibraryDialog::showSearchResults()
{
- bool enable = list->selectedItems().size();
- qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
- readButton->setEnabled(enable);
- qDebug() << " readButton";
- detailsButton->setEnabled(enable);
- qDebug() << " detailsButton";
- removeButton->setEnabled(enable);
- qDebug() << " removeButton";
+ progress->reset();
+ QList<Search::Result> results = Search::instance()->results();
+ if (results.isEmpty()) {
+ 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();
}
-#endif // Q_WS_MAEMO_5
+void LibraryDialog::onSortByAuthor()
+{
+ TRACE;
+ sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
+ Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
+ sortByAuthor->setChecked(true);
+ sortByTitle->setChecked(false);
+}
-void LibraryDialog::onCurrentBookChanged()
+void LibraryDialog::onSortByTitle()
{
- close();
+ TRACE;
+ sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
+ Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
+ sortByAuthor->setChecked(false);
+ sortByTitle->setChecked(true);
}