[gimp/nielsdg/xdg-pick-button-version-check] pickbutton: Add version check for XDG portal




commit 2202bbd53a5d200bb543555be0563cba95133776
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Sun Apr 10 15:31:27 2022 +0200

    pickbutton: Add version check for XDG portal
    
    `PickColor()` API only got added in version 2, so check for that before
    trying to use it.

 libgimpwidgets/gimppickbutton-xdg.c | 47 +++++++++++++++++++++++++++----------
 1 file changed, 34 insertions(+), 13 deletions(-)
---
diff --git a/libgimpwidgets/gimppickbutton-xdg.c b/libgimpwidgets/gimppickbutton-xdg.c
index 3b3dcee33a..5a2c637f19 100644
--- a/libgimpwidgets/gimppickbutton-xdg.c
+++ b/libgimpwidgets/gimppickbutton-xdg.c
@@ -36,7 +36,10 @@
 gboolean
 _gimp_pick_button_xdg_available (void)
 {
-  GDBusProxy *proxy = NULL;
+  gboolean    ret     = TRUE;
+  GDBusProxy *proxy   = NULL;
+  GError     *error   = NULL;
+  GVariant   *version = NULL;
 
   proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                          G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
@@ -46,24 +49,42 @@ _gimp_pick_button_xdg_available (void)
                                          "org.freedesktop.portal.Screenshot",
                                          NULL, NULL);
 
-  if (proxy)
+  if (proxy == NULL)
     {
-      GError *error = NULL;
+      ret = FALSE;
+         goto out;
+    }
 
-      g_dbus_proxy_call_sync (proxy, "org.freedesktop.DBus.Peer.Ping",
-                              NULL,
-                              G_DBUS_CALL_FLAGS_NONE,
-                              -1, NULL, &error);
-      if (! error)
-        return TRUE;
+  /* Ping the service to be sure it's available */
+  g_dbus_proxy_call_sync (proxy, "org.freedesktop.DBus.Peer.Ping",
+                          NULL,
+                          G_DBUS_CALL_FLAGS_NONE,
+                          -1, NULL, &error);
+  if (error != NULL)
+    {
+      ret = FALSE;
+      goto out;
+    }
 
-      g_clear_error (&error);
+  /* Finally, PickColor is only available starting V2 of the portal */
+  version = g_dbus_proxy_get_cached_property (proxy, "version");
+  if (version == NULL)
+    {
+      ret = FALSE;
+      goto out;
+    }
 
-      g_object_unref (proxy);
-      proxy = NULL;
+  if (g_variant_get_uint32 (version) < 2)
+    {
+      ret = FALSE;
+      goto out;
     }
 
-  return FALSE;
+out:
+  g_clear_pointer (&version, g_variant_unref);
+  g_clear_error (&error);
+  g_clear_object (&proxy);
+  return ret;
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]