8 #include <QtMaemo5/QMaemo5InformationBox>
11 #include "librarydialog.h"
13 #include "sortedlibrary.h"
15 #include "infodialog.h"
17 #include "listwindow.h"
19 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
21 setWindowTitle(tr("Library"));
23 // Create and add list view
25 list = new QListView(this);
26 sortedLibrary = new SortedLibrary(this);
27 list->setModel(sortedLibrary);
28 list->setSelectionMode(QAbstractItemView::SingleSelection);
30 list->setUniformItemSizes(true);
31 Library *library = Library::instance();
32 QModelIndex current = library->nowReading();
39 addAction(tr("Details"), this, SLOT(onDetails()));
40 addAction(tr("Read"), this, SLOT(onRead()));
41 addAction(tr("Delete"), this, SLOT(onRemove()));
42 #endif // ! Q_WS_MAEMO_5
44 addAction(tr("Add"), this, SLOT(onAdd()));
45 addAction(tr("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 &)));
56 connect(list->selectionModel(),
57 SIGNAL(selectionChanged(const QItemSelection &,
58 const QItemSelection &)),
59 this, SLOT(onItemSelectionChanged()));
60 onItemSelectionChanged();
61 #endif // !Q_WS_MAEMO_5
64 void LibraryDialog::onAdd()
66 Library *library = Library::instance();
68 // Figure out directory to show
69 QString lastDir = Settings::instance()->value("lastdir").toString();
71 lastDir = QDir::homePath();
75 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
76 lastDir, "Books (*.epub)");
81 // Save directory selected
82 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
84 // Add book to library
85 QModelIndex index = library->find(path);
86 if (index.isValid()) {
88 QMaemo5InformationBox::information(this,
89 tr("This book is already in the library"),
90 QMaemo5InformationBox::DefaultTimeout);
92 (void)QMessageBox::information(this, tr("Dorian"),
93 tr("This book is already in the library"), QMessageBox::Ok);
94 #endif // Q_WS_MAEMO_5
102 void LibraryDialog::onBookAdded()
104 Library *library = Library::instance();
105 setSelected(library->index(library->rowCount() - 1));
110 void LibraryDialog::onRemove()
112 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
113 if (current.isValid()) {
114 Book *currentBook = Library::instance()->book(current);
115 QString title = currentBook->name();
116 if (QMessageBox::Yes ==
117 QMessageBox::question(this, tr("Delete book"),
118 tr("Delete book \"%1\" from library?").
119 arg(currentBook->shortName()),
120 QMessageBox::Yes | QMessageBox::No)) {
121 Library::instance()->remove(current);
126 void LibraryDialog::onRead()
128 qDebug() << "LibraryDialog::onRead";
129 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
130 if (current.isValid()) {
131 Library::instance()->setNowReading(current);
135 void LibraryDialog::onDetails()
137 onItemActivated(list->currentIndex());
140 #endif // Q_WS_MAEMO_5
142 void LibraryDialog::onItemActivated(const QModelIndex &index)
144 qDebug() << "LibraryDialog::onItemActivated";
145 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
146 Book *book = Library::instance()->book(libraryIndex);
147 (new InfoDialog(book, this))->exec();
150 QString LibraryDialog::createItemText(const Book *book)
152 QString text = book->title + "\n";
153 if (book->creators.size()) {
154 text += book->creators[0];
155 for (int i = 1; i < book->creators.size(); i++) {
156 text += ", " + book->creators[i];
164 void LibraryDialog::onItemSelectionChanged()
166 #if 0 // FIXME: API missing from ListWindow
167 bool enable = selected().isValid();
168 readButton->setEnabled(enable);
169 detailsButton->setEnabled(enable);
170 removeButton->setEnabled(enable);
174 #endif // Q_WS_MAEMO_5
176 void LibraryDialog::onCurrentBookChanged()
181 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
183 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
184 list->selectionModel()->clearSelection();
185 if (sortedIndex.isValid()) {
186 list->selectionModel()->select(sortedIndex,
187 QItemSelectionModel::Select);
188 list->setCurrentIndex(sortedIndex);
192 QModelIndex LibraryDialog::selected() const
194 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
195 if (selectedItems.size()) {
196 return sortedLibrary->mapToSource(selectedItems[0]);
198 return QModelIndex();
201 void LibraryDialog::onShowFolders()