8 Library *Library::mInstance = 0;
10 Library::Library(QObject *parent): QAbstractListModel(parent), mCurrent(0)
20 Library *Library::instance()
23 mInstance = new Library();
28 int Library::rowCount(const QModelIndex &parent) const
34 QVariant Library::data(const QModelIndex &index, int role) const
38 return mBooks[index.row()]->title;
40 return QVariant::fromValue<Book>(*mBooks[index.row()]);
56 int size = settings.value("lib/size").toInt();
57 for (int i = 0; i < size; i++) {
58 QString key = "lib/book" + QString::number(i);
59 QString path = settings.value(key).toString();
60 Book *book = new Book(path);
62 qDebug() << "Library::load: Add" << book->title << "from" << path;
65 QString currentPath = settings.value("lib/current").toString();
66 QModelIndex index = find(currentPath);
67 if (index.isValid()) {
68 mCurrent = mBooks[index.row()];
69 qDebug() << "Library::load: Current book is" << mCurrent->path();
75 qDebug() << "Library::save";
78 settings.setValue("lib/size", mBooks.size());
79 for (int i = 0; i < mBooks.size(); i++) {
80 QString key = "lib/book" + QString::number(i);
81 settings.setValue(key, mBooks[i]->path());
83 settings.setValue("lib/current", mCurrent? mCurrent->path(): QString());
86 bool Library::add(QString path)
88 qDebug() << "Library::add" << path;
90 qWarning() << "Library::add: Empty path";
93 if (find(path).isValid()) {
94 qDebug() << " Book already exists in library";
97 int size = mBooks.size();
98 Book *book = new Book(path);
99 beginInsertRows(QModelIndex(), size - 1, size);
106 void Library::remove(const QModelIndex &index)
108 int row = index.row();
109 if ((row < 0) || (row >= mBooks.size())) {
112 beginRemoveRows(QModelIndex(), row, row + 1);
113 Book *book = mBooks[row];
114 mBooks.removeAt(row);
117 if (book == mCurrent) {
119 emit currentBookChanged();
124 Book *Library::current() const
129 void Library::setCurrent(const QModelIndex index)
131 int row = index.row();
132 qDebug() << "Library::setCurrent" << row;
133 if ((row >= 0) && (row < mBooks.size())) {
134 mCurrent = mBooks[row];
136 emit currentBookChanged();
140 void Library::clear()
142 for (int i = 0; i < mBooks.size(); i++) {
149 QModelIndex Library::find(QString path) const
152 QString absolutePath = QFileInfo(path).absoluteFilePath();
153 for (int i = 0; i < mBooks.size(); i++) {
154 if (absolutePath == mBooks[i]->path()) {
159 return QModelIndex();
162 QModelIndex Library::find(const Book *book) const
164 for (int i = 0; i < mBooks.size(); i++) {
165 if (book == mBooks[i]) {
169 return QModelIndex();