]> Pileus Git - ~andy/gtk/blob - gtk/gtk-launch.c
Updated Persian translations
[~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 #include <gio/gdesktopappinfo.h>
32 #include <gtk.h>
33
34 static gchar **args = NULL;
35
36 static GOptionEntry entries[] = {
37   { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, NULL },
38   { NULL}
39 };
40
41 int
42 main (int argc, char *argv[])
43 {
44   GError *error = NULL;
45   GOptionContext *context = NULL;
46   gchar *summary;
47   gchar *app_name;
48   gchar *desktop_file_name;
49   GDesktopAppInfo *info;
50   GAppLaunchContext *launch_context;
51   GList *l;
52   GFile *f;
53
54   setlocale (LC_ALL, "");
55
56 #ifdef ENABLE_NLS
57   bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
58   textdomain (GETTEXT_PACKAGE);
59 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
60   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
61 #endif
62 #endif
63
64   g_type_init ();
65
66   /* Translators: this message will appear immediately after the */
67   /* usage string - Usage: COMMAND [OPTION]... <THIS_MESSAGE>    */
68   context =
69     g_option_context_new (_("APPLICATION [URI...] - launch an APPLICATION with URI."));
70
71   /* Translators: this message will appear after the usage string */
72   /* and before the list of options.                              */
73   summary = _("Launch specified application by its desktop file info\n"
74               "optionally passing list of URIs as arguments.");
75   g_option_context_set_summary (context, summary);
76   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
77   g_option_context_add_group (context, gtk_get_option_group (FALSE));
78
79   g_option_context_parse (context, &argc, &argv, &error);
80
81   g_option_context_free (context);
82
83   if (error != NULL)
84     {
85       g_printerr (_("Error parsing commandline options: %s\n"), error->message);
86       g_printerr ("\n");
87       g_printerr (_("Try \"%s --help\" for more information."),
88                   g_get_prgname ());
89       g_printerr ("\n");
90       g_error_free(error);
91       return 1;
92     }
93
94   if (!args)
95     {
96       /* Translators: the %s is the program name. This error message */
97       /* means the user is calling gtk-launch without any argument.  */
98       g_printerr (_("%s: missing application name"), g_get_prgname ());
99       g_printerr ("\n");
100       g_printerr (_("Try \"%s --help\" for more information."),
101                   g_get_prgname ());
102       g_printerr ("\n");
103       return 1;
104     }
105
106
107   gtk_init (&argc, &argv);
108
109   app_name = *args;
110   if (g_str_has_suffix (app_name, ".desktop"))
111     desktop_file_name = g_strdup (app_name);
112   else 
113     desktop_file_name = g_strconcat (app_name, ".desktop", NULL);
114   info = g_desktop_app_info_new (desktop_file_name);
115   g_free (desktop_file_name);
116   args++;
117
118   if (!info)
119     {
120       /* Translators: the first %s is the program name, the second one */
121       /* is the application name.                                      */
122       g_printerr (_("%s: no such application %s"),
123                   g_get_prgname (), app_name);
124       g_printerr ("\n");
125       return 2;
126     }
127
128   l = NULL;
129   for (; *args; args++)
130     {
131       f = g_file_new_for_commandline_arg (*args);
132       l = g_list_append (l, f);
133     }
134
135   launch_context = (GAppLaunchContext*) gdk_display_get_app_launch_context (gdk_display_get_default ());
136   if (!g_app_info_launch (G_APP_INFO (info), l, launch_context, &error))
137     {
138        /* Translators: the first %s is the program name, the second one  */
139        /* is the error message.                                          */
140        g_printerr (_("%s: error launching application: %s\n"),
141                    g_get_prgname (), error->message);
142        return 3;
143     }
144   g_object_unref (info);
145   g_object_unref (launch_context);
146   g_list_free_full (l, g_object_unref);
147
148   return 0;
149 }