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");
37 addAction(tr("Search the Web"), this, SLOT(onSearch()), ":/icons/search.png");
39 // Create and add list view
40 list = new ListView(this);
41 sortedLibrary = new SortedLibrary(this);
42 list->setModel(sortedLibrary);
43 list->setSelectionMode(QAbstractItemView::SingleSelection);
45 Library *library = Library::instance();
46 QModelIndex current = library->nowReading();
50 progress = new QProgressDialog(tr("Adding books"), "", 0, 0, this);
52 progress->setMinimumDuration(0);
53 progress->setWindowModality(Qt::WindowModal);
54 progress->setCancelButton(0);
56 connect(Library::instance(), SIGNAL(nowReadingChanged()),
57 this, SLOT(onCurrentBookChanged()));
58 connect(Library::instance(),
59 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
62 connect(list, SIGNAL(activated(const QModelIndex &)),
63 this, SLOT(onItemActivated(const QModelIndex &)));
66 void LibraryDialog::onAdd()
68 Library *library = Library::instance();
70 // Figure out directory to show
71 QString lastDir = Settings::instance()->value("lastdir").toString();
73 lastDir = QDir::homePath();
77 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
78 lastDir, "Books (*.epub)");
83 // Save directory selected
84 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
86 // Add book to library
87 QModelIndex index = library->find(path);
88 if (index.isValid()) {
90 QMaemo5InformationBox::information(this,
91 tr("This book is already in the library"),
92 QMaemo5InformationBox::DefaultTimeout);
94 (void)QMessageBox::information(this, tr("Dorian"),
95 tr("This book is already in the library"), QMessageBox::Ok);
96 #endif // Q_WS_MAEMO_5
104 void LibraryDialog::onBookAdded()
106 Library *library = Library::instance();
107 setSelected(library->index(library->rowCount() - 1));
112 void LibraryDialog::onRemove()
114 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
115 if (current.isValid()) {
116 Book *currentBook = Library::instance()->book(current);
117 QString title = currentBook->name();
118 if (QMessageBox::Yes ==
119 QMessageBox::question(this, tr("Delete book"),
120 tr("Delete book \"%1\" from library?").
121 arg(currentBook->shortName()),
122 QMessageBox::Yes | QMessageBox::No)) {
123 Library::instance()->remove(current);
128 void LibraryDialog::onRead()
130 qDebug() << "LibraryDialog::onRead";
131 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
132 if (current.isValid()) {
133 Library::instance()->setNowReading(current);
137 void LibraryDialog::onDetails()
139 onItemActivated(list->currentIndex());
142 #endif // Q_WS_MAEMO_5
144 void LibraryDialog::onItemActivated(const QModelIndex &index)
146 qDebug() << "LibraryDialog::onItemActivated";
147 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
148 Book *book = Library::instance()->book(libraryIndex);
149 (new InfoDialog(book, this))->exec();
152 QString LibraryDialog::createItemText(const Book *book)
154 QString text = book->title + "\n";
155 if (book->creators.size()) {
156 text += book->creators[0];
157 for (int i = 1; i < book->creators.size(); i++) {
158 text += ", " + book->creators[i];
164 void LibraryDialog::onCurrentBookChanged()
169 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
171 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
172 list->selectionModel()->clearSelection();
173 if (sortedIndex.isValid()) {
174 list->selectionModel()->select(sortedIndex,
175 QItemSelectionModel::Select);
176 list->setCurrentIndex(sortedIndex);
180 QModelIndex LibraryDialog::selected() const
182 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
183 if (selectedItems.size()) {
184 return sortedLibrary->mapToSource(selectedItems[0]);
186 return QModelIndex();
189 void LibraryDialog::onAddFolder()
191 Trace t("LibraryDialog::onAddFolder");
194 Settings *settings = Settings::instance();
196 settings->value("lastfolderadded", QDir::homePath()).toString();
198 QFileDialog::getExistingDirectory(this, tr("Select folder"), last);
202 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
205 // Add books from folder
206 progress->setWindowTitle(tr("Adding books"));
207 BookFinder *bookFinder = new BookFinder(this);
208 Library *library = Library::instance();
209 connect(bookFinder, SIGNAL(begin(int)), progress, SLOT(setMaximum(int)));
210 connect(bookFinder, SIGNAL(add(const QString &)),
211 this, SLOT(onAddFromFolder(const QString &)));
212 connect(bookFinder, SIGNAL(add(const QString &)),
213 library, SLOT(add(const QString &)));
214 connect(bookFinder, SIGNAL(done(int)),
215 this, SLOT(onAddFromFolderDone(int)));
216 bookFinder->find(path, Library::instance()->bookPaths());
219 void LibraryDialog::onAddFromFolderDone(int added)
224 case 0: msg = tr("No new books found"); break;
225 case 1: msg = tr("One book added"); break;
226 default: msg = tr("%1 books added").arg(added);
230 qDebug() << "LibraryDialog::onRefreshDone:" << msg;
232 QMaemo5InformationBox::information(this, msg);
238 void LibraryDialog::onAddFromFolder(const QString &path)
240 progress->setLabelText(QFileInfo(path).fileName());
241 progress->setValue(progress->value() + 1);