Pass model test.
[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     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     case BookRole:
49         return QVariant::fromValue<Book>(*mBooks[index.row()]);
50     default:
51         return QVariant();
52     }
53 }
54
55 void Library::close()
56 {
57     delete mInstance;
58     mInstance = 0;
59 }
60
61 void Library::load()
62 {
63     QSettings settings;
64     clear();
65     int size = settings.value("lib/size").toInt();
66     for (int i = 0; i < size; i++) {
67         QString key = "lib/book" + QString::number(i);
68         QString path = settings.value(key).toString();
69         Book *book = new Book(path);
70         book->load();
71         qDebug() << "Library::load: Add" << book->title << "from" << path;
72         mBooks.append(book);
73     }
74     QString currentPath = settings.value("lib/current").toString();
75     QModelIndex index = find(currentPath);
76     if (index.isValid()) {
77         mCurrent = mBooks[index.row()];
78         qDebug() << "Library::load: Current book is" << mCurrent->path();
79     }
80 }
81
82 void Library::save()
83 {
84     qDebug() << "Library::save";
85
86     QSettings settings;
87     settings.setValue("lib/size", mBooks.size());
88     for (int i = 0; i < mBooks.size(); i++) {
89         QString key = "lib/book" + QString::number(i);
90         settings.setValue(key, mBooks[i]->path());
91     }
92     settings.setValue("lib/current", mCurrent? mCurrent->path(): QString());
93 }
94
95 bool Library::add(QString path)
96 {
97     qDebug() << "Library::add" << path;
98     if (path == "") {
99         qWarning() << "Library::add: Empty path";
100         return false;
101     }
102     if (find(path).isValid()) {
103         qDebug() << " Book already exists in library";
104         return false;
105     }
106     int size = mBooks.size();
107     Book *book = new Book(path);
108     beginInsertRows(QModelIndex(), size, size);
109     mBooks.append(book);
110     save();
111     endInsertRows();
112     return true;
113 }
114
115 void Library::remove(const QModelIndex &index)
116 {
117     if (!index.isValid()) {
118         return;
119     }
120     int row = index.row();
121     if ((row < 0) || (row >= mBooks.size())) {
122         return;
123     }
124     beginRemoveRows(QModelIndex(), row, row);
125     Book *book = mBooks[row];
126     mBooks.removeAt(row);
127     save();
128     endRemoveRows();
129     if (book == mCurrent) {
130         mCurrent = 0;
131         emit currentBookChanged();
132     }
133     delete book;
134 }
135
136 Book *Library::current() const
137 {
138     return mCurrent;
139 }
140
141 void Library::setCurrent(const QModelIndex index)
142 {
143     int row = index.row();
144     qDebug() << "Library::setCurrent" << row;
145     if ((row >= 0) && (row < mBooks.size())) {
146         mCurrent = mBooks[row];
147         save();
148         emit currentBookChanged();
149     }
150 }
151
152 void Library::clear()
153 {
154     for (int i = 0; i < mBooks.size(); i++) {
155         delete mBooks[i];
156     }
157     mBooks.clear();
158     mCurrent = 0;
159 }
160
161 QModelIndex Library::find(QString path) const
162 {
163     if (path != "") {
164         QString absolutePath = QFileInfo(path).absoluteFilePath();
165         for (int i = 0; i < mBooks.size(); i++) {
166             if (absolutePath == mBooks[i]->path()) {
167                 return index(i);
168             }
169         }
170     }
171     return QModelIndex();
172 }
173
174 QModelIndex Library::find(const Book *book) const
175 {
176     for (int i = 0; i < mBooks.size(); i++) {
177         if (book == mBooks[i]) {
178             return index(i);
179         }
180     }
181     return QModelIndex();
182 }