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