mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-11 15:06:14 +01:00
gopenuriportal: Use task data instead of object data
The docs recommend using a small number of `keys` when setting data for an object, the OpenURI portal uses three different `keys` that can easily be replaced with `g_task_set_task_data()`.
This commit is contained in:
parent
ef064d625b
commit
89488e98e9
@ -165,6 +165,29 @@ enum {
|
|||||||
XDG_DESKTOP_PORTAL_FAILED = 2
|
XDG_DESKTOP_PORTAL_FAILED = 2
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *response_handle;
|
||||||
|
unsigned int response_signal_id;
|
||||||
|
gboolean open_file;
|
||||||
|
} CallData;
|
||||||
|
|
||||||
|
static CallData *
|
||||||
|
call_data_new (void)
|
||||||
|
{
|
||||||
|
return g_new0 (CallData, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
call_data_free (gpointer data)
|
||||||
|
{
|
||||||
|
CallData *call = data;
|
||||||
|
|
||||||
|
g_assert (call->response_signal_id == 0);
|
||||||
|
g_clear_pointer (&call->response_handle, g_free);
|
||||||
|
g_free_sized (data, sizeof (CallData));
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
response_received (GDBusConnection *connection,
|
response_received (GDBusConnection *connection,
|
||||||
const char *sender_name,
|
const char *sender_name,
|
||||||
@ -175,11 +198,12 @@ response_received (GDBusConnection *connection,
|
|||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
GTask *task = user_data;
|
GTask *task = user_data;
|
||||||
|
CallData *call_data;
|
||||||
guint32 response;
|
guint32 response;
|
||||||
guint signal_id;
|
|
||||||
|
|
||||||
signal_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (task), "signal-id"));
|
call_data = g_task_get_task_data (task);
|
||||||
g_dbus_connection_signal_unsubscribe (connection, signal_id);
|
g_dbus_connection_signal_unsubscribe (connection, call_data->response_signal_id);
|
||||||
|
call_data->response_signal_id = 0;
|
||||||
|
|
||||||
g_variant_get (parameters, "(u@a{sv})", &response, NULL);
|
g_variant_get (parameters, "(u@a{sv})", &response, NULL);
|
||||||
|
|
||||||
@ -208,17 +232,15 @@ open_call_done (GObject *source,
|
|||||||
GXdpOpenURI *openuri = GXDP_OPEN_URI (source);
|
GXdpOpenURI *openuri = GXDP_OPEN_URI (source);
|
||||||
GDBusConnection *connection;
|
GDBusConnection *connection;
|
||||||
GTask *task = user_data;
|
GTask *task = user_data;
|
||||||
|
CallData *call_data;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
gboolean open_file;
|
|
||||||
gboolean res;
|
gboolean res;
|
||||||
char *path = NULL;
|
char *path = NULL;
|
||||||
const char *handle;
|
|
||||||
guint signal_id;
|
|
||||||
|
|
||||||
|
call_data = g_task_get_task_data (task);
|
||||||
connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (openuri));
|
connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (openuri));
|
||||||
open_file = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "open-file"));
|
|
||||||
|
|
||||||
if (open_file)
|
if (call_data->open_file)
|
||||||
res = gxdp_open_uri_call_open_file_finish (openuri, &path, NULL, result, &error);
|
res = gxdp_open_uri_call_open_file_finish (openuri, &path, NULL, result, &error);
|
||||||
else
|
else
|
||||||
res = gxdp_open_uri_call_open_uri_finish (openuri, &path, result, &error);
|
res = gxdp_open_uri_call_open_uri_finish (openuri, &path, result, &error);
|
||||||
@ -231,11 +253,12 @@ open_call_done (GObject *source,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
handle = (const char *)g_object_get_data (G_OBJECT (task), "handle");
|
if (g_strcmp0 (call_data->response_handle, path) != 0)
|
||||||
if (g_strcmp0 (handle, path) != 0)
|
|
||||||
{
|
{
|
||||||
signal_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (task), "signal-id"));
|
guint signal_id;
|
||||||
g_dbus_connection_signal_unsubscribe (connection, signal_id);
|
|
||||||
|
g_dbus_connection_signal_unsubscribe (connection, call_data->response_signal_id);
|
||||||
|
call_data->response_signal_id = 0;
|
||||||
|
|
||||||
signal_id = g_dbus_connection_signal_subscribe (connection,
|
signal_id = g_dbus_connection_signal_subscribe (connection,
|
||||||
"org.freedesktop.portal.Desktop",
|
"org.freedesktop.portal.Desktop",
|
||||||
@ -247,7 +270,9 @@ open_call_done (GObject *source,
|
|||||||
response_received,
|
response_received,
|
||||||
task,
|
task,
|
||||||
NULL);
|
NULL);
|
||||||
g_object_set_data (G_OBJECT (task), "signal-id", GINT_TO_POINTER (signal_id));
|
g_clear_pointer (&call_data->response_handle, g_free);
|
||||||
|
call_data->response_signal_id = signal_id;
|
||||||
|
call_data->response_handle = g_steal_pointer (&path);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free (path);
|
g_free (path);
|
||||||
@ -261,6 +286,7 @@ g_openuri_portal_open_file_async (GFile *file,
|
|||||||
GAsyncReadyCallback callback,
|
GAsyncReadyCallback callback,
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
|
CallData *call_data;
|
||||||
GDBusConnection *connection;
|
GDBusConnection *connection;
|
||||||
GTask *task;
|
GTask *task;
|
||||||
GVariant *opts = NULL;
|
GVariant *opts = NULL;
|
||||||
@ -293,7 +319,6 @@ g_openuri_portal_open_file_async (GFile *file,
|
|||||||
sender[i] = '_';
|
sender[i] = '_';
|
||||||
|
|
||||||
handle = g_strdup_printf ("/org/freedesktop/portal/desktop/request/%s/%s", sender, token);
|
handle = g_strdup_printf ("/org/freedesktop/portal/desktop/request/%s/%s", sender, token);
|
||||||
g_object_set_data_full (G_OBJECT (task), "handle", handle, g_free);
|
|
||||||
g_free (sender);
|
g_free (sender);
|
||||||
|
|
||||||
signal_id = g_dbus_connection_signal_subscribe (connection,
|
signal_id = g_dbus_connection_signal_subscribe (connection,
|
||||||
@ -306,7 +331,6 @@ g_openuri_portal_open_file_async (GFile *file,
|
|||||||
response_received,
|
response_received,
|
||||||
task,
|
task,
|
||||||
NULL);
|
NULL);
|
||||||
g_object_set_data (G_OBJECT (task), "signal-id", GINT_TO_POINTER (signal_id));
|
|
||||||
|
|
||||||
g_variant_builder_init_static (&opt_builder, G_VARIANT_TYPE_VARDICT);
|
g_variant_builder_init_static (&opt_builder, G_VARIANT_TYPE_VARDICT);
|
||||||
g_variant_builder_add (&opt_builder, "{sv}", "handle_token", g_variant_new_string (token));
|
g_variant_builder_add (&opt_builder, "{sv}", "handle_token", g_variant_new_string (token));
|
||||||
@ -318,9 +342,17 @@ g_openuri_portal_open_file_async (GFile *file,
|
|||||||
g_variant_new_string (startup_id));
|
g_variant_new_string (startup_id));
|
||||||
|
|
||||||
opts = g_variant_builder_end (&opt_builder);
|
opts = g_variant_builder_end (&opt_builder);
|
||||||
|
|
||||||
|
call_data = call_data_new ();
|
||||||
|
call_data->response_handle = g_steal_pointer (&handle);
|
||||||
|
call_data->response_signal_id = signal_id;
|
||||||
|
g_task_set_task_data (task, call_data, call_data_free);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
task = NULL;
|
{
|
||||||
|
call_data = NULL;
|
||||||
|
task = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (g_file_is_native (file))
|
if (g_file_is_native (file))
|
||||||
{
|
{
|
||||||
@ -328,8 +360,8 @@ g_openuri_portal_open_file_async (GFile *file,
|
|||||||
GUnixFDList *fd_list = NULL;
|
GUnixFDList *fd_list = NULL;
|
||||||
int fd, fd_id, errsv;
|
int fd, fd_id, errsv;
|
||||||
|
|
||||||
if (task)
|
if (call_data)
|
||||||
g_object_set_data (G_OBJECT (task), "open-file", GINT_TO_POINTER (TRUE));
|
call_data->open_file = TRUE;
|
||||||
|
|
||||||
path = g_file_get_path (file);
|
path = g_file_get_path (file);
|
||||||
fd = g_open (path, O_RDONLY | O_CLOEXEC);
|
fd = g_open (path, O_RDONLY | O_CLOEXEC);
|
||||||
|
Loading…
Reference in New Issue
Block a user