More on library as model.
[dorian] / library.cpp
1 #include <QSettings>
2 #include <QDebug>
3 #include <QFileInfo>
4
5 #include "library.h"
6 #include "book.h"
7
8 Library *Library::mInstance = 0;
9
10 Library::Library(QObject *parent): QAbstractListModel(parent), mCurrent(0)
11 {
12     load();
13 }
14
15 Library::~Library()
16 {
17     clear();
18 }
19
20 Library *Library::instance()
21 {
22     if (!mInstance) {
23         mInstance = new Library();
24     }
25     return mInstance;
26 }
27
28 int Library::rowCount(const QModelIndex &parent) const
29 {
30     Q_UNUSED(parent);
31     return mBooks.size();
32 }
33
34 QVariant Library::data(const QModelIndex &index, int role) const
35 {
36     switch (role) {
37     case Qt::DisplayRole:
38         return mBooks[index.row()]->title;
39     case BookRole:
40         return QVariant::fromValue<Book>(*mBooks[index.row()]);
41     default:
42         return QVariant();
43     }
44 }
45
46 void Library::close()
47 {
48     delete mInstance;
49     mInstance = 0;
50 }
51
52 void Library::load()
53 {
54     QSettings settings;
55     clear();
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);
61         book->load();
62         qDebug() << "Library::load: Add" << book->title << "from" << path;
63         mBooks.append(book);
64     }
65     QString currentPath = settings.value("lib/current").toString();
66     int index = find(currentPath);
67     if (-1 != index) {
68         mCurrent = mBooks[index];
69         qDebug() << "Library::load: Current book is" << mCurrent->path();
70     }
71 }
72
73 void Library::save()
74 {
75     qDebug() << "Library::save";
76
77     QSettings settings;
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());
82     }
83     settings.setValue("lib/current", mCurrent? mCurrent->path(): QString());
84 }
85
86 bool Library::add(QString path)
87 {
88     if (path == "") {
89         return false;
90     }
91     if (find(path) != -1) {
92         return false;
93     }
94     int size = mBooks.size();
95     Book *book = new Book(path);
96     beginInsertRows(QModelIndex(), size - 1, size);
97     mBooks.append(book);
98     save();
99     endInsertRows();
100     return true;
101 }
102
103 void Library::remove(int index)
104 {
105     if ((index < 0) || (index >= mBooks.size())) {
106         return;
107     }
108     beginRemoveRows(QModelIndex(), index, index + 1);
109     Book *book = mBooks[index];
110     mBooks.removeAt(index);
111     save();
112     endRemoveRows();
113     if (book == mCurrent) {
114         mCurrent = 0;
115         emit currentBookChanged();
116     }
117     delete book;
118 }
119
120 int Library::size() const
121 {
122     return mBooks.size();
123 }
124
125 Book *Library::at(int index) const
126 {
127     return mBooks[index];
128 }
129
130 Book *Library::current() const
131 {
132     return mCurrent;
133 }
134
135 void Library::setCurrent(int index)
136 {
137     qDebug() << "Library::setCurrent" << index;
138     if ((index >= 0) && (index < mBooks.size()))
139     {
140         mCurrent = mBooks[index];
141         save();
142         qDebug() << " Emit currentBookChanged()";
143         emit currentBookChanged();
144     }
145 }
146
147 void Library::clear()
148 {
149     for (int i = 0; i < mBooks.size(); i++) {
150         delete mBooks[i];
151     }
152     mBooks.clear();
153     mCurrent = 0;
154 }
155
156 int Library::find(QString path) const
157 {
158     if (path != "") {
159         QString absolutePath = QFileInfo(path).absoluteFilePath();
160         for (int i = 0; i < mBooks.size(); i++) {
161             if (absolutePath == mBooks[i]->path()) {
162                 return i;
163             }
164         }
165     }
166     return -1;
167 }
168
169 int Library::find(const Book *book) const
170 {
171     for (int i = 0; i < mBooks.size(); i++) {
172         if (book == mBooks[i]) {
173             return i;
174         }
175     }
176     return -1;
177 }