Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libportal: Add activation-token to notification-action-invoked #88

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jobs:
runs-on: ubuntu-latest
container: fedora:35
env:
LAST_ABI_BREAK: "467a397fd7996557f837cdc26ac07c01c62810e5"
LAST_ABI_BREAK: "37ceaba91f399239864b767736ea3c4be0c22b3b"

steps:
- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions libportal/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generated_files = []
version = '1.0.0'
gir_version = '1.0'
version = '2.0.0'
gir_version = '2.0'

headers = [
'portal.h',
Expand Down
165 changes: 126 additions & 39 deletions libportal/notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,46 @@ action_invoked (GDBusConnection *bus,
gpointer data)
{
XdpPortal *portal = data;
g_autoptr(GVariant) info = NULL;
const char *id;
const char *action;
g_autoptr(GVariant) parameter = NULL;
g_autoptr(GVariant) platform_data = NULL;

g_variant_get (parameters, "(&s&s@av)", &id, &action, &parameter);
if (g_str_equal (signal_name, "ActionInvoked2"))
{
g_variant_get (parameters, "(&s&s@a{sv}@av)",
&id, &action, &platform_data, &parameter);
}
else
{
GVariantBuilder n;

g_variant_get (parameters, "(&s&s@av)", &id, &action, &parameter);

g_variant_builder_init (&n, G_VARIANT_TYPE_VARDICT);
platform_data = g_variant_ref_sink (g_variant_builder_end (&n));
}

g_signal_emit_by_name (portal, "notification-action-invoked",
id, action, parameter);
id, action, platform_data, parameter);
}

static void
ensure_action_invoked_connection (XdpPortal *portal)
{
const char *signal_name;

if (portal->notification_interface_version >= 2)
signal_name = "ActionInvoked2";
else
signal_name = "ActionInvoked";

if (portal->action_invoked_signal == 0)
portal->action_invoked_signal =
g_dbus_connection_signal_subscribe (portal->bus,
PORTAL_BUS_NAME,
"org.freedesktop.portal.Notification",
"ActionInvoked",
signal_name,
PORTAL_OBJECT_PATH,
NULL,
G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
Expand All @@ -60,23 +80,97 @@ ensure_action_invoked_connection (XdpPortal *portal)
NULL);
}

typedef struct {
XdpPortal *portal;
GAsyncReadyCallback callback;
gpointer data;
} CallDoneData;

static void
call_done (GObject *source,
GAsyncResult *result,
gpointer data)
{
CallDoneData *call_done_data = data;
g_autoptr(GVariant) res = NULL;
g_autoptr(GTask) task = data;
GError *error = NULL;
XdpPortal *portal;

portal = g_task_get_source_object (task);
res = g_dbus_connection_call_finish (portal->bus, result, &error);

if (error)
g_task_return_error (task, error);
else
g_task_return_boolean (task, !!res);
}

static void
add_notification_internal (XdpPortal *portal,
GVariant *parameters,
GCancellable *cancellable,
GTask *task)
{
GAsyncReadyCallback call_done_cb = task ? call_done : NULL;

call_done_data->callback (G_OBJECT (call_done_data->portal), result, call_done_data->data);
ensure_action_invoked_connection (portal);

g_object_unref (call_done_data->portal);
g_free (call_done_data);
g_dbus_connection_call (portal->bus,
PORTAL_BUS_NAME,
PORTAL_OBJECT_PATH,
"org.freedesktop.portal.Notification",
"AddNotification",
parameters,
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
cancellable,
call_done_cb,
task);
}

static void
get_version_returned (GObject *object,
GAsyncResult *result,
gpointer data)
{
g_autoptr(GVariant) version_variant = NULL;
g_autoptr(GVariant) ret = NULL;
g_autoptr(GTask) task = data;
XdpPortal *portal = g_task_get_source_object (task);
GError *error = NULL;
GVariant *notification_params;
GCancellable *cancellable;

ret = g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &error);
if (error)
{
g_task_return_error (task, error);
return;
}

