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"
20 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
23 setWindowTitle(tr("Library"));
24 setAttribute(Qt::WA_DeleteOnClose, true);
28 sortByTitle = addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle()));
30 addMenuAction(tr("Sort by author"), this, SLOT(onSortByAuthor()));
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();
50 setSelected(sortedLibrary->mapFromSource(current));
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()));
69 // Retrieve default sort criteria
70 switch (Settings::instance()->value("lib/sortby").toInt()) {
71 case SortedLibrary::SortByAuthor:
79 void LibraryDialog::onAdd()
81 Library *library = Library::instance();
83 // Figure out directory to show
84 QString lastDir = Settings::instance()->value("lastdir").toString();
86 lastDir = QDir::homePath();
90 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
91 lastDir, "Books (*.epub)");
96 // Save directory selected
97 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
99 // Add book to library
100 QModelIndex index = library->find(path);
101 if (index.isValid()) {
102 Platform::instance()->information(
103 tr("This book is already in the library"), this);
104 setSelected(sortedLibrary->mapFromSource(index));
111 void LibraryDialog::onBookAdded()
113 Library *library = Library::instance();
114 setSelected(sortedLibrary->
115 mapFromSource(library->index(library->rowCount() - 1)));
120 void LibraryDialog::onRemove()
122 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
123 if (current.isValid()) {
124 Book *currentBook = Library::instance()->book(current);
125 QString title = currentBook->name();
126 if (QMessageBox::Yes ==
127 QMessageBox::question(this, tr("Delete book"),
128 tr("Delete book \"%1\" from library?").
129 arg(currentBook->shortName()),
130 QMessageBox::Yes | QMessageBox::No)) {
131 Library::instance()->remove(current);
136 void LibraryDialog::onRead()
138 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
139 if (current.isValid()) {
140 Library::instance()->setNowReading(current);
144 void LibraryDialog::onDetails()
146 onItemActivated(list->currentIndex());
149 #endif // Q_WS_MAEMO_5
151 void LibraryDialog::onItemActivated(const QModelIndex &index)
154 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
155 Book *book = Library::instance()->book(libraryIndex);
156 (new InfoDialog(book, this))->exec();
159 QString LibraryDialog::createItemText(Book *book)
162 QString text = book->shortName() + "\n";
163 if (book->creators.size()) {
164 text += book->creators[0];
165 for (int i = 1; i < book->creators.size(); i++) {
166 text += ", " + book->creators[i];
172 void LibraryDialog::onCurrentBookChanged()
177 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
179 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
180 list->selectionModel()->clearSelection();
181 if (sortedIndex.isValid()) {
182 list->selectionModel()->select(sortedIndex,
183 QItemSelectionModel::Select);
184 list->setCurrentIndex(sortedIndex);
188 QModelIndex LibraryDialog::selected() const
190 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
191 if (selectedItems.size()) {
192 return sortedLibrary->mapToSource(selectedItems[0]);
194 return QModelIndex();
197 void LibraryDialog::onAddFolder()
202 Settings *settings = Settings::instance();
204 settings->value("lastfolderadded", QDir::homePath()).toString();
206 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
210 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
213 // Add books from folder
214 progress->setWindowTitle(tr("Adding books"));
215 BookFinder *bookFinder = new BookFinder(this);
216 Library *library = Library::instance();
217 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
218 connect(bookFinder, SIGNAL(add(const QString &)),
219 this, SLOT(onAddFromFolder(const QString &)));
220 connect(bookFinder, SIGNAL(add(const QString &)),
221 library, SLOT(add(const QString &)));
222 connect(bookFinder, SIGNAL(done(int)),
223 this, SLOT(onAddFromFolderDone(int)));
224 bookFinder->find(path, Library::instance()->bookPaths());
227 void LibraryDialog::onAddFromFolderDone(int added)
232 case 0: msg = tr("No new books found"); break;
233 case 1: msg = tr("One book added"); break;
234 default: msg = tr("%1 books added").arg(added);
238 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
239 Platform::instance()->information(msg, this);
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->showWait();
256 Search::instance()->start(searchDialog->query());
259 void LibraryDialog::showSearchResults()
262 QList<Search::Result> results = Search::instance()->results();
263 if (results.isEmpty()) {
264 QMessageBox::information(this, tr("Search results"),
265 tr("No books found"));
269 SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
270 connect(dialog, SIGNAL(add(const Search::Result &)),
271 this, SLOT(onAddSearchResult(const Search::Result &)));
275 void LibraryDialog::onSortByAuthor()
278 sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
279 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
280 sortByAuthor->setChecked(true);
281 sortByTitle->setChecked(false);
284 void LibraryDialog::onSortByTitle()
287 sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
288 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
289 sortByAuthor->setChecked(false);
290 sortByTitle->setChecked(true);