Set zoom level in steps. Add icons to some buttons. Adjust dialog box layout accordin...
[dorian] / librarydialog.cpp
1 #include <QtGui>
2 #include <QDebug>
3 #include <QFileInfo>
4 #include <QDir>
5 #include <QModelIndex>
6
7 #ifdef Q_WS_MAEMO_5
8 #include <QtMaemo5/QMaemo5InformationBox>
9 #endif
10
11 #include "librarydialog.h"
12 #include "library.h"
13 #include "sortedlibrary.h"
14 #include "book.h"
15 #include "infodialog.h"
16 #include "settings.h"
17 #include "listwindow.h"
18 #include "listview.h"
19 #include "trace.h"
20 #include "bookfinder.h"
21
22 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
23 {
24     setWindowTitle(tr("Library"));
25
26     // Add actions
27
28 #ifndef Q_WS_MAEMO_5
29     addItemAction(tr("Details"), this, SLOT(onDetails()));
30     addItemAction(tr("Read"), this, SLOT(onRead()));
31     addItemAction(tr("Delete"), this, SLOT(onRemove()));
32 #endif // ! Q_WS_MAEMO_5
33
34     addAction(tr("Add book"), this, SLOT(onAdd()), ":/icons/add.png");
35     addAction(tr("Add books from folder"), this, SLOT(onAddFolder()),
36               ":/icons/folder.png");
37
38     // Create and add list view
39     list = new ListView(this);
40     sortedLibrary = new SortedLibrary(this);
41     list->setModel(sortedLibrary);
42     list->setSelectionMode(QAbstractItemView::SingleSelection);
43     list->setSpacing(1);
44     Library *library = Library::instance();
45     QModelIndex current = library->nowReading();
46     setSelected(current);
47     addList(list);
48
49     progress = new QProgressDialog(tr("Adding books"), "", 0, 0, this);
50     progress->reset();
51     progress->setMinimumDuration(0);
52     progress->setWindowModality(Qt::WindowModal);
53     progress->setCancelButton(0);
54
55     connect(Library::instance(), SIGNAL(nowReadingChanged()),
56             this, SLOT(onCurrentBookChanged()));
57     connect(Library::instance(),
58             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
59             this,
60             SLOT(onBookAdded()));
61     connect(list, SIGNAL(activated(const QModelIndex &)),
62             this, SLOT(onItemActivated(const QModelIndex &)));
63 }
64
65 void LibraryDialog::onAdd()
66 {
67     Library *library = Library::instance();
68
69     // Figure out directory to show
70     QString lastDir = Settings::instance()->value("lastdir").toString();
71     if (lastDir == "") {
72         lastDir = QDir::homePath();
73     }
74
75     // Get book file name
76     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
77                                                 lastDir, "Books (*.epub)");
78     if (path == "") {
79         return;
80     }
81
82     // Save directory selected
83     Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
84
85     // Add book to library
86     QModelIndex index = library->find(path);
87     if (index.isValid()) {
88 #ifdef Q_WS_MAEMO_5
89         QMaemo5InformationBox::information(this,
90             tr("This book is already in the library"),
91             QMaemo5InformationBox::DefaultTimeout);
92 #else
93         (void)QMessageBox::information(this, tr("Dorian"),
94             tr("This book is already in the library"), QMessageBox::Ok);
95 #endif // Q_WS_MAEMO_5
96         setSelected(index);
97     }
98     else {
99         library->add(path);
100     }
101 }
102
103 void LibraryDialog::onBookAdded()
104 {
105     Library *library = Library::instance();
106     setSelected(library->index(library->rowCount() - 1));
107 }
108
109 #ifndef Q_WS_MAEMO_5
110
111 void LibraryDialog::onRemove()
112 {
113     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
114     if (current.isValid()) {
115         Book *currentBook = Library::instance()->book(current);
116         QString title = currentBook->name();
117         if (QMessageBox::Yes ==
118             QMessageBox::question(this, tr("Delete book"),
119                 tr("Delete book \"%1\" from library?").
120                     arg(currentBook->shortName()),
121                 QMessageBox::Yes | QMessageBox::No)) {
122             Library::instance()->remove(current);
123         }
124     }
125 }
126
127 void LibraryDialog::onRead()
128 {
129     qDebug() << "LibraryDialog::onRead";
130     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
131     if (current.isValid()) {
132         Library::instance()->setNowReading(current);
133     }
134 }
135
136 void LibraryDialog::onDetails()
137 {
138     onItemActivated(list->currentIndex());
139 }
140
141 #endif // Q_WS_MAEMO_5
142
143 void LibraryDialog::onItemActivated(const QModelIndex &index)
144 {
145     qDebug() << "LibraryDialog::onItemActivated";
146     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
147     Book *book = Library::instance()->book(libraryIndex);
148     (new InfoDialog(book, this))->exec();
149 }
150
151 QString LibraryDialog::createItemText(const Book *book)
152 {
153     QString text = book->title + "\n";
154     if (book->creators.size()) {
155         text += book->creators[0];
156         for (int i = 1; i < book->creators.size(); i++) {
157             text += ", " + book->creators[i];
158         }
159     }
160     return text;
161 }
162
163 void LibraryDialog::onCurrentBookChanged()
164 {
165     close();
166 }
167
168 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
169 {
170     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
171     list->selectionModel()->clearSelection();
172     if (sortedIndex.isValid()) {
173         list->selectionModel()->select(sortedIndex,
174                                        QItemSelectionModel::Select);
175         list->setCurrentIndex(sortedIndex);
176     }
177 }
178
179 QModelIndex LibraryDialog::selected() const
180 {
181     QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
182     if (selectedItems.size()) {
183         return sortedLibrary->mapToSource(selectedItems[0]);
184     }
185     return QModelIndex();
186 }
187
188 void LibraryDialog::onAddFolder()
189 {
190     Trace t("LibraryDialog::onAddFolder");
191
192     // Get folder name
193     Settings *settings = Settings::instance();
194     QString last =
195             settings->value("lastfolderadded", QDir::homePath()).toString();
196     QString path =
197             QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
198     if (path == "") {
199         return;
200     }
201     settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
202     qDebug() << path;
203
204     // Add books from folder
205     progress->setWindowTitle(tr("Adding books"));
206     BookFinder *bookFinder = new BookFinder(this);
207     Library *library = Library::instance();
208     connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
209     connect(bookFinder, SIGNAL(add(const QString &)),
210             this, SLOT(onAddFromFolder(const QString &)));
211     connect(bookFinder, SIGNAL(add(const QString &)),
212             library, SLOT(add(const QString &)));
213     connect(bookFinder, SIGNAL(done(int)),
214             this, SLOT(onAddFromFolderDone(int)));
215     bookFinder->find(path, Library::instance()->bookPaths());
216 }
217
218 void LibraryDialog::onAddFromFolderDone(int added)
219 {
220     QString msg;
221
222     switch (added) {
223     case 0: msg = tr("No new books found"); break;
224     case 1: msg = tr("One new book added"); break;
225     default: msg = tr("%1 new books added").arg(added);
226     }
227
228     progress->reset();
229     qDebug() << "LibraryDialog::onRefreshDone:" << msg;
230 #ifdef Q_WS_MAEMO_5
231     QMaemo5InformationBox::information(this, msg);
232 #else
233     // FIXME
234 #endif
235 }
236
237 void LibraryDialog::onAddFromFolder(const QString &path)
238 {
239     progress->setLabelText(QFileInfo(path).fileName());
240     progress->setValue(progress->value() + 1);
241 }