g_variant_get_child (ret, 0, "v", &version_variant);
portal->notification_interface_version = g_variant_get_uint32 (version_variant);

g_debug ("Notification version: %u", portal->notification_interface_version);

notification_params = g_task_get_task_data (task);
cancellable = g_task_get_cancellable (task);

add_notification_internal (portal, notification_params, cancellable,
g_steal_pointer (&task));
}

static void
get_notification_interface_version (XdpPortal *portal,
GTask *task)
{
g_dbus_connection_call (portal->bus,
PORTAL_BUS_NAME,
PORTAL_OBJECT_PATH,
"org.freedesktop.DBus.Properties",
"Get",
g_variant_new ("(ss)", "org.freedesktop.portal.Notification", "version"),
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
g_task_get_cancellable (task),
get_version_returned,
task);
}

/**
Expand Down Expand Up @@ -130,35 +224,29 @@ xdp_portal_add_notification (XdpPortal *portal,
GAsyncReadyCallback callback,
gpointer data)
{
GAsyncReadyCallback call_done_cb = NULL;
CallDoneData *call_done_data = NULL;
GTask *task = NULL;
GVariant *parameters;

g_return_if_fail (XDP_IS_PORTAL (portal));
g_return_if_fail (flags == XDP_NOTIFICATION_FLAG_NONE);

ensure_action_invoked_connection (portal);
parameters = g_variant_new ("(s@a{sv})", id, notification);

if (callback)
if (callback || !portal->notification_interface_version)
{
call_done_cb = call_done;
call_done_data = g_new (CallDoneData, 1);
call_done_data->portal = g_object_ref (portal);
call_done_data->callback = callback,
call_done_data->data = data;
task = g_task_new (portal, cancellable, callback, data);
g_task_set_task_data (task, g_variant_ref_sink (parameters),
(GDestroyNotify) g_variant_unref);
}

g_dbus_connection_call (portal->bus,
PORTAL_BUS_NAME,
PORTAL_OBJECT_PATH,
"org.freedesktop.portal.Notification",
"AddNotification",
g_variant_new ("(s@a{sv})", id, notification),
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
cancellable,
call_done_cb,
call_done_data);
if (!portal->notification_interface_version)
{
get_notification_interface_version (portal, task);
}
else
{
add_notification_internal (portal, parameters, cancellable, task);
}
}

/**
Expand All @@ -178,11 +266,10 @@ xdp_portal_add_notification_finish (XdpPortal *portal,
GAsyncResult *result,
GError **error)
{
g_autoptr(GVariant) res = NULL;

res = g_dbus_connection_call_finish (portal->bus, result, error);
g_return_val_if_fail (XDP_IS_PORTAL (portal), FALSE);
g_return_val_if_fail (g_task_is_valid (result, portal), FALSE);

return !!res;
return g_task_propagate_boolean (G_TASK (result), error);
}

/**
Expand Down
1 change: 1 addition & 0 deletions libportal/portal-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct _XdpPortal {
guint location_updated_signal;

/* notification */
guint notification_interface_version;
guint action_invoked_signal;

/* screencast */
Expand Down
6 changes: 4 additions & 2 deletions libportal/portal.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ xdp_portal_class_init (XdpPortalClass *klass)
* @portal: the [class@Portal]
* @id: the notification ID
* @action: the action name
* @parameter: (nullable): the target parameter for the action
* @platform_data: the platform data for the action
* @parameter: the target parameter for the action
*
* Emitted when a non-exported action is activated on a notification.
*/
Expand All @@ -248,9 +249,10 @@ xdp_portal_class_init (XdpPortalClass *klass)
0,
NULL, NULL,
NULL,
G_TYPE_NONE, 3,
G_TYPE_NONE, 4,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_VARIANT,
G_TYPE_VARIANT);
}

Expand Down