mBooks.append(book);
}
QString currentPath = settings.value("lib/current").toString();
- int index = find(currentPath);
- if (-1 != index) {
- mCurrent = mBooks[index];
+ QModelIndex index = find(currentPath);
+ if (index.isValid()) {
+ mCurrent = mBooks[index.row()];
qDebug() << "Library::load: Current book is" << mCurrent->path();
}
}
bool Library::add(QString path)
{
+ qDebug() << "Library::add" << path;
if (path == "") {
+ qWarning() << "Library::add: Empty path";
return false;
}
- if (find(path) != -1) {
+ if (find(path).isValid()) {
+ qDebug() << " Book already exists in library";
return false;
}
int size = mBooks.size();
return true;
}
-void Library::remove(int index)
+void Library::remove(const QModelIndex &index)
{
- if ((index < 0) || (index >= mBooks.size())) {
+ int row = index.row();
+ if ((row < 0) || (row >= mBooks.size())) {
return;
}
- beginRemoveRows(QModelIndex(), index, index + 1);
- Book *book = mBooks[index];
- mBooks.removeAt(index);
+ beginRemoveRows(QModelIndex(), row, row + 1);
+ Book *book = mBooks[row];
+ mBooks.removeAt(row);
save();
endRemoveRows();
if (book == mCurrent) {
delete book;
}
-int Library::size() const
-{
- return mBooks.size();
-}
-
-Book *Library::at(int index) const
-{
- return mBooks[index];
-}
-
Book *Library::current() const
{
return mCurrent;
}
-void Library::setCurrent(int index)
+void Library::setCurrent(const QModelIndex index)
{
- qDebug() << "Library::setCurrent" << index;
- if ((index >= 0) && (index < mBooks.size()))
- {
- mCurrent = mBooks[index];
+ int row = index.row();
+ qDebug() << "Library::setCurrent" << row;
+ if ((row >= 0) && (row < mBooks.size())) {
+ mCurrent = mBooks[row];
save();
- qDebug() << " Emit currentBookChanged()";
emit currentBookChanged();
}
}
mCurrent = 0;
}
-int Library::find(QString path) const
+QModelIndex Library::find(QString path) const
{
if (path != "") {
QString absolutePath = QFileInfo(path).absoluteFilePath();
for (int i = 0; i < mBooks.size(); i++) {
if (absolutePath == mBooks[i]->path()) {
- return i;
+ return index(i);
}
}
}
- return -1;
+ return QModelIndex();
}
-int Library::find(const Book *book) const
+QModelIndex Library::find(const Book *book) const
{
for (int i = 0; i < mBooks.size(); i++) {
if (book == mBooks[i]) {
- return i;
+ return index(i);
}
}
- return -1;
+ return QModelIndex();
}
#include <QList>
class QObject;
+class QModelIndex;
class Book;
/** Library of books. */
static Library *instance();
static void close();
- int rowCount(const QModelIndex & parent = QModelIndex()) const;
+ int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
void save();
- int find(QString path) const;
- int find(const Book *book) const;
- Book *at(int index) const;
- int size() const;
+ QModelIndex find(QString path) const;
+ QModelIndex find(const Book *book) const;
Book *current() const;
bool add(QString path);
- void remove(int index);
- void setCurrent(int index);
+ void remove(const QModelIndex &index);
+ void setCurrent(const QModelIndex index);
signals:
void currentBookChanged();
sortedLibrary = new SortedLibrary(this);
list->setModel(sortedLibrary);
list->setSelectionMode(QAbstractItemView::SingleSelection);
+ list->setUniformItemSizes(true);
#ifndef Q_WS_MAEMO_5
setSizeGripEnabled(true);
#endif
horizontalLayout->addWidget(buttonBox);
-#if 0 // FIXME
- connect(library, SIGNAL(currentBookChanged()),
+ connect(Library::instance(), SIGNAL(currentBookChanged()),
this, SLOT(onCurrentBookChanged()));
-#endif
-
#ifndef Q_WS_MAEMO_5
connect(list, SIGNAL(itemSelectionChanged()),
this, SLOT(onItemSelectionChanged()));
{
Library *library = Library::instance();
+ // Figure out directory to show
if (lastDir == "") {
- if (library->size()) {
- QFileInfo info(library->at(library->size() - 1)->path());
+ if (library->rowCount()) {
+ QModelIndex lastIndex = library->index(library->rowCount() - 1);
+ Book lastBook = library->data(lastIndex,
+ Library::BookRole).value<Book>();
+ QFileInfo info(lastBook.path());
lastDir = info.absolutePath();
}
}
if (lastDir == "") {
lastDir = QDir::homePath();
}
+
+ // Get book file name
QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
lastDir, "Books (*.epub)");
qDebug() << "LibraryDialog::onAdd" << path;
return;
}
+ // Add book to library
lastDir = QFileInfo(path).absolutePath();
- if (library->find(path) != -1) {
+ if (library->find(path).isValid()) {
#ifdef Q_WS_MAEMO_5
QMaemo5InformationBox::information(this,
tr("This book is already in the library"),
void LibraryDialog::onBookAdded()
{
-#if 0
+#if 0 // FIXME
Library *library = Library::instance();
int index = library->size() - 1;
Book *book = library->at(index);
void LibraryDialog::onRemove()
{
-#if 0
qDebug() << "LibraryDialog::onRemove";
- if (list->selectedItems().isEmpty()) {
- return;
- }
- QListWidgetItem *item = list->selectedItems()[0];
- int row = list->row(item);
- QString title = Library::instance()->at(row)->title;
- if (QMessageBox::Yes ==
- QMessageBox::question(this, "Delete book",
- "Delete book \"" + title + "\"?", QMessageBox::Yes
+ QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
+ if (current.isValid()) {
+ Book currentBook =
+ Library::instance()->data(current, Library::BookRole).value<Book>();
+ QString title = currentBook.title;
+ if (QMessageBox::Yes ==
+ QMessageBox::question(this, "Delete book",
+ "Delete book \"" + title + "\"?",
+ QMessageBox::Yes
#ifndef Q_WS_MAEMO_5
- , QMessageBox::No
+ , QMessageBox::No
#endif
- )) {
- list->takeItem(row);
- Library::instance()->remove(row);
+ )) {
+ Library::instance()->remove(current);
+ }
}
-#endif
}
void LibraryDialog::onRead()
{
qDebug() << "LibraryDialog::onRead";
-#if 0 // FIXME
- if (list->selectedItems().isEmpty()) {
- return;
+ QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
+ if (current.isValid()) {
+ Library::instance()->setCurrent(current);
}
- QListWidgetItem *item = list->selectedItems()[0];
- int row = list->row(item);
- Library::instance()->setCurrent(row);
-#endif
}
void LibraryDialog::onDetails()