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 list->setUniformItemSizes(true);
32 Library *library = Library::instance();
33 QModelIndex current = library->nowReading();
40 addAction(tr("Details"), this, SLOT(onDetails()));
41 addAction(tr("Read"), this, SLOT(onRead()));
42 addAction(tr("Delete"), this, SLOT(onRemove()));
43 #endif // ! Q_WS_MAEMO_5
45 addAction(tr("Add book"), this, SLOT(onAdd()));
46 addAction(tr("Manage folders"), this, SLOT(onShowFolders()));
48 connect(Library::instance(), SIGNAL(nowReadingChanged()),
49 this, SLOT(onCurrentBookChanged()));
50 connect(Library::instance(),
51 SIGNAL(rowsInserted(const QModelIndex &, int, int)),
54 connect(list, SIGNAL(activated(const QModelIndex &)),
55 this, SLOT(onItemActivated(const QModelIndex &)));
57 connect(list->selectionModel(),
58 SIGNAL(selectionChanged(const QItemSelection &,
59 const QItemSelection &)),
60 this, SLOT(onItemSelectionChanged()));
61 onItemSelectionChanged();
62 #endif // !Q_WS_MAEMO_5
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];
165 void LibraryDialog::onItemSelectionChanged()
167 #if 0 // FIXME: API missing from ListWindow
168 bool enable = selected().isValid();
169 readButton->setEnabled(enable);
170 detailsButton->setEnabled(enable);
171 removeButton->setEnabled(enable);
175 #endif // Q_WS_MAEMO_5
177 void LibraryDialog::onCurrentBookChanged()
182 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
184 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
185 list->selectionModel()->clearSelection();
186 if (sortedIndex.isValid()) {
187 list->selectionModel()->select(sortedIndex,
188 QItemSelectionModel::Select);
189 list->setCurrentIndex(sortedIndex);
193 QModelIndex LibraryDialog::selected() const
195 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
196 if (selectedItems.size()) {
197 return sortedLibrary->mapToSource(selectedItems[0]);
199 return QModelIndex();
202 void LibraryDialog::onShowFolders()
204 FoldersDialog *folders = new FoldersDialog(this);