4 #include "librarydialog.h"
6 #include "sortedlibrary.h"
8 #include "infodialog.h"
10 #include "listwindow.h"
13 #include "bookfinder.h"
14 #include "searchdialog.h"
16 #include "searchresultsdialog.h"
17 #include "progressdialog.h"
19 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
21 setWindowTitle(tr("Library"));
26 addItemAction(tr("Details"), this, SLOT(onDetails()));
27 addItemAction(tr("Read"), this, SLOT(onRead()));
28 addItemAction(tr("Delete"), this, SLOT(onRemove()));
29 #endif // ! Q_WS_MAEMO_5
31 addAction(tr("Add book"), this, SLOT(onAdd()), "add");
32 addAction(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
33 addAction(tr("Search the Web"), this, SLOT(onSearch()), "search");
35 // Create and add list view
36 list = new ListView(this);
37 sortedLibrary = new SortedLibrary(this);
38 list->setModel(sortedLibrary);
39 list->setSelectionMode(QAbstractItemView::SingleSelection);
41 Library *library = Library::instance();
42 QModelIndex current = library->nowReading();
46 progress = new ProgressDialog(tr("Adding books"), this);
48 connect(Library::instance(), SIGNAL(nowReadingChanged()),
49 this, SLOT(onCurrentBookChanged()));
50 connect(Library::instance(),
51 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
54 connect(list, SIGNAL(activated(const QModelIndex &)),
55 this, SLOT(onItemActivated(const QModelIndex &)));
57 // Create search dialog
58 searchDialog = new SearchDialog(this);
59 connect(Search::instance(), SIGNAL(endSearch()),
60 this, SLOT(showSearchResults()));
63 void LibraryDialog::onAdd()
65 Library *library = Library::instance();
67 // Figure out directory to show
68 QString lastDir = Settings::instance()->value("lastdir").toString();
70 lastDir = QDir::homePath();
74 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
75 lastDir, "Books (*.epub)");
80 // Save directory selected
81 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
83 // Add book to library
84 QModelIndex index = library->find(path);
85 if (index.isValid()) {
86 Platform::information(tr("This book is already in the library"), this);
94 void LibraryDialog::onBookAdded()
96 Library *library = Library::instance();
97 setSelected(library->index(library->rowCount() - 1));
102 void LibraryDialog::onRemove()
104 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
105 if (current.isValid()) {
106 Book *currentBook = Library::instance()->book(current);
107 QString title = currentBook->name();
108 if (QMessageBox::Yes ==
109 QMessageBox::question(this, tr("Delete book"),
110 tr("Delete book \"%1\" from library?").
111 arg(currentBook->shortName()),
112 QMessageBox::Yes | QMessageBox::No)) {
113 Library::instance()->remove(current);
118 void LibraryDialog::onRead()
120 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
121 if (current.isValid()) {
122 Library::instance()->setNowReading(current);
126 void LibraryDialog::onDetails()
128 onItemActivated(list->currentIndex());
131 #endif // Q_WS_MAEMO_5
133 void LibraryDialog::onItemActivated(const QModelIndex &index)
136 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
137 Book *book = Library::instance()->book(libraryIndex);
138 (new InfoDialog(book, this))->exec();
141 QString LibraryDialog::createItemText(const Book *book)
143 QString text = book->title + "\n";
144 if (book->creators.size()) {
145 text += book->creators[0];
146 for (int i = 1; i < book->creators.size(); i++) {
147 text += ", " + book->creators[i];
153 void LibraryDialog::onCurrentBookChanged()
158 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
160 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
161 list->selectionModel()->clearSelection();
162 if (sortedIndex.isValid()) {
163 list->selectionModel()->select(sortedIndex,
164 QItemSelectionModel::Select);
165 list->setCurrentIndex(sortedIndex);
169 QModelIndex LibraryDialog::selected() const
171 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
172 if (selectedItems.size()) {
173 return sortedLibrary->mapToSource(selectedItems[0]);
175 return QModelIndex();
178 void LibraryDialog::onAddFolder()
183 Settings *settings = Settings::instance();
185 settings->value("lastfolderadded", QDir::homePath()).toString();
187 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
191 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
194 // Add books from folder
195 progress->setWindowTitle(tr("Adding books"));
196 BookFinder *bookFinder = new BookFinder(this);
197 Library *library = Library::instance();
198 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
199 connect(bookFinder, SIGNAL(add(const QString &)),
200 this, SLOT(onAddFromFolder(const QString &)));
201 connect(bookFinder, SIGNAL(add(const QString &)),
202 library, SLOT(add(const QString &)));
203 connect(bookFinder, SIGNAL(done(int)),
204 this, SLOT(onAddFromFolderDone(int)));
205 bookFinder->find(path, Library::instance()->bookPaths());
208 void LibraryDialog::onAddFromFolderDone(int added)
213 case 0: msg = tr("No new books found"); break;
214 case 1: msg = tr("One book added"); break;
215 default: msg = tr("%1 books added").arg(added);
219 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
220 Platform::information(msg, this);
223 void LibraryDialog::onAddFromFolder(const QString &path)
225 progress->setLabelText(QFileInfo(path).fileName());
226 progress->setValue(progress->value() + 1);
229 void LibraryDialog::onSearch()
231 int ret = searchDialog->exec();
232 if (ret != QDialog::Accepted) {
235 progress->setLabelText(tr("Searching Project Gutenberg"));
236 progress->showWait();
237 Search::instance()->start(searchDialog->query());
240 void LibraryDialog::showSearchResults()
243 QList<Search::Result> results = Search::instance()->results();
244 if (results.count() == 0) {
245 QMessageBox::information(this, tr("Search results"),
246 tr("No books found"));
250 SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
251 connect(dialog, SIGNAL(add(const Search::Result &)),
252 this, SLOT(onAddSearchResult(const Search::Result &)));