1 /*
2  * Copyright (C) 2015 Dan Leinir Turthra Jensen <admin@leinir.dk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) version 3, or any
8  * later version accepted by the membership of KDE e.V. (or its
9  * successor approved by the membership of KDE e.V.), which shall
10  * act as a proxy defined in Section 6 of version 3 of the license.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include "BookModel.h"
23 #include "qtquick_debug.h"
24 
25 #include <AcbfDocument.h>
26 
27 #include <KFileMetaData/UserMetaData>
28 
29 struct BookPage {
BookPageBookPage30     BookPage() {}
31     QString url;
32     QString title;
33 };
34 
35 class BookModel::Private {
36 public:
Private()37     Private()
38         : currentPage(0)
39         , acbfData(nullptr)
40         , processing(false)
41     {}
42     QString filename;
43     QString author;
44     QString publisher;
45     QString title;
46     QList<BookPage*> entries;
47     int currentPage;
48     AdvancedComicBookFormat::Document* acbfData;
49     bool processing;
50     QString processingDescription;
51 };
52 
BookModel(QObject * parent)53 BookModel::BookModel(QObject* parent)
54     : QAbstractListModel(parent)
55     , d(new Private)
56 {
57 }
58 
~BookModel()59 BookModel::~BookModel()
60 {
61     delete d;
62 }
63 
roleNames() const64 QHash<int, QByteArray> BookModel::roleNames() const
65 {
66     QHash<int, QByteArray> roles;
67     roles[UrlRole] = "url";
68     roles[TitleRole] = "title";
69     return roles;
70 }
71 
data(const QModelIndex & index,int role) const72 QVariant BookModel::data(const QModelIndex& index, int role) const
73 {
74     QVariant result;
75     if(index.isValid() && index.row() > -1 && index.row() < d->entries.count())
76     {
77         const BookPage* entry = d->entries[index.row()];
78         switch(role)
79         {
80             case UrlRole:
81                 result.setValue(entry->url);
82                 break;
83             case TitleRole:
84                 result.setValue(entry->title);
85                 break;
86             default:
87                 result.setValue(QString("Unknown role"));
88                 break;
89         }
90     }
91     return result;
92 }
93 
rowCount(const QModelIndex & parent) const94 int BookModel::rowCount(const QModelIndex& parent) const
95 {
96     if(parent.isValid())
97         return 0;
98     return d->entries.count();
99 }
100 
addPage(QString url,QString title)101 void BookModel::addPage(QString url, QString title)
102 {
103     BookPage* page = new BookPage();
104     page->url = url;
105     page->title = title;
106 
107     beginInsertRows(QModelIndex(), d->entries.count(), d->entries.count());
108     d->entries.append(page);
109     emit pageCountChanged();
110     endInsertRows();
111 }
112 
removePage(int pageNumber)113 void BookModel::removePage(int pageNumber)
114 {
115     QModelIndex index  = createIndex(pageNumber, 0);
116     beginRemoveRows(QModelIndex(), index.row(), index.row());
117     d->entries.removeAt(pageNumber);
118     emit pageCountChanged();
119     endRemoveRows();
120 }
121 
clearPages()122 void BookModel::clearPages()
123 {
124     beginResetModel();
125     qDeleteAll(d->entries);
126     d->entries.clear();
127     emit pageCountChanged();
128     endResetModel();
129 }
130 
filename() const131 QString BookModel::filename() const
132 {
133     return d->filename;
134 }
135 
setFilename(QString newFilename)136 void BookModel::setFilename(QString newFilename)
137 {
138     d->filename = newFilename;
139     d->title = newFilename.split('/').last().left(newFilename.lastIndexOf('.'));
140     emit filenameChanged();
141     emit titleChanged();
142 }
143 
author() const144 QString BookModel::author() const
145 {
146     return d->author;
147 }
148 
setAuthor(QString newAuthor)149 void BookModel::setAuthor(QString newAuthor)
150 {
151     d->author = newAuthor;
152     emit authorChanged();
153 }
154 
publisher() const155 QString BookModel::publisher() const
156 {
157     return d->publisher;
158 }
159 
setPublisher(QString newPublisher)160 void BookModel::setPublisher(QString newPublisher)
161 {
162     d->publisher = newPublisher;
163     emit publisherChanged();
164 }
165 
title() const166 QString BookModel::title() const
167 {
168     return d->title;
169 }
170 
setTitle(QString newTitle)171 void BookModel::setTitle(QString newTitle)
172 {
173     d->title = newTitle;
174     emit titleChanged();
175 }
176 
pageCount() const177 int BookModel::pageCount() const
178 {
179     return d->entries.count();
180 }
181 
currentPage() const182 int BookModel::currentPage() const
183 {
184     return d->currentPage;
185 }
186 
setCurrentPage(int newCurrentPage,bool updateFilesystem)187 void BookModel::setCurrentPage(int newCurrentPage, bool updateFilesystem)
188 {
189 //     qCDebug(QTQUICK_LOG) << Q_FUNC_INFO << d->filename << newCurrentPage << updateFilesystem;
190     if(updateFilesystem)
191     {
192         KFileMetaData::UserMetaData data(d->filename);
193         data.setAttribute("peruse.currentPage", QString::number(newCurrentPage));
194     }
195     d->currentPage = newCurrentPage;
196     emit currentPageChanged();
197 }
198 
acbfData() const199 QObject * BookModel::acbfData() const
200 {
201     return d->acbfData;
202 }
203 
setAcbfData(QObject * obj)204 void BookModel::setAcbfData(QObject* obj)
205 {
206     d->acbfData = qobject_cast<AdvancedComicBookFormat::Document*>(obj);
207     emit acbfDataChanged();
208 }
209 
processing() const210 bool BookModel::processing() const
211 {
212     return d->processing;
213 }
214 
setProcessing(bool processing)215 void BookModel::setProcessing(bool processing)
216 {
217     d->processing = processing;
218     emit processingChanged();
219 }
220 
processingDescription() const221 QString BookModel::processingDescription() const
222 {
223     return d->processingDescription;
224 }
225 
setProcessingDescription(const QString & description)226 void BookModel::setProcessingDescription ( const QString& description )
227 {
228     d->processingDescription = description;
229     qCDebug(QTQUICK_LOG) << description;
230     Q_EMIT processingDescriptionChanged();
231 }
232 
swapPages(int swapThisIndex,int withThisIndex)233 void BookModel::swapPages(int swapThisIndex, int withThisIndex)
234 {
235     if(swapThisIndex > -1 && withThisIndex > -1 && swapThisIndex < d->entries.count() && withThisIndex < d->entries.count()) {
236         QModelIndex firstIndex = createIndex(swapThisIndex, 0);
237         QModelIndex secondIndex = createIndex(withThisIndex, 0);
238         d->entries.swapItemsAt(swapThisIndex, withThisIndex);
239         dataChanged(firstIndex, secondIndex);
240     }
241 }
242