9 Library *Library::mInstance = 0;
11 Library::Library(QObject *parent): QAbstractListModel(parent)
21 Library *Library::instance()
24 mInstance = new Library();
29 int Library::rowCount(const QModelIndex &parent) const
31 if (parent.isValid()) {
38 QVariant Library::data(const QModelIndex &index, int role) const
40 if (!index.isValid()) {
46 return mBooks[index.row()]->name();
47 case Qt::DecorationRole:
48 return QPixmap::fromImage(mBooks[index.row()]->cover);
54 Book *Library::book(const QModelIndex &index)
56 if (index.isValid()) {
57 if ((index.row() >= 0) && (index.row() < mBooks.size())) {
58 return mBooks[index.row()];
60 qCritical() << "Library::book: Bad index" << index.row();
76 int size = settings.value("lib/size").toInt();
77 for (int i = 0; i < size; i++) {
78 QString key = "lib/book" + QString::number(i);
79 QString path = settings.value(key).toString();
80 Book *book = new Book(path);
81 connect(book, SIGNAL(opened(const QString &)),
82 this, SLOT(onBookOpened(const QString &)));
86 QString currentPath = settings.value("lib/nowreading").toString();
87 mNowReading = find(currentPath);
93 settings.setValue("lib/size", mBooks.size());
94 for (int i = 0; i < mBooks.size(); i++) {
95 QString key = "lib/book" + QString::number(i);
96 settings.setValue(key, mBooks[i]->path());
98 Book *currentBook = book(mNowReading);
99 settings.setValue("lib/nowreading",
100 currentBook? currentBook->path(): QString());
103 bool Library::add(const QString &path)
105 Trace t("Library::add " + path);
107 qCritical() << "Library::add: Empty path";
110 if (find(path).isValid()) {
111 qDebug() << "Book already exists in library";
114 int size = mBooks.size();
115 beginInsertRows(QModelIndex(), size, size);
116 Book *book = new Book(path);
124 void Library::remove(const QModelIndex &index)
126 Book *toRemove = book(index);
130 int row = index.row();
131 beginRemoveRows(QModelIndex(), row, row);
132 mBooks.removeAt(row);
135 if (index == mNowReading) {
136 mNowReading = QModelIndex();
137 emit nowReadingChanged();
142 void Library::remove(const QString &path)
147 QModelIndex Library::nowReading() const
152 void Library::setNowReading(const QModelIndex &index)
156 emit nowReadingChanged();
159 void Library::clear()
161 for (int i = 0; i < mBooks.size(); i++) {
165 mNowReading = QModelIndex();
168 QModelIndex Library::find(QString path) const
171 QString absolutePath = QFileInfo(path).absoluteFilePath();
172 for (int i = 0; i < mBooks.size(); i++) {
173 if (absolutePath == mBooks[i]->path()) {
178 return QModelIndex();
181 QModelIndex Library::find(const Book *book) const
184 for (int i = 0; i < mBooks.size(); i++) {
185 if (book == mBooks[i]) {
190 return QModelIndex();
193 void Library::onBookOpened(const QString &path)
195 Trace t("Library::onBookOpened " + path);
196 QModelIndex index = find(path);
197 if (index.isValid()) {
198 emit dataChanged(index, index);
202 QStringList Library::bookPaths()
205 foreach (Book *book, mBooks) {
206 ret.append(book->path());