gappinfo: Pass activation token from launch context to open_uri/file portal

The `activation_token` option was added to the portal in v4, so let's
actually pass a activation token if we have one.

Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/2868
This commit is contained in:
Julian Sparber 2024-02-16 11:19:56 +01:00
parent 6210708f05
commit 23b858a3de
3 changed files with 58 additions and 10 deletions

View File

@ -25,6 +25,7 @@
#include "gappinfo.h"
#include "gappinfoprivate.h"
#include "gcontextspecificgroup.h"
#include "gdesktopappinfo.h"
#include "gtask.h"
#include "gcancellable.h"
@ -1205,16 +1206,32 @@ g_app_info_launch_default_for_uri (const char *uri,
{
GFile *file = NULL;
const char *parent_window = NULL;
char *startup_id = NULL;
/* Reset any error previously set by launch_default_for_uri */
g_clear_error (error);
if (launch_context && launch_context->priv->envp)
file = g_file_new_for_uri (uri);
if (launch_context)
{
GList *file_list;
if (launch_context->priv->envp)
parent_window = g_environ_getenv (launch_context->priv->envp, "PARENT_WINDOW_ID");
file = g_file_new_for_uri (uri);
res = g_openuri_portal_open_file (file, parent_window, error);
file_list = g_list_prepend (NULL, file);
startup_id = g_app_launch_context_get_startup_notify_id (launch_context,
NULL,
file_list);
g_list_free (file_list);
}
res = g_openuri_portal_open_file (file, parent_window, startup_id, error);
g_object_unref (file);
g_free (startup_id);
}
#endif
@ -1263,21 +1280,37 @@ launch_default_for_uri_portal_open_uri (GTask *task, GError *error)
{
GFile *file;
const char *parent_window = NULL;
char *startup_id = NULL;
/* Reset any error previously set by launch_default_for_uri */
g_error_free (error);
if (data->context && data->context->priv->envp)
file = g_file_new_for_uri (data->uri);
if (data->context)
{
GList *file_list;
if (data->context->priv->envp)
parent_window = g_environ_getenv (data->context->priv->envp,
"PARENT_WINDOW_ID");
file = g_file_new_for_uri (data->uri);
file_list = g_list_prepend (NULL, file);
startup_id = g_app_launch_context_get_startup_notify_id (data->context,
NULL,
file_list);
g_list_free (file_list);
}
g_openuri_portal_open_file_async (file,
parent_window,
startup_id,
cancellable,
launch_default_for_uri_portal_open_uri_cb,
g_steal_pointer (&task));
g_object_unref (file);
g_free (startup_id);
return;
}
@ -1783,7 +1816,7 @@ g_app_launch_context_get_display (GAppLaunchContext *context,
* g_app_launch_context_get_startup_notify_id:
* @context: the launch context
* @info: (nullable): the app info
* @files: (nullable): (element-type GFile): a list of [iface@Gio.File] objects
* @files: (nullable) (element-type GFile): a list of [iface@Gio.File] objects
*
* Initiates startup notification for the application and returns the
* `XDG_ACTIVATION_TOKEN` or `DESKTOP_STARTUP_ID` for the launched operation,
@ -1798,7 +1831,8 @@ g_app_launch_context_get_display (GAppLaunchContext *context,
* [freedesktop.org Startup Notification Protocol](http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt).
*
* Support for the XDG Activation Protocol was added in GLib 2.76.
* Since GLib 2.82 @info and @files can be `NULL`, but if it's not supported the returned token may be `NULL`.
* Since GLib 2.82 @info and @files can be `NULL`. If thats not supported by the backend,
* the returned token will be `NULL`.
*
* Returns: (nullable): a startup notification ID for the application, or `NULL` if
* not supported.

View File

@ -82,6 +82,7 @@ init_openuri_portal (void)
gboolean
g_openuri_portal_open_file (GFile *file,
const char *parent_window,
const char *startup_id,
GError **error)
{
GVariantBuilder opt_builder;
@ -96,6 +97,11 @@ g_openuri_portal_open_file (GFile *file,
g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
if (startup_id)
g_variant_builder_add (&opt_builder, "{sv}",
"activation_token",
g_variant_new_string (startup_id));
if (g_file_is_native (file))
{
char *path = NULL;
@ -248,6 +254,7 @@ open_call_done (GObject *source,
void
g_openuri_portal_open_file_async (GFile *file,
const char *parent_window,
const char *startup_id,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
@ -303,6 +310,11 @@ g_openuri_portal_open_file_async (GFile *file,
g_variant_builder_add (&opt_builder, "{sv}", "handle_token", g_variant_new_string (token));
g_free (token);
if (startup_id)
g_variant_builder_add (&opt_builder, "{sv}",
"activation_token",
g_variant_new_string (startup_id));
opts = g_variant_builder_end (&opt_builder);
}
else

View File

@ -27,10 +27,12 @@ G_BEGIN_DECLS
gboolean g_openuri_portal_open_file (GFile *file,
const char *parent_window,
const char *startup_id,
GError **error);
void g_openuri_portal_open_file_async (GFile *file,
const char *parent_window,
const char *startup_id,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);