Don't reload volume if bookmark is in the same volume.
[dorian] / library.cpp
1 #include <QSettings>
2 #include <QString>
3 #include <QDebug>
4 #include <QFileInfo>
5
6 #include "library.h"
7
8 Library *Library::mInstance = 0;
9
10 Library::Library(): 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 void Library::close()
29 {
30     delete mInstance;
31     mInstance = 0;
32 }
33
34 void Library::load()
35 {
36     QSettings settings;
37     clear();
38     int size = settings.value("lib/size").toInt();
39     for (int i = 0; i < size; i++) {
40         QString key = "lib/book" + QString::number(i);
41         QString path = settings.value(key).toString();
42         Book *book = new Book(path);
43         book->load();
44         qDebug() << "Library::load: Add" << book->title << "from" << path;
45         mBooks.append(book);
46     }
47     QString currentPath = settings.value("lib/current").toString();
48     int index = find(currentPath);
49     if (-1 != index) {
50         mCurrent = mBooks[index];
51         qDebug() << "Library::load: Current book is" << mCurrent->path();
52     }
53 }
54
55 void Library::save()
56 {
57     qDebug() << "Library::save";
58
59     QSettings settings;
60     settings.setValue("lib/size", mBooks.size());
61     for (int i = 0; i < mBooks.size(); i++) {
62         QString key = "lib/book" + QString::number(i);
63         settings.setValue(key, mBooks[i]->path());
64     }
65     settings.setValue("lib/current", mCurrent? mCurrent->path(): QString());
66 }
67
68 bool Library::add(QString path)
69 {
70     if (path == "") {
71         return false;
72     }
73     if (find(path) != -1) {
74         return false;
75     }
76     Book *book = new Book(path);
77     mBooks.append(book);
78     save();
79     emit bookAdded();
80     return true;
81 }
82
83 void Library::remove(int index)
84 {
85     if ((index < 0) || (index >= mBooks.size())) {
86         return;
87     }
88     Book *book = mBooks[index];
89     mBooks.removeAt(index);
90     if (book == mCurrent) {
91         mCurrent = 0;
92         emit currentBookChanged();
93     }
94     emit bookRemoved(index);
95     delete book;
96     save();
97 }
98
99 int Library::size() const
100 {
101     return mBooks.size();
102 }
103
104 Book *Library::at(int index) const
105 {
106     return mBooks[index];
107 }
108
109 Book *Library::current() const
110 {
111     return mCurrent;
112 }
113
114 void Library::setCurrent(int index)
115 {
116     qDebug() << "Library::setCurrent" << index;
117     if ((index >= 0) && (index < mBooks.size()))
118     {
119         mCurrent = mBooks[index];
120         save();
121         qDebug() << " Emit currentBookChanged()";
122         emit currentBookChanged();
123     }
124 }
125
126 void Library::clear()
127 {
128     for (int i = 0; i < mBooks.size(); i++) {
129         delete mBooks[i];
130     }
131     mBooks.clear();
132     mCurrent = 0;
133 }
134
135 int Library::find(QString path) const
136 {
137     if (path != "") {
138         QString absolutePath = QFileInfo(path).absoluteFilePath();
139         for (int i = 0; i < mBooks.size(); i++) {
140             if (absolutePath == mBooks[i]->path()) {
141                 return i;
142             }
143         }
144     }
145     return -1;
146 }
147
148 int Library::find(const Book *book) const
149 {
150     for (int i = 0; i < mBooks.size(); i++) {
151         if (book == mBooks[i]) {
152             return i;
153         }
154     }
155     return -1;
156 }