Get rid of unused Options menu items on Symbian.
[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 "listview.h"
12 #include "trace.h"
13 #include "bookfinder.h"
14 #include "searchdialog.h"
15 #include "platform.h"
16 #include "searchresultsdialog.h"
17 #include "progressdialog.h"
18 #include "settings.h"
19
20 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
21 {
22     TRACE;
23     setWindowTitle(tr("Library"));
24     setAttribute(Qt::WA_DeleteOnClose, true);
25
26     // Add actions
27
28     sortByTitle = addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle()));
29     sortByAuthor =
30             addMenuAction(tr("Sort by author"), this, SLOT(onSortByAuthor()));
31
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");
35
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);
41     list->setSpacing(1);
42     Library *library = Library::instance();
43     QModelIndex current = library->nowReading();
44     setSelected(sortedLibrary->mapFromSource(current));
45     addList(list);
46
47     progress = new ProgressDialog(tr("Adding books"), this);
48
49     connect(Library::instance(),
50             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
51             this,
52             SLOT(onBookAdded()));
53     connect(list, SIGNAL(activated(const QModelIndex &)),
54             this, SLOT(onItemActivated(const QModelIndex &)));
55
56     // Create search dialog
57     searchDialog = new SearchDialog(this);
58     connect(Search::instance(), SIGNAL(endSearch()),
59             this, SLOT(showSearchResults()));
60
61     // Retrieve default sort criteria
62     switch (Settings::instance()->value("lib/sortby").toInt()) {
63     case SortedLibrary::SortByAuthor:
64         onSortByAuthor();
65         break;
66     default:
67         onSortByTitle();
68     }
69 }
70
71 void LibraryDialog::onAdd()
72 {
73     Library *library = Library::instance();
74
75     // Figure out directory to show
76     QString lastDir = Settings::instance()->value("lastdir").toString();
77     if (lastDir == "") {
78         lastDir = QDir::homePath();
79     }
80
81     // Get book file name
82     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
83                                                 lastDir, "Books (*.epub)");
84     if (path == "") {
85         return;
86     }
87
88     // Save directory selected
89     Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
90
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));
97     }
98     else {
99         library->add(path);
100     }
101 }
102
103 void LibraryDialog::onBookAdded()
104 {
105     Library *library = Library::instance();
106     setSelected(sortedLibrary->
107                 mapFromSource(library->index(library->rowCount() - 1)));
108 }
109
110 void LibraryDialog::onItemActivated(const QModelIndex &index)
111 {
112     TRACE;
113     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
114     Book *book = Library::instance()->book(libraryIndex);
115     int ret = (new InfoDialog(book, this))->exec();
116
117     switch (ret) {
118     case InfoDialog::Read:
119         {
120             QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
121             Q_ASSERT(current.isValid());
122             Library::instance()->setNowReading(current);
123             close();
124         }
125         break;
126     case InfoDialog::Delete:
127         {
128             QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
129             Library::instance()->remove(current);
130         }
131         break;
132     default:
133         ;
134     }
135 }
136
137 QString LibraryDialog::createItemText(Book *book)
138 {
139     Q_ASSERT(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];
145         }
146     }
147     return text;
148 }
149
150 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
151 {
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);
158     }
159 }
160
161 QModelIndex LibraryDialog::selected() const
162 {
163     QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
164     if (selectedItems.size()) {
165         return sortedLibrary->mapToSource(selectedItems[0]);
166     }
167     return QModelIndex();
168 }
169
170 void LibraryDialog::onAddFolder()
171 {
172     TRACE;
173
174     // Get folder name
175     Settings *settings = Settings::instance();
176     QString last =
177             settings->value("lastfolderadded", QDir::homePath()).toString();
178     QString path =
179             QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
180     if (path == "") {
181         return;
182     }
183     settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
184     qDebug() << path;
185
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());
198 }
199
200 void LibraryDialog::onAddFromFolderDone(int added)
201 {
202     QString msg;
203
204     switch (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);
208     }
209
210     progress->reset();
211     qDebug() << "LibraryDialog::onRefreshDone:" << msg;
212     Platform::instance()->information(msg, this);
213 }
214
215 void LibraryDialog::onAddFromFolder(const QString &path)
216 {
217     progress->setLabelText(QFileInfo(path).fileName());
218     progress->setValue(progress->value() + 1);
219 }
220
221 void LibraryDialog::onSearch()
222 {
223     int ret = searchDialog->exec();
224     if (ret != QDialog::Accepted) {
225         return;
226     }
227     progress->setLabelText(tr("Searching Project Gutenberg"));
228     progress->showWait();
229     Search::instance()->start(searchDialog->query());
230 }
231
232 void LibraryDialog::showSearchResults()
233 {
234     progress->reset();
235     QList<Search::Result> results = Search::instance()->results();
236     if (results.isEmpty()) {
237         QMessageBox::information(this, tr("Search results"),
238                                  tr("No books found"));
239         return;
240     }
241
242     SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
243     connect(dialog, SIGNAL(add(const Search::Result &)),
244             this, SLOT(onAddSearchResult(const Search::Result &)));
245     dialog->show();
246 }
247
248 void LibraryDialog::onSortByAuthor()
249 {
250     TRACE;
251     sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
252     Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
253     sortByAuthor->setChecked(true);
254     sortByTitle->setChecked(false);
255 }
256
257 void LibraryDialog::onSortByTitle()
258 {
259     TRACE;
260     sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
261     Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
262     sortByAuthor->setChecked(false);
263     sortByTitle->setChecked(true);
264 }