More modelification.
[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     QModelIndex index = find(currentPath);
67     if (index.isValid()) {
68         mCurrent = mBooks[index.row()];
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     qDebug() << "Library::add" << path;
89     if (path == "") {
90         qWarning() << "Library::add: Empty path";
91         return false;
92     }
93     if (find(path).isValid()) {
94         qDebug() << " Book already exists in library";
95         return false;
96     }
97     int size = mBooks.size();
98     Book *book = new Book(path);
99     beginInsertRows(QModelIndex(), size - 1, size);
100     mBooks.append(book);
101     save();
102     endInsertRows();
103     return true;
104 }
105
106 void Library::remove(const QModelIndex &index)
107 {
108     int row = index.row();
109     if ((row < 0) || (row >= mBooks.size())) {
110         return;
111     }
112     beginRemoveRows(QModelIndex(), row, row + 1);
113     Book *book = mBooks[row];
114     mBooks.removeAt(row);
115     save();
116     endRemoveRows();
117     if (book == mCurrent) {
118         mCurrent = 0;
119         emit currentBookChanged();
120     }
121     delete book;
122 }
123
124 Book *Library::current() const
125 {
126     return mCurrent;
127 }
128
129 void Library::setCurrent(const QModelIndex index)
130 {
131     int row = index.row();
132     qDebug() << "Library::setCurrent" << row;
133     if ((row >= 0) && (row < mBooks.size())) {
134         mCurrent = mBooks[row];
135         save();
136         emit currentBookChanged();
137     }
138 }
139
140 void Library::clear()
141 {
142     for (int i = 0; i < mBooks.size(); i++) {
143         delete mBooks[i];
144     }
145     mBooks.clear();
146     mCurrent = 0;
147 }
148
149 QModelIndex Library::find(QString path) const
150 {
151     if (path != "") {
152         QString absolutePath = QFileInfo(path).absoluteFilePath();
153         for (int i = 0; i < mBooks.size(); i++) {
154             if (absolutePath == mBooks[i]->path()) {
155                 return index(i);
156             }
157         }
158     }
159     return QModelIndex();
160 }
161
162 QModelIndex Library::find(const Book *book) const
163 {
164     for (int i = 0; i < mBooks.size(); i++) {
165         if (book == mBooks[i]) {
166             return index(i);
167         }
168     }
169     return QModelIndex();
170 }