8 #include <QtMaemo5/QMaemo5InformationBox>
11 #include "librarydialog.h"
13 #include "sortedlibrary.h"
15 #include "infodialog.h"
17 #include "listwindow.h"
20 #include "bookfinder.h"
21 #include "searchdialog.h"
23 #include "searchresultsdialog.h"
25 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
27 setWindowTitle(tr("Library"));
32 addItemAction(tr("Details"), this, SLOT(onDetails()));
33 addItemAction(tr("Read"), this, SLOT(onRead()));
34 addItemAction(tr("Delete"), this, SLOT(onRemove()));
35 #endif // ! Q_WS_MAEMO_5
37 addAction(tr("Add book"), this, SLOT(onAdd()), "add");
38 addAction(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
39 addAction(tr("Search the Web"), this, SLOT(onSearch()), "search");
41 // Create and add list view
42 list = new ListView(this);
43 sortedLibrary = new SortedLibrary(this);
44 list->setModel(sortedLibrary);
45 list->setSelectionMode(QAbstractItemView::SingleSelection);
47 Library *library = Library::instance();
48 QModelIndex current = library->nowReading();
52 progress = new QProgressDialog(tr("Adding books"), "", 0, 0, this);
54 progress->setMinimumDuration(0);
55 progress->setWindowModality(Qt::WindowModal);
56 progress->setCancelButton(0);
58 progress->setFixedWidth(
59 QApplication::desktop()->availableGeometry().width());
62 connect(Library::instance(), SIGNAL(nowReadingChanged()),
63 this, SLOT(onCurrentBookChanged()));
64 connect(Library::instance(),
65 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
68 connect(list, SIGNAL(activated(const QModelIndex &)),
69 this, SLOT(onItemActivated(const QModelIndex &)));
71 // Create search dialog
72 searchDialog = new SearchDialog(this);
73 connect(Search::instance(), SIGNAL(endSearch()),
74 this, SLOT(showSearchResults()));
77 void LibraryDialog::onAdd()
79 Library *library = Library::instance();
81 // Figure out directory to show
82 QString lastDir = Settings::instance()->value("lastdir").toString();
84 lastDir = QDir::homePath();
88 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
89 lastDir, "Books (*.epub)");
94 // Save directory selected
95 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
97 // Add book to library
98 QModelIndex index = library->find(path);
99 if (index.isValid()) {
101 QMaemo5InformationBox::information(this,
102 tr("This book is already in the library"),
103 QMaemo5InformationBox::DefaultTimeout);
105 (void)QMessageBox::information(this, tr("Dorian"),
106 tr("This book is already in the library"), QMessageBox::Ok);
107 #endif // Q_WS_MAEMO_5
115 void LibraryDialog::onBookAdded()
117 Library *library = Library::instance();
118 setSelected(library->index(library->rowCount() - 1));
123 void LibraryDialog::onRemove()
125 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
126 if (current.isValid()) {
127 Book *currentBook = Library::instance()->book(current);
128 QString title = currentBook->name();
129 if (QMessageBox::Yes ==
130 QMessageBox::question(this, tr("Delete book"),
131 tr("Delete book \"%1\" from library?").
132 arg(currentBook->shortName()),
133 QMessageBox::Yes | QMessageBox::No)) {
134 Library::instance()->remove(current);
139 void LibraryDialog::onRead()
141 qDebug() << "LibraryDialog::onRead";
142 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
143 if (current.isValid()) {
144 Library::instance()->setNowReading(current);
148 void LibraryDialog::onDetails()
150 onItemActivated(list->currentIndex());
153 #endif // Q_WS_MAEMO_5
155 void LibraryDialog::onItemActivated(const QModelIndex &index)
157 qDebug() << "LibraryDialog::onItemActivated";
158 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
159 Book *book = Library::instance()->book(libraryIndex);
160 (new InfoDialog(book, this))->exec();
163 QString LibraryDialog::createItemText(const Book *book)
165 QString text = book->title + "\n";
166 if (book->creators.size()) {
167 text += book->creators[0];
168 for (int i = 1; i < book->creators.size(); i++) {
169 text += ", " + book->creators[i];
175 void LibraryDialog::onCurrentBookChanged()
180 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
182 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
183 list->selectionModel()->clearSelection();
184 if (sortedIndex.isValid()) {
185 list->selectionModel()->select(sortedIndex,
186 QItemSelectionModel::Select);
187 list->setCurrentIndex(sortedIndex);
191 QModelIndex LibraryDialog::selected() const
193 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
194 if (selectedItems.size()) {
195 return sortedLibrary->mapToSource(selectedItems[0]);
197 return QModelIndex();
200 void LibraryDialog::onAddFolder()
202 Trace t("LibraryDialog::onAddFolder");
205 Settings *settings = Settings::instance();
207 settings->value("lastfolderadded", QDir::homePath()).toString();
209 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
213 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
216 // Add books from folder
217 progress->setWindowTitle(tr("Adding books"));
218 BookFinder *bookFinder = new BookFinder(this);
219 Library *library = Library::instance();
220 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
221 connect(bookFinder, SIGNAL(add(const QString &)),
222 this, SLOT(onAddFromFolder(const QString &)));
223 connect(bookFinder, SIGNAL(add(const QString &)),
224 library, SLOT(add(const QString &)));
225 connect(bookFinder, SIGNAL(done(int)),
226 this, SLOT(onAddFromFolderDone(int)));
227 bookFinder->find(path, Library::instance()->bookPaths());
230 void LibraryDialog::onAddFromFolderDone(int added)
235 case 0: msg = tr("No new books found"); break;
236 case 1: msg = tr("One book added"); break;
237 default: msg = tr("%1 books added").arg(added);
241 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
243 QMaemo5InformationBox::information(this, msg);
249 void LibraryDialog::onAddFromFolder(const QString &path)
251 progress->setLabelText(QFileInfo(path).fileName());
252 progress->setValue(progress->value() + 1);
255 void LibraryDialog::onSearch()
257 int ret = searchDialog->exec();
258 if (ret != QDialog::Accepted) {
261 Search::instance()->start(searchDialog->query());
264 void LibraryDialog::showSearchResults()
266 QList<Search::Result> results = Search::instance()->results();
267 if (results.count() == 0) {
268 QMessageBox::information(this, tr("Search results"), tr("No books found"));
272 SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
273 connect(dialog, SIGNAL(add(const Search::Result &)),
274 this, SLOT(onAddSearchResult(const Search::Result &)));