Turn library into proper 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), 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     Q_UNUSED(role);
37     Book *book = mBooks[index.row()];
38     return QVariant::fromValue<Book>(*book);
39 }
40
41 void Library::close()
42 {
43     delete mInstance;
44     mInstance = 0;
45 }
46
47 void Library::load()
48 {
49     QSettings settings;
50     clear();
51     int size = settings.value("lib/size").toInt();
52     for (int i = 0; i < size; i++) {
53         QString key = "lib/book" + QString::number(i);
54         QString path = settings.value(key).toString();
55         Book *book = new Book(path);
56         book->load();
57         qDebug() << "Library::load: Add" << book->title << "from" << path;
58         mBooks.append(book);
59     }
60     QString currentPath = settings.value("lib/current").toString();
61     int index = find(currentPath);
62     if (-1 != index) {
63         mCurrent = mBooks[index];
64         qDebug() << "Library::load: Current book is" << mCurrent->path();
65     }
66 }
67
68 void Library::save()
69 {
70     qDebug() << "Library::save";
71
72     QSettings settings;
73     settings.setValue("lib/size", mBooks.size());
74     for (int i = 0; i < mBooks.size(); i++) {
75         QString key = "lib/book" + QString::number(i);
76         settings.setValue(key, mBooks[i]->path());
77     }
78     settings.setValue("lib/current", mCurrent? mCurrent->path(): QString());
79 }
80
81 bool Library::add(QString path)
82 {
83     if (path == "") {
84         return false;
85     }
86     if (find(path) != -1) {
87         return false;
88     }
89     Book *book = new Book(path);
90     mBooks.append(book);
91     save();
92     emit bookAdded();
93     return true;
94 }
95
96 void Library::remove(int index)
97 {
98     if ((index < 0) || (index >= mBooks.size())) {
99         return;
100     }
101     Book *book = mBooks[index];
102     mBooks.removeAt(index);
103     if (book == mCurrent) {
104         mCurrent = 0;
105         emit currentBookChanged();
106     }
107     emit bookRemoved(index);
108     delete book;
109     save();
110 }
111
112 int Library::size() const
113 {
114     return mBooks.size();
115 }
116
117 Book *Library::at(int index) const
118 {
119     return mBooks[index];
120 }
121
122 Book *Library::current() const
123 {
124     return mCurrent;
125 }
126
127 void Library::setCurrent(int index)
128 {
129     qDebug() << "Library::setCurrent" << index;
130     if ((index >= 0) && (index < mBooks.size()))
131     {
132         mCurrent = mBooks[index];
133         save();
134         qDebug() << " Emit currentBookChanged()";
135         emit currentBookChanged();
136     }
137 }
138
139 void Library::clear()
140 {
141     for (int i = 0; i < mBooks.size(); i++) {
142         delete mBooks[i];
143     }
144     mBooks.clear();
145     mCurrent = 0;
146 }
147
148 int Library::find(QString path) const
149 {
150     if (path != "") {
151         QString absolutePath = QFileInfo(path).absoluteFilePath();
152         for (int i = 0; i < mBooks.size(); i++) {
153             if (absolutePath == mBooks[i]->path()) {
154                 return i;
155             }
156         }
157     }
158     return -1;
159 }
160
161 int Library::find(const Book *book) const
162 {
163     for (int i = 0; i < mBooks.size(); i++) {
164         if (book == mBooks[i]) {
165             return i;
166         }
167     }
168     return -1;
169 }