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