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()));
32 addAction(tr("Add book"), this, SLOT(onAdd()), "add");
33 addAction(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
34 addAction(tr("Search the Web"), this, SLOT(onSearch()), "search");
36 // Create and add list view
37 list = new ListView(this);
38 sortedLibrary = new SortedLibrary(this);
39 list->setModel(sortedLibrary);
40 list->setSelectionMode(QAbstractItemView::SingleSelection);
42 Library *library = Library::instance();
43 QModelIndex current = library->nowReading();
44 setSelected(sortedLibrary->mapFromSource(current));
47 progress = new ProgressDialog(tr("Adding books"), this);
49 connect(Library::instance(),
50 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
53 connect(list, SIGNAL(activated(const QModelIndex &)),
54 this, SLOT(onItemActivated(const QModelIndex &)));
56 // Create search dialog
57 searchDialog = new SearchDialog(this);
58 connect(Search::instance(), SIGNAL(endSearch()),
59 this, SLOT(showSearchResults()));
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)
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:
120 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
121 Q_ASSERT(current.isValid());
122 Library::instance()->setNowReading(current);
126 case InfoDialog::Delete:
128 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
129 Library::instance()->remove(current);
137 QString LibraryDialog::createItemText(Book *book)
140 QString text = book->shortName() + "\n";
141 if (book->creators.size()) {
142 text += book->creators[0];
143 for (int i = 1; i < book->creators.size(); i++) {
144 text += ", " + book->creators[i];
150 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
152 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
153 list->selectionModel()->clearSelection();
154 if (sortedIndex.isValid()) {
155 list->selectionModel()->select(sortedIndex,
156 QItemSelectionModel::Select);
157 list->setCurrentIndex(sortedIndex);
161 QModelIndex LibraryDialog::selected() const
163 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
164 if (selectedItems.size()) {
165 return sortedLibrary->mapToSource(selectedItems[0]);
167 return QModelIndex();
170 void LibraryDialog::onAddFolder()
175 Settings *settings = Settings::instance();
177 settings->value("lastfolderadded", QDir::homePath()).toString();
179 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
183 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
186 // Add books from folder
187 progress->setWindowTitle(tr("Adding books"));
188 BookFinder *bookFinder = new BookFinder(this);
189 Library *library = Library::instance();
190 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
191 connect(bookFinder, SIGNAL(add(const QString &)),
192 this, SLOT(onAddFromFolder(const QString &)));
193 connect(bookFinder, SIGNAL(add(const QString &)),
194 library, SLOT(add(const QString &)));
195 connect(bookFinder, SIGNAL(done(int)),
196 this, SLOT(onAddFromFolderDone(int)));
197 bookFinder->find(path, Library::instance()->bookPaths());
200 void LibraryDialog::onAddFromFolderDone(int added)
205 case 0: msg = tr("No new books found"); break;
206 case 1: msg = tr("One book added"); break;
207 default: msg = tr("%1 books added").arg(added);
211 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
212 Platform::instance()->information(msg, this);
215 void LibraryDialog::onAddFromFolder(const QString &path)
217 progress->setLabelText(QFileInfo(path).fileName());
218 progress->setValue(progress->value() + 1);
221 void LibraryDialog::onSearch()
223 int ret = searchDialog->exec();
224 if (ret != QDialog::Accepted) {
227 progress->setLabelText(tr("Searching Project Gutenberg"));
228 progress->showWait();
229 Search::instance()->start(searchDialog->query());
232 void LibraryDialog::showSearchResults()
235 QList<Search::Result> results = Search::instance()->results();
236 if (results.isEmpty()) {
237 QMessageBox::information(this, tr("Search results"),
238 tr("No books found"));
242 SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
243 connect(dialog, SIGNAL(add(const Search::Result &)),
244 this, SLOT(onAddSearchResult(const Search::Result &)));
248 void LibraryDialog::onSortByAuthor()
251 sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
252 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
253 sortByAuthor->setChecked(true);
254 sortByTitle->setChecked(false);
257 void LibraryDialog::onSortByTitle()
260 sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
261 Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
262 sortByAuthor->setChecked(false);
263 sortByTitle->setChecked(true);