5 #include <QtMaemo5/QMaemo5InformationBox>
8 #include "librarydialog.h"
10 #include "sortedlibrary.h"
12 #include "infodialog.h"
14 #include "listwindow.h"
17 #include "bookfinder.h"
18 #include "searchdialog.h"
20 #include "searchresultsdialog.h"
21 #include "progressdialog.h"
23 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
25 setWindowTitle(tr("Library"));
30 addItemAction(tr("Details"), this, SLOT(onDetails()));
31 addItemAction(tr("Read"), this, SLOT(onRead()));
32 addItemAction(tr("Delete"), this, SLOT(onRemove()));
33 #endif // ! Q_WS_MAEMO_5
35 addAction(tr("Add book"), this, SLOT(onAdd()), "add");
36 addAction(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
37 addAction(tr("Search the Web"), this, SLOT(onSearch()), "search");
39 // Create and add list view
40 list = new ListView(this);
41 sortedLibrary = new SortedLibrary(this);
42 list->setModel(sortedLibrary);
43 list->setSelectionMode(QAbstractItemView::SingleSelection);
45 Library *library = Library::instance();
46 QModelIndex current = library->nowReading();
50 progress = new ProgressDialog(tr("Adding books"), this);
52 connect(Library::instance(), SIGNAL(nowReadingChanged()),
53 this, SLOT(onCurrentBookChanged()));
54 connect(Library::instance(),
55 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
58 connect(list, SIGNAL(activated(const QModelIndex &)),
59 this, SLOT(onItemActivated(const QModelIndex &)));
61 // Create search dialog
62 searchDialog = new SearchDialog(this);
63 connect(Search::instance(), SIGNAL(endSearch()),
64 this, SLOT(showSearchResults()));
67 void LibraryDialog::onAdd()
69 Library *library = Library::instance();
71 // Figure out directory to show
72 QString lastDir = Settings::instance()->value("lastdir").toString();
74 lastDir = QDir::homePath();
78 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
79 lastDir, "Books (*.epub)");
84 // Save directory selected
85 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
87 // Add book to library
88 QModelIndex index = library->find(path);
89 if (index.isValid()) {
91 QMaemo5InformationBox::information(this,
92 tr("This book is already in the library"),
93 QMaemo5InformationBox::DefaultTimeout);
95 (void)QMessageBox::information(this, tr("Dorian"),
96 tr("This book is already in the library"), QMessageBox::Ok);
97 #endif // Q_WS_MAEMO_5
105 void LibraryDialog::onBookAdded()
107 Library *library = Library::instance();
108 setSelected(library->index(library->rowCount() - 1));
113 void LibraryDialog::onRemove()
115 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
116 if (current.isValid()) {
117 Book *currentBook = Library::instance()->book(current);
118 QString title = currentBook->name();
119 if (QMessageBox::Yes ==
120 QMessageBox::question(this, tr("Delete book"),
121 tr("Delete book \"%1\" from library?").
122 arg(currentBook->shortName()),
123 QMessageBox::Yes | QMessageBox::No)) {
124 Library::instance()->remove(current);
129 void LibraryDialog::onRead()
131 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
132 if (current.isValid()) {
133 Library::instance()->setNowReading(current);
137 void LibraryDialog::onDetails()
139 onItemActivated(list->currentIndex());
142 #endif // Q_WS_MAEMO_5
144 void LibraryDialog::onItemActivated(const QModelIndex &index)
147 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
148 Book *book = Library::instance()->book(libraryIndex);
149 (new InfoDialog(book, this))->exec();
152 QString LibraryDialog::createItemText(const Book *book)
154 QString text = book->title + "\n";
155 if (book->creators.size()) {
156 text += book->creators[0];
157 for (int i = 1; i < book->creators.size(); i++) {
158 text += ", " + book->creators[i];
164 void LibraryDialog::onCurrentBookChanged()
169 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
171 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
172 list->selectionModel()->clearSelection();
173 if (sortedIndex.isValid()) {
174 list->selectionModel()->select(sortedIndex,
175 QItemSelectionModel::Select);
176 list->setCurrentIndex(sortedIndex);
180 QModelIndex LibraryDialog::selected() const
182 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
183 if (selectedItems.size()) {
184 return sortedLibrary->mapToSource(selectedItems[0]);
186 return QModelIndex();
189 void LibraryDialog::onAddFolder()
194 Settings *settings = Settings::instance();
196 settings->value("lastfolderadded", QDir::homePath()).toString();
198 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
202 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
205 // Add books from folder
206 progress->setWindowTitle(tr("Adding books"));
207 BookFinder *bookFinder = new BookFinder(this);
208 Library *library = Library::instance();
209 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
210 connect(bookFinder, SIGNAL(add(const QString &)),
211 this, SLOT(onAddFromFolder(const QString &)));
212 connect(bookFinder, SIGNAL(add(const QString &)),
213 library, SLOT(add(const QString &)));
214 connect(bookFinder, SIGNAL(done(int)),
215 this, SLOT(onAddFromFolderDone(int)));
216 bookFinder->find(path, Library::instance()->bookPaths());
219 void LibraryDialog::onAddFromFolderDone(int added)
224 case 0: msg = tr("No new books found"); break;
225 case 1: msg = tr("One book added"); break;
226 default: msg = tr("%1 books added").arg(added);
230 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
232 QMaemo5InformationBox::information(this, msg);
234 QMessageBox::information(this, tr("Done adding books"), msg);
238 void LibraryDialog::onAddFromFolder(const QString &path)
240 progress->setLabelText(QFileInfo(path).fileName());
241 progress->setValue(progress->value() + 1);
244 void LibraryDialog::onSearch()
246 int ret = searchDialog->exec();
247 if (ret != QDialog::Accepted) {
250 progress->setLabelText(tr("Searching Project Gutenberg"));
251 progress->showWait();
252 Search::instance()->start(searchDialog->query());
255 void LibraryDialog::showSearchResults()
258 QList<Search::Result> results = Search::instance()->results();
259 if (results.count() == 0) {
260 QMessageBox::information(this, tr("Search results"),
261 tr("No books found"));
265 SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
266 connect(dialog, SIGNAL(add(const Search::Result &)),
267 this, SLOT(onAddSearchResult(const Search::Result &)));