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 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/>.
25 #include "gdocumentportal.h"
30 #include "gunixfdlist.h"
39 #define HAVE_O_CLOEXEC 1
42 static GXdpDocuments
*documents
;
43 static char *documents_mountpoint
;
46 init_document_portal (void)
48 static gsize documents_inited
= 0;
50 if (g_once_init_enter (&documents_inited
))
53 GDBusConnection
*connection
= g_bus_get_sync (G_BUS_TYPE_SESSION
, NULL
, &error
);
55 if (connection
!= NULL
)
57 documents
= gxdp_documents_proxy_new_sync (connection
, 0,
58 "org.freedesktop.portal.Documents",
59 "/org/freedesktop/portal/documents",
61 if (documents
!= NULL
)
63 gxdp_documents_call_get_mount_point_sync (documents
,
64 &documents_mountpoint
,
69 g_warning ("Cannot get document portal mount point: %s", error
->message
);
75 g_warning ("Cannot create document portal proxy: %s", error
->message
);
79 g_object_unref (connection
);
83 g_warning ("Cannot connect to session bus when initializing document portal: %s",
88 g_once_init_leave (&documents_inited
, 1);
91 return (documents
!= NULL
&& documents_mountpoint
!= NULL
);
95 g_document_portal_add_document (GFile
*file
,
98 char *doc_path
, *basename
;
100 char *doc_uri
= NULL
;
102 GUnixFDList
*fd_list
= NULL
;
106 if (!init_document_portal ())
108 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
109 "Document portal is not available");
113 path
= g_file_get_path (file
);
114 fd
= g_open (path
, O_PATH
| O_CLOEXEC
);
118 g_set_error (error
, G_IO_ERROR
, g_io_error_from_errno (errno
),
119 "Failed to open %s", path
);
123 #ifndef HAVE_O_CLOEXEC
124 fcntl (fd
, F_SETFD
, FD_CLOEXEC
);
127 fd_list
= g_unix_fd_list_new ();
128 fd_in
= g_unix_fd_list_append (fd_list
, fd
, error
);
134 ret
= gxdp_documents_call_add_sync (documents
,
135 g_variant_new_handle (fd_in
),
147 basename
= g_path_get_basename (path
);
148 doc_path
= g_build_filename (documents_mountpoint
, doc_id
, basename
, NULL
);
151 doc_uri
= g_filename_to_uri (doc_path
, NULL
, NULL
);
156 g_object_unref (fd_list
);