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><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-cmdline.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
58 * <example id="gapplication-example-cmdline2"><title>Complicated commandline handling</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-cmdline2.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
69 struct _GApplicationCommandLinePrivate
71 GVariant *platform_data;
77 /* All subclasses represent remote invocations of some kind. */
78 #define IS_REMOTE(cmdline) (G_TYPE_FROM_INSTANCE (cmdline) != \
79 G_TYPE_APPLICATION_COMMAND_LINE)
82 grok_platform_data (GApplicationCommandLine *cmdline)
88 g_variant_iter_init (&iter, cmdline->priv->platform_data);
90 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
91 if (strcmp (key, "cwd") == 0)
93 if (!cmdline->priv->cwd)
94 cmdline->priv->cwd = g_variant_ref (value);
99 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
100 const gchar *message)
102 g_print ("%s\n", message);
106 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
107 const gchar *message)
109 g_printerr ("%s\n", message);
113 g_application_command_line_get_property (GObject *object,
118 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
123 g_value_set_variant (value, cmdline->priv->arguments);
126 case PROP_PLATFORM_DATA:
127 g_value_set_variant (value, cmdline->priv->platform_data);
131 g_value_set_boolean (value, IS_REMOTE (cmdline));
135 g_assert_not_reached ();
140 g_application_command_line_set_property (GObject *object,
145 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
150 g_assert (cmdline->priv->arguments == NULL);
151 cmdline->priv->arguments = g_value_dup_variant (value);
154 case PROP_PLATFORM_DATA:
155 g_assert (cmdline->priv->platform_data == NULL);
156 cmdline->priv->platform_data = g_value_dup_variant (value);
157 if (cmdline->priv->platform_data != NULL)
158 grok_platform_data (cmdline);
162 g_assert_not_reached ();
167 g_application_command_line_finalize (GObject *object)
169 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
171 if (cmdline->priv->platform_data)
172 g_variant_unref (cmdline->priv->platform_data);
173 if (cmdline->priv->arguments)
174 g_variant_unref (cmdline->priv->arguments);
175 if (cmdline->priv->cwd)
176 g_variant_unref (cmdline->priv->cwd);
178 G_OBJECT_CLASS (g_application_command_line_parent_class)
183 g_application_command_line_init (GApplicationCommandLine *cmdline)
186 G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
187 G_TYPE_APPLICATION_COMMAND_LINE,
188 GApplicationCommandLinePrivate);
192 g_application_command_line_class_init (GApplicationCommandLineClass *class)
194 GObjectClass *object_class = G_OBJECT_CLASS (class);
196 object_class->get_property = g_application_command_line_get_property;
197 object_class->set_property = g_application_command_line_set_property;
198 object_class->finalize = g_application_command_line_finalize;
199 class->printerr_literal = g_application_command_line_real_printerr_literal;
200 class->print_literal = g_application_command_line_real_print_literal;
202 g_object_class_install_property (object_class, PROP_ARGUMENTS,
203 g_param_spec_variant ("arguments",
204 P_("Commandline arguments"),
205 P_("The commandline that caused this ::command-line signal emission"),
206 G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,
207 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
208 G_PARAM_STATIC_STRINGS));
210 g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
211 g_param_spec_variant ("platform-data",
213 P_("Platform-specific data for the commandline"),
214 G_VARIANT_TYPE ("a{sv}"), NULL,
215 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
216 G_PARAM_STATIC_STRINGS));
218 g_object_class_install_property (object_class, PROP_IS_REMOTE,
219 g_param_spec_boolean ("is-remote",
221 P_("TRUE if this is a remote commandline"),
223 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
225 g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
230 * g_application_command_line_get_arguments:
231 * @cmdline: a #GApplicationCommandLine
232 * @argc: the length of the arguments array, or %NULL
234 * Gets the list of arguments that was passed on the command line.
236 * The strings in the array may contain non-utf8 data.
238 * The return value is %NULL-terminated and should be freed using
241 * Returns: the string array containing the arguments (the argv)
246 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
252 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
254 argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
263 * g_application_command_line_get_cwd:
264 * @cmdline: a #GApplicationCommandLine
266 * Gets the working directory of the command line invocation. The
267 * string may contain non-utf8 data.
269 * It is possible that the remote application did not send a working
270 * directory, so this may be %NULL.
272 * The return value should not be modified or freed and is valid for as
273 * long as @cmdline exists.
275 * Returns: the current directory, or %NULL
280 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
282 if (cmdline->priv->cwd)
283 return g_variant_get_bytestring (cmdline->priv->cwd);
289 * g_application_command_line_get_is_remote:
290 * @cmdline: a #GApplicationCommandLine
292 * Determines if @cmdline represents a remote invocation.
294 * Returns: %TRUE if the invocation was remote
299 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
301 return IS_REMOTE (cmdline);
305 * g_application_command_line_print:
306 * @cmdline: a #GApplicationCommandLine
307 * @format: a printf-style format string
308 * @...: arguments, as per @format
310 * Formats a message and prints it using the stdout print handler in the
313 * If @cmdline is a local invocation then this is exactly equivalent to
314 * g_print(). If @cmdline is remote then this is equivalent to calling
315 * g_print() in the invoking process.
320 g_application_command_line_print (GApplicationCommandLine *cmdline,
327 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
328 g_return_if_fail (format != NULL);
330 va_start (ap, format);
331 message = g_strdup_vprintf (format, ap);
334 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
335 ->print_literal (cmdline, message);
340 * g_application_command_line_printerr:
341 * @cmdline: a #GApplicationCommandLine
342 * @format: a printf-style format string
343 * @...: arguments, as per @format
345 * Formats a message and prints it using the stderr print handler in the
348 * If @cmdline is a local invocation then this is exactly equivalent to
349 * g_printerr(). If @cmdline is remote then this is equivalent to
350 * calling g_printerr() in the invoking process.
355 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
362 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
363 g_return_if_fail (format != NULL);
365 va_start (ap, format);
366 message = g_strdup_vprintf (format, ap);
369 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
370 ->printerr_literal (cmdline, message);
375 * g_application_command_line_set_exit_status:
376 * @cmdline: a #GApplicationCommandLine
377 * @exit_status: the exit status
379 * Sets the exit status that will be used when the invoking process
382 * The return value of the #GApplication::command-line signal is
383 * passed to this function when the handler returns. This is the usual
384 * way of setting the exit status.
386 * In the event that you want the remote invocation to continue running
387 * and want to decide on the exit status in the future, you can use this
388 * call. For the case of a remote invocation, the remote process will
389 * typically exit when the last reference is dropped on @cmdline. The
390 * exit status of the remote process will be equal to the last value
391 * that was set with this function.
393 * In the case that the commandline invocation is local, the situation
394 * is slightly more complicated. If the commandline invocation results
395 * in the mainloop running (ie: because the use-count of the application
396 * increased to a non-zero value) then the application is considered to
397 * have been 'successful' in a certain sense, and the exit status is
398 * always zero. If the application use count is zero, though, the exit
399 * status of the local #GApplicationCommandLine is used.
404 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
407 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
409 cmdline->priv->exit_status = exit_status;
413 * g_application_command_line_get_exit_status:
414 * @cmdline: a #GApplicationCommandLine
416 * Gets the exit status of @cmdline. See
417 * g_application_command_line_set_exit_status() for more information.
419 * Returns: the exit status
424 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
426 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
428 return cmdline->priv->exit_status;
432 * g_application_command_line_get_platform_data:
433 * @cmdline: #GApplicationCommandLine
435 * Gets the platform data associated with the invocation of @cmdline.
437 * This is a #GVariant dictionary containing information about the
438 * context in which the invocation occured. It typically contains
439 * information like the current working directory and the startup
442 * For local invocation, it will be %NULL.
444 * Returns: the platform data, or %NULL
449 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
451 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
453 if (cmdline->priv->platform_data)
454 return g_variant_ref (cmdline->priv->platform_data);