More model fixes.
[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)
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     if (!index.isValid()) {
40         return QVariant();
41     }
42
43     switch (role) {
44     case Qt::DisplayRole:
45         return mBooks[index.row()]->name();
46     default:
47         return QVariant();
48     }
49 }
50
51 Book *Library::book(const QModelIndex &index)
52 {
53     if (index.isValid()) {
54         if ((index.row() >= 0) && (index.row() < mBooks.size())) {
55             qDebug() << "Library::book:" << index.row() << "is"
56                     << mBooks[index.row()]->name();
57             return mBooks[index.row()];
58         } else {
59             qWarning() << "*** Library::book: Bad index" << index.row();
60         }
61     }
62     return 0;
63 }
64
65 void Library::close()
66 {
67     delete mInstance;
68     mInstance = 0;
69 }
70
71 void Library::load()
72 {
73     QSettings settings;
74     clear();
75     int size = settings.value("lib/size").toInt();
76     for (int i = 0; i < size; i++) {
77         QString key = "lib/book" + QString::number(i);
78         QString path = settings.value(key).toString();
79         Book *book = new Book(path);
80         book->load();
81         qDebug() << "Library::load: Add" << book->title << "from" << path;
82         mBooks.append(book);
83     }
84     QString currentPath = settings.value("lib/nowreading").toString();
85     mNowReading = find(currentPath);
86 }
87
88 void Library::save()
89 {
90     qDebug() << "Library::save";
91
92     QSettings settings;
93     settings.setValue("lib/size", mBooks.size());
94     for (int i = 0; i < mBooks.size(); i++) {
95         QString key = "lib/book" + QString::number(i);
96         settings.setValue(key, mBooks[i]->path());
97     }
98     Book *currentBook = book(mNowReading);
99     settings.setValue("lib/nowreading",
100                       currentBook? currentBook->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     Book *toRemove = book(index);
126     if (!toRemove) {
127         return;
128     }
129     int row = index.row();
130     beginRemoveRows(QModelIndex(), row, row);
131     mBooks.removeAt(row);
132     save();
133     endRemoveRows();
134     if (index == mNowReading) {
135         mNowReading = QModelIndex();
136         emit nowReadingChanged();
137     }
138     delete toRemove;
139 }
140
141 QModelIndex Library::nowReading() const
142 {
143     qDebug() << "Library::nowReading" << mNowReading.row();
144     return mNowReading;
145 }
146
147 void Library::setNowReading(const QModelIndex &index)
148 {
149     mNowReading = index;
150     save();
151     emit nowReadingChanged();
152 }
153
154 void Library::clear()
155 {
156     for (int i = 0; i < mBooks.size(); i++) {
157         delete mBooks[i];
158     }
159     mBooks.clear();
160     mNowReading = QModelIndex();
161 }
162
163 QModelIndex Library::find(QString path) const
164 {
165     if (path != "") {
166         QString absolutePath = QFileInfo(path).absoluteFilePath();
167         for (int i = 0; i < mBooks.size(); i++) {
168             if (absolutePath == mBooks[i]->path()) {
169                 return index(i);
170             }
171         }
172     }
173     return QModelIndex();
174 }
175
176 QModelIndex Library::find(const Book *book) const
177 {
178     if (book) {
179         for (int i = 0; i < mBooks.size(); i++) {
180             if (book == mBooks[i]) {
181                 return index(i);
182             }
183         }
184     }
185     return QModelIndex();
186 }