2012-07-16 15:29:01 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Tomas Bzatek <tbzatek@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <gio/gio.h>
|
2017-03-14 20:00:37 +00:00
|
|
|
#if defined(HAVE_GIO_UNIX) && !defined(__APPLE__)
|
2012-07-16 15:29:01 +00:00
|
|
|
#include <gio/gdesktopappinfo.h>
|
2012-10-28 19:05:23 +00:00
|
|
|
#endif
|
2012-07-16 15:29:01 +00:00
|
|
|
#include <gtk.h>
|
|
|
|
|
2016-01-31 17:05:35 +00:00
|
|
|
static gboolean show_version;
|
2020-07-24 18:40:36 +00:00
|
|
|
static char **args = NULL;
|
2012-07-16 15:29:01 +00:00
|
|
|
|
|
|
|
static GOptionEntry entries[] = {
|
2016-09-11 14:51:19 +00:00
|
|
|
{ "version", 0, 0, G_OPTION_ARG_NONE, &show_version, N_("Show program version"), NULL },
|
2012-07-16 15:29:01 +00:00
|
|
|
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, NULL },
|
|
|
|
{ NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
GOptionContext *context = NULL;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *summary;
|
|
|
|
char *app_name;
|
2012-10-28 19:05:23 +00:00
|
|
|
#ifdef G_OS_UNIX
|
2020-07-24 18:40:36 +00:00
|
|
|
char *desktop_file_name;
|
|
|
|
char *bus_name = NULL;
|
2012-10-28 19:05:23 +00:00
|
|
|
#endif
|
|
|
|
GAppInfo *info = NULL;
|
2012-07-16 15:29:01 +00:00
|
|
|
GAppLaunchContext *launch_context;
|
|
|
|
GList *l;
|
|
|
|
GFile *f;
|
|
|
|
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
|
|
|
bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
|
|
|
|
textdomain (GETTEXT_PACKAGE);
|
|
|
|
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
|
|
|
|
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
|
|
|
|
#endif
|
|
|
|
|
2016-10-03 14:45:20 +00:00
|
|
|
context =
|
2012-07-16 15:29:01 +00:00
|
|
|
/* Translators: this message will appear immediately after the */
|
2016-10-03 16:20:03 +00:00
|
|
|
/* usage string - Usage: COMMAND [OPTION…] <THIS_MESSAGE> */
|
|
|
|
g_option_context_new (_("APPLICATION [URI…] — launch an APPLICATION"));
|
2012-07-16 15:29:01 +00:00
|
|
|
|
|
|
|
/* Translators: this message will appear after the usage string */
|
|
|
|
/* and before the list of options. */
|
2016-01-31 17:05:35 +00:00
|
|
|
summary = _("Launch an application (specified by its desktop file name),\n"
|
|
|
|
"optionally passing one or more URIs as arguments.");
|
2012-07-16 15:29:01 +00:00
|
|
|
g_option_context_set_summary (context, summary);
|
|
|
|
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
|
|
|
|
g_option_context_parse (context, &argc, &argv, &error);
|
|
|
|
|
|
|
|
g_option_context_free (context);
|
|
|
|
|
|
|
|
if (error != NULL)
|
|
|
|
{
|
|
|
|
g_printerr (_("Error parsing commandline options: %s\n"), error->message);
|
|
|
|
g_printerr ("\n");
|
2016-10-03 16:20:03 +00:00
|
|
|
g_printerr (_("Try “%s --help” for more information."), g_get_prgname ());
|
2012-07-16 15:29:01 +00:00
|
|
|
g_printerr ("\n");
|
2016-01-31 17:05:35 +00:00
|
|
|
g_error_free (error);
|
2012-07-16 15:29:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-01-31 17:05:35 +00:00
|
|
|
if (show_version)
|
|
|
|
{
|
|
|
|
g_print ("%d.%d.%d\n",
|
|
|
|
gtk_get_major_version (),
|
|
|
|
gtk_get_minor_version (),
|
|
|
|
gtk_get_micro_version ());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-16 15:29:01 +00:00
|
|
|
if (!args)
|
|
|
|
{
|
|
|
|
/* Translators: the %s is the program name. This error message */
|
|
|
|
/* means the user is calling gtk-launch without any argument. */
|
|
|
|
g_printerr (_("%s: missing application name"), g_get_prgname ());
|
|
|
|
g_printerr ("\n");
|
2016-10-03 16:20:03 +00:00
|
|
|
g_printerr (_("Try “%s --help” for more information."), g_get_prgname ());
|
2012-07-16 15:29:01 +00:00
|
|
|
g_printerr ("\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2012-07-16 15:29:01 +00:00
|
|
|
|
|
|
|
app_name = *args;
|
2017-03-14 20:00:37 +00:00
|
|
|
#if defined(HAVE_GIO_UNIX) && !defined(__APPLE__)
|
2016-03-04 15:02:34 +00:00
|
|
|
bus_name = g_strdup (app_name);
|
2012-07-16 15:29:01 +00:00
|
|
|
if (g_str_has_suffix (app_name, ".desktop"))
|
2016-03-04 15:02:34 +00:00
|
|
|
{
|
|
|
|
desktop_file_name = g_strdup (app_name);
|
|
|
|
bus_name[strlen (bus_name) - strlen(".desktop")] = '\0';
|
|
|
|
}
|
2016-01-31 17:05:35 +00:00
|
|
|
else
|
2016-03-04 15:02:34 +00:00
|
|
|
{
|
|
|
|
desktop_file_name = g_strconcat (app_name, ".desktop", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_dbus_is_name (bus_name))
|
|
|
|
g_clear_pointer (&bus_name, g_free);
|
2012-10-28 19:05:23 +00:00
|
|
|
info = G_APP_INFO (g_desktop_app_info_new (desktop_file_name));
|
2012-07-16 15:29:01 +00:00
|
|
|
g_free (desktop_file_name);
|
2012-10-28 19:05:23 +00:00
|
|
|
#else
|
|
|
|
#warning Please add support for creating AppInfo from id for your OS
|
|
|
|
g_printerr (_("Creating AppInfo from id not supported on non unix operating systems"));
|
|
|
|
#endif
|
2012-07-16 15:29:01 +00:00
|
|
|
args++;
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
{
|
|
|
|
/* Translators: the first %s is the program name, the second one */
|
|
|
|
/* is the application name. */
|
|
|
|
g_printerr (_("%s: no such application %s"),
|
|
|
|
g_get_prgname (), app_name);
|
|
|
|
g_printerr ("\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
l = NULL;
|
|
|
|
for (; *args; args++)
|
|
|
|
{
|
|
|
|
f = g_file_new_for_commandline_arg (*args);
|
|
|
|
l = g_list_append (l, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
launch_context = (GAppLaunchContext*) gdk_display_get_app_launch_context (gdk_display_get_default ());
|
2012-10-28 19:05:23 +00:00
|
|
|
if (!g_app_info_launch (info, l, launch_context, &error))
|
2012-07-16 15:29:01 +00:00
|
|
|
{
|
|
|
|
/* Translators: the first %s is the program name, the second one */
|
|
|
|
/* is the error message. */
|
|
|
|
g_printerr (_("%s: error launching application: %s\n"),
|
|
|
|
g_get_prgname (), error->message);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
g_object_unref (info);
|
|
|
|
g_object_unref (launch_context);
|
2016-03-04 15:02:34 +00:00
|
|
|
|
|
|
|
#ifdef G_OS_UNIX
|
|
|
|
if (bus_name != NULL)
|
|
|
|
{
|
|
|
|
GDBusConnection *connection;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *object_path, *p;
|
2016-03-04 15:02:34 +00:00
|
|
|
|
|
|
|
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
|
|
|
|
|
|
|
|
object_path = g_strdup_printf ("/%s", bus_name);
|
|
|
|
for (p = object_path; *p != '\0'; p++)
|
|
|
|
if (*p == '.')
|
|
|
|
*p = '/';
|
|
|
|
|
|
|
|
if (connection)
|
|
|
|
g_dbus_connection_call_sync (connection,
|
|
|
|
bus_name,
|
|
|
|
object_path,
|
|
|
|
"org.freedesktop.DBus.Peer",
|
|
|
|
"Ping",
|
|
|
|
NULL, NULL,
|
|
|
|
G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
|
|
|
|
g_clear_pointer (&object_path, g_free);
|
|
|
|
g_clear_object (&connection);
|
|
|
|
g_clear_pointer (&bus_name, g_free);
|
|
|
|
}
|
|
|
|
#endif
|
2012-07-16 15:29:01 +00:00
|
|
|
g_list_free_full (l, g_object_unref);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|