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, SLOT(onAddFolder()), "folder");
39 addButton(tr("Search the Web"), this, SLOT(onSearch()), "search");
42 Library *library = Library::instance();
43 QModelIndex current = library->nowReading();
44 setCurrentItem(sortedLibrary->mapFromSource(current));
47 searchDialog = new SearchDialog(this);
48 connect(Search::instance(), SIGNAL(endSearch()),
49 this, SLOT(showSearchResults()));
52 progress = new ProgressDialog(tr("Adding books"), this);
54 connect(library, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
55 this, SLOT(onBookAdded()));
56 connect(this, SIGNAL(activated(const QModelIndex &)),
57 this, SLOT(onItemActivated(const QModelIndex &)));
59 // Retrieve default sort criteria
60 switch (Settings::instance()->value("lib/sortby").toInt()) {
61 case SortedLibrary::SortByAuthor:
69 void LibraryDialog::onAdd()
71 Library *library = Library::instance();
73 // Figure out directory to show
74 QString lastDir = Settings::instance()->value("lastdir").toString();
76 lastDir = QDir::homePath();
80 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
81 lastDir, "Books (*.epub)");
86 // Save directory selected
87 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
89 // Add book to library
90 QModelIndex index = library->find(path);
91 if (index.isValid()) {
92 Platform::instance()->information(
93 tr("This book is already in the library"), this);
94 setSelected(sortedLibrary->mapFromSource(index));
101 void LibraryDialog::onBookAdded()
103 Library *library = Library::instance();
104 setSelected(sortedLibrary->
105 mapFromSource(library->index(library->rowCount() - 1)));
108 void LibraryDialog::onItemActivated(const QModelIndex &index)
112 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
113 Book *book = Library::instance()->book(libraryIndex);
114 int ret = (new InfoDialog(book, this))->exec();
117 case InfoDialog::Read:
118 Library::instance()->setNowReading(libraryIndex);
121 case InfoDialog::Delete:
122 Library::instance()->remove(libraryIndex);
129 QString LibraryDialog::createItemText(Book *book)
132 QString text = book->shortName() + "\n";
133 if (book->creators.size()) {
134 text += book->creators[0];
135 for (int i = 1; i < book->creators.size(); i++) {
136 text += ", " + book->creators[i];
142 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
144 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
145 if (sortedIndex.isValid()) {
146 setCurrentItem(sortedIndex);
150 void LibraryDialog::onAddFolder()
155 Settings *settings = Settings::instance();
157 settings->value("lastfolderadded", QDir::homePath()).toString();
159 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
163 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
166 // Add books from folder
167 progress->setWindowTitle(tr("Adding books"));
168 BookFinder *bookFinder = new BookFinder(this);
169 Library *library = Library::instance();
170 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
171 connect(bookFinder, SIGNAL(add(const QString &)),
172 this, SLOT(onAddFromFolder(const QString &)));
173 connect(bookFinder, SIGNAL(add(const QString &)),
174 library, SLOT(add(const QString &)));
175 connect(bookFinder, SIGNAL(done(int)),
176 this, SLOT(onAddFromFolderDone(int)));
177 bookFinder->find(path, Library::instance()->bookPaths());
180 void LibraryDialog::onAddFromFolderDone(int added)
185 case 0: msg = tr("No new books found"); break;
186 case 1: msg = tr("One book added"); break;
187 default: msg = tr("%1 books added").arg(added);
191 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
192 Platform::instance()->information(msg, this);
195 void LibraryDialog::onAddFromFolder(const QString &path)
197 progress->setLabelText(QFileInfo(path).fileName());
198 progress->setValue(progress->value() + 1);
201 void LibraryDialog::onSearch()
204 int ret = searchDialog->exec();
205 qDebug() << "Search dialog returned" << ret;
206 if (ret != QDialog::Accepted) {
209 progress->setLabelText(tr("Searching Project Gutenberg"));
210 progress->showWait();
211 Search::instance()->start(searchDialog->query());
214 void LibraryDialog::showSearchResults()
217 QList<Search::Result> results = Search::instance()->results();
218 if (results.isEmpty()) {
219 QMessageBox::information(this, tr("Search results"),
220 tr("No books found"));
224 SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
225 connect(dialog, SIGNAL(add(const Search::Result &)),
226 this, SLOT(onAddSearchResult(const Search::Result &)));
230 void LibraryDialog::onSortByAuthor()
233 sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
234 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
235 sortByAuthor->setChecked(true);
236 sortByTitle->setChecked(false);
239 void LibraryDialog::onSortByTitle()
242 sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
243 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
244 sortByAuthor->setChecked(false);
245 sortByTitle->setChecked(true);