7522d0b4dea0fffac3e804ad018f5614e4e944fd
[platform/upstream/glib.git] / gio / gdocumentportal.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright 2016 Endless Mobile, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include "config.h"
20
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <errno.h>
24
25 #include "gdocumentportal.h"
26 #include "xdp-dbus.h"
27 #include "gstdio.h"
28
29 #ifdef G_OS_UNIX
30 #include "gunixfdlist.h"
31 #endif
32
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
41
42 static GXdpDocuments *documents;
43 static char *documents_mountpoint;
44
45 static gboolean
46 init_document_portal (void)
47 {
48   static gsize documents_inited = 0;
49
50   if (g_once_init_enter (&documents_inited))
51     {
52       GError *error = NULL;
53       GDBusConnection *connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
54
55       if (connection != NULL)
56         {
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)
62             {
63               gxdp_documents_call_get_mount_point_sync (documents,
64                                                         &documents_mountpoint,
65                                                         NULL, &error);
66
67               if (error != NULL)
68                 {
69                   g_warning ("Cannot get document portal mount point: %s", error->message);
70                   g_error_free (error);
71                 }
72             }
73           else
74             {
75               g_warning ("Cannot create document portal proxy: %s", error->message);
76               g_error_free (error);
77             }
78
79           g_object_unref (connection);
80         }
81       else
82         {
83           g_warning ("Cannot connect to session bus when initializing document portal: %s",
84                      error->message);
85           g_error_free (error);
86         }
87
88       g_once_init_leave (&documents_inited, 1);
89     }
90
91   return (documents != NULL && documents_mountpoint != NULL);
92 }
93
94 char *
95 g_document_portal_add_document (GFile   *file,
96                                 GError **error)
97 {
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;
105
106   if (!init_document_portal ())
107     {
108       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
109                    "Document portal is not available");
110       goto out;
111     }
112
113   path = g_file_get_path (file);
114   fd = g_open (path, O_PATH | O_CLOEXEC);
115
116   if (fd == -1)
117     {
118       g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
119                    "Failed to open %s", path);
120       goto out;
121     }
122
123 #ifndef HAVE_O_CLOEXEC
124   fcntl (fd, F_SETFD, FD_CLOEXEC);
125 #endif
126
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);
130
131   if (fd_in == -1)
132     goto out;
133
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);
143
144   if (!ret)
145     goto out;
146
147   basename = g_path_get_basename (path);
148   doc_path = g_build_filename (documents_mountpoint, doc_id, basename, NULL);
149   g_free (basename);
150
151   doc_uri = g_filename_to_uri (doc_path, NULL, NULL);
152   g_free (doc_path);
153
154  out:
155   if (fd_list)
156     g_object_unref (fd_list);
157   g_free (path);
158   g_free (doc_id);
159
160   return doc_uri;
161 }