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): ListWindow(tr("(No books)"), parent)
22 setWindowTitle(tr("Library"));
23 setAttribute(Qt::WA_DeleteOnClose, true);
26 sortByTitle = addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle()));
28 addMenuAction(tr("Sort by author"), this, SLOT(onSortByAuthor()));
31 sortedLibrary = new SortedLibrary(this);
32 setModel(sortedLibrary);
35 addButton(tr("Add book"), this, SLOT(onAdd()), "add");
36 addButton(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
37 addButton(tr("Search the Web"), this, SLOT(onSearch()), "search");
40 Library *library = Library::instance();
41 QModelIndex current = library->nowReading();
42 setCurrentItem(sortedLibrary->mapFromSource(current));
45 searchDialog = new SearchDialog(this);
46 connect(Search::instance(), SIGNAL(endSearch()),
47 this, SLOT(showSearchResults()));
50 progress = new ProgressDialog(tr("Adding books"), this);
52 connect(library, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
53 this, SLOT(onBookAdded()));
54 connect(this, SIGNAL(activated(const QModelIndex &)),
55 this, SLOT(onItemActivated(const QModelIndex &)));
57 // Retrieve default sort criteria
58 switch (Settings::instance()->value("lib/sortby").toInt()) {
59 case SortedLibrary::SortByAuthor:
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()) {
90 Platform::instance()->information(
91 tr("This book is already in the library"), this);
92 setSelected(sortedLibrary->mapFromSource(index));
99 void LibraryDialog::onBookAdded()
101 Library *library = Library::instance();
102 setSelected(sortedLibrary->
103 mapFromSource(library->index(library->rowCount() - 1)));
106 void LibraryDialog::onItemActivated(const QModelIndex &index)
110 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
111 Book *book = Library::instance()->book(libraryIndex);
112 int ret = (new InfoDialog(book, this))->exec();
115 case InfoDialog::Read:
116 Library::instance()->setNowReading(libraryIndex);
119 case InfoDialog::Delete:
120 Library::instance()->remove(libraryIndex);
127 QString LibraryDialog::createItemText(Book *book)
130 QString text = book->shortName() + "\n";
131 if (book->creators.size()) {
132 text += book->creators[0];
133 for (int i = 1; i < book->creators.size(); i++) {
134 text += ", " + book->creators[i];
140 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
142 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
143 if (sortedIndex.isValid()) {
144 setCurrentItem(sortedIndex);
148 void LibraryDialog::onAddFolder()
153 Settings *settings = Settings::instance();
155 settings->value("lastfolderadded", QDir::homePath()).toString();
157 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
161 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
164 // Add books from folder
165 progress->setWindowTitle(tr("Adding books"));
166 BookFinder *bookFinder = new BookFinder(this);
167 Library *library = Library::instance();
168 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
169 connect(bookFinder, SIGNAL(add(const QString &)),
170 this, SLOT(onAddFromFolder(const QString &)));
171 connect(bookFinder, SIGNAL(add(const QString &)),
172 library, SLOT(add(const QString &)));
173 connect(bookFinder, SIGNAL(done(int)),
174 this, SLOT(onAddFromFolderDone(int)));
175 bookFinder->find(path, Library::instance()->bookPaths());
178 void LibraryDialog::onAddFromFolderDone(int added)
183 case 0: msg = tr("No new books found"); break;
184 case 1: msg = tr("One book added"); break;
185 default: msg = tr("%1 books added").arg(added);
189 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
190 Platform::instance()->information(msg, this);
193 void LibraryDialog::onAddFromFolder(const QString &path)
195 progress->setLabelText(QFileInfo(path).fileName());
196 progress->setValue(progress->value() + 1);
199 void LibraryDialog::onSearch()
202 int ret = searchDialog->exec();
203 qDebug() << "Search dialog returned" << ret;
204 if (ret != QDialog::Accepted) {
207 progress->setLabelText(tr("Searching Project Gutenberg"));
208 progress->showWait();
209 Search::instance()->start(searchDialog->query());
212 void LibraryDialog::showSearchResults()
215 QList<Search::Result> results = Search::instance()->results();
216 if (results.isEmpty()) {
217 QMessageBox::information(this, tr("Search results"),
218 tr("No books found"));
222 SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
223 connect(dialog, SIGNAL(add(const Search::Result &)),
224 this, SLOT(onAddSearchResult(const Search::Result &)));
228 void LibraryDialog::onSortByAuthor()
231 sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
232 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
233 sortByAuthor->setChecked(true);
234 sortByTitle->setChecked(false);
237 void LibraryDialog::onSortByTitle()
240 sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
241 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
242 sortByAuthor->setChecked(false);
243 sortByTitle->setChecked(true);