8 Library *Library::mInstance = 0;
10 Library::Library(): mCurrent(0)
20 Library *Library::instance()
23 mInstance = new Library();
38 int size = settings.value("lib/size").toInt();
39 for (int i = 0; i < size; i++) {
40 QString key = "lib/book" + QString::number(i);
41 QString path = settings.value(key).toString();
42 Book *book = new Book(path);
44 qDebug() << "Library::load: Add" << book->title << "from" << path;
47 QString currentPath = settings.value("lib/current").toString();
48 int index = find(currentPath);
50 mCurrent = mBooks[index];
51 qDebug() << "Library::load: Current book is" << mCurrent->path();
57 qDebug() << "Library::save";
60 settings.setValue("lib/size", mBooks.size());
61 for (int i = 0; i < mBooks.size(); i++) {
62 QString key = "lib/book" + QString::number(i);
63 settings.setValue(key, mBooks[i]->path());
65 settings.setValue("lib/current", mCurrent? mCurrent->path(): QString());
68 bool Library::add(QString path)
73 if (find(path) != -1) {
76 Book *book = new Book(path);
83 void Library::remove(int index)
85 if ((index < 0) || (index >= mBooks.size())) {
88 Book *book = mBooks[index];
89 mBooks.removeAt(index);
90 if (book == mCurrent) {
92 emit currentBookChanged();
94 emit bookRemoved(index);
99 int Library::size() const
101 return mBooks.size();
104 Book *Library::at(int index) const
106 return mBooks[index];
109 Book *Library::current() const
114 void Library::setCurrent(int index)
116 qDebug() << "Library::setCurrent" << index;
117 if ((index >= 0) && (index < mBooks.size()))
119 mCurrent = mBooks[index];
121 qDebug() << " Emit currentBookChanged()";
122 emit currentBookChanged();
126 void Library::clear()
128 for (int i = 0; i < mBooks.size(); i++) {
135 int Library::find(QString path) const
138 QString absolutePath = QFileInfo(path).absoluteFilePath();
139 for (int i = 0; i < mBooks.size(); i++) {
140 if (absolutePath == mBooks[i]->path()) {
148 int Library::find(const Book *book) const
150 for (int i = 0; i < mBooks.size(); i++) {
151 if (book == mBooks[i]) {