Make orientation switch explicit on Symbian, too.
[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,
39               SLOT(onAddFolder()), "folder");
40     addButton(tr("Search the Web"), this, SLOT(onSearch()), "search");
41     addItemButton(tr("Delete"), this, SLOT(onDelete()), "delete");
42
43     // Set selected item
44     Library *library = Library::instance();
45     QModelIndex current = library->nowReading();
46     setCurrentItem(sortedLibrary->mapFromSource(current));
47
48     // Search dialog box
49     searchDialog = new SearchDialog(this);
50     connect(Search::instance(), SIGNAL(endSearch()),
51             this, SLOT(showSearchResults()));
52
53     // Progress bar
54     progress = new ProgressDialog(tr("Adding books"), this);
55
56     connect(library, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
57             this, SLOT(onBookAdded()));
58     connect(this, SIGNAL(activated(const QModelIndex &)),
59             this, SLOT(onItemActivated(const QModelIndex &)));
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
114     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
115     Book *book = Library::instance()->book(libraryIndex);
116     int ret = (new InfoDialog(book, this))->exec();
117
118     switch (ret) {
119     case InfoDialog::Read:
120         Library::instance()->setNowReading(libraryIndex);
121         close();
122         break;
123     case InfoDialog::Delete:
124         Library::instance()->remove(libraryIndex);
125         break;
126     default:
127         ;
128     }
129 }
130
131 void LibraryDialog::onDelete()
132 {
133     QModelIndex current = currentItem();
134     if (!current.isValid()) {
135         return;
136     }
137     QModelIndex libraryIndex = sortedLibrary->mapToSource(current);
138     Book *book = Library::instance()->book(libraryIndex);
139     if (QMessageBox::Yes !=
140         QMessageBox::question(this, tr("Delete book"),
141             tr("Delete book \"%1\"?").arg(book->shortName()),
142             QMessageBox::Yes | QMessageBox::No)) {
143         return;
144     }
145     Library::instance()->remove(libraryIndex);
146 }
147
148 QString LibraryDialog::createItemText(Book *book)
149 {
150     Q_ASSERT(book);
151     QString text = book->shortName() + "\n";
152     if (book->creators.size()) {
153         text += book->creators[0];
154         for (int i = 1; i < book->creators.size(); i++) {
155             text += ", " + book->creators[i];
156         }
157     }
158     return text;
159 }
160
161 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
162 {
163     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
164     if (sortedIndex.isValid()) {
165         setCurrentItem(sortedIndex);
166     }
167 }
168
169 void LibraryDialog::onAddFolder()
170 {
171     TRACE;
172
173     // Get folder name
174     Settings *settings = Settings::instance();
175     QString last =
176             settings->value("lastfolderadded", QDir::homePath()).toString();
177     QString path =
178             QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
179     if (path == "") {
180         return;
181     }
182     settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
183     qDebug() << path;
184
185     // Add books from folder
186     progress->setWindowTitle(tr("Adding books"));
187     BookFinder *bookFinder = new BookFinder(this);
188     Library *library = Library::instance();
189     connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
190     connect(bookFinder, SIGNAL(add(const QString &)),
191             this, SLOT(onAddFromFolder(const QString &)));
192     connect(bookFinder, SIGNAL(add(const QString &)),
193             library, SLOT(add(const QString &)));
194     connect(bookFinder, SIGNAL(done(int)),
195             this, SLOT(onAddFromFolderDone(int)));
196     bookFinder->find(path, Library::instance()->bookPaths());
197 }
198
199 void LibraryDialog::onAddFromFolderDone(int added)
200 {
201     QString msg;
202
203     switch (added) {
204     case 0: msg = tr("No new books found"); break;
205     case 1: msg = tr("One book added"); break;
206     default: msg = tr("%1 books added").arg(added);
207     }
208
209     progress->reset();
210     qDebug() << "LibraryDialog::onRefreshDone:" << msg;
211     Platform::instance()->information(msg, this);
212 }
213
214 void LibraryDialog::onAddFromFolder(const QString &path)
215 {
216     progress->setLabelText(QFileInfo(path).fileName());
217     progress->setValue(progress->value() + 1);
218 }
219
220 void LibraryDialog::onSearch()
221 {
222     TRACE;
223     int ret = searchDialog->exec();
224     qDebug() << "Search dialog returned" << ret;
225     if (ret != QDialog::Accepted) {
226         return;
227     }
228     progress->setLabelText(tr("Searching Project Gutenberg"));
229     progress->showWait();
230     Search::instance()->start(searchDialog->query());
231 }
232
233 void LibraryDialog::showSearchResults()
234 {
235     progress->reset();
236     QList<Search::Result> results = Search::instance()->results();
237     if (results.isEmpty()) {
238         QMessageBox::information(this, tr("Search results"),
239                                  tr("No books found"));
240         return;
241     }
242
243     SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
244     connect(dialog, SIGNAL(add(const Search::Result &)),
245             this, SLOT(onAddSearchResult(const Search::Result &)));
246     dialog->show();
247 }
248
249 void LibraryDialog::onSortByAuthor()
250 {
251     TRACE;
252     sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
253     Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
254     sortByAuthor->setChecked(true);
255     sortByTitle->setChecked(false);
256 }
257
258 void LibraryDialog::onSortByTitle()
259 {
260     TRACE;
261     sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
262     Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
263     sortByAuthor->setChecked(false);
264     sortByTitle->setChecked(true);
265 }