Branch data Line data Source code
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 : 0 : launch_default_for_uri_cb (GObject *source_object, 43 : : GAsyncResult *res, 44 : : gpointer user_data) 45 : : { 46 : 0 : GError *error = NULL; 47 : 0 : gchar *uri = user_data; 48 : : 49 [ # # ]: 0 : if (!g_app_info_launch_default_for_uri_finish (res, &error)) 50 : : { 51 : 0 : print_error ("%s: %s", uri, error->message); 52 : 0 : g_clear_error (&error); 53 : 0 : success = FALSE; 54 : : } 55 : : 56 : 0 : n_outstanding--; 57 : : 58 : 0 : g_free (uri); 59 : 0 : } 60 : : 61 : : int 62 : 0 : handle_open (int argc, char *argv[], gboolean do_help) 63 : : { 64 : : GOptionContext *context; 65 : : gchar *param; 66 : 0 : GError *error = NULL; 67 : : int i; 68 : : 69 : 0 : g_set_prgname ("gio open"); 70 : : 71 : : /* Translators: commandline placeholder */ 72 : 0 : param = g_strdup_printf ("%s…", _("LOCATION")); 73 : 0 : context = g_option_context_new (param); 74 : 0 : g_free (param); 75 : 0 : g_option_context_set_help_enabled (context, FALSE); 76 : 0 : g_option_context_set_summary (context, 77 : 0 : _("Open files with the default application that\n" 78 : : "is registered to handle files of this type.")); 79 : 0 : g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); 80 : : 81 [ # # ]: 0 : if (do_help) 82 : : { 83 : 0 : show_help (context, NULL); 84 : 0 : g_option_context_free (context); 85 : 0 : return 0; 86 : : } 87 : : 88 [ # # ]: 0 : if (!g_option_context_parse (context, &argc, &argv, &error)) 89 : : { 90 : 0 : show_help (context, error->message); 91 : 0 : g_error_free (error); 92 : 0 : g_option_context_free (context); 93 : 0 : return 1; 94 : : } 95 : : 96 [ # # ]: 0 : if (argc < 2) 97 : : { 98 : 0 : show_help (context, _("No locations given")); 99 : 0 : g_option_context_free (context); 100 : 0 : return 1; 101 : : } 102 : : 103 : 0 : g_option_context_free (context); 104 : : 105 [ # # ]: 0 : for (i = 1; i < argc; i++) 106 : : { 107 : 0 : 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 : 0 : uri_scheme = g_uri_parse_scheme (argv[i]); 115 [ # # # # ]: 0 : if (!uri_scheme || uri_scheme[0] == '\0') 116 : 0 : { 117 : : GFile *file; 118 : : 119 : 0 : file = g_file_new_for_commandline_arg (argv[i]); 120 : 0 : uri = g_file_get_uri (file); 121 : 0 : g_object_unref (file); 122 : : } 123 : : else 124 : 0 : uri = g_strdup (argv[i]); 125 : 0 : g_free (uri_scheme); 126 : : 127 : 0 : g_app_info_launch_default_for_uri_async (uri, 128 : : NULL, 129 : : NULL, 130 : : launch_default_for_uri_cb, 131 : 0 : g_strdup (uri)); 132 : : 133 : 0 : n_outstanding++; 134 : : 135 : 0 : g_free (uri); 136 : : } 137 : : 138 [ # # ]: 0 : while (n_outstanding > 0) 139 : 0 : g_main_context_iteration (NULL, TRUE); 140 : : 141 [ # # ]: 0 : return success ? 0 : 2; 142 : : }