More on 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), mNowReading(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     if (parent.isValid()) {
31         return 0;
32     } else {
33         return mBooks.size();
34     }
35 }
36
37 QVariant Library::data(const QModelIndex &index, int role) const
38 {
39     qDebug() << "Library::data, row" << index.row() << "role" << role;
40
41     if (!index.isValid()) {
42         return QVariant();
43     }
44
45     switch (role) {
46     case Qt::DisplayRole:
47         return mBooks[index.row()]->name();
48     default:
49         return QVariant();
50     }
51 }
52
53 Book *Library::book(const QModelIndex &index)
54 {
55     if (index.isValid()) {
56         return mBooks[index.row()];
57     } else {
58         return 0;
59     }
60 }
61
62 void Library::close()
63 {
64     delete mInstance;
65     mInstance = 0;
66 }
67
68 void Library::load()
69 {
70     QSettings settings;
71     clear();
72     int size = settings.value("lib/size").toInt();
73     for (int i = 0; i < size; i++) {
74         QString key = "lib/book" + QString::number(i);
75         QString path = settings.value(key).toString();
76         Book *book = new Book(path);
77         book->load();
78         qDebug() << "Library::load: Add" << book->title << "from" << path;
79         mBooks.append(book);
80     }
81     QString currentPath = settings.value("lib/nowreading").toString();
82     QModelIndex index = find(currentPath);
83     if (index.isValid()) {
84         mNowReading = mBooks[index.row()];
85         qDebug() << "Library::load: Now reading" << mNowReading->path();
86     }
87 }
88
89 void Library::save()
90 {
91     qDebug() << "Library::save";
92
93     QSettings settings;
94     settings.setValue("lib/size", mBooks.size());
95     for (int i = 0; i < mBooks.size(); i++) {
96         QString key = "lib/book" + QString::number(i);
97         settings.setValue(key, mBooks[i]->path());
98     }
99     settings.setValue("lib/nowreading",
100                       mNowReading? mNowReading->path(): QString());
101 }
102
103 bool Library::add(QString path)
104 {
105     qDebug() << "Library::add" << path;
106     if (path == "") {
107         qWarning() << "Library::add: Empty path";
108         return false;
109     }
110     if (find(path).isValid()) {
111         qDebug() << " Book already exists in library";
112         return false;
113     }
114     int size = mBooks.size();
115     Book *book = new Book(path);
116     beginInsertRows(QModelIndex(), size, size);
117     mBooks.append(book);
118     save();
119     endInsertRows();
120     return true;
121 }
122
123 void Library::remove(const QModelIndex &index)
124 {
125     if (!index.isValid()) {
126         return;
127     }
128     int row = index.row();
129     if ((row < 0) || (row >= mBooks.size())) {
130         return;
131     }
132     beginRemoveRows(QModelIndex(), row, row);
133     Book *book = mBooks[row];
134     mBooks.removeAt(row);
135     save();
136     endRemoveRows();
137     if (book == mNowReading) {
138         mNowReading = 0;
139         emit nowReadingChanged();
140     }
141     delete book;
142 }
143
144 QModelIndex Library::nowReading() const
145 {
146     return find(mNowReading);
147 }
148
149 void Library::setNowReading(const QModelIndex index)
150 {
151     if (index.isValid()) {
152         int row = index.row();
153         if ((row >= 0) && (row < mBooks.size())) {
154             mNowReading = mBooks[row];
155         }
156     } else {
157         mNowReading = 0;
158     }
159     save();
160     emit nowReadingChanged();
161 }
162
163 void Library::clear()
164 {
165     for (int i = 0; i < mBooks.size(); i++) {
166         delete mBooks[i];
167     }
168     mBooks.clear();
169     mNowReading = 0;
170 }
171
172 QModelIndex Library::find(QString path) const
173 {
174     if (path != "") {
175         QString absolutePath = QFileInfo(path).absoluteFilePath();
176         for (int i = 0; i < mBooks.size(); i++) {
177             if (absolutePath == mBooks[i]->path()) {
178                 return index(i);
179             }
180         }
181     }
182     return QModelIndex();
183 }
184
185 QModelIndex Library::find(const Book *book) const
186 {
187     for (int i = 0; i < mBooks.size(); i++) {
188         if (book == mBooks[i]) {
189             return index(i);
190         }
191     }
192     return QModelIndex();
193 }