Update Friulian translation
[glib.git] / gio / gdocumentportal.c
blob3e85bd22c8c89eed33065a09987a35d99f11df47
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, errsv;
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);
116 errsv = errno;
118 if (fd == -1)
120 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
121 "Failed to open %s", path);
122 goto out;
125 #ifndef HAVE_O_CLOEXEC
126 fcntl (fd, F_SETFD, FD_CLOEXEC);
127 #endif
129 fd_list = g_unix_fd_list_new ();
130 fd_in = g_unix_fd_list_append (fd_list, fd, error);
131 g_close (fd, NULL);
133 if (fd_in == -1)
134 goto out;
136 ret = gxdp_documents_call_add_sync (documents,
137 g_variant_new_handle (fd_in),
138 TRUE,
139 TRUE,
140 fd_list,
141 &doc_id,
142 NULL,
143 NULL,
144 error);
146 if (!ret)
147 goto out;
149 basename = g_path_get_basename (path);
150 doc_path = g_build_filename (documents_mountpoint, doc_id, basename, NULL);
151 g_free (basename);
153 doc_uri = g_filename_to_uri (doc_path, NULL, NULL);
154 g_free (doc_path);
156 out:
157 if (fd_list)
158 g_object_unref (fd_list);
159 g_free (path);
160 g_free (doc_id);
162 return doc_uri;
165 /* Flags accepted by org.freedesktop.portal.Documents.AddFull */
166 enum {
167 XDP_ADD_FLAGS_REUSE_EXISTING = (1 << 0),
168 XDP_ADD_FLAGS_PERSISTENT = (1 << 1),
169 XDP_ADD_FLAGS_AS_NEEDED_BY_APP = (1 << 2),
170 XDP_ADD_FLAGS_FLAGS_ALL = ((1 << 3) - 1)
173 GList *
174 g_document_portal_add_documents (GList *uris,
175 const char *app_id,
176 GError **error)
178 int length;
179 GList *ruris = NULL;
180 gboolean *as_is;
181 GVariantBuilder builder;
182 GUnixFDList *fd_list = NULL;
183 GList *l;
184 gsize i, j;
185 const char *permissions[] = { "read", "write", NULL };
186 char **doc_ids = NULL;
187 GVariant *extra_out = NULL;
189 if (!init_document_portal ())
191 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
192 "Document portal is not available");
193 return NULL;
196 length = g_list_length (uris);
197 as_is = g_new0 (gboolean, length);
199 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ah"));
201 fd_list = g_unix_fd_list_new ();
202 for (l = uris, i = 0; l; l = l->next, i++)
204 const char *uri = l->data;
205 int idx = -1;
206 g_autofree char *path = NULL;
208 path = g_filename_from_uri (uri, NULL, NULL);
209 if (path != NULL)
211 int fd;
213 fd = g_open (path, O_CLOEXEC | O_PATH);
214 if (fd >= 0)
216 #ifndef HAVE_O_CLOEXEC
217 fcntl (fd, F_SETFD, FD_CLOEXEC);
218 #endif
219 idx = g_unix_fd_list_append (fd_list, fd, NULL);
220 close (fd);
224 if (idx != -1)
225 g_variant_builder_add (&builder, "h", idx);
226 else
227 as_is[i] = TRUE;
230 if (g_unix_fd_list_get_length (fd_list) > 0)
232 if (!gxdp_documents_call_add_full_sync (documents,
233 g_variant_builder_end (&builder),
234 XDP_ADD_FLAGS_AS_NEEDED_BY_APP,
235 app_id,
236 permissions,
237 fd_list,
238 &doc_ids,
239 &extra_out,
240 NULL,
241 NULL,
242 error))
243 goto out;
245 for (l = uris, i = 0, j = 0; l; l = l->next, i++)
247 const char *uri = l->data;
248 char *ruri;
250 if (as_is[i]) /* use as-is, not a file uri */
252 ruri = g_strdup (uri);
254 else if (strcmp (doc_ids[j], "") == 0) /* not rewritten */
256 ruri = g_strdup (uri);
257 j++;
259 else
261 char *basename = g_path_get_basename (uri + strlen ("file:"));
262 char *doc_path = g_build_filename (documents_mountpoint, doc_ids[j], basename, NULL);
263 ruri = g_strconcat ("file:", doc_path, NULL);
264 g_free (basename);
265 g_free (doc_path);
266 j++;
269 ruris = g_list_prepend (ruris, ruri);
272 ruris = g_list_reverse (ruris);
274 else
276 ruris = g_list_copy_deep (uris, (GCopyFunc)g_strdup, NULL);
279 out:
280 g_clear_object (&fd_list);
281 g_clear_pointer (&extra_out, g_variant_unref);
282 g_clear_pointer (&doc_ids, g_strfreev);
283 g_free (as_is);
285 return ruris;