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
37 Book *book = mBooks[index.row()];
38 return QVariant::fromValue<Book>(*book);
51 int size = settings.value("lib/size").toInt();
52 for (int i = 0; i < size; i++) {
53 QString key = "lib/book" + QString::number(i);
54 QString path = settings.value(key).toString();
55 Book *book = new Book(path);
57 qDebug() << "Library::load: Add" << book->title << "from" << path;
60 QString currentPath = settings.value("lib/current").toString();
61 int index = find(currentPath);
63 mCurrent = mBooks[index];
64 qDebug() << "Library::load: Current book is" << mCurrent->path();
70 qDebug() << "Library::save";
73 settings.setValue("lib/size", mBooks.size());
74 for (int i = 0; i < mBooks.size(); i++) {
75 QString key = "lib/book" + QString::number(i);
76 settings.setValue(key, mBooks[i]->path());
78 settings.setValue("lib/current", mCurrent? mCurrent->path(): QString());
81 bool Library::add(QString path)
86 if (find(path) != -1) {
89 Book *book = new Book(path);
96 void Library::remove(int index)
98 if ((index < 0) || (index >= mBooks.size())) {
101 Book *book = mBooks[index];
102 mBooks.removeAt(index);
103 if (book == mCurrent) {
105 emit currentBookChanged();
107 emit bookRemoved(index);
112 int Library::size() const
114 return mBooks.size();
117 Book *Library::at(int index) const
119 return mBooks[index];
122 Book *Library::current() const
127 void Library::setCurrent(int index)
129 qDebug() << "Library::setCurrent" << index;
130 if ((index >= 0) && (index < mBooks.size()))
132 mCurrent = mBooks[index];
134 qDebug() << " Emit currentBookChanged()";
135 emit currentBookChanged();
139 void Library::clear()
141 for (int i = 0; i < mBooks.size(); i++) {
148 int Library::find(QString path) const
151 QString absolutePath = QFileInfo(path).absoluteFilePath();
152 for (int i = 0; i < mBooks.size(); i++) {
153 if (absolutePath == mBooks[i]->path()) {
161 int Library::find(const Book *book) const
163 for (int i = 0; i < mBooks.size(); i++) {
164 if (book == mBooks[i]) {