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
30 if (parent.isValid()) {
37 QVariant Library::data(const QModelIndex &index, int role) const
39 qDebug() << "Library::data, row" << index.row() << "role" << role;
41 if (!index.isValid()) {
47 return mBooks[index.row()]->name();
49 return QVariant::fromValue<Book>(*mBooks[index.row()]);
65 int size = settings.value("lib/size").toInt();
66 for (int i = 0; i < size; i++) {
67 QString key = "lib/book" + QString::number(i);
68 QString path = settings.value(key).toString();
69 Book *book = new Book(path);
71 qDebug() << "Library::load: Add" << book->title << "from" << path;
74 QString currentPath = settings.value("lib/current").toString();
75 QModelIndex index = find(currentPath);
76 if (index.isValid()) {
77 mCurrent = mBooks[index.row()];
78 qDebug() << "Library::load: Current book is" << mCurrent->path();
84 qDebug() << "Library::save";
87 settings.setValue("lib/size", mBooks.size());
88 for (int i = 0; i < mBooks.size(); i++) {
89 QString key = "lib/book" + QString::number(i);
90 settings.setValue(key, mBooks[i]->path());
92 settings.setValue("lib/current", mCurrent? mCurrent->path(): QString());
95 bool Library::add(QString path)
97 qDebug() << "Library::add" << path;
99 qWarning() << "Library::add: Empty path";
102 if (find(path).isValid()) {
103 qDebug() << " Book already exists in library";
106 int size = mBooks.size();
107 Book *book = new Book(path);
108 beginInsertRows(QModelIndex(), size, size);
115 void Library::remove(const QModelIndex &index)
117 if (!index.isValid()) {
120 int row = index.row();
121 if ((row < 0) || (row >= mBooks.size())) {
124 beginRemoveRows(QModelIndex(), row, row);
125 Book *book = mBooks[row];
126 mBooks.removeAt(row);
129 if (book == mCurrent) {
131 emit currentBookChanged();
136 Book *Library::current() const
141 void Library::setCurrent(const QModelIndex index)
143 int row = index.row();
144 qDebug() << "Library::setCurrent" << row;
145 if ((row >= 0) && (row < mBooks.size())) {
146 mCurrent = mBooks[row];
148 emit currentBookChanged();
152 void Library::clear()
154 for (int i = 0; i < mBooks.size(); i++) {
161 QModelIndex Library::find(QString path) const
164 QString absolutePath = QFileInfo(path).absoluteFilePath();
165 for (int i = 0; i < mBooks.size(); i++) {
166 if (absolutePath == mBooks[i]->path()) {
171 return QModelIndex();
174 QModelIndex Library::find(const Book *book) const
176 for (int i = 0; i < mBooks.size(); i++) {
177 if (book == mBooks[i]) {
181 return QModelIndex();