1/*
2 * Copyright 2015 Red Hat, Inc.
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 * Author: Matthias Clasen <mclasen@redhat.com>
18 */
19
20#include "config.h"
21
22#include <gio/gio.h>
23
24#if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
25#include <gio/gdesktopappinfo.h>
26#endif
27
28#include <gi18n.h>
29
30#include "gio-tool.h"
31
32static int n_outstanding = 0;
33static gboolean success = TRUE;
34
35static const GOptionEntry entries[] = {
36 { NULL }
37};
38
39static void
40launch_default_for_uri_cb (GObject *source_object,
41 GAsyncResult *res,
42 gpointer user_data)
43{
44 GError *error = NULL;
45 gchar *uri = user_data;
46
47 if (!g_app_info_launch_default_for_uri_finish (result: res, error: &error))
48 {
49 print_error (format: "%s: %s", uri, error->message);
50 g_clear_error (err: &error);
51 success = FALSE;
52 }
53
54 n_outstanding--;
55
56 g_free (mem: uri);
57}
58
59int
60handle_open (int argc, char *argv[], gboolean do_help)
61{
62 GOptionContext *context;
63 gchar *param;
64 GError *error = NULL;
65 int i;
66
67 g_set_prgname (prgname: "gio open");
68
69 /* Translators: commandline placeholder */
70 param = g_strdup_printf (format: "%s…", _("LOCATION"));
71 context = g_option_context_new (parameter_string: param);
72 g_free (mem: param);
73 g_option_context_set_help_enabled (context, FALSE);
74 g_option_context_set_summary (context,
75 _("Open files with the default application that\n"
76 "is registered to handle files of this type."));
77 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
78
79 if (do_help)
80 {
81 show_help (context, NULL);
82 g_option_context_free (context);
83 return 0;
84 }
85
86 if (!g_option_context_parse (context, argc: &argc, argv: &argv, error: &error))
87 {
88 show_help (context, message: error->message);
89 g_error_free (error);
90 g_option_context_free (context);
91 return 1;
92 }
93
94 if (argc < 2)
95 {
96 show_help (context, _("No locations given"));
97 g_option_context_free (context);
98 return 1;
99 }
100
101 g_option_context_free (context);
102
103 for (i = 1; i < argc; i++)
104 {
105 char *uri = NULL;
106 char *uri_scheme;
107
108 /* Workaround to handle non-URI locations. We still use the original
109 * location for other cases, because GFile might modify the URI in ways
110 * we don't want. See:
111 * https://bugzilla.gnome.org/show_bug.cgi?id=779182 */
112 uri_scheme = g_uri_parse_scheme (uri: argv[i]);
113 if (!uri_scheme || uri_scheme[0] == '\0')
114 {
115 GFile *file;
116
117 file = g_file_new_for_commandline_arg (arg: argv[i]);
118 uri = g_file_get_uri (file);
119 g_object_unref (object: file);
120 }
121 else
122 uri = g_strdup (str: argv[i]);
123 g_free (mem: uri_scheme);
124
125 g_app_info_launch_default_for_uri_async (uri,
126 NULL,
127 NULL,
128 callback: launch_default_for_uri_cb,
129 user_data: g_strdup (str: uri));
130
131 n_outstanding++;
132
133 g_free (mem: uri);
134 }
135
136 while (n_outstanding > 0)
137 g_main_context_iteration (NULL, TRUE);
138
139 return success ? 0 : 2;
140}
141

source code of gtk/subprojects/glib/gio/gio-tool-open.c