4 #include "librarydialog.h"
6 #include "sortedlibrary.h"
8 #include "infodialog.h"
10 #include "listwindow.h"
12 #include "bookfinder.h"
13 #include "searchdialog.h"
15 #include "searchresultsdialog.h"
16 #include "progressdialog.h"
19 LibraryDialog::LibraryDialog(QWidget *parent):
20 ListWindow(tr("(No books)"), parent)
23 setWindowTitle(tr("Library"));
24 setAttribute(Qt::WA_DeleteOnClose, true);
28 addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle()));
30 addMenuAction(tr("Sort by author"), this, SLOT(onSortByAuthor()));
33 sortedLibrary = new SortedLibrary(this);
34 setModel(sortedLibrary);
37 addButton(tr("Add book"), this, SLOT(onAdd()), "add");
38 addButton(tr("Add books from folder"), this,
39 SLOT(onAddFolder()), "folder");
40 addButton(tr("Search the Web"), this, SLOT(onSearch()), "search");
41 addItemButton(tr("Delete"), this, SLOT(onDelete()), "delete");
44 Library *library = Library::instance();
45 QModelIndex current = library->nowReading();
46 setCurrentItem(sortedLibrary->mapFromSource(current));
49 searchDialog = new SearchDialog(this);
50 connect(Search::instance(), SIGNAL(endSearch()),
51 this, SLOT(showSearchResults()));
54 progress = new ProgressDialog(tr("Adding books"), this);
56 connect(library, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
57 this, SLOT(onBookAdded()));
58 connect(this, SIGNAL(activated(const QModelIndex &)),
59 this, SLOT(onItemActivated(const QModelIndex &)));
61 // Retrieve default sort criteria
62 switch (Settings::instance()->value("lib/sortby").toInt()) {
63 case SortedLibrary::SortByAuthor:
71 void LibraryDialog::onAdd()
73 Library *library = Library::instance();
75 // Figure out directory to show
76 QString lastDir = Settings::instance()->value("lastdir").toString();
78 lastDir = QDir::homePath();
82 QString path = QFileDialog::getOpenFileName(this, tr("Add book"),
83 lastDir, "Books (*.epub)");
88 // Save directory selected
89 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
91 // Add book to library
92 QModelIndex index = library->find(path);
93 if (index.isValid()) {
94 Platform::instance()->information(
95 tr("This book is already in the library"), this);
96 setSelected(sortedLibrary->mapFromSource(index));
103 void LibraryDialog::onBookAdded()
105 Library *library = Library::instance();
106 setSelected(sortedLibrary->
107 mapFromSource(library->index(library->rowCount() - 1)));
110 void LibraryDialog::onItemActivated(const QModelIndex &index)
114 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
115 Book *book = Library::instance()->book(libraryIndex);
116 int ret = (new InfoDialog(book, this))->exec();
119 case InfoDialog::Read:
120 Library::instance()->setNowReading(libraryIndex);
123 case InfoDialog::Delete:
124 Library::instance()->remove(libraryIndex);
131 void LibraryDialog::onDelete()
133 QModelIndex current = currentItem();
134 if (!current.isValid()) {
137 QModelIndex libraryIndex = sortedLibrary->mapToSource(current);
138 Book *book = Library::instance()->book(libraryIndex);
139 if (QMessageBox::Yes !=
140 QMessageBox::question(this, tr("Delete book"),
141 tr("Delete book \"%1\"?").arg(book->shortName()),
142 QMessageBox::Yes | QMessageBox::No)) {
145 Library::instance()->remove(libraryIndex);
148 QString LibraryDialog::createItemText(Book *book)
151 QString text = book->shortName() + "\n";
152 if (book->creators.size()) {
153 text += book->creators[0];
154 for (int i = 1; i < book->creators.size(); i++) {
155 text += ", " + book->creators[i];
161 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
163 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
164 if (sortedIndex.isValid()) {
165 setCurrentItem(sortedIndex);
169 void LibraryDialog::onAddFolder()
174 Settings *settings = Settings::instance();
176 settings->value("lastfolderadded", QDir::homePath()).toString();
178 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
182 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
185 // Add books from folder
186 progress->setWindowTitle(tr("Adding books"));
187 BookFinder *bookFinder = new BookFinder(this);
188 Library *library = Library::instance();
189 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
190 connect(bookFinder, SIGNAL(add(const QString &)),
191 this, SLOT(onAddFromFolder(const QString &)));
192 connect(bookFinder, SIGNAL(add(const QString &)),
193 library, SLOT(add(const QString &)));
194 connect(bookFinder, SIGNAL(done(int)),
195 this, SLOT(onAddFromFolderDone(int)));
196 bookFinder->find(path, Library::instance()->bookPaths());
199 void LibraryDialog::onAddFromFolderDone(int added)
204 case 0: msg = tr("No new books found"); break;
205 case 1: msg = tr("One book added"); break;
206 default: msg = tr("%1 books added").arg(added);
210 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
211 Platform::instance()->information(msg, this);
214 void LibraryDialog::onAddFromFolder(const QString &path)
216 progress->setLabelText(QFileInfo(path).fileName());
217 progress->setValue(progress->value() + 1);
220 void LibraryDialog::onSearch()
223 int ret = searchDialog->exec();
224 qDebug() << "Search dialog returned" << ret;
225 if (ret != QDialog::Accepted) {
228 progress->setLabelText(tr("Searching Project Gutenberg"));
229 progress->showWait();
230 Search::instance()->start(searchDialog->query());
233 void LibraryDialog::showSearchResults()
236 QList<Search::Result> results = Search::instance()->results();
237 if (results.isEmpty()) {
238 QMessageBox::information(this, tr("Search results"),
239 tr("No books found"));
243 SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
244 connect(dialog, SIGNAL(add(const Search::Result &)),
245 this, SLOT(onAddSearchResult(const Search::Result &)));
249 void LibraryDialog::onSortByAuthor()
252 sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
253 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
254 sortByAuthor->setChecked(true);
255 sortByTitle->setChecked(false);
258 void LibraryDialog::onSortByTitle()
261 sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
262 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
263 sortByAuthor->setChecked(false);
264 sortByTitle->setChecked(true);