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(QDir::temp().absoluteFilePath("dorian")).
101 absoluteFilePath(tmpName);
106 Trace t("Book::extract");
108 QString tmp = tmpDir();
109 qDebug() << "Extracting" << mPath << "to" << tmp;
111 QDir::setCurrent(QDir::rootPath());
112 if (!clearDir(tmp)) {
113 qCritical() << "Book::extract: Failed to remove" << tmp;
118 if (!d.mkpath(tmp)) {
119 qCritical() << "Book::extract: Could not create" << tmp;
124 // If book comes from resource, copy it to the temporary directory first
125 QString bookPath = path();
126 if (bookPath.startsWith(":/books/")) {
128 QString dst(QDir(tmp).absoluteFilePath("book.epub"));
129 if (!src.copy(dst)) {
130 qCritical() << "Book::extract: Failed to copy built-in book"
131 << bookPath << "to" << dst;
137 QString oldDir = QDir::currentPath();
138 if (!QDir::setCurrent(tmp)) {
139 qCritical() << "Book::extract: Could not change to" << tmp;
142 ret = extractZip(bookPath);
144 qCritical() << "Book::extract: Extracting ZIP failed";
146 QDir::setCurrent(oldDir);
152 Trace t("Book::parse");
156 QString opsFileName = opsPath();
157 qDebug() << "Parsing OPS file" << opsFileName;
158 QFile opsFile(opsFileName);
159 QXmlSimpleReader reader;
160 QXmlInputSource *source = new QXmlInputSource(&opsFile);
161 OpsHandler *opsHandler = new OpsHandler(*this);
162 XmlErrorHandler *errorHandler = new XmlErrorHandler();
163 reader.setContentHandler(opsHandler);
164 reader.setErrorHandler(errorHandler);
165 ret = reader.parse(source);
170 // Initially, put all content items in the chapter list.
171 // This will be refined by parsing the NCX file later
175 QStringList coverKeys;
176 coverKeys << "cover-image" << "img-cover-jpeg" << "cover";
177 foreach (QString key, coverKeys) {
178 if (content.contains(key)) {
179 qDebug() << "Loading cover image from" << content[key].href;
180 cover = makeCover(rootPath() + "/" + content[key].href);
185 // If there is an "ncx" item in content, parse it: That's the real table of
188 if (content.contains("ncx")) {
189 ncxFileName = content["ncx"].href;
190 } else if (content.contains("ncxtoc")) {
191 ncxFileName = content["ncxtoc"].href;
192 } else if (content.contains("toc")) {
193 ncxFileName = content["toc"].href;
195 qDebug() << "No NCX table of contents";
197 if (!ncxFileName.isEmpty()) {
198 qDebug() << "Parsing NCX file" << ncxFileName;
199 QFile ncxFile(rootPath() + "/" + ncxFileName);
200 source = new QXmlInputSource(&ncxFile);
201 NcxHandler *ncxHandler = new NcxHandler(*this);
202 errorHandler = new XmlErrorHandler();
203 reader.setContentHandler(ncxHandler);
204 reader.setErrorHandler(errorHandler);
205 ret = reader.parse(source);
211 // Calculate book part sizes
213 foreach (QString part, parts) {
214 QFileInfo info(content[part].href);
215 content[part].size = info.size();
216 size += content[part].size;
222 bool Book::clearDir(const QString &dir)
228 QDirIterator i(dir, QDirIterator::Subdirectories);
229 while (i.hasNext()) {
230 QString entry = i.next();
231 if (entry.endsWith("/.") || entry.endsWith("/..")) {
234 QFileInfo info(entry);
236 if (!clearDir(entry)) {
241 if (!QFile::remove(entry)) {
242 qCritical() << "Book::clearDir: Could not remove" << entry;
243 // FIXME: To be investigated: This is happening too often
267 Trace t("Book::load");
268 qDebug() << "path" << path();
270 QString key = "book/" + path() + "/";
271 qDebug() << "key" << key;
274 title = settings.value(key + "title").toString();
276 creators = settings.value(key + "creators").toStringList();
277 date = settings.value(key + "date").toString();
278 publisher = settings.value(key + "publisher").toString();
279 datePublished = settings.value(key + "datepublished").toString();
280 subject = settings.value(key + "subject").toString();
281 source = settings.value(key + "source").toString();
282 rights = settings.value(key + "rights").toString();
283 mLastBookmark.part = settings.value(key + "lastpart").toInt();
284 mLastBookmark.pos = settings.value(key + "lastpos").toReal();
285 cover = settings.value(key + "cover").value<QImage>().scaled(COVER_WIDTH,
286 COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
287 if (cover.isNull()) {
288 cover = makeCover(":/icons/book.png");
292 int size = settings.value(key + "bookmarks").toInt();
293 for (int i = 0; i < size; i++) {
294 int part = settings.value(key + "bookmark" + QString::number(i) +
296 qreal pos = settings.value(key + "bookmark" + QString::number(i) +
298 qDebug() << QString("Bookmark %1 at part %2, %3").
299 arg(i).arg(part).arg(pos);
300 mBookmarks.append(Bookmark(part, pos));
306 Trace t("Book::save");
308 QString key = "book/" + path() + "/";
309 qDebug() << "key" << key;
312 settings.setValue(key + "title", title);
313 qDebug() << "title" << title;
314 settings.setValue(key + "creators", creators);
315 settings.setValue(key + "date", date);
316 settings.setValue(key + "publisher", publisher);
317 settings.setValue(key + "datepublished", datePublished);
318 settings.setValue(key + "subject", subject);
319 settings.setValue(key + "source", source);
320 settings.setValue(key + "rights", rights);
321 settings.setValue(key + "lastpart", mLastBookmark.part);
322 settings.setValue(key + "lastpos", mLastBookmark.pos);
323 settings.setValue(key + "cover", cover);
326 settings.setValue(key + "bookmarks", mBookmarks.size());
327 for (int i = 0; i < mBookmarks.size(); i++) {
328 qDebug() << QString("Bookmark %1 at %2, %3").
329 arg(i).arg(mBookmarks[i].part).arg(mBookmarks[i].pos);
330 settings.setValue(key + "bookmark" + QString::number(i) + "/part",
332 settings.setValue(key + "bookmark" + QString::number(i) + "/pos",
337 void Book::setLastBookmark(int part, qreal position)
339 mLastBookmark.part = part;
340 mLastBookmark.pos = position;
344 Book::Bookmark Book::lastBookmark() const
346 return Book::Bookmark(mLastBookmark);
349 void Book::addBookmark(int part, qreal position)
351 mBookmarks.append(Bookmark(part, position));
352 qSort(mBookmarks.begin(), mBookmarks.end());
356 void Book::deleteBookmark(int index)
358 mBookmarks.removeAt(index);
362 QList<Book::Bookmark> Book::bookmarks() const
367 QString Book::opsPath()
369 Trace t("Book::opsPath");
372 QFile container(tmpDir() + "/META-INF/container.xml");
373 qDebug() << container.fileName();
374 QXmlSimpleReader reader;
375 QXmlInputSource *source = new QXmlInputSource(&container);
376 ContainerHandler *containerHandler = new ContainerHandler();
377 XmlErrorHandler *errorHandler = new XmlErrorHandler();
378 reader.setContentHandler(containerHandler);
379 reader.setErrorHandler(errorHandler);
380 if (reader.parse(source)) {
381 ret = tmpDir() + "/" + containerHandler->rootFile;
382 mRootPath = QFileInfo(ret).absoluteDir().absolutePath();
383 qDebug() << "OSP path" << ret << "\nRoot dir" << mRootPath;
386 delete containerHandler;
391 QString Book::rootPath() const
396 QString Book::name() const
400 if (creators.length()) {
401 ret += "\nBy " + creators[0];
402 for (int i = 1; i < creators.length(); i++) {
403 ret += ", " + creators[i];
412 QString Book::shortName() const
414 return (title.isEmpty())? QFileInfo(path()).baseName(): title;
417 int Book::chapterFromPart(int index)
421 QString partId = parts[index];
422 QString partHref = content[partId].href;
424 for (int i = 0; i < chapters.size(); i++) {
425 QString id = chapters[i];
426 QString href = content[id].href;
427 QString baseRef(href);
428 QUrl url(QString("file://") + href);
429 if (url.hasFragment()) {
430 QString fragment = url.fragment();
431 baseRef.chop(fragment.length() + 1);
433 if (baseRef == partHref) {
435 // Don't break, keep looking
442 int Book::partFromChapter(int index)
444 Trace t("Book::partFromChapter");
445 QString id = chapters[index];
446 QString href = content[id].href;
447 QString baseRef(href);
448 QUrl url(QString("file://") + href);
449 if (url.hasFragment()) {
450 QString fragment = url.fragment();
451 baseRef.chop(fragment.length() + 1);
453 qDebug() << "Chapter" << index;
454 qDebug() << " id" << id;
455 qDebug() << " href" << href;
456 qDebug() << " base href" << baseRef;
458 for (int i = 0; i < parts.size(); i++) {
459 QString partId = parts[i];
460 if (content[partId].href == baseRef) {
461 qDebug() << "Part index for" << baseRef << "is" << i;
466 qWarning() << "Book::partFromChapter: Could not find part index for"
471 qreal Book::getProgress(int part, qreal position)
473 Q_ASSERT(part < parts.size());
476 for (int i = 0; i < part; i++) {
478 partSize += content[key].size;
481 partSize += content[key].size * position;
482 return partSize / (qreal)size;
485 bool Book::extractMetaData()