8 #include <QtMaemo5/QMaemo5InformationBox>
11 #include "librarydialog.h"
13 #include "sortedlibrary.h"
15 #include "infodialog.h"
17 LibraryDialog::LibraryDialog(QWidget *parent):
18 QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
19 Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
21 setWindowTitle(tr("Library"));
22 list = new QListView(this);
23 sortedLibrary = new SortedLibrary(this);
24 list->setModel(sortedLibrary);
25 list->setSelectionMode(QAbstractItemView::SingleSelection);
26 list->setUniformItemSizes(true);
28 setSizeGripEnabled(true);
32 Book *current = library->current();
33 if (library->size() && current) {
34 list->setItemSelected(list->item(library->find(current)), true);
38 QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
39 horizontalLayout->addWidget(list);
41 QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
43 detailsButton = new QPushButton(tr("Details"), this);
44 readButton = new QPushButton(tr("Read"), this);
45 removeButton = new QPushButton(tr("Delete"), this);
47 addButton = new QPushButton(tr("Add"), this);
50 buttonBox->addButton(detailsButton, QDialogButtonBox::ActionRole);
51 buttonBox->addButton(readButton, QDialogButtonBox::AcceptRole);
52 buttonBox->addButton(removeButton, QDialogButtonBox::ActionRole);
53 #endif // Q_WS_MAEMO_5
54 buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
56 horizontalLayout->addWidget(buttonBox);
58 connect(Library::instance(), SIGNAL(currentBookChanged()),
59 this, SLOT(onCurrentBookChanged()));
61 connect(list, SIGNAL(itemSelectionChanged()),
62 this, SLOT(onItemSelectionChanged()));
63 connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
64 connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
65 connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
67 connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
69 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
70 this, SLOT(onItemActivated(QListWidgetItem *)));
74 onItemSelectionChanged();
78 void LibraryDialog::onAdd()
80 Library *library = Library::instance();
82 // Figure out directory to show
84 if (library->rowCount()) {
85 QModelIndex lastIndex = library->index(library->rowCount() - 1);
86 Book lastBook = library->data(lastIndex,
87 Library::BookRole).value<Book>();
88 QFileInfo info(lastBook.path());
89 lastDir = info.absolutePath();
93 lastDir = QDir::homePath();
97 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
98 lastDir, "Books (*.epub)");
99 qDebug() << "LibraryDialog::onAdd" << path;
104 // Add book to library
105 lastDir = QFileInfo(path).absolutePath();
106 if (library->find(path).isValid()) {
108 QMaemo5InformationBox::information(this,
109 tr("This book is already in the library"),
110 QMaemo5InformationBox::DefaultTimeout);
112 // FIXME: Select existing book
119 void LibraryDialog::onBookAdded()
122 Library *library = Library::instance();
123 int index = library->size() - 1;
124 Book *book = library->at(index);
125 QListWidgetItem *item = new QListWidgetItem(book->cover,
126 createItemText(book));
128 list->setCurrentItem(item);
134 void LibraryDialog::onRemove()
136 qDebug() << "LibraryDialog::onRemove";
137 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
138 if (current.isValid()) {
140 Library::instance()->data(current, Library::BookRole).value<Book>();
141 QString title = currentBook.name();
142 if (QMessageBox::Yes ==
143 QMessageBox::question(this, "Delete book",
144 "Delete book \"" + title + "\"?",
150 Library::instance()->remove(current);
155 void LibraryDialog::onRead()
157 qDebug() << "LibraryDialog::onRead";
158 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
159 if (current.isValid()) {
160 Library::instance()->setCurrent(current);
164 void LibraryDialog::onDetails()
167 onItemActivated(list->selectedItems()[0]);
171 #endif // Q_WS_MAEMO_5
173 void LibraryDialog::onItemActivated(const QModelIndex &index)
175 qDebug() << "LibraryDialog::onItemActivated";
177 int row = list->row(item);
178 Book *book = Library::instance()->at(row);
179 InfoDialog *info = new InfoDialog(book, this);
184 QString LibraryDialog::createItemText(const Book *book)
186 QString text = book->title + "\n";
187 if (book->creators.size()) {
188 text += book->creators[0];
189 for (int i = 1; i < book->creators.size(); i++) {
190 text += ", " + book->creators[i];
198 void LibraryDialog::onItemSelectionChanged()
201 bool enable = list->selectedItems().size();
202 qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
203 readButton->setEnabled(enable);
204 qDebug() << " readButton";
205 detailsButton->setEnabled(enable);
206 qDebug() << " detailsButton";
207 removeButton->setEnabled(enable);
208 qDebug() << " removeButton";
212 #endif // Q_WS_MAEMO_5
214 void LibraryDialog::onCurrentBookChanged()