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"
24 #include "progressdialog.h"
26 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
28 setWindowTitle(tr("Library"));
33 addItemAction(tr("Details"), this, SLOT(onDetails()));
34 addItemAction(tr("Read"), this, SLOT(onRead()));
35 addItemAction(tr("Delete"), this, SLOT(onRemove()));
36 #endif // ! Q_WS_MAEMO_5
38 addAction(tr("Add book"), this, SLOT(onAdd()), "add");
39 addAction(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
40 addAction(tr("Search the Web"), this, SLOT(onSearch()), "search");
42 // Create and add list view
43 list = new ListView(this);
44 sortedLibrary = new SortedLibrary(this);
45 list->setModel(sortedLibrary);
46 list->setSelectionMode(QAbstractItemView::SingleSelection);
48 Library *library = Library::instance();
49 QModelIndex current = library->nowReading();
53 progress = new ProgressDialog(tr("Adding books"), this);
55 connect(Library::instance(), SIGNAL(nowReadingChanged()),
56 this, SLOT(onCurrentBookChanged()));
57 connect(Library::instance(),
58 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
61 connect(list, SIGNAL(activated(const QModelIndex &)),
62 this, SLOT(onItemActivated(const QModelIndex &)));
64 // Create search dialog
65 searchDialog = new SearchDialog(this);
66 connect(Search::instance(), SIGNAL(endSearch()),
67 this, SLOT(showSearchResults()));
70 void LibraryDialog::onAdd()
72 Library *library = Library::instance();
74 // Figure out directory to show
75 QString lastDir = Settings::instance()->value("lastdir").toString();
77 lastDir = QDir::homePath();
81 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
82 lastDir, "Books (*.epub)");
87 // Save directory selected
88 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
90 // Add book to library
91 QModelIndex index = library->find(path);
92 if (index.isValid()) {
94 QMaemo5InformationBox::information(this,
95 tr("This book is already in the library"),
96 QMaemo5InformationBox::DefaultTimeout);
98 (void)QMessageBox::information(this, tr("Dorian"),
99 tr("This book is already in the library"), QMessageBox::Ok);
100 #endif // Q_WS_MAEMO_5
108 void LibraryDialog::onBookAdded()
110 Library *library = Library::instance();
111 setSelected(library->index(library->rowCount() - 1));
116 void LibraryDialog::onRemove()
118 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
119 if (current.isValid()) {
120 Book *currentBook = Library::instance()->book(current);
121 QString title = currentBook->name();
122 if (QMessageBox::Yes ==
123 QMessageBox::question(this, tr("Delete book"),
124 tr("Delete book \"%1\" from library?").
125 arg(currentBook->shortName()),
126 QMessageBox::Yes | QMessageBox::No)) {
127 Library::instance()->remove(current);
132 void LibraryDialog::onRead()
134 qDebug() << "LibraryDialog::onRead";
135 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
136 if (current.isValid()) {
137 Library::instance()->setNowReading(current);
141 void LibraryDialog::onDetails()
143 onItemActivated(list->currentIndex());
146 #endif // Q_WS_MAEMO_5
148 void LibraryDialog::onItemActivated(const QModelIndex &index)
150 qDebug() << "LibraryDialog::onItemActivated";
151 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
152 Book *book = Library::instance()->book(libraryIndex);
153 (new InfoDialog(book, this))->exec();
156 QString LibraryDialog::createItemText(const Book *book)
158 QString text = book->title + "\n";
159 if (book->creators.size()) {
160 text += book->creators[0];
161 for (int i = 1; i < book->creators.size(); i++) {
162 text += ", " + book->creators[i];
168 void LibraryDialog::onCurrentBookChanged()
173 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
175 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
176 list->selectionModel()->clearSelection();
177 if (sortedIndex.isValid()) {
178 list->selectionModel()->select(sortedIndex,
179 QItemSelectionModel::Select);
180 list->setCurrentIndex(sortedIndex);
184 QModelIndex LibraryDialog::selected() const
186 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
187 if (selectedItems.size()) {
188 return sortedLibrary->mapToSource(selectedItems[0]);
190 return QModelIndex();
193 void LibraryDialog::onAddFolder()
195 Trace t("LibraryDialog::onAddFolder");
198 Settings *settings = Settings::instance();
200 settings->value("lastfolderadded", QDir::homePath()).toString();
202 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
206 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
209 // Add books from folder
210 progress->setWindowTitle(tr("Adding books"));
211 BookFinder *bookFinder = new BookFinder(this);
212 Library *library = Library::instance();
213 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
214 connect(bookFinder, SIGNAL(add(const QString &)),
215 this, SLOT(onAddFromFolder(const QString &)));
216 connect(bookFinder, SIGNAL(add(const QString &)),
217 library, SLOT(add(const QString &)));
218 connect(bookFinder, SIGNAL(done(int)),
219 this, SLOT(onAddFromFolderDone(int)));
220 bookFinder->find(path, Library::instance()->bookPaths());
223 void LibraryDialog::onAddFromFolderDone(int added)
228 case 0: msg = tr("No new books found"); break;
229 case 1: msg = tr("One book added"); break;
230 default: msg = tr("%1 books added").arg(added);
234 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
236 QMaemo5InformationBox::information(this, msg);
242 void LibraryDialog::onAddFromFolder(const QString &path)
244 progress->setLabelText(QFileInfo(path).fileName());
245 progress->setValue(progress->value() + 1);
248 void LibraryDialog::onSearch()
250 int ret = searchDialog->exec();
251 if (ret != QDialog::Accepted) {
254 progress->setLabelText(tr("Searching Project Gutenberg"));
255 progress->setMinimum(0);
256 progress->setMaximum(0);
257 progress->setValue(0);
259 Search::instance()->start(searchDialog->query());
262 void LibraryDialog::showSearchResults()
265 QList<Search::Result> results = Search::instance()->results();
266 if (results.count() == 0) {
267 QMessageBox::information(this, tr("Search results"), tr("No books found"));
271 SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
273 connect(dialog, SIGNAL(add(const Search::Result &)),
274 this, SLOT(onAddSearchResult(const Search::Result &)));