8 #include <QtMaemo5/QMaemo5InformationBox>
11 #include "librarydialog.h"
13 #include "sortedlibrary.h"
15 #include "infodialog.h"
17 #include "listwindow.h"
18 #include "foldersdialog.h"
20 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
22 setWindowTitle(tr("Library"));
24 // Create and add list view
26 list = new QListView(this);
27 sortedLibrary = new SortedLibrary(this);
28 list->setModel(sortedLibrary);
29 list->setSelectionMode(QAbstractItemView::SingleSelection);
31 Library *library = Library::instance();
32 QModelIndex current = library->nowReading();
39 addItemAction(tr("Details"), this, SLOT(onDetails()));
40 addItemAction(tr("Read"), this, SLOT(onRead()));
41 addItemAction(tr("Delete"), this, SLOT(onRemove()));
42 #endif // ! Q_WS_MAEMO_5
44 addAction(tr("Add book"), this, SLOT(onAdd()));
45 addAction(tr("Manage folders"), this, SLOT(onShowFolders()));
47 connect(Library::instance(), SIGNAL(nowReadingChanged()),
48 this, SLOT(onCurrentBookChanged()));
49 connect(Library::instance(),
50 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
53 connect(list, SIGNAL(activated(const QModelIndex &)),
54 this, SLOT(onItemActivated(const QModelIndex &)));
57 void LibraryDialog::onAdd()
59 Library *library = Library::instance();
61 // Figure out directory to show
62 QString lastDir = Settings::instance()->value("lastdir").toString();
64 lastDir = QDir::homePath();
68 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
69 lastDir, "Books (*.epub)");
74 // Save directory selected
75 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
77 // Add book to library
78 QModelIndex index = library->find(path);
79 if (index.isValid()) {
81 QMaemo5InformationBox::information(this,
82 tr("This book is already in the library"),
83 QMaemo5InformationBox::DefaultTimeout);
85 (void)QMessageBox::information(this, tr("Dorian"),
86 tr("This book is already in the library"), QMessageBox::Ok);
87 #endif // Q_WS_MAEMO_5
95 void LibraryDialog::onBookAdded()
97 Library *library = Library::instance();
98 setSelected(library->index(library->rowCount() - 1));
103 void LibraryDialog::onRemove()
105 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
106 if (current.isValid()) {
107 Book *currentBook = Library::instance()->book(current);
108 QString title = currentBook->name();
109 if (QMessageBox::Yes ==
110 QMessageBox::question(this, tr("Delete book"),
111 tr("Delete book \"%1\" from library?").
112 arg(currentBook->shortName()),
113 QMessageBox::Yes | QMessageBox::No)) {
114 Library::instance()->remove(current);
119 void LibraryDialog::onRead()
121 qDebug() << "LibraryDialog::onRead";
122 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
123 if (current.isValid()) {
124 Library::instance()->setNowReading(current);
128 void LibraryDialog::onDetails()
130 onItemActivated(list->currentIndex());
133 #endif // Q_WS_MAEMO_5
135 void LibraryDialog::onItemActivated(const QModelIndex &index)
137 qDebug() << "LibraryDialog::onItemActivated";
138 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
139 Book *book = Library::instance()->book(libraryIndex);
140 (new InfoDialog(book, this))->exec();
143 QString LibraryDialog::createItemText(const Book *book)
145 QString text = book->title + "\n";
146 if (book->creators.size()) {
147 text += book->creators[0];
148 for (int i = 1; i < book->creators.size(); i++) {
149 text += ", " + book->creators[i];
155 void LibraryDialog::onCurrentBookChanged()
160 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
162 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
163 list->selectionModel()->clearSelection();
164 if (sortedIndex.isValid()) {
165 list->selectionModel()->select(sortedIndex,
166 QItemSelectionModel::Select);
167 list->setCurrentIndex(sortedIndex);
171 QModelIndex LibraryDialog::selected() const
173 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
174 if (selectedItems.size()) {
175 return sortedLibrary->mapToSource(selectedItems[0]);
177 return QModelIndex();
180 void LibraryDialog::onShowFolders()
182 FoldersDialog *folders = new FoldersDialog(this);