Save document capability

This commit is contained in:
Jakub Melka
2020-06-07 17:46:49 +02:00
parent e6d4eea9a2
commit 4e4bf111da
6 changed files with 118 additions and 19 deletions

View File

@@ -21,6 +21,7 @@
#include "pdfparser.h"
#include <QFile>
#include <QSaveFile>
namespace pdf
{
@@ -170,19 +171,45 @@ void PDFWriteObjectVisitor::visitReference(const PDFObjectReference reference)
m_device->write("R ");
}
PDFOperationResult PDFDocumentWriter::write(const QString& fileName, const PDFDocument* document)
PDFOperationResult PDFDocumentWriter::write(const QString& fileName, const PDFDocument* document, bool safeWrite)
{
QFile file(fileName);
if (file.open(QFile::WriteOnly | QFile::Truncate))
if (safeWrite)
{
PDFOperationResult result = write(&file, document);
file.close();
return result;
QSaveFile file(fileName);
file.setDirectWriteFallback(true);
if (file.open(QFile::WriteOnly | QFile::Truncate))
{
PDFOperationResult result = write(&file, document);
if (result)
{
file.commit();
}
else
{
file.cancelWriting();
}
return result;
}
else
{
return tr("File '%1' can't be opened for writing. %2").arg(fileName, file.errorString());
}
}
else
{
return tr("File '%1' can't be opened for writing. %2").arg(fileName, file.errorString());
QFile file(fileName);
if (file.open(QFile::WriteOnly | QFile::Truncate))
{
PDFOperationResult result = write(&file, document);
file.close();
return result;
}
else
{
return tr("File '%1' can't be opened for writing. %2").arg(fileName, file.errorString());
}
}
}

View File

@@ -40,7 +40,20 @@ public:
}
PDFOperationResult write(const QString& fileName, const PDFDocument* document);
/// Writes document to the file. If \p safeWrite is true, then document is first
/// written to the temporary file, and then renamed to original file name atomically,
/// so no data can be lost on, for example, power failure. If it is not possible to
/// create temporary file, then writing operation will attempt to write to the file
/// directly.
/// \param fileName File name
/// \param document Document
/// \param safeWrite Write document to the temporary file and then rename
PDFOperationResult write(const QString& fileName, const PDFDocument* document, bool safeWrite);
/// Write document to the output device. Device must be writable (i.e. opened
/// for writing).
/// \param device Output device
/// \param document Document
PDFOperationResult write(QIODevice* device, const PDFDocument* document);
/// Calculates document file size, as if it is written to the disk.