2 * Copyright 2020 Frederic Martinsons
4 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
19 * Author: Frederic Martinsons <frederic.martinsons@sigfox.com>
26 #if defined(G_OS_UNIX) && !defined(__APPLE__)
27 #include <gio/gdesktopappinfo.h>
34 static const GOptionEntry entries[] = {
39 handle_launch (int argc, char *argv[], gboolean do_help)
41 GOptionContext *context;
43 #if defined(G_OS_UNIX) && !defined(__APPLE__)
46 GAppLaunchContext *app_context = NULL;
47 GKeyFile *keyfile = NULL;
49 char *desktop_file = NULL;
53 g_set_prgname ("gio launch");
55 /* Translators: commandline placeholder */
56 context = g_option_context_new (_("DESKTOP-FILE [FILE-ARG …]"));
57 g_option_context_set_help_enabled (context, FALSE);
58 g_option_context_set_summary (context,
59 _("Launch an application from a desktop file, passing optional filename arguments to it."));
60 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
64 show_help (context, NULL);
65 g_option_context_free (context);
69 if (!g_option_context_parse (context, &argc, &argv, &error))
71 show_help (context, error->message);
73 g_option_context_free (context);
79 show_help (context, _("No desktop file given"));
80 g_option_context_free (context);
84 g_option_context_free (context);
86 #if !defined(G_OS_UNIX) || defined(__APPLE__)
87 print_error (_("The launch command is not currently supported on this platform"));
91 desktop_file = argv[1];
93 /* Use keyfile api for loading desktop app in order to check for
94 * - not existing file.
95 * - invalid keyfile format.
97 keyfile = g_key_file_new ();
98 if (!g_key_file_load_from_file (keyfile, desktop_file, G_KEY_FILE_NONE, &error))
100 print_error (_("Unable to load ‘%s‘: %s"), desktop_file, error->message);
101 g_clear_error (&error);
106 app = (GAppInfo*)g_desktop_app_info_new_from_keyfile (keyfile);
109 print_error (_("Unable to load application information for ‘%s‘"), desktop_file);
114 for (i = 2; i < argc; i++)
116 args = g_list_append (args, g_file_new_for_commandline_arg (argv[i]));
118 app_context = g_app_launch_context_new ();
119 if (!g_app_info_launch (app, args, app_context, &error))
121 print_error (_("Unable to launch application ‘%s’: %s"), desktop_file, error->message);
122 g_clear_error (&error);
125 g_list_free_full (args, g_object_unref);
126 g_clear_object (&app_context);
128 g_clear_object (&app);
130 g_key_file_free (keyfile);