8 #include <QtMaemo5/QMaemo5InformationBox>
11 #include "librarydialog.h"
13 #include "sortedlibrary.h"
15 #include "infodialog.h"
17 #include "listwindow.h"
20 #include "bookfinder.h"
22 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
24 setWindowTitle(tr("Library"));
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
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");
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);
44 Library *library = Library::instance();
45 QModelIndex current = library->nowReading();
49 progress = new QProgressDialog(tr("Adding books"), "", 0, 0, this);
51 progress->setMinimumDuration(0);
52 progress->setWindowModality(Qt::WindowModal);
53 progress->setCancelButton(0);
55 connect(Library::instance(), SIGNAL(nowReadingChanged()),
56 this, SLOT(onCurrentBookChanged()));
57 connect(Library::instance(),
58 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
61 connect(list, SIGNAL(activated(const QModelIndex &)),
62 this, SLOT(onItemActivated(const QModelIndex &)));
65 void LibraryDialog::onAdd()
67 Library *library = Library::instance();
69 // Figure out directory to show
70 QString lastDir = Settings::instance()->value("lastdir").toString();
72 lastDir = QDir::homePath();
76 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
77 lastDir, "Books (*.epub)");
82 // Save directory selected
83 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
85 // Add book to library
86 QModelIndex index = library->find(path);
87 if (index.isValid()) {
89 QMaemo5InformationBox::information(this,
90 tr("This book is already in the library"),
91 QMaemo5InformationBox::DefaultTimeout);
93 (void)QMessageBox::information(this, tr("Dorian"),
94 tr("This book is already in the library"), QMessageBox::Ok);
95 #endif // Q_WS_MAEMO_5
103 void LibraryDialog::onBookAdded()
105 Library *library = Library::instance();
106 setSelected(library->index(library->rowCount() - 1));
111 void LibraryDialog::onRemove()
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);
127 void LibraryDialog::onRead()
129 qDebug() << "LibraryDialog::onRead";
130 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
131 if (current.isValid()) {
132 Library::instance()->setNowReading(current);
136 void LibraryDialog::onDetails()
138 onItemActivated(list->currentIndex());
141 #endif // Q_WS_MAEMO_5
143 void LibraryDialog::onItemActivated(const QModelIndex &index)
145 qDebug() << "LibraryDialog::onItemActivated";
146 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
147 Book *book = Library::instance()->book(libraryIndex);
148 (new InfoDialog(book, this))->exec();
151 QString LibraryDialog::createItemText(const Book *book)
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];
163 void LibraryDialog::onCurrentBookChanged()
168 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
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);
179 QModelIndex LibraryDialog::selected() const
181 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
182 if (selectedItems.size()) {
183 return sortedLibrary->mapToSource(selectedItems[0]);
185 return QModelIndex();
188 void LibraryDialog::onAddFolder()
190 Trace t("LibraryDialog::onAddFolder");
193 Settings *settings = Settings::instance();
195 settings->value("lastfolderadded", QDir::homePath()).toString();
197 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
201 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
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());
218 void LibraryDialog::onAddFromFolderDone(int added)
223 case 0: msg = tr("No new books found"); break;
224 case 1: msg = tr("One book added"); break;
225 default: msg = tr("%1 books added").arg(added);
229 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
231 QMaemo5InformationBox::information(this, msg);
237 void LibraryDialog::onAddFromFolder(const QString &path)
239 progress->setLabelText(QFileInfo(path).fileName());
240 progress->setValue(progress->value() + 1);