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 addItemAction(tr("Details"), this, SLOT(onDetails()));
41 addItemAction(tr("Read"), this, SLOT(onRead()));
42 addItemAction(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 &)));
58 void LibraryDialog::onAdd()
60 Library *library = Library::instance();
62 // Figure out directory to show
63 QString lastDir = Settings::instance()->value("lastdir").toString();
65 lastDir = QDir::homePath();
69 QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
70 lastDir, "Books (*.epub)");
75 // Save directory selected
76 Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
78 // Add book to library
79 QModelIndex index = library->find(path);
80 if (index.isValid()) {
82 QMaemo5InformationBox::information(this,
83 tr("This book is already in the library"),
84 QMaemo5InformationBox::DefaultTimeout);
86 (void)QMessageBox::information(this, tr("Dorian"),
87 tr("This book is already in the library"), QMessageBox::Ok);
88 #endif // Q_WS_MAEMO_5
96 void LibraryDialog::onBookAdded()
98 Library *library = Library::instance();
99 setSelected(library->index(library->rowCount() - 1));
104 void LibraryDialog::onRemove()
106 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
107 if (current.isValid()) {
108 Book *currentBook = Library::instance()->book(current);
109 QString title = currentBook->name();
110 if (QMessageBox::Yes ==
111 QMessageBox::question(this, tr("Delete book"),
112 tr("Delete book \"%1\" from library?").
113 arg(currentBook->shortName()),
114 QMessageBox::Yes | QMessageBox::No)) {
115 Library::instance()->remove(current);
120 void LibraryDialog::onRead()
122 qDebug() << "LibraryDialog::onRead";
123 QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
124 if (current.isValid()) {
125 Library::instance()->setNowReading(current);
129 void LibraryDialog::onDetails()
131 onItemActivated(list->currentIndex());
134 #endif // Q_WS_MAEMO_5
136 void LibraryDialog::onItemActivated(const QModelIndex &index)
138 qDebug() << "LibraryDialog::onItemActivated";
139 QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
140 Book *book = Library::instance()->book(libraryIndex);
141 (new InfoDialog(book, this))->exec();
144 QString LibraryDialog::createItemText(const Book *book)
146 QString text = book->title + "\n";
147 if (book->creators.size()) {
148 text += book->creators[0];
149 for (int i = 1; i < book->creators.size(); i++) {
150 text += ", " + book->creators[i];
156 void LibraryDialog::onCurrentBookChanged()
161 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
163 QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
164 list->selectionModel()->clearSelection();
165 if (sortedIndex.isValid()) {
166 list->selectionModel()->select(sortedIndex,
167 QItemSelectionModel::Select);
168 list->setCurrentIndex(sortedIndex);
172 QModelIndex LibraryDialog::selected() const
174 QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
175 if (selectedItems.size()) {
176 return sortedLibrary->mapToSource(selectedItems[0]);
178 return QModelIndex();
181 void LibraryDialog::onShowFolders()
183 FoldersDialog *folders = new FoldersDialog(this);