#include <QSettings>
-#include <QString>
#include <QDebug>
#include <QFileInfo>
#include "library.h"
+#include "book.h"
+#include "trace.h"
Library *Library::mInstance = 0;
-Library::Library(): mCurrent(0)
+Library::Library(QObject *parent): QAbstractListModel(parent)
{
load();
}
return mInstance;
}
+int Library::rowCount(const QModelIndex &parent) const
+{
+ if (parent.isValid()) {
+ return 0;
+ } else {
+ return mBooks.size();
+ }
+}
+
+QVariant Library::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid()) {
+ return QVariant();
+ }
+
+ switch (role) {
+ case Qt::DisplayRole:
+ return mBooks[index.row()]->name();
+ case Qt::DecorationRole:
+ return QPixmap::fromImage(mBooks[index.row()]->cover);
+ default:
+ return QVariant();
+ }
+}
+
+Book *Library::book(const QModelIndex &index)
+{
+ if (index.isValid()) {
+ if ((index.row() >= 0) && (index.row() < mBooks.size())) {
+ return mBooks[index.row()];
+ } else {
+ qCritical() << "*** Library::book: Bad index" << index.row();
+ }
+ }
+ return 0;
+}
+
void Library::close()
{
delete mInstance;
QString key = "lib/book" + QString::number(i);
QString path = settings.value(key).toString();
Book *book = new Book(path);
+ connect(book, SIGNAL(opened(const QString &)),
+ this, SLOT(onBookOpened(const QString &)));
book->load();
- qDebug() << "Library::load: Add" << book->title << "from" << path;
mBooks.append(book);
}
- QString currentPath = settings.value("lib/current").toString();
- int index = find(currentPath);
- if (-1 != index) {
- mCurrent = mBooks[index];
- qDebug() << "Library::load: Current book is" << mCurrent->path();
- }
+ QString currentPath = settings.value("lib/nowreading").toString();
+ mNowReading = find(currentPath);
}
void Library::save()
{
- qDebug() << "Library::save";
-
QSettings settings;
settings.setValue("lib/size", mBooks.size());
for (int i = 0; i < mBooks.size(); i++) {
QString key = "lib/book" + QString::number(i);
settings.setValue(key, mBooks[i]->path());
}
- settings.setValue("lib/current", mCurrent? mCurrent->path(): QString());
+ Book *currentBook = book(mNowReading);
+ settings.setValue("lib/nowreading",
+ currentBook? currentBook->path(): QString());
}
bool Library::add(QString path)
{
+ Trace t("Library::add " + path);
if (path == "") {
+ qCritical() << "*** Library::add: Empty path";
return false;
}
- if (find(path) != -1) {
+ if (find(path).isValid()) {
+ t.trace("Book already exists in library");
return false;
}
+ int size = mBooks.size();
Book *book = new Book(path);
+ beginInsertRows(QModelIndex(), size, size);
mBooks.append(book);
save();
- emit bookAdded();
+ endInsertRows();
return true;
}
-void Library::remove(int index)
+void Library::remove(const QModelIndex &index)
{
- if ((index < 0) || (index >= mBooks.size())) {
+ Book *toRemove = book(index);
+ if (!toRemove) {
return;
}
- Book *book = mBooks[index];
- mBooks.removeAt(index);
- if (book == mCurrent) {
- mCurrent = 0;
- emit currentBookChanged();
- }
- emit bookRemoved(index);
- delete book;
+ int row = index.row();
+ beginRemoveRows(QModelIndex(), row, row);
+ mBooks.removeAt(row);
save();
+ endRemoveRows();
+ if (index == mNowReading) {
+ mNowReading = QModelIndex();
+ emit nowReadingChanged();
+ }
+ delete toRemove;
}
-int Library::size() const
-{
- return mBooks.size();
-}
-
-Book *Library::at(int index) const
-{
- return mBooks[index];
-}
-
-Book *Library::current() const
+QModelIndex Library::nowReading() const
{
- return mCurrent;
+ return mNowReading;
}
-void Library::setCurrent(int index)
+void Library::setNowReading(const QModelIndex &index)
{
- qDebug() << "Library::setCurrent" << index;
- if ((index >= 0) && (index < mBooks.size()))
- {
- mCurrent = mBooks[index];
- save();
- qDebug() << " Emit currentBookChanged()";
- emit currentBookChanged();
- }
+ mNowReading = index;
+ save();
+ emit nowReadingChanged();
}
void Library::clear()
delete mBooks[i];
}
mBooks.clear();
- mCurrent = 0;
+ mNowReading = QModelIndex();
}
-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;
+ if (book) {
+ for (int i = 0; i < mBooks.size(); i++) {
+ if (book == mBooks[i]) {
+ return index(i);
+ }
}
}
- return -1;
+ return QModelIndex();
+}
+
+void Library::onBookOpened(const QString &path)
+{
+ Trace t("Library::onBookOpened " + path);
+ QModelIndex index = find(path);
+ if (index.isValid()) {
+ emit dataChanged(index, index);
+ }
}