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"
40 #define HAVE_O_CLOEXEC 1
43 static GXdpDocuments
*documents
;
44 static char *documents_mountpoint
;
47 init_document_portal (void)
49 static gsize documents_inited
= 0;
51 if (g_once_init_enter (&documents_inited
))
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",
62 if (documents
!= NULL
)
64 gxdp_documents_call_get_mount_point_sync (documents
,
65 &documents_mountpoint
,
70 g_warning ("Cannot get document portal mount point: %s", error
->message
);
76 g_warning ("Cannot create document portal proxy: %s", error
->message
);
80 g_object_unref (connection
);
84 g_warning ("Cannot connect to session bus when initializing document portal: %s",
89 g_once_init_leave (&documents_inited
, 1);
92 return (documents
!= NULL
&& documents_mountpoint
!= NULL
);
96 g_document_portal_add_document (GFile
*file
,
99 char *doc_path
, *basename
;
101 char *doc_uri
= NULL
;
103 GUnixFDList
*fd_list
= NULL
;
107 if (!init_document_portal ())
109 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_NOT_INITIALIZED
,
110 "Document portal is not available");
114 path
= g_file_get_path (file
);
115 fd
= g_open (path
, O_PATH
| O_CLOEXEC
);
119 g_set_error (error
, G_IO_ERROR
, g_io_error_from_errno (errno
),
120 "Failed to open %s", path
);
124 #ifndef HAVE_O_CLOEXEC
125 fcntl (fd
, F_SETFD
, FD_CLOEXEC
);
128 fd_list
= g_unix_fd_list_new ();
129 fd_in
= g_unix_fd_list_append (fd_list
, fd
, error
);
135 ret
= gxdp_documents_call_add_sync (documents
,
136 g_variant_new_handle (fd_in
),
148 basename
= g_path_get_basename (path
);
149 doc_path
= g_build_filename (documents_mountpoint
, doc_id
, basename
, NULL
);
152 doc_uri
= g_filename_to_uri (doc_path
, NULL
, NULL
);
157 g_object_unref (fd_list
);
164 /* Flags accepted by org.freedesktop.portal.Documents.AddFull */
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)
173 g_document_portal_add_documents (GList
*uris
,
180 GVariantBuilder builder
;
181 GUnixFDList
*fd_list
= NULL
;
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");
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
;
205 g_autofree
char *path
= NULL
;
207 path
= g_filename_from_uri (uri
, NULL
, NULL
);
212 fd
= g_open (path
, O_CLOEXEC
| O_PATH
);
215 #ifndef HAVE_O_CLOEXEC
216 fcntl (fd
, F_SETFD
, FD_CLOEXEC
);
218 idx
= g_unix_fd_list_append (fd_list
, fd
, NULL
);
224 g_variant_builder_add (&builder
, "h", idx
);
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
,
244 for (l
= uris
, i
= 0, j
= 0; l
; l
= l
->next
, i
++)
246 const char *uri
= l
->data
;
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
);
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
);
268 ruris
= g_list_prepend (ruris
, ruri
);
271 ruris
= g_list_reverse (ruris
);
275 ruris
= g_list_copy_deep (uris
, (GCopyFunc
)g_strdup
, NULL
);
279 g_clear_object (&fd_list
);
280 g_clear_pointer (&extra_out
, g_variant_unref
);
281 g_clear_pointer (&doc_ids
, g_strfreev
);