1 #include <qtextdocument.h> // Qt::escape is currently defined here...
4 #include "opshandler.h"
5 #include "xmlerrorhandler.h"
6 #include "extractzip.h"
8 #include "containerhandler.h"
9 #include "ncxhandler.h"
13 const int COVER_WIDTH = 53;
14 const int COVER_HEIGHT = 59;
16 static QImage makeCover(const QString &path)
18 return QImage(path).scaled(COVER_WIDTH, COVER_HEIGHT,
19 Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation).
20 scaled(COVER_WIDTH, COVER_HEIGHT, Qt::KeepAspectRatio);
23 Book::Book(const QString &p, QObject *parent): QObject(parent), loaded(false)
28 mPath = info.absoluteFilePath();
29 title = info.baseName();
30 cover = makeCover(":/icons/book.png");
47 if (path().isEmpty()) {
51 if (!extract(QStringList())) {
69 if (path().isEmpty()) {
73 if (!extractMetaData()) {
88 QDir::setCurrent(QDir::rootPath());
92 QString Book::tmpDir() const
94 QString tmpName = QFileInfo(mTempFile.fileName()).fileName();
95 return QDir(QDir::temp().absoluteFilePath("dorian")).
96 absoluteFilePath(tmpName);
99 bool Book::extract(const QStringList &excludedExtensions)
103 QString tmp = tmpDir();
104 qDebug() << "Extracting" << mPath << "to" << tmp;
107 QDir::setCurrent(QDir::rootPath());
108 if (!clearDir(tmp)) {
109 qCritical() << "Book::extract: Failed to remove" << tmp;
114 if (!d.mkpath(tmp)) {
115 qCritical() << "Book::extract: Could not create" << tmp;
120 // If book comes from resource, copy it to the temporary directory first
121 QString bookPath = path();
122 if (bookPath.startsWith(":/books/")) {
124 QString dst(QDir(tmp).absoluteFilePath("book.epub"));
125 if (!src.copy(dst)) {
126 qCritical() << "Book::extract: Failed to copy built-in book"
127 << bookPath << "to" << dst;
133 QString oldDir = QDir::currentPath();
134 if (!QDir::setCurrent(tmp)) {
135 qCritical() << "Book::extract: Could not change to" << tmp;
138 ret = extractZip(bookPath, excludedExtensions);
140 qCritical() << "Book::extract: Extracting ZIP failed";
142 QDir::setCurrent(oldDir);
154 QString opsFileName = opsPath();
155 qDebug() << "Parsing OPS file" << opsFileName;
156 QFile opsFile(opsFileName);
157 QXmlSimpleReader reader;
158 QXmlInputSource *source = new QXmlInputSource(&opsFile);
159 OpsHandler *opsHandler = new OpsHandler(*this);
160 XmlErrorHandler *errorHandler = new XmlErrorHandler();
161 reader.setContentHandler(opsHandler);
162 reader.setErrorHandler(errorHandler);
163 ret = reader.parse(source);
168 // Initially, put all content items in the chapter list.
169 // This will be refined by parsing the NCX file later
174 QStringList coverKeys;
175 coverKeys << "cover-image" << "img-cover-jpeg" << "cover";
176 foreach (QString key, coverKeys) {
177 if (content.contains(key)) {
178 coverPath = QDir(rootPath()).absoluteFilePath(content[key].href);
182 if (coverPath.isEmpty()) {
184 QString coverJpeg = QDir(rootPath()).absoluteFilePath("cover.jpg");
185 if (QFileInfo(coverJpeg).exists()) {
186 coverPath = coverJpeg;
189 if (!coverPath.isEmpty()) {
190 qDebug() << "Loading cover image from" << coverPath;
191 cover = makeCover(coverPath);
194 // If there is an "ncx" item in content, parse it: That's the real table of
197 if (content.contains("ncx")) {
198 ncxFileName = content["ncx"].href;
199 } else if (content.contains("ncxtoc")) {
200 ncxFileName = content["ncxtoc"].href;
201 } else if (content.contains("toc")) {
202 ncxFileName = content["toc"].href;
204 qDebug() << "No NCX table of contents";
206 if (!ncxFileName.isEmpty()) {
207 qDebug() << "Parsing NCX file" << ncxFileName;
208 QFile ncxFile(QDir(rootPath()).absoluteFilePath(ncxFileName));
209 source = new QXmlInputSource(&ncxFile);
210 NcxHandler *ncxHandler = new NcxHandler(*this);
211 errorHandler = new XmlErrorHandler();
212 reader.setContentHandler(ncxHandler);
213 reader.setErrorHandler(errorHandler);
214 ret = reader.parse(source);
220 // Calculate book part sizes
222 foreach (QString part, parts) {
223 QFileInfo info(QDir(rootPath()).absoluteFilePath(content[part].href));
224 content[part].size = info.size();
225 size += content[part].size;
231 bool Book::clearDir(const QString &dir)
237 QDirIterator i(dir, QDirIterator::Subdirectories);
238 while (i.hasNext()) {
239 QString entry = i.next();
240 if (entry.endsWith("/.") || entry.endsWith("/..")) {
243 QFileInfo info(entry);
245 if (!clearDir(entry)) {
250 if (!QFile::remove(entry)) {
251 qCritical() << "Book::clearDir: Could not remove" << entry;
252 // FIXME: To be investigated: This is happening too often
282 qDebug() << "path" << path();
284 QVariantHash data = BookDb::instance()->load(path());
285 title = data["title"].toString();
287 creators = data["creators"].toStringList();
288 date = data["date"].toString();
289 publisher = data["publisher"].toString();
290 datePublished = data["datepublished"].toString();
291 subject = data["subject"].toString();
292 source = data["source"].toString();
293 rights = data["rights"].toString();
294 mLastBookmark.part = data["lastpart"].toInt();
295 mLastBookmark.pos = data["lastpos"].toReal();
296 cover = data["cover"].value<QImage>().scaled(COVER_WIDTH,
297 COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
298 if (cover.isNull()) {
299 cover = makeCover(":/icons/book.png");
301 int size = data["bookmarks"].toInt();
302 for (int i = 0; i < size; i++) {
303 int part = data[QString("bookmark%1part").arg(i)].toInt();
304 qreal pos = data[QString("bookmark%1pos").arg(i)].toReal();
305 QString note = data[QString("bookmark%1note").arg(i)].toString();
306 mBookmarks.append(Bookmark(part, pos, note));
316 data["title"] = title;
317 data["creators"] = creators;
319 data["publisher"] = publisher;
320 data["datepublished"] = datePublished;
321 data["subject"] = subject;
322 data["source"] = source;
323 data["rights"] = rights;
324 data["lastpart"] = mLastBookmark.part;
325 data["lastpos"] = mLastBookmark.pos;
326 data["cover"] = cover;
327 data["bookmarks"] = mBookmarks.size();
328 for (int i = 0; i < mBookmarks.size(); i++) {
329 data[QString("bookmark%1part").arg(i)] = mBookmarks[i].part;
330 data[QString("bookmark%1pos").arg(i)] = mBookmarks[i].pos;
331 data[QString("bookmark%1note").arg(i)] = mBookmarks[i].note;
333 BookDb::instance()->save(path(), data);
336 void Book::setLastBookmark(int part, qreal position)
340 mLastBookmark.part = part;
341 mLastBookmark.pos = position;
345 Book::Bookmark Book::lastBookmark()
348 return Book::Bookmark(mLastBookmark);
351 void Book::addBookmark(int part, qreal position, const QString ¬e)
354 mBookmarks.append(Bookmark(part, position, note));
355 qSort(mBookmarks.begin(), mBookmarks.end());
359 void Book::deleteBookmark(int index)
362 mBookmarks.removeAt(index);
366 QList<Book::Bookmark> Book::bookmarks()
372 QString Book::opsPath()
378 QFile container(tmpDir() + "/META-INF/container.xml");
379 qDebug() << container.fileName();
380 QXmlSimpleReader reader;
381 QXmlInputSource *source = new QXmlInputSource(&container);
382 ContainerHandler *containerHandler = new ContainerHandler();
383 XmlErrorHandler *errorHandler = new XmlErrorHandler();
384 reader.setContentHandler(containerHandler);
385 reader.setErrorHandler(errorHandler);
386 if (reader.parse(source)) {
387 ret = tmpDir() + "/" + containerHandler->rootFile;
388 mRootPath = QFileInfo(ret).absoluteDir().absolutePath();
389 qDebug() << "OSP path" << ret << "\nRoot dir" << mRootPath;
392 delete containerHandler;
397 QString Book::rootPath()
408 if (creators.length()) {
409 ret += "\nBy " + creators[0];
410 for (int i = 1; i < creators.length(); i++) {
411 ret += ", " + creators[i];
420 QString Book::shortName()
423 return (title.isEmpty())? QFileInfo(path()).baseName(): title;
426 int Book::chapterFromPart(int index)
432 QString partId = parts[index];
433 QString partHref = content[partId].href;
435 for (int i = 0; i < chapters.size(); i++) {
436 QString id = chapters[i];
437 QString href = content[id].href;
438 QString baseRef(href);
439 QUrl url(QString("file://") + href);
440 if (url.hasFragment()) {
441 QString fragment = url.fragment();
442 baseRef.chop(fragment.length() + 1);
444 if (baseRef == partHref) {
446 // Don't break, keep looking
453 int Book::partFromChapter(int index, QString &fragment)
458 QString id = chapters[index];
459 QString href = content[id].href;
460 int hashPos = href.indexOf("#");
462 fragment = href.mid(hashPos);
463 href = href.left(hashPos);
466 qDebug() << "Chapter" << index;
467 qDebug() << " id" << id;
468 qDebug() << " href" << href;
469 qDebug() << " fragment" << fragment;
471 for (int i = 0; i < parts.size(); i++) {
472 QString partId = parts[i];
473 if (content[partId].href == href) {
474 qDebug() << "Part index for" << href << "is" << i;
479 qWarning() << "Book::partFromChapter: Could not find part index for"
484 qreal Book::getProgress(int part, qreal position)
487 Q_ASSERT(part < parts.size());
490 for (int i = 0; i < part; i++) {
492 partSize += content[key].size;
495 partSize += content[key].size * position;
496 return partSize / (qreal)size;
499 bool Book::extractMetaData()
501 QStringList excludedExtensions;
502 excludedExtensions << ".html" << ".xhtml" << ".xht" << ".htm" << ".gif"
504 return extract(excludedExtensions);
511 // Load book from old database (QSettings)
514 QString key = "book/" + path() + "/";
515 title = settings.value(key + "title").toString();
517 creators = settings.value(key + "creators").toStringList();
518 date = settings.value(key + "date").toString();
519 publisher = settings.value(key + "publisher").toString();
520 datePublished = settings.value(key + "datepublished").toString();
521 subject = settings.value(key + "subject").toString();
522 source = settings.value(key + "source").toString();
523 rights = settings.value(key + "rights").toString();
524 mLastBookmark.part = settings.value(key + "lastpart").toInt();
525 mLastBookmark.pos = settings.value(key + "lastpos").toReal();
526 cover = settings.value(key + "cover").value<QImage>().scaled(COVER_WIDTH,
527 COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
528 if (cover.isNull()) {
529 cover = makeCover(":/icons/book.png");
531 int size = settings.value(key + "bookmarks").toInt();
532 for (int i = 0; i < size; i++) {
533 int part = settings.value(key + "bookmark" + QString::number(i) +
535 qreal pos = settings.value(key + "bookmark" + QString::number(i) +
537 qDebug() << QString("Bookmark %1 at part %2, %3").
538 arg(i).arg(part).arg(pos);
539 mBookmarks.append(Bookmark(part, pos));
542 // Save book to new database
551 BookDb::instance()->remove(path());