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
65 struct _GApplicationCommandLinePrivate
67 GVariant *platform_data;
73 /* All subclasses represent remote invocations of some kind. */
74 #define IS_REMOTE(cmdline) (G_TYPE_FROM_INSTANCE (cmdline) != \
75 G_TYPE_APPLICATION_COMMAND_LINE)
78 grok_platform_data (GApplicationCommandLine *cmdline)
84 g_variant_iter_init (&iter, cmdline->priv->platform_data);
86 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
87 if (strcmp (key, "cwd") == 0)
89 if (!cmdline->priv->cwd)
90 cmdline->priv->cwd = g_variant_ref (value);
95 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
98 g_print ("%s\n", message);
102 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
103 const gchar *message)
105 g_printerr ("%s\n", message);
109 g_application_command_line_get_property (GObject *object,
114 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
119 g_value_set_variant (value, cmdline->priv->arguments);
122 case PROP_PLATFORM_DATA:
123 g_value_set_variant (value, cmdline->priv->platform_data);
127 g_value_set_boolean (value, IS_REMOTE (cmdline));
131 g_assert_not_reached ();
136 g_application_command_line_set_property (GObject *object,
141 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
146 g_assert (cmdline->priv->arguments == NULL);
147 cmdline->priv->arguments = g_value_dup_variant (value);
150 case PROP_PLATFORM_DATA:
151 g_assert (cmdline->priv->platform_data == NULL);
152 cmdline->priv->platform_data = g_value_dup_variant (value);
153 if (cmdline->priv->platform_data != NULL)
154 grok_platform_data (cmdline);
158 g_assert_not_reached ();
163 g_application_command_line_finalize (GObject *object)
165 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
167 if (cmdline->priv->platform_data)
168 g_variant_unref (cmdline->priv->platform_data);
169 if (cmdline->priv->arguments)
170 g_variant_unref (cmdline->priv->arguments);
171 if (cmdline->priv->cwd)
172 g_variant_unref (cmdline->priv->cwd);
174 G_OBJECT_CLASS (g_application_command_line_parent_class)
179 g_application_command_line_init (GApplicationCommandLine *cmdline)
182 G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
183 G_TYPE_APPLICATION_COMMAND_LINE,
184 GApplicationCommandLinePrivate);
188 g_application_command_line_class_init (GApplicationCommandLineClass *class)
190 GObjectClass *object_class = G_OBJECT_CLASS (class);
192 object_class->get_property = g_application_command_line_get_property;
193 object_class->set_property = g_application_command_line_set_property;
194 object_class->finalize = g_application_command_line_finalize;
195 class->printerr_literal = g_application_command_line_real_printerr_literal;
196 class->print_literal = g_application_command_line_real_print_literal;
198 g_object_class_install_property (object_class, PROP_ARGUMENTS,
199 g_param_spec_variant ("arguments",
200 P_("Commandline arguments"),
201 P_("The commandline that caused this ::command-line signal emission"),
202 G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL,
203 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
204 G_PARAM_STATIC_STRINGS));
206 g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
207 g_param_spec_variant ("platform-data",
209 P_("Platform-specific data for the commandline"),
210 G_VARIANT_TYPE ("a{sv}"), NULL,
211 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
212 G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (object_class, PROP_IS_REMOTE,
215 g_param_spec_boolean ("is-remote",
217 P_("TRUE if this is a remote commandline"),
219 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
221 g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
226 * g_application_command_line_get_arguments:
227 * @cmdline: a #GApplicationCommandLine
228 * @argc: the length of the arguments array, or %NULL
230 * Gets the list of arguments that was passed on the command line.
232 * The strings in the array may contain non-utf8 data.
234 * The return value is %NULL-terminated and should be freed using
237 * Returns: the string array containing the arguments (the argv)
242 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
248 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
250 argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
259 * g_application_command_line_get_cwd:
260 * @cmdline: a #GApplicationCommandLine
262 * Gets the working directory of the command line invocation. The
263 * string may contain non-utf8 data.
265 * It is possible that the remote application did not send a working
266 * directory, so this may be %NULL.
268 * The return value should not be modified or freed and is valid for as
269 * long as @cmdline exists.
271 * Returns: the current directory, or %NULL
276 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
278 if (cmdline->priv->cwd)
279 return g_variant_get_bytestring (cmdline->priv->cwd);
285 * g_application_command_line_get_is_remote:
286 * @cmdline: a #GApplicationCommandLine
288 * Determines if @cmdline represents a remote invocation.
290 * Returns: %TRUE if the invocation was remote
295 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
297 return IS_REMOTE (cmdline);
301 * g_application_command_line_print:
302 * @cmdline: a #GApplicationCommandLine
303 * @format: a printf-style format string
304 * @...: arguments, as per @format
306 * Formats a message and prints it using the stdout print handler in the
309 * If @cmdline is a local invocation then this is exactly equivalent to
310 * g_print(). If @cmdline is remote then this is equivalent to calling
311 * g_print() in the invoking process.
316 g_application_command_line_print (GApplicationCommandLine *cmdline,
323 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
324 g_return_if_fail (format != NULL);
326 va_start (ap, format);
327 message = g_strdup_vprintf (format, ap);
330 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
331 ->print_literal (cmdline, message);
336 * g_application_command_line_printerr:
337 * @cmdline: a #GApplicationCommandLine
338 * @format: a printf-style format string
339 * @...: arguments, as per @format
341 * Formats a message and prints it using the stderr print handler in the
344 * If @cmdline is a local invocation then this is exactly equivalent to
345 * g_printerr(). If @cmdline is remote then this is equivalent to
346 * calling g_printerr() in the invoking process.
351 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
358 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
359 g_return_if_fail (format != NULL);
361 va_start (ap, format);
362 message = g_strdup_vprintf (format, ap);
365 G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
366 ->printerr_literal (cmdline, message);
371 * g_application_command_line_set_exit_status:
372 * @cmdline: a #GApplicationCommandLine
373 * @exit_status: the exit status
375 * Sets the exit status that will be used when the invoking process
378 * The return value of the #GApplication::command-line signal is
379 * passed to this function when the handler returns. This is the usual
380 * way of setting the exit status.
382 * In the event that you want the remote invocation to continue running
383 * and want to decide on the exit status in the future, you can use this
384 * call. For the case of a remote invocation, the remote process will
385 * typically exit when the last reference is dropped on @cmdline. The
386 * exit status of the remote process will be equal to the last value
387 * that was set with this function.
389 * In the case that the commandline invocation is local, the situation
390 * is slightly more complicated. If the commandline invocation results
391 * in the mainloop running (ie: because the use-count of the application
392 * increased to a non-zero value) then the application is considered to
393 * have been 'successful' in a certain sense, and the exit status is
394 * always zero. If the application use count is zero, though, the exit
395 * status of the local #GApplicationCommandLine is used.
400 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
403 g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
405 cmdline->priv->exit_status = exit_status;
409 * g_application_command_line_get_exit_status:
410 * @cmdline: a #GApplicationCommandLine
412 * Gets the exit status of @cmdline. See
413 * g_application_command_line_set_exit_status() for more information.
415 * Returns: the exit status
420 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
422 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
424 return cmdline->priv->exit_status;
428 * g_application_command_line_get_platform_data:
429 * @cmdline: #GApplicationCommandLine
431 * Gets the platform data associated with the invocation of @cmdline.
433 * This is a #GVariant dictionary containing information about the
434 * context in which the invocation occured. It typically contains
435 * information like the current working directory and the startup
438 * For local invocation, it will be %NULL.
440 * Returns: the platform data, or %NULL
445 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
447 g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
449 if (cmdline->priv->platform_data)
450 return g_variant_ref (cmdline->priv->platform_data);