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 =
115 g_variant_get_bytestring_array (value, NULL);
120 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
121 const gchar *message)
123 g_print ("%s\n", message);
127 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
128 const gchar *message)
130 g_printerr ("%s\n", message);
134 g_application_command_line_get_property (GObject *object,
139 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
144 g_value_set_variant (value, cmdline->priv->arguments);
147 case PROP_PLATFORM_DATA:
148 g_value_set_variant (value, cmdline->priv->platform_data);
152 g_value_set_boolean (value, IS_REMOTE (cmdline));
156 g_assert_not_reached ();
161 g_application_command_line_set_property (GObject *object,
166 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
171 g_assert (cmdline->priv->arguments == NULL);
172 cmdline->priv->arguments = g_value_dup_variant (value);
175 case PROP_PLATFORM_DATA:
176 g_assert (cmdline->priv->platform_data == NULL);
177 cmdline->priv->platform_data = g_value_dup_variant (value);
178 if (cmdline->priv->platform_data != NULL)
179 grok_platform_data (cmdline);
183 g_assert_not_reached ();
188 g_application_command_line_finalize (GObject *object)
190 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
192 if (cmdline->priv->platform_data)
193 g_variant_unref (cmdline->priv->platform_data);
194 if (cmdline->priv->arguments)
195 g_variant_unref (cmdline->priv->arguments);
196 if (cmdline->priv->cwd)
197 g_variant_unref (cmdline->priv->cwd);
199 G_OBJECT_CLASS (g_application_command_line_parent_class)
204 g_application_command_line_init (GApplicationCommandLine *cmdline)
207 G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
208 G_TYPE_APPLICATION_COMMAND_LINE,
209 GApplicationCommandLinePrivate);
213 g_application_command_line_class_init (GApplicationCommandLineClass *class)
215 GObjectClass *object_class = G_OBJECT_CLASS (class);
217 object_class->get_property = g_application_command_line_get_property;
218 object_class->set_property = g_application_command_line_set_property;
219 object_class->finalize = g_application_command_line_finalize;
220 class->printerr_literal = g_application_command_line_real_printerr_literal;
221 class->print_literal = g_application_command_line_real_print_literal;
223 g_object_class_install_property (object_class, PROP_ARGUMENTS,
224 g_param_spec_variant ("arguments",
225 P_("Commandline arguments"),
226 P_("The commandline that caused this ::command-line signal emission"),
227 G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,
228 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
229 G_PARAM_STATIC_STRINGS));
231 g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
232 g_param_spec_variant ("platform-data",
234 P_("Platform-specific data for the commandline"),
235 G_VARIANT_TYPE ("a{sv}"), NULL,
236 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
237 G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (object_class, PROP_IS_REMOTE,
240 g_param_spec_boolean ("is-remote",
242 P_("TRUE if this is a remote commandline"),
244 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
246 g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
251 * g_application_command_line_get_arguments:
252 * @cmdline: a #GApplicationCommandLine
253 * @argc: the length of the arguments array, or %NULL
255 * Gets the list of arguments that was passed on the command line.
257 * The strings in the array may contain non-utf8 data.
259 * The return value is %NULL-terminated and should be freed using
262 * Returns: the string array containing the arguments (the argv)
267 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
273 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
275 argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
284 * g_application_command_line_get_cwd:
285 * @cmdline: a #GApplicationCommandLine
287 * Gets the working directory of the command line invocation. The
288 * string may contain non-utf8 data.
290 * It is possible that the remote application did not send a working
291 * directory, so this may be %NULL.
293 * The return value should not be modified or freed and is valid for as
294 * long as @cmdline exists.
296 * Returns: the current directory, or %NULL
301 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
303 if (cmdline->priv->cwd)
304 return g_variant_get_bytestring (cmdline->priv->cwd);
310 * g_application_command_line_get_environ:
311 * @cmdline: a #GApplicationCommandLine
313 * Gets the contents of the 'environ' variable of the command line
314 * invocation, as would be returned by g_get_environ(). The strings may
315 * contain non-utf8 data.
317 * The remote application usually does not send an environment. Use
318 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
319 * set it is possible that the environment is still not available (due
320 * to invocation messages from other applications).
322 * The return value should not be modified or freed and is valid for as
323 * long as @cmdline exists.
325 * Returns: the environment strings, or %NULL if they were not sent
329 const gchar * const *
330 g_application_command_line_get_environ (GApplicationCommandLine *cmdline)
332 return cmdline->priv->environ;
336 * g_application_command_line_getenv:
337 * @cmdline: a #GApplicationCommandLine
339 * Gets the value of a particular environment variable of the command
340 * line invocation, as would be returned by g_getenv(). The strings may
341 * contain non-utf8 data.
343 * The remote application usually does not send an environment. Use
344 * %G_APPLICATION_SEND_ENVIRONMENT to affect that. Even with this flag
345 * set it is possible that the environment is still not available (due
346 * to invocation messages from other applications).
348 * The return value should not be modified or freed and is valid for as
349 * long as @cmdline exists.
351 * Returns: the value of the variable, or %NULL if unset or unsent
356 g_application_command_line_getenv (GApplicationCommandLine *cmdline,
359 gint length = strlen (name);
362 /* TODO: expand on windows */
363 if (cmdline->priv->environ)
364 for (i = 0; cmdline->priv->environ[i]; i++)
365 if (strncmp (cmdline->priv->environ[i], name, length) == 0 &&
366 cmdline->priv->environ[i][length] == '=')
367 return cmdline->priv->environ[i] + length + 1;
373 * g_application_command_line_get_is_remote:
374 * @cmdline: a #GApplicationCommandLine
376 * Determines if @cmdline represents a remote invocation.
378 * Returns: %TRUE if the invocation was remote
383 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
385 return IS_REMOTE (cmdline);
389 * g_application_command_line_print:
390 * @cmdline: a #GApplicationCommandLine
391 * @format: a printf-style format string
392 * @...: arguments, as per @format
394 * Formats a message and prints it using the stdout print handler in the
397 * If @cmdline is a local invocation then this is exactly equivalent to
398 * g_print(). If @cmdline is remote then this is equivalent to calling
399 * g_print() in the invoking process.
404 g_application_command_line_print (GApplicationCommandLine *cmdline,
411 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
412 g_return_if_fail (format != NULL);
414 va_start (ap, format);
415 message = g_strdup_vprintf (format, ap);
418 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
419 ->print_literal (cmdline, message);
424 * g_application_command_line_printerr:
425 * @cmdline: a #GApplicationCommandLine
426 * @format: a printf-style format string
427 * @...: arguments, as per @format
429 * Formats a message and prints it using the stderr print handler in the
432 * If @cmdline is a local invocation then this is exactly equivalent to
433 * g_printerr(). If @cmdline is remote then this is equivalent to
434 * calling g_printerr() in the invoking process.
439 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
446 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
447 g_return_if_fail (format != NULL);
449 va_start (ap, format);
450 message = g_strdup_vprintf (format, ap);
453 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
454 ->printerr_literal (cmdline, message);
459 * g_application_command_line_set_exit_status:
460 * @cmdline: a #GApplicationCommandLine
461 * @exit_status: the exit status
463 * Sets the exit status that will be used when the invoking process
466 * The return value of the #GApplication::command-line signal is
467 * passed to this function when the handler returns. This is the usual
468 * way of setting the exit status.
470 * In the event that you want the remote invocation to continue running
471 * and want to decide on the exit status in the future, you can use this
472 * call. For the case of a remote invocation, the remote process will
473 * typically exit when the last reference is dropped on @cmdline. The
474 * exit status of the remote process will be equal to the last value
475 * that was set with this function.
477 * In the case that the commandline invocation is local, the situation
478 * is slightly more complicated. If the commandline invocation results
479 * in the mainloop running (ie: because the use-count of the application
480 * increased to a non-zero value) then the application is considered to
481 * have been 'successful' in a certain sense, and the exit status is
482 * always zero. If the application use count is zero, though, the exit
483 * status of the local #GApplicationCommandLine is used.
488 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
491 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
493 cmdline->priv->exit_status = exit_status;
497 * g_application_command_line_get_exit_status:
498 * @cmdline: a #GApplicationCommandLine
500 * Gets the exit status of @cmdline. See
501 * g_application_command_line_set_exit_status() for more information.
503 * Returns: the exit status
508 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
510 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
512 return cmdline->priv->exit_status;
516 * g_application_command_line_get_platform_data:
517 * @cmdline: #GApplicationCommandLine
519 * Gets the platform data associated with the invocation of @cmdline.
521 * This is a #GVariant dictionary containing information about the
522 * context in which the invocation occured. It typically contains
523 * information like the current working directory and the startup
526 * For local invocation, it will be %NULL.
528 * Returns: the platform data, or %NULL
533 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
535 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
537 if (cmdline->priv->platform_data)
538 return g_variant_ref (cmdline->priv->platform_data);