5 #include <qtextdocument.h> // Qt::escape is currently defined here...
6 #include <QDirIterator>
8 #include <QtAlgorithms>
11 #include "opshandler.h"
12 #include "xmlerrorhandler.h"
13 #include "extractzip.h"
15 #include "containerhandler.h"
16 #include "ncxhandler.h"
19 const int COVER_WIDTH = 53;
20 const int COVER_HEIGHT = 59;
22 Book::Book(const QString &p, QObject *parent): QObject(parent)
27 mPath = info.absoluteFilePath();
28 title = info.baseName();
29 cover = QImage(":/icons/book.png").scaled(COVER_WIDTH, COVER_HEIGHT,
30 Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation).
31 scaled(COVER_WIDTH, COVER_HEIGHT, Qt::KeepAspectRatio);
35 QString Book::path() const
42 Trace t("Book::open");
46 if (path().isEmpty()) {
63 Trace t("Book::close");
66 QDir::setCurrent(QDir::rootPath());
70 QString Book::tmpDir() const
72 return QDir::tempPath() + "/dorian/book";
77 Trace t("Book::extract");
79 QString tmp = tmpDir();
80 t.trace("Extracting " + mPath + " to " + tmp);
82 QDir::setCurrent(QDir::rootPath());
84 qCritical() << "Book::extract: Failed to remove" << tmp;
89 qCritical() << "Book::extract: Could not create" << tmp;
93 // If book comes from resource, copy it to the temporary directory first
94 QString bookPath = path();
95 if (bookPath.startsWith(":/books/")) {
97 QString dst(tmp + "/book.epub");
99 qCritical() << "Book::extract: Failed to copy built-in book to"
106 QString oldDir = QDir::currentPath();
107 if (!QDir::setCurrent(tmp)) {
108 qCritical() << "Book::extract: Could not change to" << tmp;
111 ret = extractZip(bookPath);
113 qCritical() << "Book::extract: Extracting ZIP failed";
115 QDir::setCurrent(oldDir);
121 Trace t("Book::parse");
125 QString opsFileName = opsPath();
126 t.trace("Parsing OPS file" + opsFileName);
127 QFile opsFile(opsFileName);
128 QXmlSimpleReader reader;
129 QXmlInputSource *source = new QXmlInputSource(&opsFile);
130 OpsHandler *opsHandler = new OpsHandler(*this);
131 XmlErrorHandler *errorHandler = new XmlErrorHandler();
132 reader.setContentHandler(opsHandler);
133 reader.setErrorHandler(errorHandler);
134 ret = reader.parse(source);
139 // Initially, put all content items in the chapter list.
140 // This will be refined by parsing the NCX file later
144 QStringList coverKeys;
145 coverKeys << "cover-image" << "img-cover-jpeg" << "cover";
146 foreach (QString key, coverKeys) {
147 if (content.contains(key)) {
148 t.trace("Loading cover image from " + content[key].href);
149 cover = QImage(content[key].href).scaled(COVER_WIDTH, COVER_HEIGHT,
150 Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation).
151 scaled(COVER_WIDTH, COVER_HEIGHT, Qt::KeepAspectRatio);
156 // If there is an "ncx" item in content, parse it: That's the real table of
158 if (content.contains("ncx")) {
159 QString ncxFileName = content["ncx"].href;
160 t.trace("Parsing NCX file " + ncxFileName);
161 QFile ncxFile(ncxFileName);
162 source = new QXmlInputSource(&ncxFile);
163 NcxHandler *ncxHandler = new NcxHandler(*this);
164 errorHandler = new XmlErrorHandler();
165 reader.setContentHandler(ncxHandler);
166 reader.setErrorHandler(errorHandler);
167 ret = reader.parse(source);
173 // Calculate book part sizes
174 foreach (QString part, parts) {
175 QFileInfo info(content[part].href);
176 content[part].size = info.size();
177 t.trace(QString("Size of part %1: %2").arg(part).arg(content[part].size));
183 bool Book::clearDir(const QString &dir)
189 QDirIterator i(dir, QDirIterator::Subdirectories);
190 while (i.hasNext()) {
191 QString entry = i.next();
192 if (entry.endsWith("/.") || entry.endsWith("/..")) {
195 QFileInfo info(entry);
197 if (!clearDir(entry)) {
202 if (!QFile::remove(entry)) {
203 qCritical() << "Book::clearDir: Could not remove" << entry;
204 // FIXME: To be investigated: This is happening too often
228 Trace t("Book::load");
229 t.trace("path: " + path());
231 QString key = "book/" + path() + "/";
232 t.trace("key: " + key);
235 title = settings.value(key + "title").toString();
237 creators = settings.value(key + "creators").toStringList();
238 date = settings.value(key + "date").toString();
239 publisher = settings.value(key + "publisher").toString();
240 datePublished = settings.value(key + "datepublished").toString();
241 subject = settings.value(key + "subject").toString();
242 source = settings.value(key + "source").toString();
243 rights = settings.value(key + "rights").toString();
244 mLastBookmark.part = settings.value(key + "lastpart").toInt();
245 mLastBookmark.pos = settings.value(key + "lastpos").toReal();
246 cover = settings.value(key + "cover").value<QImage>().scaled(COVER_WIDTH,
247 COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
248 if (cover.isNull()) {
249 cover = QImage(":/icons/book.png").scaled(COVER_WIDTH, COVER_HEIGHT,
250 Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation).
251 scaled(COVER_WIDTH, COVER_HEIGHT, Qt::KeepAspectRatio);
255 int size = settings.value(key + "bookmarks").toInt();
256 for (int i = 0; i < size; i++) {
257 int part = settings.value(key + "bookmark" + QString::number(i) +
259 qreal pos = settings.value(key + "bookmark" + QString::number(i) +
261 t.trace(QString("Bookmark %1 at part %2, %3").
262 arg(i).arg(part).arg(pos));
263 mBookmarks.append(Bookmark(part, pos));
269 Trace t("Book::save");
271 QString key = "book/" + path() + "/";
272 t.trace("key: " + key);
275 settings.setValue(key + "title", title);
276 t.trace("title: " + title);
277 settings.setValue(key + "creators", creators);
278 settings.setValue(key + "date", date);
279 settings.setValue(key + "publisher", publisher);
280 settings.setValue(key + "datepublished", datePublished);
281 settings.setValue(key + "subject", subject);
282 settings.setValue(key + "source", source);
283 settings.setValue(key + "rights", rights);
284 settings.setValue(key + "lastpart", mLastBookmark.part);
285 settings.setValue(key + "lastpos", mLastBookmark.pos);
286 settings.setValue(key + "cover", cover);
289 settings.setValue(key + "bookmarks", mBookmarks.size());
290 for (int i = 0; i < mBookmarks.size(); i++) {
291 t.trace(QString("Bookmark %1 at %2, %3").
292 arg(i).arg(mBookmarks[i].part).arg(mBookmarks[i].pos));
293 settings.setValue(key + "bookmark" + QString::number(i) + "/part",
295 settings.setValue(key + "bookmark" + QString::number(i) + "/pos",
300 void Book::setLastBookmark(int part, qreal position)
302 mLastBookmark.part = part;
303 mLastBookmark.pos = position;
307 Book::Bookmark Book::lastBookmark() const
309 return Book::Bookmark(mLastBookmark);
312 void Book::addBookmark(int part, qreal position)
314 mBookmarks.append(Bookmark(part, position));
315 qSort(mBookmarks.begin(), mBookmarks.end());
319 void Book::deleteBookmark(int index)
321 mBookmarks.removeAt(index);
325 QList<Book::Bookmark> Book::bookmarks() const
330 QString Book::opsPath()
332 Trace t("Book::opsPath");
335 QFile container(tmpDir() + "/META-INF/container.xml");
336 t.trace(container.fileName());
337 QXmlSimpleReader reader;
338 QXmlInputSource *source = new QXmlInputSource(&container);
339 ContainerHandler *containerHandler = new ContainerHandler();
340 XmlErrorHandler *errorHandler = new XmlErrorHandler();
341 reader.setContentHandler(containerHandler);
342 reader.setErrorHandler(errorHandler);
343 if (reader.parse(source)) {
344 ret = tmpDir() + "/" + containerHandler->rootFile;
345 mRootPath = QFileInfo(ret).absoluteDir().absolutePath();
346 t.trace("OSP path: " + ret);
347 t.trace("Root dir: " + mRootPath);
350 delete containerHandler;
355 QString Book::rootPath() const
360 QString Book::name() const
364 if (creators.length()) {
365 ret += "\nBy " + creators[0];
366 for (int i = 1; i < creators.length(); i++) {
367 ret += ", " + creators[i];
376 QString Book::shortName() const
378 return (title.isEmpty())? QFileInfo(path()).baseName(): title;
381 int Book::chapterFromPart(int index)
389 int Book::partFromChapter(int index)
391 Trace t("Book::partFromChapter");
392 QString id = chapters[index];
393 QString href = content[id].href;
394 QString baseRef(href);
395 QUrl url(QString("file://") + href);
396 if (url.hasFragment()) {
397 QString fragment = url.fragment();
398 baseRef.chop(fragment.length() + 1);
401 // Swipe through all content items to find the one matching the chapter href
402 // FIXME: Do we need to index content items by href, too?
405 foreach (contentKey, content.keys()) {
406 if (contentKey == id) {
409 if (content[contentKey].href == baseRef) {
411 t.trace(QString("Key for %1 is %2").arg(baseRef).arg(contentKey));
416 t.trace("Could not find key for " + baseRef);
419 int partIndex = parts.indexOf(contentKey);
420 if (partIndex == -1) {
422 << "Book::partFromChapter: Could not find part index of chapter"