From 71657b9e41d60fbe03886508e12ad84de2be35bc Mon Sep 17 00:00:00 2001 From: Ashish Kulkarni Date: Sat, 3 Jan 2015 17:39:34 +0530 Subject: [PATCH] fix handling of temporary files during PDF conversion via API The TempFile class was able to handle only one file, but was used in scenarios where multiple temp files were required. Change the implementation and API to support this usage scenario, which also fixes #1790. Ideally, one should do away with this and use an in-memory QtWebKit protocol [1] e.g. mem://uuid.html but that is something to be taken up after refactoring of the current codebase. [1] http://doc.qt.digia.com/qq/32/qq32-webkit-protocols.html --- CHANGELOG.md | 1 + src/lib/multipageloader.cc | 2 +- src/lib/pdfconverter.cc | 2 +- src/lib/pdfconverter_p.hh | 4 +--- src/lib/tempfile.cc | 16 ++++++++-------- src/lib/tempfile.hh | 6 +++--- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78b52b437..7430fa028 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ v0.12.2 (unreleased) * **#1722**: **[qt]** fix broken hyphenation with soft-hyphens * **#1769**: fixed unicode URLs in links * **#1772**: added variable 'isodate' for substitution in headers/footers +* **#1790**: fix handling of temporary files during PDF conversion via API * **#1808**: fix [sitepage] and [sitepages] not working without HTML headers/footers * **#1825**: fix handling of non-ASCII characters in command-line arguments * **#1863**: **[qt]** blank page or crash with low DPI on Windows diff --git a/src/lib/multipageloader.cc b/src/lib/multipageloader.cc index 324ce4b3b..0f7c6345a 100644 --- a/src/lib/multipageloader.cc +++ b/src/lib/multipageloader.cc @@ -550,7 +550,7 @@ void MultiPageLoaderPrivate::clearResources() { ResourceObject *tmp = resources.takeFirst(); tmp->deleteLater(); } - tempIn.remove(); + tempIn.removeAll(); } void MultiPageLoaderPrivate::cancel() { diff --git a/src/lib/pdfconverter.cc b/src/lib/pdfconverter.cc index 8c3d418b8..78d52bc82 100644 --- a/src/lib/pdfconverter.cc +++ b/src/lib/pdfconverter.cc @@ -487,7 +487,7 @@ void PdfConverterPrivate::loadTocs() { QString style = ps.tocXsl; if (style.isEmpty()) { - style = obj.tocStyleFile.create(".xsl"); + style = obj.tocFile.create(".xsl"); StreamDumper styleDump(style); dumpDefaultTOCStyleSheet(styleDump.stream, ps.toc); } diff --git a/src/lib/pdfconverter_p.hh b/src/lib/pdfconverter_p.hh index 829e4a519..a845afc45 100644 --- a/src/lib/pdfconverter_p.hh +++ b/src/lib/pdfconverter_p.hh @@ -71,7 +71,6 @@ public: QList headers; QList footers; int pageCount; - TempFile tocStyleFile; TempFile tocFile; void clear() { @@ -84,8 +83,7 @@ public: footers.clear(); webPageToObject.remove(page); page=0; - tocStyleFile.remove(); - tocFile.remove(); + tocFile.removeAll(); } PageObject(const settings::PdfObject & set, const QString * d=NULL): diff --git a/src/lib/tempfile.cc b/src/lib/tempfile.cc index a4dd74bf2..27d52e7e0 100644 --- a/src/lib/tempfile.cc +++ b/src/lib/tempfile.cc @@ -39,25 +39,25 @@ TempFile::TempFile() { } TempFile::~TempFile() { - remove(); + removeAll(); } /*! - \brief Create a new temporary file, deleting the old if one exists + \brief Create a new temporary file \param ext The extention of the temporary file \returns Path of the new temporary file */ QString TempFile::create(const QString & ext) { - remove(); - path = QDir::tempPath()+"/wktemp-"+QUuid::createUuid().toString().mid(1,36)+ext; + QString path = QDir::tempPath()+"/wktemp-"+QUuid::createUuid().toString().mid(1,36)+ext; + paths.append(path); return path; } /*! - \brief Remove the temporary file hold by this object it it exists + \brief Remove all the temporary files held by this object */ -void TempFile::remove() { - if (!path.isEmpty()) +void TempFile::removeAll() { + foreach (const QString &path, paths) QFile::remove(path); - path=""; + paths.clear(); } diff --git a/src/lib/tempfile.hh b/src/lib/tempfile.hh index eeb6482c7..e012d458b 100644 --- a/src/lib/tempfile.hh +++ b/src/lib/tempfile.hh @@ -21,18 +21,18 @@ #ifndef __TEMPFILE_HH__ #define __TEMPFILE_HH__ -#include +#include #include "dllbegin.inc" class DLL_LOCAL TempFile { private: - QString path; + QStringList paths; public: TempFile(); ~TempFile(); QString create(const QString & ext); - void remove(); + void removeAll(); }; #include "dllend.inc"