Merge branch 'doc-types' into 'master'
[glib.git] / gio / gio-tool-open.c
blob73863c7c504bf2e3a25fcd80d97474a7b6378c2c
1 /*
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>
20 #include "config.h"
22 #include <gio/gio.h>
24 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
25 #include <gio/gdesktopappinfo.h>
26 #endif
28 #include <gi18n.h>
30 #include "gio-tool.h"
33 static const GOptionEntry entries[] = {
34 { NULL }
37 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
38 static gboolean
39 get_bus_name_and_path_from_uri (const char *uri,
40 char **bus_name_out,
41 char **object_path_out)
43 GAppInfo *app_info = NULL;
44 char *bus_name = NULL;
45 char *object_path = NULL;
46 char *uri_scheme;
47 const char *filename;
48 char *basename = NULL;
49 char *p;
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);
55 g_free (uri_scheme);
57 if (app_info == NULL)
59 GFile *file;
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"))
68 goto out;
70 filename = g_desktop_app_info_get_filename (G_DESKTOP_APP_INFO (app_info));
71 if (filename == NULL)
72 goto out;
74 basename = g_path_get_basename (filename);
75 if (!g_str_has_suffix (basename, ".desktop"))
76 goto out;
78 basename[strlen (basename) - strlen (".desktop")] = '\0';
79 if (!g_dbus_is_name (basename))
80 goto out;
82 bus_name = g_strdup (basename);
83 object_path = g_strdup_printf ("/%s", bus_name);
84 for (p = object_path; *p != '\0'; p++)
85 if (*p == '.')
86 *p = '/';
88 *bus_name_out = g_steal_pointer (&bus_name);
89 *object_path_out = g_steal_pointer (&object_path);
90 got_name = TRUE;
92 out:
93 g_clear_object (&app_info);
94 g_clear_pointer (&basename, g_free);
96 return got_name;
98 #endif
101 handle_open (int argc, char *argv[], gboolean do_help)
103 GOptionContext *context;
104 gchar *param;
105 GError *error = NULL;
106 int i;
107 gboolean success;
108 gboolean res;
110 g_set_prgname ("gio open");
112 /* Translators: commandline placeholder */
113 param = g_strdup_printf ("%s…", _("LOCATION"));
114 context = g_option_context_new (param);
115 g_free (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);
122 if (do_help)
124 show_help (context, NULL);
125 g_option_context_free (context);
126 return 0;
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);
134 return 1;
137 if (argc < 2)
139 show_help (context, _("No locations given"));
140 g_option_context_free (context);
141 return 1;
144 g_option_context_free (context);
146 success = TRUE;
147 for (i = 1; i < argc; i++)
149 char *uri = NULL;
150 char *uri_scheme;
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')
159 GFile *file;
161 file = g_file_new_for_commandline_arg (argv[i]);
162 uri = g_file_get_uri (file);
163 g_object_unref (file);
165 g_free (uri_scheme);
167 res = g_app_info_launch_default_for_uri (uri ? uri : argv[i], NULL, &error);
168 if (!res)
170 print_error ("%s: %s", uri ? uri : argv[i], error->message);
171 g_clear_error (&error);
172 success = FALSE;
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
179 if (res)
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);
189 if (connection)
190 g_dbus_connection_call_sync (connection,
191 bus_name,
192 object_path,
193 "org.freedesktop.DBus.Peer",
194 "Ping",
195 NULL, NULL,
196 G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
197 g_clear_object (&connection);
198 g_free (bus_name);
199 g_free (object_path);
202 #endif
204 g_free (uri);
207 return success ? 0 : 2;