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