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");
43 Library *library = Library::instance();
44 QModelIndex current = library->nowReading();
45 setCurrentItem(sortedLibrary->mapFromSource(current));
48 searchDialog = new SearchDialog(this);
49 connect(Search::instance(), SIGNAL(endSearch()),
50 this, SLOT(showSearchResults()));
53 progress = new ProgressDialog(tr("Adding books"), this);
55 connect(library, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
56 this, SLOT(onBookAdded()));
57 connect(this, SIGNAL(activated(const QModelIndex &)),
58 this, SLOT(onItemActivated(const QModelIndex &)));
60 // Retrieve default sort criteria
61 switch (Settings::instance()->value("lib/sortby").toInt()) {
62 case SortedLibrary::SortByAuthor:
70 void LibraryDialog::onAdd()
72 Library *library = Library::instance();
74 // Figure out directory to show
75 QString lastDir = Settings::instance()->value("lastdir").toString();
77 lastDir = QDir::homePath();
81 QString path = QFileDialog::getOpenFileName(this, tr("Add book"),
82 lastDir, "Books (*.epub)");
87 // Save directory selected
88 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
90 // Add book to library
91 QModelIndex index = library->find(path);
92 if (index.isValid()) {
93 Platform::instance()->information(
94 tr("This book is already in the library"), this);
95 setSelected(sortedLibrary->mapFromSource(index));
102 void LibraryDialog::onBookAdded()
104 Library *library = Library::instance();
105 setSelected(sortedLibrary->
106 mapFromSource(library->index(library->rowCount() - 1)));
109 void LibraryDialog::onItemActivated(const QModelIndex &index)
113 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
114 Book *book = Library::instance()->book(libraryIndex);
115 int ret = (new InfoDialog(book, this))->exec();
118 case InfoDialog::Read:
119 Library::instance()->setNowReading(libraryIndex);
122 case InfoDialog::Delete:
123 Library::instance()->remove(libraryIndex);
130 QString LibraryDialog::createItemText(Book *book)
133 QString text = book->shortName() + "\n";
134 if (book->creators.size()) {
135 text += book->creators[0];
136 for (int i = 1; i < book->creators.size(); i++) {
137 text += ", " + book->creators[i];
143 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
145 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
146 if (sortedIndex.isValid()) {
147 setCurrentItem(sortedIndex);
151 void LibraryDialog::onAddFolder()
156 Settings *settings = Settings::instance();
158 settings->value("lastfolderadded", QDir::homePath()).toString();
160 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
164 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
167 // Add books from folder
168 progress->setWindowTitle(tr("Adding books"));
169 BookFinder *bookFinder = new BookFinder(this);
170 Library *library = Library::instance();
171 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
172 connect(bookFinder, SIGNAL(add(const QString &)),
173 this, SLOT(onAddFromFolder(const QString &)));
174 connect(bookFinder, SIGNAL(add(const QString &)),
175 library, SLOT(add(const QString &)));
176 connect(bookFinder, SIGNAL(done(int)),
177 this, SLOT(onAddFromFolderDone(int)));
178 bookFinder->find(path, Library::instance()->bookPaths());
181 void LibraryDialog::onAddFromFolderDone(int added)
186 case 0: msg = tr("No new books found"); break;
187 case 1: msg = tr("One book added"); break;
188 default: msg = tr("%1 books added").arg(added);
192 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
193 Platform::instance()->information(msg, this);
196 void LibraryDialog::onAddFromFolder(const QString &path)
198 progress->setLabelText(QFileInfo(path).fileName());
199 progress->setValue(progress->value() + 1);
202 void LibraryDialog::onSearch()
205 int ret = searchDialog->exec();
206 qDebug() << "Search dialog returned" << ret;
207 if (ret != QDialog::Accepted) {
210 progress->setLabelText(tr("Searching Project Gutenberg"));
211 progress->showWait();
212 Search::instance()->start(searchDialog->query());
215 void LibraryDialog::showSearchResults()
218 QList<Search::Result> results = Search::instance()->results();
219 if (results.isEmpty()) {
220 QMessageBox::information(this, tr("Search results"),
221 tr("No books found"));
225 SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
226 connect(dialog, SIGNAL(add(const Search::Result &)),
227 this, SLOT(onAddSearchResult(const Search::Result &)));
231 void LibraryDialog::onSortByAuthor()
234 sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
235 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
236 sortByAuthor->setChecked(true);
237 sortByTitle->setChecked(false);
240 void LibraryDialog::onSortByTitle()
243 sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
244 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
245 sortByAuthor->setChecked(false);
246 sortByTitle->setChecked(true);