Add portal version checking helper

Add gtk_get_portal_interface_version() to check the version of a portal.
This commit is contained in:
Bastien Nocera 2020-03-23 14:56:43 +01:00
parent bd55c5842e
commit 027ca22def
2 changed files with 59 additions and 0 deletions

View File

@ -268,6 +268,63 @@ _gtk_ensure_resources (void)
g_once (&register_resources_once, register_resources, NULL);
}
/*
* gtk_get_portal_interface_version:
* @connection: a session #GDBusConnection
* @interface_name: the interface name for the portal interface
* we're interested in.
*
* Returns: the version number of the portal, or 0 on error.
*/
guint
gtk_get_portal_interface_version (GDBusConnection *connection,
const char *interface_name)
{
GDBusProxy *proxy = NULL;
GError *error = NULL;
GVariant *ret = NULL;
char *owner = NULL;
guint version = 0;
proxy = g_dbus_proxy_new_sync (connection,
0,
NULL,
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
interface_name,
NULL,
&error);
if (!proxy)
{
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning ("Could not query portal version on interface '%s': %s",
interface_name, error->message);
goto out;
}
owner = g_dbus_proxy_get_name_owner (proxy);
if (owner == NULL)
{
g_debug ("%s not provided by any service", interface_name);
goto out;
}
ret = g_dbus_proxy_get_cached_property (proxy, "version");
if (ret)
version = g_variant_get_uint32 (ret);
g_debug ("Got version %u for portal interface '%s'",
version, interface_name);
out:
g_clear_object (&proxy);
g_clear_error (&error);
g_clear_pointer (&ret, g_variant_unref);
g_clear_pointer (&owner, g_free);
return version;
}
static char *
get_portal_path (GDBusConnection *connection,
const char *kind,

View File

@ -115,6 +115,8 @@ char *gtk_get_portal_request_path (GDBusConnection *connection,
char **token);
char *gtk_get_portal_session_path (GDBusConnection *connection,
char **token);
guint gtk_get_portal_interface_version (GDBusConnection *connection,
const char *interface_name);
#define PORTAL_BUS_NAME "org.freedesktop.portal.Desktop"
#define PORTAL_OBJECT_PATH "/org/freedesktop/portal/desktop"