Skip to content

Commit 0a2666f

Browse files
matthiasclasenfmuellner
authored andcommitted
Use OpenFile for local files
The OpenURI portal method no longer accepts file: uris.
1 parent 7805ba4 commit 0a2666f

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ PKG_PROG_PKG_CONFIG([0.24])
3232
LT_PREREQ([2.2.6])
3333
LT_INIT([disable-static])
3434

35-
PKG_CHECK_MODULES(FLATPAK_XDG_UTILS, [gio-2.0 >= 2.50])
35+
PKG_CHECK_MODULES(FLATPAK_XDG_UTILS, [gio-2.0 >= 2.50 gio-unix-2.0])
3636

3737
AC_CONFIG_FILES([
3838
Makefile

src/xdg-open.c

+59-13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include <gio/gio.h>
2222

2323
#include <glib/gi18n.h>
24+
#include <gio/gunixfdlist.h>
25+
#include <sys/stat.h>
26+
#include <fcntl.h>
27+
#include <errno.h>
2428

2529
#define PORTAL_BUS_NAME "org.freedesktop.portal.Desktop"
2630
#define PORTAL_OBJECT_PATH "/org/freedesktop/portal/desktop"
@@ -45,6 +49,7 @@ main (int argc, char *argv[])
4549
GError *error = NULL;
4650
GDBusConnection *connection;
4751
GVariantBuilder opt_builder;
52+
GFile *file;
4853

4954
context = g_option_context_new ("{ file | URL }");
5055

@@ -92,19 +97,59 @@ main (int argc, char *argv[])
9297

9398
g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
9499

95-
g_dbus_connection_call_sync (connection,
96-
PORTAL_BUS_NAME,
97-
PORTAL_OBJECT_PATH,
98-
PORTAL_IFACE_NAME,
99-
"OpenURI",
100-
g_variant_new ("(ss@a{sv})",
101-
"", *uris,
102-
g_variant_builder_end (&opt_builder)),
103-
NULL,
104-
G_DBUS_CALL_FLAGS_NONE,
105-
-1,
106-
NULL,
107-
&error);
100+
file = g_file_new_for_commandline_arg (uris[0]);
101+
if (g_file_is_native (file))
102+
{
103+
char *path;
104+
int fd;
105+
GUnixFDList *fd_list;
106+
107+
path = g_file_get_path (file);
108+
fd = open (path, O_PATH | O_CLOEXEC);
109+
if (fd == -1)
110+
{
111+
g_printerr ("Failed to open '%s': %s", path, g_strerror (errno));
112+
return 5;
113+
}
114+
115+
fd_list = g_unix_fd_list_new_from_array (&fd, 1);
116+
fd = -1;
117+
118+
g_dbus_connection_call_with_unix_fd_list_sync (connection,
119+
PORTAL_BUS_NAME,
120+
PORTAL_OBJECT_PATH,
121+
PORTAL_IFACE_NAME,
122+
"OpenFile",
123+
g_variant_new ("(sh@a{sv})",
124+
"", 0,
125+
g_variant_builder_end (&opt_builder)),
126+
NULL,
127+
G_DBUS_CALL_FLAGS_NONE,
128+
-1,
129+
fd_list,
130+
NULL,
131+
NULL,
132+
&error);
133+
134+
g_object_unref (fd_list);
135+
g_free (path);
136+
}
137+
else
138+
{
139+
g_dbus_connection_call_sync (connection,
140+
PORTAL_BUS_NAME,
141+
PORTAL_OBJECT_PATH,
142+
PORTAL_IFACE_NAME,
143+
"OpenURI",
144+
g_variant_new ("(ss@a{sv})",
145+
"", uris[0],
146+
g_variant_builder_end (&opt_builder)),
147+
NULL,
148+
G_DBUS_CALL_FLAGS_NONE,
149+
-1,
150+
NULL,
151+
&error);
152+
}
108153

109154
if (error)
110155
{
@@ -117,6 +162,7 @@ main (int argc, char *argv[])
117162
}
118163

119164
g_object_unref (connection);
165+
g_object_unref (file);
120166

121167
return 0;
122168
}

0 commit comments

Comments
 (0)