2 * Copyright 2015 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Matthias Clasen <mclasen@redhat.com>
24 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
25 #include <gio/gdesktopappinfo.h>
33 static const GOptionEntry entries
[] = {
37 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
39 get_bus_name_and_path_from_uri (const char *uri
,
41 char **object_path_out
)
43 GAppInfo
*app_info
= NULL
;
44 char *bus_name
= NULL
;
45 char *object_path
= NULL
;
48 char *basename
= NULL
;
50 gboolean got_name
= FALSE
;
52 uri_scheme
= g_uri_parse_scheme (uri
);
53 if (uri_scheme
&& uri_scheme
[0] != '\0')
54 app_info
= g_app_info_get_default_for_uri_scheme (uri_scheme
);
61 file
= g_file_new_for_uri (uri
);
62 app_info
= g_file_query_default_handler (file
, NULL
, NULL
);
63 g_object_unref (file
);
66 if (app_info
== NULL
|| !G_IS_DESKTOP_APP_INFO (app_info
) ||
67 !g_desktop_app_info_get_boolean (G_DESKTOP_APP_INFO (app_info
), "DBusActivatable"))
70 filename
= g_desktop_app_info_get_filename (G_DESKTOP_APP_INFO (app_info
));
74 basename
= g_path_get_basename (filename
);
75 if (!g_str_has_suffix (basename
, ".desktop"))
78 basename
[strlen (basename
) - strlen (".desktop")] = '\0';
79 if (!g_dbus_is_name (basename
))
82 bus_name
= g_strdup (basename
);
83 object_path
= g_strdup_printf ("/%s", bus_name
);
84 for (p
= object_path
; *p
!= '\0'; p
++)
88 *bus_name_out
= g_steal_pointer (&bus_name
);
89 *object_path_out
= g_steal_pointer (&object_path
);
93 g_clear_object (&app_info
);
94 g_clear_pointer (&basename
, g_free
);
101 handle_open (int argc
, char *argv
[], gboolean do_help
)
103 GOptionContext
*context
;
105 GError
*error
= NULL
;
110 g_set_prgname ("gio open");
112 /* Translators: commandline placeholder */
113 param
= g_strdup_printf ("%s…", _("LOCATION"));
114 context
= g_option_context_new (param
);
116 g_option_context_set_help_enabled (context
, FALSE
);
117 g_option_context_set_summary (context
,
118 _("Open files with the default application that\n"
119 "is registered to handle files of this type."));
120 g_option_context_add_main_entries (context
, entries
, GETTEXT_PACKAGE
);
124 show_help (context
, NULL
);
125 g_option_context_free (context
);
129 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
131 show_help (context
, error
->message
);
132 g_error_free (error
);
133 g_option_context_free (context
);
139 show_help (context
, _("No locations given"));
140 g_option_context_free (context
);
144 g_option_context_free (context
);
147 for (i
= 1; i
< argc
; i
++)
152 /* Workaround to handle non-URI locations. We still use the original
153 * location for other cases, because GFile might modify the URI in ways
154 * we don't want. See:
155 * https://bugzilla.gnome.org/show_bug.cgi?id=779182 */
156 uri_scheme
= g_uri_parse_scheme (argv
[i
]);
157 if (!uri_scheme
|| uri_scheme
[0] == '\0')
161 file
= g_file_new_for_commandline_arg (argv
[i
]);
162 uri
= g_file_get_uri (file
);
163 g_object_unref (file
);
167 res
= g_app_info_launch_default_for_uri (uri
? uri
: argv
[i
], NULL
, &error
);
170 print_error ("%s: %s", uri
? uri
: argv
[i
], error
->message
);
171 g_clear_error (&error
);
175 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
176 /* FIXME: This chunk of madness is a workaround for a dbus-daemon bug.
177 * See https://bugzilla.gnome.org/show_bug.cgi?id=780296
181 char *bus_name
= NULL
;
182 char *object_path
= NULL
;
184 if (get_bus_name_and_path_from_uri (uri
? uri
: argv
[i
], &bus_name
, &object_path
))
186 GDBusConnection
*connection
;
187 connection
= g_bus_get_sync (G_BUS_TYPE_SESSION
, NULL
, NULL
);
190 g_dbus_connection_call_sync (connection
,
193 "org.freedesktop.DBus.Peer",
196 G_DBUS_CALL_FLAGS_NONE
, -1, NULL
, NULL
);
197 g_clear_object (&connection
);
199 g_free (object_path
);
207 return success
? 0 : 2;