Branch data Line data Source code
1 : : /* 2 : : * Copyright 2020 Frederic Martinsons 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: Frederic Martinsons <frederic.martinsons@sigfox.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 const GOptionEntry entries[] = { 35 : : G_OPTION_ENTRY_NULL 36 : : }; 37 : : 38 : : int 39 : 0 : handle_launch (int argc, char *argv[], gboolean do_help) 40 : : { 41 : : GOptionContext *context; 42 : 0 : GError *error = NULL; 43 : : #if defined(G_OS_UNIX) && !defined(__APPLE__) 44 : : int i; 45 : 0 : GAppInfo *app = NULL; 46 : 0 : GAppLaunchContext *app_context = NULL; 47 : 0 : GKeyFile *keyfile = NULL; 48 : 0 : GList *args = NULL; 49 : 0 : char *desktop_file = NULL; 50 : : #endif 51 : : int retval; 52 : : 53 : 0 : g_set_prgname ("gio launch"); 54 : : 55 : : /* Translators: commandline placeholder */ 56 : 0 : context = g_option_context_new (_("DESKTOP-FILE [FILE-ARG …]")); 57 : 0 : g_option_context_set_help_enabled (context, FALSE); 58 : 0 : g_option_context_set_summary (context, 59 : 0 : _("Launch an application from a desktop file, passing optional filename arguments to it.")); 60 : 0 : g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); 61 : : 62 [ # # ]: 0 : if (do_help) 63 : : { 64 : 0 : show_help (context, NULL); 65 : 0 : g_option_context_free (context); 66 : 0 : return 0; 67 : : } 68 : : 69 [ # # ]: 0 : if (!g_option_context_parse (context, &argc, &argv, &error)) 70 : : { 71 : 0 : show_help (context, error->message); 72 : 0 : g_error_free (error); 73 : 0 : g_option_context_free (context); 74 : 0 : return 1; 75 : : } 76 : : 77 [ # # ]: 0 : if (argc < 2) 78 : : { 79 : 0 : show_help (context, _("No desktop file given")); 80 : 0 : g_option_context_free (context); 81 : 0 : return 1; 82 : : } 83 : : 84 : 0 : g_option_context_free (context); 85 : : 86 : : #if !defined(G_OS_UNIX) || defined(__APPLE__) 87 : : print_error (_("The launch command is not currently supported on this platform")); 88 : : retval = 1; 89 : : #else 90 : 0 : retval = 0; 91 : 0 : desktop_file = argv[1]; 92 : : 93 : : /* Use keyfile api for loading desktop app in order to check for 94 : : * - not existing file. 95 : : * - invalid keyfile format. 96 : : */ 97 : 0 : keyfile = g_key_file_new (); 98 [ # # ]: 0 : if (!g_key_file_load_from_file (keyfile, desktop_file, G_KEY_FILE_NONE, &error)) 99 : : { 100 : 0 : print_error (_("Unable to load ‘%s‘: %s"), desktop_file, error->message); 101 : 0 : g_clear_error (&error); 102 : 0 : retval = 1; 103 : : } 104 : : else 105 : : { 106 : 0 : app = (GAppInfo*)g_desktop_app_info_new_from_keyfile (keyfile); 107 [ # # ]: 0 : if (!app) 108 : : { 109 : 0 : print_error (_("Unable to load application information for ‘%s‘"), desktop_file); 110 : 0 : retval = 1; 111 : : } 112 : : else 113 : : { 114 [ # # ]: 0 : for (i = 2; i < argc; i++) 115 : : { 116 : 0 : args = g_list_append (args, g_file_new_for_commandline_arg (argv[i])); 117 : : } 118 : 0 : app_context = g_app_launch_context_new (); 119 [ # # ]: 0 : if (!g_app_info_launch (app, args, app_context, &error)) 120 : : { 121 : 0 : print_error (_("Unable to launch application ‘%s’: %s"), desktop_file, error->message); 122 : 0 : g_clear_error (&error); 123 : 0 : retval = 1; 124 : : } 125 : 0 : g_list_free_full (args, g_object_unref); 126 : 0 : g_clear_object (&app_context); 127 : : } 128 : 0 : g_clear_object (&app); 129 : : } 130 : 0 : g_key_file_free (keyfile); 131 : : #endif 132 : 0 : return retval; 133 : : }