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 int index = find(currentPath);
68 mCurrent = mBooks[index];
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)
91 if (find(path) != -1) {
94 int size = mBooks.size();
95 Book *book = new Book(path);
96 beginInsertRows(QModelIndex(), size - 1, size);
103 void Library::remove(int index)
105 if ((index < 0) || (index >= mBooks.size())) {
108 beginRemoveRows(QModelIndex(), index, index + 1);
109 Book *book = mBooks[index];
110 mBooks.removeAt(index);
113 if (book == mCurrent) {
115 emit currentBookChanged();
120 int Library::size() const
122 return mBooks.size();
125 Book *Library::at(int index) const
127 return mBooks[index];
130 Book *Library::current() const
135 void Library::setCurrent(int index)
137 qDebug() << "Library::setCurrent" << index;
138 if ((index >= 0) && (index < mBooks.size()))
140 mCurrent = mBooks[index];
142 qDebug() << " Emit currentBookChanged()";
143 emit currentBookChanged();
147 void Library::clear()
149 for (int i = 0; i < mBooks.size(); i++) {
156 int Library::find(QString path) const
159 QString absolutePath = QFileInfo(path).absoluteFilePath();
160 for (int i = 0; i < mBooks.size(); i++) {
161 if (absolutePath == mBooks[i]->path()) {
169 int Library::find(const Book *book) const
171 for (int i = 0; i < mBooks.size(); i++) {
172 if (book == mBooks[i]) {