[plasma/spectacle] src: Sync data to clipboard when saving

Cezar Craciunoiu <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit c194fcc64264a9a3edf07542956e64dd2db03de7 by Cezar Craciunoiu.
Committed on 25/07/2026 at 09:04.
Pushed by ndavis into branch 'master'.

Sync data to clipboard when saving

Ensure that data is completely flushed before other apps can access it.
To do this, use QSaveFile, and commit the output when fully written,
also ensuring to flush it.

This fix solves the the case where a big screenshot would be pasted
partially empty resulting in corrupted images, if not waiting enough.

Signed-off-by: Cezar Craciunoiu <[email protected]>

M  +11   -6    src/ExportManager.cpp

https://invent.kde.org/plasma/spectacle/-/commit/c194fcc64264a9a3edf07542956e64dd2db03de7

diff --git a/src/ExportManager.cpp b/src/ExportManager.cpp
index 840c2a3fe..3e5602cbc 100644
--- a/src/ExportManager.cpp
+++ b/src/ExportManager.cpp
@@ -24,6 +24,7 @@
 #include <QRandomGenerator>
 #include <QRegularExpression>
 #include <QRegularExpressionMatch>
+#include <QSaveFile>
 #include <QString>
 #include <QTemporaryDir>
 #include <QTemporaryFile>
@@ -484,10 +485,13 @@ bool ExportManager::localSave(const QUrl &url, const QString &suffix, QByteArray
         return false;
     }
 
-    QFile outputFile(url.toLocalFile());
+    QSaveFile outputFile(url.toLocalFile());
 
-    outputFile.open(QFile::WriteOnly);
-    if (!writeImage(&outputFile, suffix.toLatin1(), encodedImage)) {
+    if (!outputFile.open(QFile::WriteOnly)) {
+        Q_EMIT errorMessage(i18n("Cannot save screenshot. Error while opening file."));
+        return false;
+    }
+    if (!writeImage(&outputFile, suffix.toLatin1(), encodedImage) || !outputFile.commit()) {
         Q_EMIT errorMessage(i18n("Cannot save screenshot. Error while writing file."));
         return false;
     }
@@ -518,10 +522,11 @@ bool ExportManager::remoteSave(const QUrl &url, const QString &suffix, QByteArra
     QTemporaryFile tmpFile;
 
     if (tmpFile.open()) {
-        if (!writeImage(&tmpFile, suffix.toLatin1(), encodedImage)) {
+        if (!writeImage(&tmpFile, suffix.toLatin1(), encodedImage) || !tmpFile.flush()) {
             Q_EMIT errorMessage(i18n("Cannot save screenshot. Error while writing temporary local file."));
             return false;
         }
+        tmpFile.close();
 
         KIO::FileCopyJob *uploadJob = KIO::file_copy(QUrl::fromLocalFile(tmpFile.fileName()), url);
         uploadJob->exec();
@@ -556,9 +561,9 @@ QUrl ExportManager::tempSave()
 
         QString suffix = imageFileSuffix(QUrl(baseFileName));
         const QString fileName = autoIncrementFilename(baseFileName, suffix, &ExportManager::isTempFileAlreadyUsed);
-        QFile tmpFile(fileName);
+        QSaveFile tmpFile(fileName);
         if (tmpFile.open(QFile::WriteOnly)) {
-            if (writeImage(&tmpFile, suffix.toLatin1())) {
+            if (writeImage(&tmpFile, suffix.toLatin1()) && tmpFile.commit()) {
                 m_tempFile = QUrl::fromLocalFile(tmpFile.fileName());
                 // try to make sure 3rd-party which gets the url of the temporary file e.g. on export
                 // properly treats this as readonly, also hide from other users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.