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>
22 #include "gapplicationcommandline.h"
27 G_DEFINE_TYPE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJECT)
30 * SECTION:gapplicationcommandline
31 * @title: GApplicationCommandLine
32 * @short_description: A class representing a command-line invocation of
34 * @see_also: #GApplication
36 * #GApplicationCommandLine represents a command-line invocation of
37 * containing application. It is created by #GApplication and emitted
38 * in the <varname>command-line</varname> signal and virtual function.
40 * The class contains the list of arguments that the program was invoked
41 * with. It is also possible to query if the commandline invocation was
42 * local (ie: the current process is running in direct response to the
43 * invocation) or remote (ie: some other process forwarded the
44 * commandline to this process).
46 * The exit status of the originally-invoked process may be set and
47 * messages can be printed to stdout or stderr of that process. The
48 * lifecycle of the originally-invoked process is tied to the lifecycle
49 * of this object (ie: the process exits when the last reference is
61 struct _GApplicationCommandLinePrivate
63 GVariant *platform_data;
69 /* All subclasses represent remote invocations of some kind. */
70 #define IS_REMOTE(cmdline) (G_TYPE_FROM_INSTANCE (cmdline) != \
71 G_TYPE_APPLICATION_COMMAND_LINE)
74 grok_platform_data (GApplicationCommandLine *cmdline)
80 g_variant_iter_init (&iter, cmdline->priv->platform_data);
82 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
83 if (strcmp (key, "cwd") == 0)
85 if (!cmdline->priv->cwd)
86 cmdline->priv->cwd = g_variant_ref (value);
91 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
94 g_print ("%s\n", message);
98 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
101 g_printerr ("%s\n", message);
105 g_application_command_line_get_property (GObject *object, guint prop_id,
106 GValue *value, GParamSpec *pspec)
108 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
113 g_value_set_variant (value, cmdline->priv->arguments);
116 case PROP_PLATFORM_DATA:
117 g_value_set_variant (value, cmdline->priv->platform_data);
121 g_value_set_boolean (value, IS_REMOTE (cmdline));
125 g_assert_not_reached ();
130 g_application_command_line_set_property (GObject *object,
135 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
140 g_assert (cmdline->priv->arguments == NULL);
141 cmdline->priv->arguments = g_value_dup_variant (value);
144 case PROP_PLATFORM_DATA:
145 g_assert (cmdline->priv->platform_data == NULL);
146 cmdline->priv->platform_data = g_value_dup_variant (value);
147 if (cmdline->priv->platform_data != NULL)
148 grok_platform_data (cmdline);
152 g_assert_not_reached ();
157 g_application_command_line_finalize (GObject *object)
159 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
161 if (cmdline->priv->platform_data)
162 g_variant_unref (cmdline->priv->platform_data);
163 if (cmdline->priv->arguments)
164 g_variant_unref (cmdline->priv->arguments);
165 if (cmdline->priv->cwd)
166 g_variant_unref (cmdline->priv->cwd);
168 G_OBJECT_CLASS (g_application_command_line_parent_class)
173 g_application_command_line_init (GApplicationCommandLine *cmdline)
176 G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
177 G_TYPE_APPLICATION_COMMAND_LINE,
178 GApplicationCommandLinePrivate);
182 g_application_command_line_class_init (GApplicationCommandLineClass *class)
184 GObjectClass *object_class = G_OBJECT_CLASS (class);
186 object_class->get_property = g_application_command_line_get_property;
187 object_class->set_property = g_application_command_line_set_property;
188 object_class->finalize = g_application_command_line_finalize;
189 class->printerr_literal = g_application_command_line_real_printerr_literal;
190 class->print_literal = g_application_command_line_real_print_literal;
192 g_object_class_install_property (object_class, PROP_ARGUMENTS,
193 g_param_spec_variant ("arguments", "commandline arguments",
194 "the commandline that caused this cmdline",
195 G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,
196 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
197 G_PARAM_STATIC_STRINGS));
199 g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
200 g_param_spec_variant ("platform-data", "platform data",
201 "platform-specific data for the cmdline",
202 G_VARIANT_TYPE ("a{sv}"), NULL,
203 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
204 G_PARAM_STATIC_STRINGS));
206 g_object_class_install_property (object_class, PROP_IS_REMOTE,
207 g_param_spec_boolean ("is-remote", "is remote",
208 "TRUE if this is a remote cmdline", FALSE,
209 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
211 g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
216 * g_application_command_line_get_arguments:
217 * @cmdline: a #GApplicationCommandLine
218 * @argc: the length of the arguments array, or %NULL
220 * Gets the list of arguments that was passed on the command line.
222 * The strings in the array may contain non-utf8 data.
224 * The return value is %NULL-terminated and should be freed using
227 * Returns: the string array containing the arguments (the argv)
232 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
238 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
240 argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
249 * g_application_command_line_get_cwd:
250 * @cmdline: a #GApplicationCommandLine
252 * Gets the working directory of the command line invocation. The
253 * string may contain non-utf8 data.
255 * It is possible that the remote application did not send a working
256 * directory, so this may be %NULL.
258 * The return value should not be modified or freed and is valid for as
259 * long as @cmdline exists.
261 * Returns: the current directory, or %NULL
266 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
268 if (cmdline->priv->cwd)
269 return g_variant_get_bytestring (cmdline->priv->cwd);
275 * g_application_command_line_get_is_remote:
276 * @cmdline: a #GApplicationCommandLine
278 * Determines if @cmdline represents a remote invocation.
280 * Returns: %TRUE if the invocation was remote
285 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
287 return IS_REMOTE (cmdline);
291 * g_application_command_line_print:
292 * @cmdline: a #GApplicationCommandLine
293 * @format: a printf-style format string
294 * @...: arguments, as per @format
296 * Formats a message and prints it using the stdout print handler in the
299 * If @cmdline is a local invocation then this is exactly equivalent to
300 * g_print(). If @cmdline is remote then this is equivalent to calling
301 * g_print() in the invoking process.
306 g_application_command_line_print (GApplicationCommandLine *cmdline,
313 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
314 g_return_if_fail (format != NULL);
316 va_start (ap, format);
317 message = g_strdup_vprintf (format, ap);
320 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
321 ->print_literal (cmdline, message);
326 * g_application_command_line_printerr:
327 * @cmdline: a #GApplicationCommandLine
328 * @format: a printf-style format string
329 * @...: arguments, as per @format
331 * Formats a message and prints it using the stderr print handler in the
334 * If @cmdline is a local invocation then this is exactly equivalent to
335 * g_printerr(). If @cmdline is remote then this is equivalent to
336 * calling g_printerr() in the invoking process.
341 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
348 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
349 g_return_if_fail (format != NULL);
351 va_start (ap, format);
352 message = g_strdup_vprintf (format, ap);
355 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
356 ->printerr_literal (cmdline, message);
361 * g_application_command_line_set_exit_status:
362 * @cmdline: a #GApplicationCommandLine
363 * @exit_status: the exit status
365 * Sets the exit status that will be used when the invoking process
368 * The return value of the <varname>command-line</varname> signal is
369 * passed to this function when the handler returns. This is the usual
370 * way of setting the exit status.
372 * In the event that you want the remote invocation to continue running
373 * and want to decide on the exit status in the future, you can use this
374 * call. For the case of a remote invocation, the remote process will
375 * typically exit when the last reference is dropped on @cmdline. The
376 * exit status of the remote process will be equal to the last value
377 * that was set with this function.
379 * In the case that the commandline invocation is local, the situation
380 * is slightly more complicated. If the commandline invocation results
381 * in the mainloop running (ie: because the use-count of the application
382 * increased to a non-zero value) then the application is considered to
383 * have been 'successful' in a certain sense, and the exit status is
384 * always zero. If the application use count is zero, though, the exit
385 * status of the local #GApplicationCommandLine is used.
390 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
393 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
395 cmdline->priv->exit_status = exit_status;
399 * g_application_command_line_get_exit_status:
400 * @cmdline: a #GApplicationCommandLine
402 * Gets the exit status of @cmdline. See
403 * g_application_command_line_set_exit_status() for more information.
405 * Returns: the exit status
410 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
412 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
414 return cmdline->priv->exit_status;
418 * g_application_command_line_get_platform_data:
419 * @cmdline: #GApplicationCommandLine
421 * Gets the platform data associated with the invocation of @cmdline.
423 * This is a #GVariant dictionary containing information about the
424 * context in which the invocation occured. It typically contains
425 * information like the current working directory and the startup
428 * For local invocation, it will be %NULL.
430 * Returns: the platform data, or %NULL
435 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
437 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
439 if (cmdline->priv->platform_data)
440 return g_variant_ref (cmdline->priv->platform_data);