New gtk-launch command

This program launches an application specified by its desktop name
optinally taking list of URIs which are passed as arguments.

Uses GdkAppLaunchContext to get proper startup notification and
display handling for graphical apps.

https://bugzilla.gnome.org/show_bug.cgi?id=679342
This commit is contained in:
Tomas Bzatek 2012-07-16 17:29:01 +02:00 committed by Matthias Clasen
parent ad2dc27dd5
commit bdc6395d64
5 changed files with 230 additions and 2 deletions

View File

@ -141,6 +141,7 @@ content_files = \
x11.sgml \
gtk-query-immodules-3.0.xml \
gtk-update-icon-cache.xml \
gtk-launch.xml \
visual_index.xml \
getting_started.xml \
overview.xml
@ -400,7 +401,8 @@ EXTRA_DIST += version.xml.in gtk3.types.in
man_MANS = \
gtk-query-immodules-3.0.1 \
gtk-update-icon-cache.1
gtk-update-icon-cache.1 \
gtk-launch.1
if ENABLE_MAN

View File

@ -377,6 +377,7 @@
<title>GTK+ Tools</title>
<xi:include href="gtk-query-immodules-3.0.xml" />
<xi:include href="gtk-update-icon-cache.xml" />
<xi:include href="gtk-launch.xml" />
</part>
<xi:include href="glossary.xml" />

View File

@ -0,0 +1,72 @@
<?xml version="1.0"?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
]>
<refentry id="gtk-launch">
<refentryinfo>
<title>gtk-launch</title>
<productname>gtk+</productname>
<authorgroup>
<author>
<contrib>Developer</contrib>
<firstname>Tomáš</firstname>
<surname>Bžatek</surname>
<email>tbzatek@redhat.com</email>
</author>
</authorgroup>
</refentryinfo>
<refmeta>
<refentrytitle>gtk-launch</refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo class="manual">User Commands</refmiscinfo>
</refmeta>
<refnamediv>
<refname>gtk-launch</refname>
<refpurpose>Launch an application</refpurpose>
</refnamediv>
<refsynopsisdiv>
<cmdsynopsis>
<command>gtk-launch</command>
<arg>APPLICATION</arg>
<arg choice="opt" rep="repeat">URI</arg>
</cmdsynopsis>
</refsynopsisdiv>
<refsect1><title>Description</title>
<para>
<command>gtk-launch</command> launches an application using the given name.
The application is started with proper startup notification on a default
display, unless specified otherwise.
</para>
<para>
<command>gtk-launch</command> takes at least one argument, the name of
the application to launch. The name should match application desktop file name,
as residing in /usr/share/application, with or without the '.desktop' suffix.
</para>
<para>
If called with more than one argument, the rest of them besides the application name
are considered URI locations and are passed as arguments to the launched application.
</para>
</refsect1>
<refsect1><title>Options</title>
<para>The following options are understood:</para>
<variablelist>
<varlistentry>
<term><option>-?</option>, <option>--help</option></term>
<listitem><para>Prints a short help text and exits.</para></listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1><title>Bugs</title>
<para>
None known yet.
</para>
</refsect1>
</refentry>

View File

@ -1286,7 +1286,8 @@ endif
# Installed tools
#
bin_PROGRAMS = \
gtk-query-immodules-3.0
gtk-query-immodules-3.0 \
gtk-launch
if BUILD_ICON_CACHE
bin_PROGRAMS += gtk-update-icon-cache
@ -1333,6 +1334,9 @@ gtk_update_icon_cache_LDADD = $(GDK_PIXBUF_LIBS)
gtk_update_icon_cache_SOURCES = updateiconcache.c
endif
gtk_launch_LDADD = $(LDADDS)
gtk_launch_SOURCES = gtk-launch.c
.PHONY: files test test-debug
files:

149
gtk/gtk-launch.c Normal file
View File

@ -0,0 +1,149 @@
/* 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>
#include <gio/gdesktopappinfo.h>
#include <gtk.h>
static gchar **args = NULL;
static GOptionEntry entries[] = {
{ 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;
gchar *summary;
gchar *app_name;
gchar *desktop_file_name;
GDesktopAppInfo *info;
GAppLaunchContext *launch_context;
GList *l;
GFile *f;
setlocale (LC_ALL, "");
#ifdef ENABLE_NLS
bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
textdomain (GETTEXT_PACKAGE);
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif
#endif
g_type_init ();
/* Translators: this message will appear immediately after the */
/* usage string - Usage: COMMAND [OPTION]... <THIS_MESSAGE> */
context =
g_option_context_new (_("APPLICATION [URI...] - launch an APPLICATION with URI."));
/* Translators: this message will appear after the usage string */
/* and before the list of options. */
summary = _("Launch specified application by its desktop file info\n"
"optionally passing list of URIs as arguments.");
g_option_context_set_summary (context, summary);
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
g_option_context_add_group (context, gtk_get_option_group (FALSE));
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");
g_printerr (_("Try \"%s --help\" for more information."),
g_get_prgname ());
g_printerr ("\n");
g_error_free(error);
return 1;
}
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");
g_printerr (_("Try \"%s --help\" for more information."),
g_get_prgname ());
g_printerr ("\n");
return 1;
}
gtk_init (&argc, &argv);
app_name = *args;
if (g_str_has_suffix (app_name, ".desktop"))
desktop_file_name = g_strdup (app_name);
else
desktop_file_name = g_strconcat (app_name, ".desktop", NULL);
info = g_desktop_app_info_new (desktop_file_name);
g_free (desktop_file_name);
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 ());
if (!g_app_info_launch (G_APP_INFO (info), l, launch_context, &error))
{
/* 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);
g_list_free_full (l, g_object_unref);
return 0;
}