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 qDebug() << "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 qDebug() << "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 qDebug() << "Loading cover image from" << content[key].href;
177 cover = makeCover(rootPath() + "/" + content[key].href);
182 // If there is an "ncx" item in content, parse it: That's the real table of
185 if (content.contains("ncx")) {
186 ncxFileName = content["ncx"].href;
187 } else if (content.contains("ncxtoc")) {
188 ncxFileName = content["ncxtoc"].href;
190 qDebug() << "No NCX table of contents";
192 if (!ncxFileName.isEmpty()) {
193 qDebug() << "Parsing NCX file" << ncxFileName;
194 QFile ncxFile(rootPath() + "/" + ncxFileName);
195 source = new QXmlInputSource(&ncxFile);
196 NcxHandler *ncxHandler = new NcxHandler(*this);
197 errorHandler = new XmlErrorHandler();
198 reader.setContentHandler(ncxHandler);
199 reader.setErrorHandler(errorHandler);
200 ret = reader.parse(source);
206 // Calculate book part sizes
208 foreach (QString part, parts) {
209 QFileInfo info(content[part].href);
210 content[part].size = info.size();
211 size += content[part].size;
217 bool Book::clearDir(const QString &dir)
223 QDirIterator i(dir, QDirIterator::Subdirectories);
224 while (i.hasNext()) {
225 QString entry = i.next();
226 if (entry.endsWith("/.") || entry.endsWith("/..")) {
229 QFileInfo info(entry);
231 if (!clearDir(entry)) {
236 if (!QFile::remove(entry)) {
237 qCritical() << "Book::clearDir: Could not remove" << entry;
238 // FIXME: To be investigated: This is happening too often
262 Trace t("Book::load");
263 qDebug() << "path" << path();
265 QString key = "book/" + path() + "/";
266 qDebug() << "key" << key;
269 title = settings.value(key + "title").toString();
271 creators = settings.value(key + "creators").toStringList();
272 date = settings.value(key + "date").toString();
273 publisher = settings.value(key + "publisher").toString();
274 datePublished = settings.value(key + "datepublished").toString();
275 subject = settings.value(key + "subject").toString();
276 source = settings.value(key + "source").toString();
277 rights = settings.value(key + "rights").toString();
278 mLastBookmark.part = settings.value(key + "lastpart").toInt();
279 mLastBookmark.pos = settings.value(key + "lastpos").toReal();
280 cover = settings.value(key + "cover").value<QImage>().scaled(COVER_WIDTH,
281 COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
282 if (cover.isNull()) {
283 cover = makeCover(":/icons/book.png");
287 int size = settings.value(key + "bookmarks").toInt();
288 for (int i = 0; i < size; i++) {
289 int part = settings.value(key + "bookmark" + QString::number(i) +
291 qreal pos = settings.value(key + "bookmark" + QString::number(i) +
293 qDebug() << QString("Bookmark %1 at part %2, %3").
294 arg(i).arg(part).arg(pos);
295 mBookmarks.append(Bookmark(part, pos));
301 Trace t("Book::save");
303 QString key = "book/" + path() + "/";
304 qDebug() << "key" << key;
307 settings.setValue(key + "title", title);
308 qDebug() << "title" << title;
309 settings.setValue(key + "creators", creators);
310 settings.setValue(key + "date", date);
311 settings.setValue(key + "publisher", publisher);
312 settings.setValue(key + "datepublished", datePublished);
313 settings.setValue(key + "subject", subject);
314 settings.setValue(key + "source", source);
315 settings.setValue(key + "rights", rights);
316 settings.setValue(key + "lastpart", mLastBookmark.part);
317 settings.setValue(key + "lastpos", mLastBookmark.pos);
318 settings.setValue(key + "cover", cover);
321 settings.setValue(key + "bookmarks", mBookmarks.size());
322 for (int i = 0; i < mBookmarks.size(); i++) {
323 qDebug() << QString("Bookmark %1 at %2, %3").
324 arg(i).arg(mBookmarks[i].part).arg(mBookmarks[i].pos);
325 settings.setValue(key + "bookmark" + QString::number(i) + "/part",
327 settings.setValue(key + "bookmark" + QString::number(i) + "/pos",
332 void Book::setLastBookmark(int part, qreal position)
334 mLastBookmark.part = part;
335 mLastBookmark.pos = position;
339 Book::Bookmark Book::lastBookmark() const
341 return Book::Bookmark(mLastBookmark);
344 void Book::addBookmark(int part, qreal position)
346 mBookmarks.append(Bookmark(part, position));
347 qSort(mBookmarks.begin(), mBookmarks.end());
351 void Book::deleteBookmark(int index)
353 mBookmarks.removeAt(index);
357 QList<Book::Bookmark> Book::bookmarks() const
362 QString Book::opsPath()
364 Trace t("Book::opsPath");
367 QFile container(tmpDir() + "/META-INF/container.xml");
368 qDebug() << container.fileName();
369 QXmlSimpleReader reader;
370 QXmlInputSource *source = new QXmlInputSource(&container);
371 ContainerHandler *containerHandler = new ContainerHandler();
372 XmlErrorHandler *errorHandler = new XmlErrorHandler();
373 reader.setContentHandler(containerHandler);
374 reader.setErrorHandler(errorHandler);
375 if (reader.parse(source)) {
376 ret = tmpDir() + "/" + containerHandler->rootFile;
377 mRootPath = QFileInfo(ret).absoluteDir().absolutePath();
378 qDebug() << "OSP path" << ret << "\nRoot dir" << mRootPath;
381 delete containerHandler;
386 QString Book::rootPath() const
391 QString Book::name() const
395 if (creators.length()) {
396 ret += "\nBy " + creators[0];
397 for (int i = 1; i < creators.length(); i++) {
398 ret += ", " + creators[i];
407 QString Book::shortName() const
409 return (title.isEmpty())? QFileInfo(path()).baseName(): title;
412 int Book::chapterFromPart(int index)
416 QString partId = parts[index];
417 QString partHref = content[partId].href;
419 for (int i = 0; i < chapters.size(); i++) {
420 QString id = chapters[i];
421 QString href = content[id].href;
422 QString baseRef(href);
423 QUrl url(QString("file://") + href);
424 if (url.hasFragment()) {
425 QString fragment = url.fragment();
426 baseRef.chop(fragment.length() + 1);
428 if (baseRef == partHref) {
430 // Don't break, keep looking
437 int Book::partFromChapter(int index)
439 Trace t("Book::partFromChapter");
440 QString id = chapters[index];
441 QString href = content[id].href;
442 QString baseRef(href);
443 QUrl url(QString("file://") + href);
444 if (url.hasFragment()) {
445 QString fragment = url.fragment();
446 baseRef.chop(fragment.length() + 1);
448 qDebug() << "Chapter" << index;
449 qDebug() << " id" << id;
450 qDebug() << " href" << href;
451 qDebug() << " base href" << baseRef;
453 for (int i = 0; i < parts.size(); i++) {
454 QString partId = parts[i];
455 if (content[partId].href == baseRef) {
456 qDebug() << "Part index for" << baseRef << "is" << i;
461 qWarning() << "Book::partFromChapter: Could not find part index for"
466 qreal Book::getProgress(int part, qreal position)
468 Q_ASSERT(part < parts.size());
471 for (int i = 0; i < part; i++) {
473 partSize += content[key].size;
476 partSize += content[key].size * position;
477 return partSize / (qreal)size;
480 bool Book::extractMetaData()