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 progress->setMinimumDuration(0);
56 progress->setWindowModality(Qt::WindowModal);
57 progress->setCancelButton(0);
59 progress->setFixedWidth(
60 QApplication::desktop()->availableGeometry().width());
63 connect(Library::instance(), SIGNAL(nowReadingChanged()),
64 this, SLOT(onCurrentBookChanged()));
65 connect(Library::instance(),
66 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
69 connect(list, SIGNAL(activated(const QModelIndex &)),
70 this, SLOT(onItemActivated(const QModelIndex &)));
72 // Create search dialog
73 searchDialog = new SearchDialog(this);
74 connect(Search::instance(), SIGNAL(endSearch()),
75 this, SLOT(showSearchResults()));
78 void LibraryDialog::onAdd()
80 Library *library = Library::instance();
82 // Figure out directory to show
83 QString lastDir = Settings::instance()->value("lastdir").toString();
85 lastDir = QDir::homePath();
89 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
90 lastDir, "Books (*.epub)");
95 // Save directory selected
96 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
98 // Add book to library
99 QModelIndex index = library->find(path);
100 if (index.isValid()) {
102 QMaemo5InformationBox::information(this,
103 tr("This book is already in the library"),
104 QMaemo5InformationBox::DefaultTimeout);
106 (void)QMessageBox::information(this, tr("Dorian"),
107 tr("This book is already in the library"), QMessageBox::Ok);
108 #endif // Q_WS_MAEMO_5
116 void LibraryDialog::onBookAdded()
118 Library *library = Library::instance();
119 setSelected(library->index(library->rowCount() - 1));
124 void LibraryDialog::onRemove()
126 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
127 if (current.isValid()) {
128 Book *currentBook = Library::instance()->book(current);
129 QString title = currentBook->name();
130 if (QMessageBox::Yes ==
131 QMessageBox::question(this, tr("Delete book"),
132 tr("Delete book \"%1\" from library?").
133 arg(currentBook->shortName()),
134 QMessageBox::Yes | QMessageBox::No)) {
135 Library::instance()->remove(current);
140 void LibraryDialog::onRead()
142 qDebug() << "LibraryDialog::onRead";
143 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
144 if (current.isValid()) {
145 Library::instance()->setNowReading(current);
149 void LibraryDialog::onDetails()
151 onItemActivated(list->currentIndex());
154 #endif // Q_WS_MAEMO_5
156 void LibraryDialog::onItemActivated(const QModelIndex &index)
158 qDebug() << "LibraryDialog::onItemActivated";
159 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
160 Book *book = Library::instance()->book(libraryIndex);
161 (new InfoDialog(book, this))->exec();
164 QString LibraryDialog::createItemText(const Book *book)
166 QString text = book->title + "\n";
167 if (book->creators.size()) {
168 text += book->creators[0];
169 for (int i = 1; i < book->creators.size(); i++) {
170 text += ", " + book->creators[i];
176 void LibraryDialog::onCurrentBookChanged()
181 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
183 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
184 list->selectionModel()->clearSelection();
185 if (sortedIndex.isValid()) {
186 list->selectionModel()->select(sortedIndex,
187 QItemSelectionModel::Select);
188 list->setCurrentIndex(sortedIndex);
192 QModelIndex LibraryDialog::selected() const
194 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
195 if (selectedItems.size()) {
196 return sortedLibrary->mapToSource(selectedItems[0]);
198 return QModelIndex();
201 void LibraryDialog::onAddFolder()
203 Trace t("LibraryDialog::onAddFolder");
206 Settings *settings = Settings::instance();
208 settings->value("lastfolderadded", QDir::homePath()).toString();
210 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
214 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
217 // Add books from folder
218 progress->setWindowTitle(tr("Adding books"));
219 BookFinder *bookFinder = new BookFinder(this);
220 Library *library = Library::instance();
221 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
222 connect(bookFinder, SIGNAL(add(const QString &)),
223 this, SLOT(onAddFromFolder(const QString &)));
224 connect(bookFinder, SIGNAL(add(const QString &)),
225 library, SLOT(add(const QString &)));
226 connect(bookFinder, SIGNAL(done(int)),
227 this, SLOT(onAddFromFolderDone(int)));
228 bookFinder->find(path, Library::instance()->bookPaths());
231 void LibraryDialog::onAddFromFolderDone(int added)
236 case 0: msg = tr("No new books found"); break;
237 case 1: msg = tr("One book added"); break;
238 default: msg = tr("%1 books added").arg(added);
242 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
244 QMaemo5InformationBox::information(this, msg);
250 void LibraryDialog::onAddFromFolder(const QString &path)
252 progress->setLabelText(QFileInfo(path).fileName());
253 progress->setValue(progress->value() + 1);
256 void LibraryDialog::onSearch()
258 int ret = searchDialog->exec();
259 if (ret != QDialog::Accepted) {
262 Search::instance()->start(searchDialog->query());
265 void LibraryDialog::showSearchResults()
267 QList<Search::Result> results = Search::instance()->results();
268 if (results.count() == 0) {
269 QMessageBox::information(this, tr("Search results"), tr("No books found"));
273 SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
275 connect(dialog, SIGNAL(add(const Search::Result &)),
276 this, SLOT(onAddSearchResult(const Search::Result &)));