docs: Rename README.in to README.md for GitLab
[glib.git] / gio / gopenuriportal.c
blob38d60bf68abc09070cb007266f42bb60b249c4fa
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2017 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "config.h"
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <string.h>
26 #include "gopenuriportal.h"
27 #include "xdp-dbus.h"
28 #include "gstdio.h"
30 #ifdef G_OS_UNIX
31 #include "gunixfdlist.h"
32 #endif
34 #ifndef O_PATH
35 #define O_PATH 0
36 #endif
37 #ifndef O_CLOEXEC
38 #define O_CLOEXEC 0
39 #else
40 #define HAVE_O_CLOEXEC 1
41 #endif
44 static GXdpOpenURI *openuri;
46 static gboolean
47 init_openuri_portal (void)
49 static gsize openuri_inited = 0;
51 if (g_once_init_enter (&openuri_inited))
53 GError *error = NULL;
54 GDBusConnection *connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
56 if (connection != NULL)
58 openuri = gxdp_open_uri_proxy_new_sync (connection, 0,
59 "org.freedesktop.portal.Desktop",
60 "/org/freedesktop/portal/desktop",
61 NULL, &error);
62 if (openuri == NULL)
64 g_warning ("Cannot create document portal proxy: %s", error->message);
65 g_error_free (error);
68 g_object_unref (connection);
70 else
72 g_warning ("Cannot connect to session bus when initializing document portal: %s",
73 error->message);
74 g_error_free (error);
77 g_once_init_leave (&openuri_inited, 1);
80 return openuri != NULL;
83 gboolean
84 g_openuri_portal_open_uri (const char *uri,
85 const char *parent_window,
86 GError **error)
88 GFile *file = NULL;
89 GVariantBuilder opt_builder;
90 gboolean res;
92 if (!init_openuri_portal ())
94 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
95 "OpenURI portal is not available");
96 return FALSE;
99 g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
101 file = g_file_new_for_uri (uri);
102 if (g_file_is_native (file))
104 char *path = NULL;
105 GUnixFDList *fd_list = NULL;
106 int fd, fd_id, errsv;
108 path = g_file_get_path (file);
110 fd = g_open (path, O_PATH | O_CLOEXEC);
111 errsv = errno;
112 if (fd == -1)
114 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
115 "Failed to open '%s'", path);
116 return FALSE;
119 #ifndef HAVE_O_CLOEXEC
120 fcntl (fd, F_SETFD, FD_CLOEXEC);
121 #endif
122 fd_list = g_unix_fd_list_new_from_array (&fd, 1);
123 fd = -1;
124 fd_id = 0;
126 res = gxdp_open_uri_call_open_file_sync (openuri,
127 parent_window ? parent_window : "",
128 g_variant_new ("h", fd_id),
129 g_variant_builder_end (&opt_builder),
130 fd_list,
131 NULL,
132 NULL,
133 NULL,
134 error);
135 g_free (path);
136 g_object_unref (fd_list);
138 else
140 res = gxdp_open_uri_call_open_uri_sync (openuri,
141 parent_window ? parent_window : "",
142 uri,
143 g_variant_builder_end (&opt_builder),
144 NULL,
145 NULL,
146 error);
149 g_object_unref (file);
151 return res;
154 enum {
155 XDG_DESKTOP_PORTAL_SUCCESS = 0,
156 XDG_DESKTOP_PORTAL_CANCELLED = 1,
157 XDG_DESKTOP_PORTAL_FAILED = 2
160 static void
161 response_received (GDBusConnection *connection,
162 const char *sender_name,
163 const char *object_path,
164 const char *interface_name,
165 const char *signal_name,
166 GVariant *parameters,
167 gpointer user_data)
169 GTask *task = user_data;
170 guint32 response;
171 guint signal_id;
173 signal_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (task), "signal-id"));
174 g_dbus_connection_signal_unsubscribe (connection, signal_id);
176 g_variant_get (parameters, "(u@a{sv})", &response, NULL);
178 switch (response)
180 case XDG_DESKTOP_PORTAL_SUCCESS:
181 g_task_return_boolean (task, TRUE);
182 break;
183 case XDG_DESKTOP_PORTAL_CANCELLED:
184 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Launch cancelled");
185 break;
186 case XDG_DESKTOP_PORTAL_FAILED:
187 default:
188 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "Launch failed");
189 break;
192 g_object_unref (task);
195 static void
196 open_call_done (GObject *source,
197 GAsyncResult *result,
198 gpointer user_data)
200 GXdpOpenURI *openuri = GXDP_OPEN_URI (source);
201 GDBusConnection *connection;
202 GTask *task = user_data;
203 GError *error = NULL;
204 gboolean open_file;
205 gboolean res;
206 char *path = NULL;
207 const char *handle;
208 guint signal_id;
210 connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (openuri));
211 open_file = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "open-file"));
213 if (open_file)
214 res = gxdp_open_uri_call_open_file_finish (openuri, &path, NULL, result, &error);
215 else
216 res = gxdp_open_uri_call_open_uri_finish (openuri, &path, result, &error);
218 if (!res)
220 g_task_return_error (task, error);
221 g_object_unref (task);
222 g_free (path);
223 return;
226 handle = (const char *)g_object_get_data (G_OBJECT (task), "handle");
227 if (g_strcmp0 (handle, path) != 0)
229 signal_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (task), "signal-id"));
230 g_dbus_connection_signal_unsubscribe (connection, signal_id);
232 signal_id = g_dbus_connection_signal_subscribe (connection,
233 "org.freedesktop.portal.Desktop",
234 "org.freedesktop.portal.Request",
235 "Response",
236 path,
237 NULL,
238 G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
239 response_received,
240 task,
241 NULL);
242 g_object_set_data (G_OBJECT (task), "signal-id", GINT_TO_POINTER (signal_id));
246 void
247 g_openuri_portal_open_uri_async (const char *uri,
248 const char *parent_window,
249 GCancellable *cancellable,
250 GAsyncReadyCallback callback,
251 gpointer user_data)
253 GDBusConnection *connection;
254 GTask *task;
255 GFile *file;
256 GVariant *opts = NULL;
257 int i;
258 guint signal_id;
260 if (!init_openuri_portal ())
262 g_task_report_new_error (NULL, callback, user_data, NULL,
263 G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
264 "OpenURI portal is not available");
265 return;
268 connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (openuri));
270 if (callback)
272 GVariantBuilder opt_builder;
273 char *token;
274 char *sender;
275 char *handle;
277 task = g_task_new (NULL, cancellable, callback, user_data);
279 token = g_strdup_printf ("gio%d", g_random_int_range (0, G_MAXINT));
280 sender = g_strdup (g_dbus_connection_get_unique_name (connection) + 1);
281 for (i = 0; sender[i]; i++)
282 if (sender[i] == '.')
283 sender[i] = '_';
285 handle = g_strdup_printf ("/org/fredesktop/portal/desktop/request/%s/%s", sender, token);
286 g_object_set_data_full (G_OBJECT (task), "handle", handle, g_free);
287 g_free (sender);
289 signal_id = g_dbus_connection_signal_subscribe (connection,
290 "org.freedesktop.portal.Desktop",
291 "org.freedesktop.portal.Request",
292 "Response",
293 handle,
294 NULL,
295 G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
296 response_received,
297 task,
298 NULL);
299 g_object_set_data (G_OBJECT (task), "signal-id", GINT_TO_POINTER (signal_id));
301 g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
302 g_variant_builder_add (&opt_builder, "{sv}", "handle_token", g_variant_new_string (token));
303 g_free (token);
305 opts = g_variant_builder_end (&opt_builder);
307 else
308 task = NULL;
310 file = g_file_new_for_uri (uri);
311 if (g_file_is_native (file))
313 char *path = NULL;
314 GUnixFDList *fd_list = NULL;
315 int fd, fd_id, errsv;
317 if (task)
318 g_object_set_data (G_OBJECT (task), "open-file", GINT_TO_POINTER (TRUE));
320 path = g_file_get_path (file);
321 fd = g_open (path, O_PATH | O_CLOEXEC);
322 errsv = errno;
323 if (fd == -1)
325 g_task_report_new_error (NULL, callback, user_data, NULL,
326 G_IO_ERROR, g_io_error_from_errno (errsv),
327 "OpenURI portal is not available");
328 return;
331 #ifndef HAVE_O_CLOEXEC
332 fcntl (fd, F_SETFD, FD_CLOEXEC);
333 #endif
334 fd_list = g_unix_fd_list_new_from_array (&fd, 1);
335 fd = -1;
336 fd_id = 0;
338 gxdp_open_uri_call_open_file (openuri,
339 parent_window ? parent_window : "",
340 g_variant_new ("h", fd_id),
341 opts,
342 fd_list,
343 cancellable,
344 task ? open_call_done : NULL,
345 task);
346 g_object_unref (fd_list);
347 g_free (path);
349 else
351 gxdp_open_uri_call_open_uri (openuri,
352 parent_window ? parent_window : "",
353 uri,
354 opts,
355 cancellable,
356 task ? open_call_done : NULL,
357 task);
360 g_object_unref (file);
363 gboolean
364 g_openuri_portal_open_uri_finish (GAsyncResult *result,
365 GError **error)
367 return g_task_propagate_boolean (G_TASK (result), error);