Add a wrapper for the AddFull document portal api
[glib.git] / gio / gdocumentportal.c
blobe5d61ab86055d5eabb92130dbb46460e25b6c6e9
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2016 Endless Mobile, 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
16 * Public 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 "gdocumentportal.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
43 static GXdpDocuments *documents;
44 static char *documents_mountpoint;
46 static gboolean
47 init_document_portal (void)
49 static gsize documents_inited = 0;
51 if (g_once_init_enter (&documents_inited))
53 GError *error = NULL;
54 GDBusConnection *connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
56 if (connection != NULL)
58 documents = gxdp_documents_proxy_new_sync (connection, 0,
59 "org.freedesktop.portal.Documents",
60 "/org/freedesktop/portal/documents",
61 NULL, &error);
62 if (documents != NULL)
64 gxdp_documents_call_get_mount_point_sync (documents,
65 &documents_mountpoint,
66 NULL, &error);
68 if (error != NULL)
70 g_warning ("Cannot get document portal mount point: %s", error->message);
71 g_error_free (error);
74 else
76 g_warning ("Cannot create document portal proxy: %s", error->message);
77 g_error_free (error);
80 g_object_unref (connection);
82 else
84 g_warning ("Cannot connect to session bus when initializing document portal: %s",
85 error->message);
86 g_error_free (error);
89 g_once_init_leave (&documents_inited, 1);
92 return (documents != NULL && documents_mountpoint != NULL);
95 char *
96 g_document_portal_add_document (GFile *file,
97 GError **error)
99 char *doc_path, *basename;
100 char *doc_id = NULL;
101 char *doc_uri = NULL;
102 char *path = NULL;
103 GUnixFDList *fd_list = NULL;
104 int fd, fd_in;
105 gboolean ret;
107 if (!init_document_portal ())
109 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
110 "Document portal is not available");
111 goto out;
114 path = g_file_get_path (file);
115 fd = g_open (path, O_PATH | O_CLOEXEC);
117 if (fd == -1)
119 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
120 "Failed to open %s", path);
121 goto out;
124 #ifndef HAVE_O_CLOEXEC
125 fcntl (fd, F_SETFD, FD_CLOEXEC);
126 #endif
128 fd_list = g_unix_fd_list_new ();
129 fd_in = g_unix_fd_list_append (fd_list, fd, error);
130 g_close (fd, NULL);
132 if (fd_in == -1)
133 goto out;
135 ret = gxdp_documents_call_add_sync (documents,
136 g_variant_new_handle (fd_in),
137 TRUE,
138 TRUE,
139 fd_list,
140 &doc_id,
141 NULL,
142 NULL,
143 error);
145 if (!ret)
146 goto out;
148 basename = g_path_get_basename (path);
149 doc_path = g_build_filename (documents_mountpoint, doc_id, basename, NULL);
150 g_free (basename);
152 doc_uri = g_filename_to_uri (doc_path, NULL, NULL);
153 g_free (doc_path);
155 out:
156 if (fd_list)
157 g_object_unref (fd_list);
158 g_free (path);
159 g_free (doc_id);
161 return doc_uri;
164 /* Flags accepted by org.freedesktop.portal.Documents.AddFull */
165 enum {
166 XDP_ADD_FLAGS_REUSE_EXISTING = (1 << 0),
167 XDP_ADD_FLAGS_PERSISTENT = (1 << 1),
168 XDP_ADD_FLAGS_AS_NEEDED_BY_APP = (1 << 2),
169 XDP_ADD_FLAGS_FLAGS_ALL = ((1 << 3) - 1)
172 GList *
173 g_document_portal_add_documents (GList *uris,
174 const char *app_id,
175 GError **error)
177 int length;
178 GList *ruris = NULL;
179 gboolean *as_is;
180 GVariantBuilder builder;
181 GUnixFDList *fd_list = NULL;
182 GList *l;
183 gsize i, j;
184 const char *permissions[] = { "read", "write", NULL };
185 char **doc_ids = NULL;
186 GVariant *extra_out = NULL;
188 if (!init_document_portal ())
190 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
191 "Document portal is not available");
192 return NULL;
195 length = g_list_length (uris);
196 as_is = g_new0 (gboolean, length);
198 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ah"));
200 fd_list = g_unix_fd_list_new ();
201 for (l = uris, i = 0; l; l = l->next, i++)
203 const char *uri = l->data;
204 int idx = -1;
205 g_autofree char *path = NULL;
207 path = g_filename_from_uri (uri, NULL, NULL);
208 if (path != NULL)
210 int fd;
212 fd = g_open (path, O_CLOEXEC | O_PATH);
213 if (fd >= 0)
215 #ifndef HAVE_O_CLOEXEC
216 fcntl (fd, F_SETFD, FD_CLOEXEC);
217 #endif
218 idx = g_unix_fd_list_append (fd_list, fd, NULL);
219 close (fd);
223 if (idx != -1)
224 g_variant_builder_add (&builder, "h", idx);
225 else
226 as_is[i] = TRUE;
229 if (g_unix_fd_list_get_length (fd_list) > 0)
231 if (!gxdp_documents_call_add_full_sync (documents,
232 g_variant_builder_end (&builder),
233 XDP_ADD_FLAGS_AS_NEEDED_BY_APP,
234 app_id,
235 permissions,
236 fd_list,
237 &doc_ids,
238 &extra_out,
239 NULL,
240 NULL,
241 error))
242 goto out;
244 for (l = uris, i = 0, j = 0; l; l = l->next, i++)
246 const char *uri = l->data;
247 char *ruri;
249 if (as_is[i]) /* use as-is, not a file uri */
251 ruri = g_strdup (uri);
253 else if (strcmp (doc_ids[j], "") == 0) /* not rewritten */
255 ruri = g_strdup (uri);
256 j++;
258 else
260 char *basename = g_path_get_basename (uri + strlen ("file:"));
261 char *doc_path = g_build_filename (documents_mountpoint, doc_ids[j], basename, NULL);
262 ruri = g_strconcat ("file:", doc_path, NULL);
263 g_free (basename);
264 g_free (doc_path);
265 j++;
268 ruris = g_list_prepend (ruris, ruri);
271 ruris = g_list_reverse (ruris);
273 else
275 ruris = g_list_copy_deep (uris, (GCopyFunc)g_strdup, NULL);
278 out:
279 g_clear_object (&fd_list);
280 g_clear_pointer (&extra_out, g_variant_unref);
281 g_clear_pointer (&doc_ids, g_strfreev);
282 g_free (as_is);
284 return ruris;