5 #include <qtextdocument.h> // Qt::escape is currently defined here...
6 #include <QDirIterator>
8 #include <QtAlgorithms>
9 #include <QCryptographicHash>
12 #include "opshandler.h"
13 #include "xmlerrorhandler.h"
14 #include "extractzip.h"
16 #include "containerhandler.h"
17 #include "ncxhandler.h"
20 const int COVER_WIDTH = 53;
21 const int COVER_HEIGHT = 59;
23 static QImage makeCover(const QString &path)
25 return QImage(path).scaled(COVER_WIDTH, COVER_HEIGHT,
26 Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation).
27 scaled(COVER_WIDTH, COVER_HEIGHT, Qt::KeepAspectRatio);
30 Book::Book(const QString &p, QObject *parent): QObject(parent)
35 mPath = info.absoluteFilePath();
36 title = info.baseName();
37 cover = makeCover(":/icons/book.png");
42 QString Book::path() const
49 Trace t("Book::open");
53 if (path().isEmpty()) {
70 Trace t("Book::peek");
74 if (path().isEmpty()) {
78 if (!extractMetaData()) {
90 Trace t("Book::close");
93 QDir::setCurrent(QDir::rootPath());
97 QString Book::tmpDir() const
99 QString tmpName = QFileInfo(mTempFile.fileName()).fileName();
100 return QDir::tempPath() + "/dorian/" + tmpName;
105 Trace t("Book::extract");
107 QString tmp = tmpDir();
108 t.trace("Extracting " + mPath + " to " + tmp);
110 QDir::setCurrent(QDir::rootPath());
111 if (!clearDir(tmp)) {
112 qCritical() << "Book::extract: Failed to remove" << tmp;
116 if (!d.mkpath(tmp)) {
117 qCritical() << "Book::extract: Could not create" << tmp;
121 // If book comes from resource, copy it to the temporary directory first
122 QString bookPath = path();
123 if (bookPath.startsWith(":/books/")) {
125 QString dst(tmp + "/book.epub");
126 if (!src.copy(dst)) {
127 qCritical() << "Book::extract: Failed to copy built-in book to"
134 QString oldDir = QDir::currentPath();
135 if (!QDir::setCurrent(tmp)) {
136 qCritical() << "Book::extract: Could not change to" << tmp;
139 ret = extractZip(bookPath);
141 qCritical() << "Book::extract: Extracting ZIP failed";
143 QDir::setCurrent(oldDir);
149 Trace t("Book::parse");
153 QString opsFileName = opsPath();
154 t.trace("Parsing OPS file" + opsFileName);
155 QFile opsFile(opsFileName);
156 QXmlSimpleReader reader;
157 QXmlInputSource *source = new QXmlInputSource(&opsFile);
158 OpsHandler *opsHandler = new OpsHandler(*this);
159 XmlErrorHandler *errorHandler = new XmlErrorHandler();
160 reader.setContentHandler(opsHandler);
161 reader.setErrorHandler(errorHandler);
162 ret = reader.parse(source);
167 // Initially, put all content items in the chapter list.
168 // This will be refined by parsing the NCX file later
172 QStringList coverKeys;
173 coverKeys << "cover-image" << "img-cover-jpeg" << "cover";
174 foreach (QString key, coverKeys) {
175 if (content.contains(key)) {
176 t.trace("Loading cover image from " + content[key].href);
177 cover = makeCover(content[key].href);
182 // If there is an "ncx" item in content, parse it: That's the real table of
184 if (content.contains("ncx")) {
185 QString ncxFileName = content["ncx"].href;
186 t.trace("Parsing NCX file " + ncxFileName);
187 QFile ncxFile(ncxFileName);
188 source = new QXmlInputSource(&ncxFile);
189 NcxHandler *ncxHandler = new NcxHandler(*this);
190 errorHandler = new XmlErrorHandler();
191 reader.setContentHandler(ncxHandler);
192 reader.setErrorHandler(errorHandler);
193 ret = reader.parse(source);
199 // Calculate book part sizes
201 foreach (QString part, parts) {
202 QFileInfo info(content[part].href);
203 content[part].size = info.size();
204 size += content[part].size;
205 t.trace(QString("Size of part %1: %2").arg(part).arg(content[part].size));
211 bool Book::clearDir(const QString &dir)
217 QDirIterator i(dir, QDirIterator::Subdirectories);
218 while (i.hasNext()) {
219 QString entry = i.next();
220 if (entry.endsWith("/.") || entry.endsWith("/..")) {
223 QFileInfo info(entry);
225 if (!clearDir(entry)) {
230 if (!QFile::remove(entry)) {
231 qCritical() << "Book::clearDir: Could not remove" << entry;
232 // FIXME: To be investigated: This is happening too often
256 Trace t("Book::load");
257 t.trace("path: " + path());
259 QString key = "book/" + path() + "/";
260 t.trace("key: " + key);
263 title = settings.value(key + "title").toString();
265 creators = settings.value(key + "creators").toStringList();
266 date = settings.value(key + "date").toString();
267 publisher = settings.value(key + "publisher").toString();
268 datePublished = settings.value(key + "datepublished").toString();
269 subject = settings.value(key + "subject").toString();
270 source = settings.value(key + "source").toString();
271 rights = settings.value(key + "rights").toString();
272 mLastBookmark.part = settings.value(key + "lastpart").toInt();
273 mLastBookmark.pos = settings.value(key + "lastpos").toReal();
274 cover = settings.value(key + "cover").value<QImage>().scaled(COVER_WIDTH,
275 COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
276 if (cover.isNull()) {
277 cover = makeCover(":/icons/book.png");
281 int size = settings.value(key + "bookmarks").toInt();
282 for (int i = 0; i < size; i++) {
283 int part = settings.value(key + "bookmark" + QString::number(i) +
285 qreal pos = settings.value(key + "bookmark" + QString::number(i) +
287 t.trace(QString("Bookmark %1 at part %2, %3").
288 arg(i).arg(part).arg(pos));
289 mBookmarks.append(Bookmark(part, pos));
295 Trace t("Book::save");
297 QString key = "book/" + path() + "/";
298 t.trace("key: " + key);
301 settings.setValue(key + "title", title);
302 t.trace("title: " + title);
303 settings.setValue(key + "creators", creators);
304 settings.setValue(key + "date", date);
305 settings.setValue(key + "publisher", publisher);
306 settings.setValue(key + "datepublished", datePublished);
307 settings.setValue(key + "subject", subject);
308 settings.setValue(key + "source", source);
309 settings.setValue(key + "rights", rights);
310 settings.setValue(key + "lastpart", mLastBookmark.part);
311 settings.setValue(key + "lastpos", mLastBookmark.pos);
312 settings.setValue(key + "cover", cover);
315 settings.setValue(key + "bookmarks", mBookmarks.size());
316 for (int i = 0; i < mBookmarks.size(); i++) {
317 t.trace(QString("Bookmark %1 at %2, %3").
318 arg(i).arg(mBookmarks[i].part).arg(mBookmarks[i].pos));
319 settings.setValue(key + "bookmark" + QString::number(i) + "/part",
321 settings.setValue(key + "bookmark" + QString::number(i) + "/pos",
326 void Book::setLastBookmark(int part, qreal position)
328 mLastBookmark.part = part;
329 mLastBookmark.pos = position;
333 Book::Bookmark Book::lastBookmark() const
335 return Book::Bookmark(mLastBookmark);
338 void Book::addBookmark(int part, qreal position)
340 mBookmarks.append(Bookmark(part, position));
341 qSort(mBookmarks.begin(), mBookmarks.end());
345 void Book::deleteBookmark(int index)
347 mBookmarks.removeAt(index);
351 QList<Book::Bookmark> Book::bookmarks() const
356 QString Book::opsPath()
358 Trace t("Book::opsPath");
361 QFile container(tmpDir() + "/META-INF/container.xml");
362 t.trace(container.fileName());
363 QXmlSimpleReader reader;
364 QXmlInputSource *source = new QXmlInputSource(&container);
365 ContainerHandler *containerHandler = new ContainerHandler();
366 XmlErrorHandler *errorHandler = new XmlErrorHandler();
367 reader.setContentHandler(containerHandler);
368 reader.setErrorHandler(errorHandler);
369 if (reader.parse(source)) {
370 ret = tmpDir() + "/" + containerHandler->rootFile;
371 mRootPath = QFileInfo(ret).absoluteDir().absolutePath();
372 t.trace("OSP path: " + ret);
373 t.trace("Root dir: " + mRootPath);
376 delete containerHandler;
381 QString Book::rootPath() const
386 QString Book::name() const
390 if (creators.length()) {
391 ret += "\nBy " + creators[0];
392 for (int i = 1; i < creators.length(); i++) {
393 ret += ", " + creators[i];
402 QString Book::shortName() const
404 return (title.isEmpty())? QFileInfo(path()).baseName(): title;
407 int Book::chapterFromPart(int index)
415 int Book::partFromChapter(int index)
417 Trace t("Book::partFromChapter");
418 QString id = chapters[index];
419 QString href = content[id].href;
420 QString baseRef(href);
421 QUrl url(QString("file://") + href);
422 if (url.hasFragment()) {
423 QString fragment = url.fragment();
424 baseRef.chop(fragment.length() + 1);
427 // Swipe through all content items to find the one matching the chapter href
428 // FIXME: Do we need to index content items by href, too?
431 foreach (contentKey, content.keys()) {
432 if (contentKey == id) {
435 if (content[contentKey].href == baseRef) {
437 t.trace(QString("Key for %1 is %2").arg(baseRef).arg(contentKey));
442 t.trace("Could not find key for " + baseRef);
445 int partIndex = parts.indexOf(contentKey);
446 if (partIndex == -1) {
448 << "Book::partFromChapter: Could not find part index of chapter"
454 qreal Book::getProgress(int part, qreal position)
456 Q_ASSERT(part < parts.size());
459 for (int i = 0; i < part; i++) {
461 partSize += content[key].size;
464 partSize += content[key].size * position;
465 return partSize / (qreal)size;
468 bool Book::extractMetaData()