Improve identification if bookmark's chapter. Add some AppAward
[dorian] / librarydialog.cpp
1 #include <QDir>
2 #include <QtGui>
3
4 #include "librarydialog.h"
5 #include "library.h"
6 #include "sortedlibrary.h"
7 #include "book.h"
8 #include "infodialog.h"
9 #include "settings.h"
10 #include "listwindow.h"
11 #include "trace.h"
12 #include "bookfinder.h"
13 #include "searchdialog.h"
14 #include "platform.h"
15 #include "searchresultsdialog.h"
16 #include "progressdialog.h"
17 #include "settings.h"
18
19 LibraryDialog::LibraryDialog(QWidget *parent):
20         ListWindow(tr("(No books)"), parent)
21 {
22     TRACE;
23     setWindowTitle(tr("Library"));
24     setAttribute(Qt::WA_DeleteOnClose, true);
25
26     // Add menu actions
27     sortByTitle =
28             addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle()));
29     sortByAuthor =
30             addMenuAction(tr("Sort by author"), this, SLOT(onSortByAuthor()));
31
32     // Set model
33     sortedLibrary = new SortedLibrary(this);
34     setModel(sortedLibrary);
35
36     // Add action buttons
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");
40
41     // Set selected item
42     Library *library = Library::instance();
43     QModelIndex current = library->nowReading();
44     setCurrentItem(sortedLibrary->mapFromSource(current));
45
46     // Search dialog box
47     searchDialog = new SearchDialog(this);
48     connect(Search::instance(), SIGNAL(endSearch()),
49             this, SLOT(showSearchResults()));
50
51     // Progress bar
52     progress = new ProgressDialog(tr("Adding books"), this);
53
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 &)));
58
59     // Retrieve default sort criteria
60     switch (Settings::instance()->value("lib/sortby").toInt()) {
61     case SortedLibrary::SortByAuthor:
62         onSortByAuthor();
63         break;
64     default:
65         onSortByTitle();
66     }
67 }
68
69 void LibraryDialog::onAdd()
70 {
71     Library *library = Library::instance();
72
73     // Figure out directory to show
74     QString lastDir = Settings::instance()->value("lastdir").toString();
75     if (lastDir == "") {
76         lastDir = QDir::homePath();
77     }
78
79     // Get book file name
80     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
81                                                 lastDir, "Books (*.epub)");
82     if (path == "") {
83         return;
84     }
85
86     // Save directory selected
87     Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
88
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));
95     }
96     else {
97         library->add(path);
98     }
99 }
100
101 void LibraryDialog::onBookAdded()
102 {
103     Library *library = Library::instance();
104     setSelected(sortedLibrary->
105                 mapFromSource(library->index(library->rowCount() - 1)));
106 }
107
108 void LibraryDialog::onItemActivated(const QModelIndex &index)
109 {
110     TRACE;
111
112     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
113     Book *book = Library::instance()->book(libraryIndex);
114     int ret = (new InfoDialog(book, this))->exec();
115
116     switch (ret) {
117     case InfoDialog::Read:
118         Library::instance()->setNowReading(libraryIndex);
119         close();
120         break;
121     case InfoDialog::Delete:
122         Library::instance()->remove(libraryIndex);
123         break;
124     default:
125         ;
126     }
127 }
128
129 QString LibraryDialog::createItemText(Book *book)
130 {
131     Q_ASSERT(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];
137         }
138     }
139     return text;
140 }
141
142 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
143 {
144     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
145     if (sortedIndex.isValid()) {
146         setCurrentItem(sortedIndex);
147     }
148 }
149
150 void LibraryDialog::onAddFolder()
151 {
152     TRACE;
153
154     // Get folder name
155     Settings *settings = Settings::instance();
156     QString last =
157             settings->value("lastfolderadded", QDir::homePath()).toString();
158     QString path =
159             QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
160     if (path == "") {
161         return;
162     }
163     settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
164     qDebug() << path;
165
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());
178 }
179
180 void LibraryDialog::onAddFromFolderDone(int added)
181 {
182     QString msg;
183
184     switch (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);
188     }
189
190     progress->reset();
191     qDebug() << "LibraryDialog::onRefreshDone:" << msg;
192     Platform::instance()->information(msg, this);
193 }
194
195 void LibraryDialog::onAddFromFolder(const QString &path)
196 {
197     progress->setLabelText(QFileInfo(path).fileName());
198     progress->setValue(progress->value() + 1);
199 }
200
201 void LibraryDialog::onSearch()
202 {
203     TRACE;
204     int ret = searchDialog->exec();
205     qDebug() << "Search dialog returned" << ret;
206     if (ret != QDialog::Accepted) {
207         return;
208     }
209     progress->setLabelText(tr("Searching Project Gutenberg"));
210     progress->showWait();
211     Search::instance()->start(searchDialog->query());
212 }
213
214 void LibraryDialog::showSearchResults()
215 {
216     progress->reset();
217     QList<Search::Result> results = Search::instance()->results();
218     if (results.isEmpty()) {
219         QMessageBox::information(this, tr("Search results"),
220                                  tr("No books found"));
221         return;
222     }
223
224     SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
225     connect(dialog, SIGNAL(add(const Search::Result &)),
226             this, SLOT(onAddSearchResult(const Search::Result &)));
227     dialog->show();
228 }
229
230 void LibraryDialog::onSortByAuthor()
231 {
232     TRACE;
233     sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
234     Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
235     sortByAuthor->setChecked(true);
236     sortByTitle->setChecked(false);
237 }
238
239 void LibraryDialog::onSortByTitle()
240 {
241     TRACE;
242     sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
243     Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
244     sortByAuthor->setChecked(false);
245     sortByTitle->setChecked(true);
246 }