Add some more cases to the app-id unit tests
[glib.git] / gio / gdocumentportal.c
blob7522d0b4dea0fffac3e804ad018f5614e4e944fd
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/>.
19 #include "config.h"
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <errno.h>
25 #include "gdocumentportal.h"
26 #include "xdp-dbus.h"
27 #include "gstdio.h"
29 #ifdef G_OS_UNIX
30 #include "gunixfdlist.h"
31 #endif
33 #ifndef O_PATH
34 #define O_PATH 0
35 #endif
36 #ifndef O_CLOEXEC
37 #define O_CLOEXEC 0
38 #else
39 #define HAVE_O_CLOEXEC 1
40 #endif
42 static GXdpDocuments *documents;
43 static char *documents_mountpoint;
45 static gboolean
46 init_document_portal (void)
48 static gsize documents_inited = 0;
50 if (g_once_init_enter (&documents_inited))
52 GError *error = NULL;
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",
60 NULL, &error);
61 if (documents != NULL)
63 gxdp_documents_call_get_mount_point_sync (documents,
64 &documents_mountpoint,
65 NULL, &error);
67 if (error != NULL)
69 g_warning ("Cannot get document portal mount point: %s", error->message);
70 g_error_free (error);
73 else
75 g_warning ("Cannot create document portal proxy: %s", error->message);
76 g_error_free (error);
79 g_object_unref (connection);
81 else
83 g_warning ("Cannot connect to session bus when initializing document portal: %s",
84 error->message);
85 g_error_free (error);
88 g_once_init_leave (&documents_inited, 1);
91 return (documents != NULL && documents_mountpoint != NULL);
94 char *
95 g_document_portal_add_document (GFile *file,
96 GError **error)
98 char *doc_path, *basename;
99 char *doc_id = NULL;
100 char *doc_uri = NULL;
101 char *path = NULL;
102 GUnixFDList *fd_list = NULL;
103 int fd, fd_in;
104 gboolean ret;
106 if (!init_document_portal ())
108 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
109 "Document portal is not available");
110 goto out;
113 path = g_file_get_path (file);
114 fd = g_open (path, O_PATH | O_CLOEXEC);
116 if (fd == -1)
118 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
119 "Failed to open %s", path);
120 goto out;
123 #ifndef HAVE_O_CLOEXEC
124 fcntl (fd, F_SETFD, FD_CLOEXEC);
125 #endif
127 fd_list = g_unix_fd_list_new ();
128 fd_in = g_unix_fd_list_append (fd_list, fd, error);
129 g_close (fd, NULL);
131 if (fd_in == -1)
132 goto out;
134 ret = gxdp_documents_call_add_sync (documents,
135 g_variant_new_handle (fd_in),
136 TRUE,
137 TRUE,
138 fd_list,
139 &doc_id,
140 NULL,
141 NULL,
142 error);
144 if (!ret)
145 goto out;
147 basename = g_path_get_basename (path);
148 doc_path = g_build_filename (documents_mountpoint, doc_id, basename, NULL);
149 g_free (basename);
151 doc_uri = g_filename_to_uri (doc_path, NULL, NULL);
152 g_free (doc_path);
154 out:
155 if (fd_list)
156 g_object_unref (fd_list);
157 g_free (path);
158 g_free (doc_id);
160 return doc_uri;