2 * Copyright © 2010 Codethink Limited
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * licence or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
19 * Authors: Ryan Lortie <desrt@desrt.ca>
24 #include "gapplicationcommandline.h"
31 G_DEFINE_TYPE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJECT)
34 * SECTION:gapplicationcommandline
35 * @title: GApplicationCommandLine
36 * @short_description: A class representing a command-line invocation of
38 * @see_also: #GApplication
40 * #GApplicationCommandLine represents a command-line invocation of
41 * an application. It is created by #GApplication and emitted
42 * in the #GApplication::command-line signal and virtual function.
44 * The class contains the list of arguments that the program was invoked
45 * with. It is also possible to query if the commandline invocation was
46 * local (ie: the current process is running in direct response to the
47 * invocation) or remote (ie: some other process forwarded the
48 * commandline to this process).
50 * The exit status of the originally-invoked process may be set and
51 * messages can be printed to stdout or stderr of that process. The
52 * lifecycle of the originally-invoked process is tied to the lifecycle
53 * of this object (ie: the process exits when the last reference is
56 * <example id="gapplication-example-cmdline"><title>Handling commandline arguments with GApplication</title>
58 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-cmdline.c">
59 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
64 * <example id="gapplication-example-cmdline2"><title>Complicated commandline handling</title>
66 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-cmdline2.c">
67 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
81 struct _GApplicationCommandLinePrivate
83 GVariant *platform_data;
87 const gchar **environ;
91 /* All subclasses represent remote invocations of some kind. */
92 #define IS_REMOTE(cmdline) (G_TYPE_FROM_INSTANCE (cmdline) != \
93 G_TYPE_APPLICATION_COMMAND_LINE)
96 grok_platform_data (GApplicationCommandLine *cmdline)
102 g_variant_iter_init (&iter, cmdline->priv->platform_data);
104 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
105 if (strcmp (key, "cwd") == 0)
107 if (!cmdline->priv->cwd)
108 cmdline->priv->cwd = g_variant_ref (value);
111 else if (strcmp (key, "environ") == 0)
113 if (!cmdline->priv->environ)
114 cmdline->priv->environ = g_variant_get_strv (value, NULL);
119 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
120 const gchar *message)
122 g_print ("%s\n", message);
126 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
127 const gchar *message)
129 g_printerr ("%s\n", message);
133 g_application_command_line_get_property (GObject *object,
138 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
143 g_value_set_variant (value, cmdline->priv->arguments);
146 case PROP_PLATFORM_DATA:
147 g_value_set_variant (value, cmdline->priv->platform_data);
151 g_value_set_boolean (value, IS_REMOTE (cmdline));
155 g_assert_not_reached ();
160 g_application_command_line_set_property (GObject *object,
165 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
170 g_assert (cmdline->priv->arguments == NULL);
171 cmdline->priv->arguments = g_value_dup_variant (value);
174 case PROP_PLATFORM_DATA:
175 g_assert (cmdline->priv->platform_data == NULL);
176 cmdline->priv->platform_data = g_value_dup_variant (value);
177 if (cmdline->priv->platform_data != NULL)
178 grok_platform_data (cmdline);
182 g_assert_not_reached ();
187 g_application_command_line_finalize (GObject *object)
189 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
191 if (cmdline->priv->platform_data)
192 g_variant_unref (cmdline->priv->platform_data);
193 if (cmdline->priv->arguments)
194 g_variant_unref (cmdline->priv->arguments);
195 if (cmdline->priv->cwd)
196 g_variant_unref (cmdline->priv->cwd);
198 G_OBJECT_CLASS (g_application_command_line_parent_class)
203 g_application_command_line_init (GApplicationCommandLine *cmdline)
206 G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
207 G_TYPE_APPLICATION_COMMAND_LINE,
208 GApplicationCommandLinePrivate);
212 g_application_command_line_class_init (GApplicationCommandLineClass *class)
214 GObjectClass *object_class = G_OBJECT_CLASS (class);
216 object_class->get_property = g_application_command_line_get_property;
217 object_class->set_property = g_application_command_line_set_property;
218 object_class->finalize = g_application_command_line_finalize;
219 class->printerr_literal = g_application_command_line_real_printerr_literal;
220 class->print_literal = g_application_command_line_real_print_literal;
222 g_object_class_install_property (object_class, PROP_ARGUMENTS,
223 g_param_spec_variant ("arguments",
224 P_("Commandline arguments"),
225 P_("The commandline that caused this ::command-line signal emission"),
226 G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,
227 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
228 G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
231 g_param_spec_variant ("platform-data",
233 P_("Platform-specific data for the commandline"),
234 G_VARIANT_TYPE ("a{sv}"), NULL,
235 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
236 G_PARAM_STATIC_STRINGS));
238 g_object_class_install_property (object_class, PROP_IS_REMOTE,
239 g_param_spec_boolean ("is-remote",
241 P_("TRUE if this is a remote commandline"),
243 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
245 g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
250 * g_application_command_line_get_arguments:
251 * @cmdline: a #GApplicationCommandLine
252 * @argc: the length of the arguments array, or %NULL
254 * Gets the list of arguments that was passed on the command line.
256 * The strings in the array may contain non-utf8 data.
258 * The return value is %NULL-terminated and should be freed using
261 * Returns: the string array containing the arguments (the argv)
266 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
272 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
274 argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
283 * g_application_command_line_get_cwd:
284 * @cmdline: a #GApplicationCommandLine
286 * Gets the working directory of the command line invocation. The
287 * string may contain non-utf8 data.
289 * It is possible that the remote application did not send a working
290 * directory, so this may be %NULL.
292 * The return value should not be modified or freed and is valid for as
293 * long as @cmdline exists.
295 * Returns: the current directory, or %NULL
300 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
302 if (cmdline->priv->cwd)
303 return g_variant_get_bytestring (cmdline->priv->cwd);
309 * g_application_command_line_get_environ:
310 * @cmdline: a #GApplicationCommandLine
312 * Gets the contents of the 'environ' variable of the command line
313 * invocation, as would be returned by g_get_environ(). The strings may
314 * contain non-utf8 data.
316 * The remote application usually does not send an environment. Use
317 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
318 * set it is possible that the environment is still not available (due
319 * to invocation messages from other applications).
321 * The return value should not be modified or freed and is valid for as
322 * long as @cmdline exists.
324 * Returns: the environment strings, or %NULL if they were not sent
328 const gchar * const *
329 g_application_command_line_get_environ (GApplicationCommandLine *cmdline)
331 return cmdline->priv->environ;
335 * g_application_command_line_getenv:
336 * @cmdline: a #GApplicationCommandLine
338 * Gets the value of a particular environment variable of the command
339 * line invocation, as would be returned by g_getenv(). The strings may
340 * contain non-utf8 data.
342 * The remote application usually does not send an environment. Use
343 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
344 * set it is possible that the environment is still not available (due
345 * to invocation messages from other applications).
347 * The return value should not be modified or freed and is valid for as
348 * long as @cmdline exists.
350 * Returns: the value of the variable, or %NULL if unset or unsent
355 g_application_command_line_getenv (GApplicationCommandLine *cmdline,
358 gint length = strlen (name);
361 /* TODO: expand on windows */
362 if (cmdline->priv->environ)
363 for (i = 0; cmdline->priv->environ[i]; i++)
364 if (strncmp (cmdline->priv->environ[i], name, length) == 0 &&
365 cmdline->priv->environ[i][length] == '=')
366 return cmdline->priv->environ[i] + length + 1;
372 * g_application_command_line_get_is_remote:
373 * @cmdline: a #GApplicationCommandLine
375 * Determines if @cmdline represents a remote invocation.
377 * Returns: %TRUE if the invocation was remote
382 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
384 return IS_REMOTE (cmdline);
388 * g_application_command_line_print:
389 * @cmdline: a #GApplicationCommandLine
390 * @format: a printf-style format string
391 * @...: arguments, as per @format
393 * Formats a message and prints it using the stdout print handler in the
396 * If @cmdline is a local invocation then this is exactly equivalent to
397 * g_print(). If @cmdline is remote then this is equivalent to calling
398 * g_print() in the invoking process.
403 g_application_command_line_print (GApplicationCommandLine *cmdline,
410 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
411 g_return_if_fail (format != NULL);
413 va_start (ap, format);
414 message = g_strdup_vprintf (format, ap);
417 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
418 ->print_literal (cmdline, message);
423 * g_application_command_line_printerr:
424 * @cmdline: a #GApplicationCommandLine
425 * @format: a printf-style format string
426 * @...: arguments, as per @format
428 * Formats a message and prints it using the stderr print handler in the
431 * If @cmdline is a local invocation then this is exactly equivalent to
432 * g_printerr(). If @cmdline is remote then this is equivalent to
433 * calling g_printerr() in the invoking process.
438 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
445 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
446 g_return_if_fail (format != NULL);
448 va_start (ap, format);
449 message = g_strdup_vprintf (format, ap);
452 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
453 ->printerr_literal (cmdline, message);
458 * g_application_command_line_set_exit_status:
459 * @cmdline: a #GApplicationCommandLine
460 * @exit_status: the exit status
462 * Sets the exit status that will be used when the invoking process
465 * The return value of the #GApplication::command-line signal is
466 * passed to this function when the handler returns. This is the usual
467 * way of setting the exit status.
469 * In the event that you want the remote invocation to continue running
470 * and want to decide on the exit status in the future, you can use this
471 * call. For the case of a remote invocation, the remote process will
472 * typically exit when the last reference is dropped on @cmdline. The
473 * exit status of the remote process will be equal to the last value
474 * that was set with this function.
476 * In the case that the commandline invocation is local, the situation
477 * is slightly more complicated. If the commandline invocation results
478 * in the mainloop running (ie: because the use-count of the application
479 * increased to a non-zero value) then the application is considered to
480 * have been 'successful' in a certain sense, and the exit status is
481 * always zero. If the application use count is zero, though, the exit
482 * status of the local #GApplicationCommandLine is used.
487 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
490 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
492 cmdline->priv->exit_status = exit_status;
496 * g_application_command_line_get_exit_status:
497 * @cmdline: a #GApplicationCommandLine
499 * Gets the exit status of @cmdline. See
500 * g_application_command_line_set_exit_status() for more information.
502 * Returns: the exit status
507 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
509 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
511 return cmdline->priv->exit_status;
515 * g_application_command_line_get_platform_data:
516 * @cmdline: #GApplicationCommandLine
518 * Gets the platform data associated with the invocation of @cmdline.
520 * This is a #GVariant dictionary containing information about the
521 * context in which the invocation occured. It typically contains
522 * information like the current working directory and the startup
525 * For local invocation, it will be %NULL.
527 * Returns: the platform data, or %NULL
532 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
534 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
536 if (cmdline->priv->platform_data)
537 return g_variant_ref (cmdline->priv->platform_data);