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-open"><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>
67 struct _GApplicationCommandLinePrivate
69 GVariant *platform_data;
75 /* All subclasses represent remote invocations of some kind. */
76 #define IS_REMOTE(cmdline) (G_TYPE_FROM_INSTANCE (cmdline) != \
77 G_TYPE_APPLICATION_COMMAND_LINE)
80 grok_platform_data (GApplicationCommandLine *cmdline)
86 g_variant_iter_init (&iter, cmdline->priv->platform_data);
88 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
89 if (strcmp (key, "cwd") == 0)
91 if (!cmdline->priv->cwd)
92 cmdline->priv->cwd = g_variant_ref (value);
97 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
100 g_print ("%s\n", message);
104 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
105 const gchar *message)
107 g_printerr ("%s\n", message);
111 g_application_command_line_get_property (GObject *object,
116 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
121 g_value_set_variant (value, cmdline->priv->arguments);
124 case PROP_PLATFORM_DATA:
125 g_value_set_variant (value, cmdline->priv->platform_data);
129 g_value_set_boolean (value, IS_REMOTE (cmdline));
133 g_assert_not_reached ();
138 g_application_command_line_set_property (GObject *object,
143 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
148 g_assert (cmdline->priv->arguments == NULL);
149 cmdline->priv->arguments = g_value_dup_variant (value);
152 case PROP_PLATFORM_DATA:
153 g_assert (cmdline->priv->platform_data == NULL);
154 cmdline->priv->platform_data = g_value_dup_variant (value);
155 if (cmdline->priv->platform_data != NULL)
156 grok_platform_data (cmdline);
160 g_assert_not_reached ();
165 g_application_command_line_finalize (GObject *object)
167 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
169 if (cmdline->priv->platform_data)
170 g_variant_unref (cmdline->priv->platform_data);
171 if (cmdline->priv->arguments)
172 g_variant_unref (cmdline->priv->arguments);
173 if (cmdline->priv->cwd)
174 g_variant_unref (cmdline->priv->cwd);
176 G_OBJECT_CLASS (g_application_command_line_parent_class)
181 g_application_command_line_init (GApplicationCommandLine *cmdline)
184 G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
185 G_TYPE_APPLICATION_COMMAND_LINE,
186 GApplicationCommandLinePrivate);
190 g_application_command_line_class_init (GApplicationCommandLineClass *class)
192 GObjectClass *object_class = G_OBJECT_CLASS (class);
194 object_class->get_property = g_application_command_line_get_property;
195 object_class->set_property = g_application_command_line_set_property;
196 object_class->finalize = g_application_command_line_finalize;
197 class->printerr_literal = g_application_command_line_real_printerr_literal;
198 class->print_literal = g_application_command_line_real_print_literal;
200 g_object_class_install_property (object_class, PROP_ARGUMENTS,
201 g_param_spec_variant ("arguments",
202 P_("Commandline arguments"),
203 P_("The commandline that caused this ::command-line signal emission"),
204 G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,
205 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
206 G_PARAM_STATIC_STRINGS));
208 g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
209 g_param_spec_variant ("platform-data",
211 P_("Platform-specific data for the commandline"),
212 G_VARIANT_TYPE ("a{sv}"), NULL,
213 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
214 G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (object_class, PROP_IS_REMOTE,
217 g_param_spec_boolean ("is-remote",
219 P_("TRUE if this is a remote commandline"),
221 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
223 g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
228 * g_application_command_line_get_arguments:
229 * @cmdline: a #GApplicationCommandLine
230 * @argc: the length of the arguments array, or %NULL
232 * Gets the list of arguments that was passed on the command line.
234 * The strings in the array may contain non-utf8 data.
236 * The return value is %NULL-terminated and should be freed using
239 * Returns: the string array containing the arguments (the argv)
244 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
250 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
252 argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
261 * g_application_command_line_get_cwd:
262 * @cmdline: a #GApplicationCommandLine
264 * Gets the working directory of the command line invocation. The
265 * string may contain non-utf8 data.
267 * It is possible that the remote application did not send a working
268 * directory, so this may be %NULL.
270 * The return value should not be modified or freed and is valid for as
271 * long as @cmdline exists.
273 * Returns: the current directory, or %NULL
278 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
280 if (cmdline->priv->cwd)
281 return g_variant_get_bytestring (cmdline->priv->cwd);
287 * g_application_command_line_get_is_remote:
288 * @cmdline: a #GApplicationCommandLine
290 * Determines if @cmdline represents a remote invocation.
292 * Returns: %TRUE if the invocation was remote
297 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
299 return IS_REMOTE (cmdline);
303 * g_application_command_line_print:
304 * @cmdline: a #GApplicationCommandLine
305 * @format: a printf-style format string
306 * @...: arguments, as per @format
308 * Formats a message and prints it using the stdout print handler in the
311 * If @cmdline is a local invocation then this is exactly equivalent to
312 * g_print(). If @cmdline is remote then this is equivalent to calling
313 * g_print() in the invoking process.
318 g_application_command_line_print (GApplicationCommandLine *cmdline,
325 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
326 g_return_if_fail (format != NULL);
328 va_start (ap, format);
329 message = g_strdup_vprintf (format, ap);
332 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
333 ->print_literal (cmdline, message);
338 * g_application_command_line_printerr:
339 * @cmdline: a #GApplicationCommandLine
340 * @format: a printf-style format string
341 * @...: arguments, as per @format
343 * Formats a message and prints it using the stderr print handler in the
346 * If @cmdline is a local invocation then this is exactly equivalent to
347 * g_printerr(). If @cmdline is remote then this is equivalent to
348 * calling g_printerr() in the invoking process.
353 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
360 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
361 g_return_if_fail (format != NULL);
363 va_start (ap, format);
364 message = g_strdup_vprintf (format, ap);
367 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
368 ->printerr_literal (cmdline, message);
373 * g_application_command_line_set_exit_status:
374 * @cmdline: a #GApplicationCommandLine
375 * @exit_status: the exit status
377 * Sets the exit status that will be used when the invoking process
380 * The return value of the #GApplication::command-line signal is
381 * passed to this function when the handler returns. This is the usual
382 * way of setting the exit status.
384 * In the event that you want the remote invocation to continue running
385 * and want to decide on the exit status in the future, you can use this
386 * call. For the case of a remote invocation, the remote process will
387 * typically exit when the last reference is dropped on @cmdline. The
388 * exit status of the remote process will be equal to the last value
389 * that was set with this function.
391 * In the case that the commandline invocation is local, the situation
392 * is slightly more complicated. If the commandline invocation results
393 * in the mainloop running (ie: because the use-count of the application
394 * increased to a non-zero value) then the application is considered to
395 * have been 'successful' in a certain sense, and the exit status is
396 * always zero. If the application use count is zero, though, the exit
397 * status of the local #GApplicationCommandLine is used.
402 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
405 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
407 cmdline->priv->exit_status = exit_status;
411 * g_application_command_line_get_exit_status:
412 * @cmdline: a #GApplicationCommandLine
414 * Gets the exit status of @cmdline. See
415 * g_application_command_line_set_exit_status() for more information.
417 * Returns: the exit status
422 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
424 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
426 return cmdline->priv->exit_status;
430 * g_application_command_line_get_platform_data:
431 * @cmdline: #GApplicationCommandLine
433 * Gets the platform data associated with the invocation of @cmdline.
435 * This is a #GVariant dictionary containing information about the
436 * context in which the invocation occured. It typically contains
437 * information like the current working directory and the startup
440 * For local invocation, it will be %NULL.
442 * Returns: the platform data, or %NULL
447 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
449 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
451 if (cmdline->priv->platform_data)
452 return g_variant_ref (cmdline->priv->platform_data);