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/>.
26 #include "gdocumentportal.h"
31 #include "gunixfdlist.h"
37 #define HAVE_O_CLOEXEC 1
41 get_document_portal (GXdpDocuments **documents,
42 char **documents_mountpoint,
45 GDBusConnection *connection = NULL;
48 *documents_mountpoint = NULL;
50 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
51 if (connection == NULL)
53 g_prefix_error (error, "Cannot connect to session bus when initializing document portal: ");
57 *documents = gxdp_documents_proxy_new_sync (connection,
58 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
59 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
60 "org.freedesktop.portal.Documents",
61 "/org/freedesktop/portal/documents",
63 if (*documents == NULL)
65 g_prefix_error (error, "Cannot create document portal proxy: ");
69 if (!gxdp_documents_call_get_mount_point_sync (*documents,
73 g_clear_object (documents);
74 g_prefix_error (error, "Cannot get document portal mount point: ");
79 g_clear_object (&connection);
80 return *documents != NULL;
83 /* Flags accepted by org.freedesktop.portal.Documents.AddFull */
85 XDP_ADD_FLAGS_REUSE_EXISTING = (1 << 0),
86 XDP_ADD_FLAGS_PERSISTENT = (1 << 1),
87 XDP_ADD_FLAGS_AS_NEEDED_BY_APP = (1 << 2),
88 XDP_ADD_FLAGS_FLAGS_ALL = ((1 << 3) - 1)
92 g_document_portal_add_documents (GList *uris,
96 GXdpDocuments *documents = NULL;
97 char *documents_mountpoint = NULL;
101 GVariantBuilder builder;
102 GUnixFDList *fd_list = NULL;
105 const char *permissions[] = { "read", "write", NULL };
106 char **doc_ids = NULL;
107 GVariant *extra_out = NULL;
109 if (!get_document_portal (&documents, &documents_mountpoint, error))
114 length = g_list_length (uris);
115 as_is = g_new0 (gboolean, length);
117 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ah"));
119 fd_list = g_unix_fd_list_new ();
120 for (l = uris, i = 0; l; l = l->next, i++)
122 const char *uri = l->data;
126 path = g_filename_from_uri (uri, NULL, NULL);
131 fd = g_open (path, O_CLOEXEC | O_RDWR);
132 if (fd == -1 && (errno == EACCES || errno == EISDIR))
134 /* If we don't have write access, fall back to read-only,
135 * and stop requesting the write permission */
136 fd = g_open (path, O_CLOEXEC | O_RDONLY);
137 permissions[1] = NULL;
141 #ifndef HAVE_O_CLOEXEC
142 fcntl (fd, F_SETFD, FD_CLOEXEC);
144 idx = g_unix_fd_list_append (fd_list, fd, NULL);
152 g_variant_builder_add (&builder, "h", idx);
157 if (g_unix_fd_list_get_length (fd_list) > 0)
159 if (!gxdp_documents_call_add_full_sync (documents,
160 g_variant_builder_end (&builder),
161 XDP_ADD_FLAGS_AS_NEEDED_BY_APP,
172 for (l = uris, i = 0, j = 0; l; l = l->next, i++)
174 const char *uri = l->data;
177 if (as_is[i]) /* use as-is, not a file uri */
179 ruri = g_strdup (uri);
181 else if (strcmp (doc_ids[j], "") == 0) /* not rewritten */
183 ruri = g_strdup (uri);
188 char *basename = g_path_get_basename (uri + strlen ("file:"));
189 char *doc_path = g_build_filename (documents_mountpoint, doc_ids[j], basename, NULL);
190 ruri = g_strconcat ("file:", doc_path, NULL);
196 ruris = g_list_prepend (ruris, ruri);
199 ruris = g_list_reverse (ruris);
203 ruris = g_list_copy_deep (uris, (GCopyFunc)g_strdup, NULL);
207 g_clear_object (&documents);
208 g_clear_pointer (&documents_mountpoint, g_free);
209 g_clear_object (&fd_list);
210 g_clear_pointer (&extra_out, g_variant_unref);
211 g_clear_pointer (&doc_ids, g_strfreev);