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(QString path)
105 Trace t("Library::add " + path);
107 qCritical() << "*** Library::add: Empty path";
110 if (find(path).isValid()) {
111 t.trace("Book already exists in library");
114 int size = mBooks.size();
115 Book *book = new Book(path);
116 beginInsertRows(QModelIndex(), size, size);
123 void Library::remove(const QModelIndex &index)
125 Book *toRemove = book(index);
129 int row = index.row();
130 beginRemoveRows(QModelIndex(), row, row);
131 mBooks.removeAt(row);
134 if (index == mNowReading) {
135 mNowReading = QModelIndex();
136 emit nowReadingChanged();
141 QModelIndex Library::nowReading() const
146 void Library::setNowReading(const QModelIndex &index)
150 emit nowReadingChanged();
153 void Library::clear()
155 for (int i = 0; i < mBooks.size(); i++) {
159 mNowReading = QModelIndex();
162 QModelIndex Library::find(QString path) const
165 QString absolutePath = QFileInfo(path).absoluteFilePath();
166 for (int i = 0; i < mBooks.size(); i++) {
167 if (absolutePath == mBooks[i]->path()) {
172 return QModelIndex();
175 QModelIndex Library::find(const Book *book) const
178 for (int i = 0; i < mBooks.size(); i++) {
179 if (book == mBooks[i]) {
184 return QModelIndex();
187 void Library::onBookOpened(const QString &path)
189 Trace t("Library::onBookOpened " + path);
190 QModelIndex index = find(path);
191 if (index.isValid()) {
192 emit dataChanged(index, index);