[plasma/union] src: Add a method to PackageHandler to update an installed package

Arjen Hiemstra <[email protected]> Wed, 5 Aug 2026 10:33:24 +0000 (UTC)
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 62e9b04064b5dbf1f5ff9d6d5bc84288d5d1e643 by Arjen Hiemstra.
Committed on 05/08/2026 at 10:24.
Pushed by ahiemstra into branch 'master'.

Add a method to PackageHandler to update an installed package

It's basically uninstall and then install, but it's useful to have as a
dedicated operation.

M  +22   -0    src/PackageHandler.cpp
M  +19   -0    src/PackageHandler.h

https://invent.kde.org/plasma/union/-/commit/62e9b04064b5dbf1f5ff9d6d5bc84288d5d1e643

diff --git a/src/PackageHandler.cpp b/src/PackageHandler.cpp
index df32be0f..c93e3249 100644
--- a/src/PackageHandler.cpp
+++ b/src/PackageHandler.cpp
@@ -110,3 +110,25 @@ PackageHandler::Error PackageHandler::uninstall(const StylePackage &package)
 
     return Error::None;
 }
+
+PackageHandler::Error PackageHandler::update(const StylePackage &updatePackage)
+{
+    if (!updatePackage.isValid()) {
+        return Error::InvalidPackage;
+    }
+
+    auto installedPackage = package(updatePackage.id());
+    if (!installedPackage.isValid()) {
+        return Error::NotInstalled;
+    }
+
+    if (installedPackage.version() == updatePackage.version()) {
+        return Error::NotAnUpdate;
+    }
+
+    if (auto result = uninstall(installedPackage); result != Error::None) {
+        return result;
+    }
+
+    return install(updatePackage);
+}
diff --git a/src/PackageHandler.h b/src/PackageHandler.h
index 8408d5ca..a631243c 100644
--- a/src/PackageHandler.h
+++ b/src/PackageHandler.h
@@ -40,6 +40,9 @@ public:
      *      permissions to disk failure.
      * \value NotInstalled
      *      The operation failed because the provided package is not installed.
+     * \value NotAnUpdate
+     *      The operation failed because the provided package is not considered
+     *      to be an update for the existing installed package.
      */
     enum class Error {
         None,
@@ -47,6 +50,7 @@ public:
         AlreadyInstalled,
         FilesystemError,
         NotInstalled,
+        NotAnUpdate,
     };
 
     PackageHandler(const std::shared_ptr<PlatformPlugin> &platformPlugin);
@@ -82,6 +86,21 @@ public:
      * return Error::FilesystemError if an operating system error occurs.
      */
     Error uninstall(const StylePackage &package);
+    /*!
+     * Update a package.
+     *
+     * This will update an installed package with the same ID as \p updatePackage
+     * and replace its contents with the contents of \p updatePackage. It will
+     * return Error::None if the update was successful. It will return
+     * Error::NotInstalled if no installed package with the same ID could be
+     * found. It will return Error::NotAnUpdate if the given package is not
+     * considered to be an update for the installed package.
+     *
+     * Updating happens by first uninstalling the existing package and then
+     * installing \p updatePackage. Any error that occurs during these steps
+     * will cause the update to fail and will return the error.
+     */
+    Error update(const StylePackage &updatePackage);
 
 private:
     class Private;