From 9ea3571c903db1ebe72d5091eefc052288af252f Mon Sep 17 00:00:00 2001
From: Arjen Hiemstra <ahiemstra@heimr.nl>
Date: Thu, 7 Nov 2024 11:54:35 +0100
Subject: [PATCH 4/4] faces: Ensure the temporary dir for a preset remains
 until installed

QTemporaryDir deletes the temporary directory when it goes out of scope.
However, installing the preset with KPackage is an asynchronous
operation that can extend beyond the current scope. This can cause the
preset data to be deleted while we're still installing. To ensure this
doesn't happen, extend the lifetime of the QTemporaryDir into the lambda
we use to handle the job finished, so that it lives at least as long as
the job.

BUG: 485164
---
 faces/SensorFaceController.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/faces/SensorFaceController.cpp b/faces/SensorFaceController.cpp
index 06eda200..60e6a579 100644
--- a/faces/SensorFaceController.cpp
+++ b/faces/SensorFaceController.cpp
@@ -954,6 +954,11 @@ void SensorFaceController::savePreset()
         pluginName += QString::number(suffix);
     }
 
+    // Important! We need to ensure the directory remains valid as long as it has
+    // not been installed yet. Since the install is asynchronous, we need to make
+    // sure that the QTemporaryDir does not go out of scope until the install is
+    // finished, so this directory will be moved into the lambda connected to the
+    // job finished signal below to ensure it lives as long as the job.
     QTemporaryDir dir;
     if (!dir.isValid()) {
         return;
@@ -1013,7 +1018,7 @@ void SensorFaceController::savePreset()
     configGroup.sync();
 
     auto *job = KPackage::PackageJob::install(QStringLiteral("Plasma/Applet"), dir.path());
-    connect(job, &KJob::finished, this, [this]() {
+    connect(job, &KJob::finished, this, [this, dir = std::move(dir)]() {
         d->availablePresetsModel->reload();
     });
 }
-- 
2.47.0

