]> Pileus Git - ~andy/gtk/blob - gtk/gtk-launch.c
Merge branch 'bgo687196-filesystemmodel-crash'
[~andy/gtk] / gtk / gtk-launch.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2012 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Tomas Bzatek <tbzatek@redhat.com>
19  */
20
21 #include <config.h>
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <locale.h>
26 #include <errno.h>
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gio/gio.h>
31 #ifdef G_OS_UNIX
32 #include <gio/gdesktopappinfo.h>
33 #endif
34 #include <gtk.h>
35
36 static gchar **args = NULL;
37
38 static GOptionEntry entries[] = {
39   { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, NULL },
40   { NULL}
41 };
42
43 int
44 main (int argc, char *argv[])
45 {
46   GError *error = NULL;
47   GOptionContext *context = NULL;
48   gchar *summary;
49   gchar *app_name;
50 #ifdef G_OS_UNIX
51   gchar *desktop_file_name;
52 #endif
53   GAppInfo *info = NULL;
54   GAppLaunchContext *launch_context;
55   GList *l;
56   GFile *f;
57
58   setlocale (LC_ALL, "");
59
60 #ifdef ENABLE_NLS
61   bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
62   textdomain (GETTEXT_PACKAGE);
63 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
64   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
65 #endif
66 #endif
67
68   g_type_init ();
69
70   /* Translators: this message will appear immediately after the */
71   /* usage string - Usage: COMMAND [OPTION]... <THIS_MESSAGE>    */
72   context =
73     g_option_context_new (_("APPLICATION [URI...] - launch an APPLICATION with URI."));
74
75   /* Translators: this message will appear after the usage string */
76   /* and before the list of options.                              */
77   summary = _("Launch specified application by its desktop file info\n"
78               "optionally passing list of URIs as arguments.");
79   g_option_context_set_summary (context, summary);
80   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
81   g_option_context_add_group (context, gtk_get_option_group (FALSE));
82
83   g_option_context_parse (context, &argc, &argv, &error);
84
85   g_option_context_free (context);
86
87   if (error != NULL)
88     {
89       g_printerr (_("Error parsing commandline options: %s\n"), error->message);
90       g_printerr ("\n");
91       g_printerr (_("Try \"%s --help\" for more information."),
92                   g_get_prgname ());
93       g_printerr ("\n");
94       g_error_free(error);
95       return 1;
96     }
97
98   if (!args)
99     {
100       /* Translators: the %s is the program name. This error message */
101       /* means the user is calling gtk-launch without any argument.  */
102       g_printerr (_("%s: missing application name"), g_get_prgname ());
103       g_printerr ("\n");
104       g_printerr (_("Try \"%s --help\" for more information."),
105                   g_get_prgname ());
106       g_printerr ("\n");
107       return 1;
108     }
109
110
111   gtk_init (&argc, &argv);
112
113   app_name = *args;
114 #ifdef G_OS_UNIX
115   if (g_str_has_suffix (app_name, ".desktop"))
116     desktop_file_name = g_strdup (app_name);
117   else 
118     desktop_file_name = g_strconcat (app_name, ".desktop", NULL);
119   info = G_APP_INFO (g_desktop_app_info_new (desktop_file_name));
120   g_free (desktop_file_name);
121 #else
122 #warning Please add support for creating AppInfo from id for your OS
123   g_printerr (_("Creating AppInfo from id not supported on non unix operating systems"));
124 #endif
125   args++;
126
127   if (!info)
128     {
129       /* Translators: the first %s is the program name, the second one */
130       /* is the application name.                                      */
131       g_printerr (_("%s: no such application %s"),
132                   g_get_prgname (), app_name);
133       g_printerr ("\n");
134       return 2;
135     }
136
137   l = NULL;
138   for (; *args; args++)
139     {
140       f = g_file_new_for_commandline_arg (*args);
141       l = g_list_append (l, f);
142     }
143
144   launch_context = (GAppLaunchContext*) gdk_display_get_app_launch_context (gdk_display_get_default ());
145   if (!g_app_info_launch (info, l, launch_context, &error))
146     {
147        /* Translators: the first %s is the program name, the second one  */
148        /* is the error message.                                          */
149        g_printerr (_("%s: error launching application: %s\n"),
150                    g_get_prgname (), error->message);
151        return 3;
152     }
153   g_object_unref (info);
154   g_object_unref (launch_context);
155   g_list_free_full (l, g_object_unref);
156
157   return 0;
158 }