1 /*
2 * Copyright 2015 Red Hat, Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Matthias Clasen <mclasen@redhat.com>
20 */
21
22 #include "config.h"
23
24 #include <gio/gio.h>
25
26 #if defined(G_OS_UNIX) && !defined(__APPLE__)
27 #include <gio/gdesktopappinfo.h>
28 #endif
29
30 #include <gi18n.h>
31
32 #include "gio-tool.h"
33
34 static int n_outstanding = 0;
35 static gboolean success = TRUE;
36
37 static const GOptionEntry entries[] = {
38 G_OPTION_ENTRY_NULL
39 };
40
41 static void
42 launch_default_for_uri_cb (GObject *source_object,
43 GAsyncResult *res,
44 gpointer user_data)
45 {
46 GError *error = NULL;
47 gchar *uri = user_data;
48
49 if (!g_app_info_launch_default_for_uri_finish (res, &error))
50 {
51 print_error ("%s: %s", uri, error->message);
52 g_clear_error (&error);
53 success = FALSE;
54 }
55
56 n_outstanding--;
57
58 g_free (uri);
59 }
60
61 int
62 handle_open (int argc, char *argv[], gboolean do_help)
63 {
64 GOptionContext *context;
65 gchar *param;
66 GError *error = NULL;
67 int i;
68
69 g_set_prgname ("gio open");
70
71 /* Translators: commandline placeholder */
72 param = g_strdup_printf ("%s…", _("LOCATION"));
73 context = g_option_context_new (param);
74 g_free (param);
75 g_option_context_set_help_enabled (context, FALSE);
76 g_option_context_set_summary (context,
77 _("Open files with the default application that\n"
78 "is registered to handle files of this type."));
79 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
80
81 if (do_help)
82 {
83 show_help (context, NULL);
84 g_option_context_free (context);
85 return 0;
86 }
87
88 if (!g_option_context_parse (context, &argc, &argv, &error))
89 {
90 show_help (context, error->message);
91 g_error_free (error);
92 g_option_context_free (context);
93 return 1;
94 }
95
96 if (argc < 2)
97 {
98 show_help (context, _("No locations given"));
99 g_option_context_free (context);
100 return 1;
101 }
102
103 g_option_context_free (context);
104
105 for (i = 1; i < argc; i++)
106 {
107 char *uri = NULL;
108 char *uri_scheme;
109
110 /* Workaround to handle non-URI locations. We still use the original
111 * location for other cases, because GFile might modify the URI in ways
112 * we don't want. See:
113 * https://bugzilla.gnome.org/show_bug.cgi?id=779182 */
114 uri_scheme = g_uri_parse_scheme (argv[i]);
115 if (!uri_scheme || uri_scheme[0] == '\0')
116 {
117 GFile *file;
118
119 file = g_file_new_for_commandline_arg (argv[i]);
120 uri = g_file_get_uri (file);
121 g_object_unref (file);
122 }
123 else
124 uri = g_strdup (argv[i]);
125 g_free (uri_scheme);
126
127 g_app_info_launch_default_for_uri_async (uri,
128 NULL,
129 NULL,
130 launch_default_for_uri_cb,
131 g_strdup (uri));
132
133 n_outstanding++;
134
135 g_free (uri);
136 }
137
138 while (n_outstanding > 0)
139 g_main_context_iteration (NULL, TRUE);
140
141 return success ? 0 : 2;
142 }