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
;
104 int fd
, fd_in
, errsv
;
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
);
120 g_set_error (error
, G_IO_ERROR
, g_io_error_from_errno (errsv
),
121 "Failed to open %s", path
);
125 #ifndef HAVE_O_CLOEXEC
126 fcntl (fd
, F_SETFD
, FD_CLOEXEC
);
129 fd_list
= g_unix_fd_list_new ();
130 fd_in
= g_unix_fd_list_append (fd_list
, fd
, error
);
136 ret
= gxdp_documents_call_add_sync (documents
,
137 g_variant_new_handle (fd_in
),
149 basename
= g_path_get_basename (path
);
150 doc_path
= g_build_filename (documents_mountpoint
, doc_id
, basename
, NULL
);
153 doc_uri
= g_filename_to_uri (doc_path
, NULL
, NULL
);
158 g_object_unref (fd_list
);
165 /* Flags accepted by org.freedesktop.portal.Documents.AddFull */
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)
174 g_document_portal_add_documents (GList
*uris
,
181 GVariantBuilder builder
;
182 GUnixFDList
*fd_list
= NULL
;
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");
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
;
208 path
= g_filename_from_uri (uri
, NULL
, NULL
);
213 fd
= g_open (path
, O_CLOEXEC
| O_PATH
);
216 #ifndef HAVE_O_CLOEXEC
217 fcntl (fd
, F_SETFD
, FD_CLOEXEC
);
219 idx
= g_unix_fd_list_append (fd_list
, fd
, NULL
);
227 g_variant_builder_add (&builder
, "h", idx
);
232 if (g_unix_fd_list_get_length (fd_list
) > 0)
234 if (!gxdp_documents_call_add_full_sync (documents
,
235 g_variant_builder_end (&builder
),
236 XDP_ADD_FLAGS_AS_NEEDED_BY_APP
,
247 for (l
= uris
, i
= 0, j
= 0; l
; l
= l
->next
, i
++)
249 const char *uri
= l
->data
;
252 if (as_is
[i
]) /* use as-is, not a file uri */
254 ruri
= g_strdup (uri
);
256 else if (strcmp (doc_ids
[j
], "") == 0) /* not rewritten */
258 ruri
= g_strdup (uri
);
263 char *basename
= g_path_get_basename (uri
+ strlen ("file:"));
264 char *doc_path
= g_build_filename (documents_mountpoint
, doc_ids
[j
], basename
, NULL
);
265 ruri
= g_strconcat ("file:", doc_path
, NULL
);
271 ruris
= g_list_prepend (ruris
, ruri
);
274 ruris
= g_list_reverse (ruris
);
278 ruris
= g_list_copy_deep (uris
, (GCopyFunc
)g_strdup
, NULL
);
282 g_clear_object (&fd_list
);
283 g_clear_pointer (&extra_out
, g_variant_unref
);
284 g_clear_pointer (&doc_ids
, g_strfreev
);