From edf764215181ecb9918bae9e5bc88371def0070a Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sun, 26 Feb 2023 15:54:19 +0100 Subject: [PATCH] Issue #40: Finish --- Pdf4QtLib/sources/pdfdocumentsanitizer.cpp | 30 ++++++++++++++++++++++ Pdf4QtViewer/pdfprogramcontroller.cpp | 13 +++++++++- Pdf4QtViewer/pdfprogramcontroller.h | 1 + 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Pdf4QtLib/sources/pdfdocumentsanitizer.cpp b/Pdf4QtLib/sources/pdfdocumentsanitizer.cpp index e3be937..a6d4b74 100644 --- a/Pdf4QtLib/sources/pdfdocumentsanitizer.cpp +++ b/Pdf4QtLib/sources/pdfdocumentsanitizer.cpp @@ -186,6 +186,36 @@ void PDFDocumentSanitizer::performSanitizeFileAttachments() return annotation->getType() == AnnotationType::FileAttachment; }; removeAnnotations(filter, tr("File attachments removed: %1.")); + + // Remove files in name tree + PDFDocumentBuilder builder(m_storage, PDFVersion(2, 0)); + PDFObject catalogObject = builder.getObjectByReference(builder.getCatalogReference()); + const PDFDictionary* catalogDictionary = builder.getDictionaryFromObject(catalogObject); + const bool hasNames = catalogDictionary && catalogDictionary->hasKey("Names"); + + if (hasNames) + { + PDFObject namesObject = builder.getObject(catalogDictionary->get("Names")); + const PDFDictionary* namesDictionary = builder.getDictionaryFromObject(namesObject); + if (namesDictionary->hasKey("EmbeddedFiles")) + { + PDFDictionary dictionaryCopy = *namesDictionary; + dictionaryCopy.setEntry(PDFInplaceOrMemoryString("EmbeddedFiles"), PDFObject()); + namesObject = PDFObject::createDictionary(std::make_shared(qMove(dictionaryCopy))); + + PDFObjectFactory factory; + factory.beginDictionary(); + factory.beginDictionaryItem("Names"); + factory << namesObject; + factory.endDictionaryItem(); + factory.endDictionary(); + PDFObject newCatalog = factory.takeObject(); + builder.mergeTo(builder.getCatalogReference(), std::move(newCatalog)); + PDFDocument document = builder.build(); + m_storage = document.getStorage(); + Q_EMIT sanitizationProgress(tr("Embedded files were removed.")); + } + } } void PDFDocumentSanitizer::performSanitizeEmbeddedSearchIndex() diff --git a/Pdf4QtViewer/pdfprogramcontroller.cpp b/Pdf4QtViewer/pdfprogramcontroller.cpp index 20560fc..ef0ef07 100644 --- a/Pdf4QtViewer/pdfprogramcontroller.cpp +++ b/Pdf4QtViewer/pdfprogramcontroller.cpp @@ -320,6 +320,7 @@ PDFProgramController::PDFProgramController(QObject* parent) : m_recentFileManager(new PDFRecentFileManager(this)), m_optionalContentActivity(nullptr), m_textToSpeech(nullptr), + m_isDocumentSetInProgress(false), m_futureWatcher(nullptr), m_CMSManager(new pdf::PDFCMSManager(this)), m_toolManager(nullptr), @@ -1670,6 +1671,7 @@ void PDFProgramController::onDocumentModified(pdf::PDFModifiedDocument document) // We will create undo/redo step from old document, with flags from the new, // because new document is modification of old document with flags. + pdf::PDFBoolGuard guard(m_isDocumentSetInProgress); Q_ASSERT(m_pdfDocument); if (m_undoRedoManager) @@ -1677,6 +1679,12 @@ void PDFProgramController::onDocumentModified(pdf::PDFModifiedDocument document) m_undoRedoManager->createUndo(document, m_pdfDocument); } + // Retain pointer on old document, because during the update, + // old pointer must be valid, because some widgets holds raw + // pointer. + pdf::PDFDocumentPointer oldDocument = std::move(m_pdfDocument); + Q_UNUSED(oldDocument); + m_pdfDocument = document; document.setOptionalContentActivity(m_optionalContentActivity); setDocument(document); @@ -2015,7 +2023,10 @@ void PDFProgramController::onActionCertificateManagerTriggered() void PDFProgramController::onDrawSpaceChanged() { - m_mainWindowInterface->updateUI(false); + if (!m_isDocumentSetInProgress) + { + m_mainWindowInterface->updateUI(false); + } } void PDFProgramController::onPageLayoutChanged() diff --git a/Pdf4QtViewer/pdfprogramcontroller.h b/Pdf4QtViewer/pdfprogramcontroller.h index 9572ce1..b99ff3b 100644 --- a/Pdf4QtViewer/pdfprogramcontroller.h +++ b/Pdf4QtViewer/pdfprogramcontroller.h @@ -394,6 +394,7 @@ private: pdf::PDFOptionalContentActivity* m_optionalContentActivity; pdf::PDFDocumentPointer m_pdfDocument; PDFTextToSpeech* m_textToSpeech; + bool m_isDocumentSetInProgress; QFuture m_future; QFutureWatcher* m_futureWatcher;