Add another example for commandline handling
[platform/upstream/glib.git] / gio / gapplicationcommandline.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
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,
17  * USA.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include "gapplicationcommandline.h"
25
26 #include "glibintl.h"
27
28 #include <string.h>
29 #include <stdio.h>
30
31 G_DEFINE_TYPE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJECT)
32
33 /**
34  * SECTION:gapplicationcommandline
35  * @title: GApplicationCommandLine
36  * @short_description: A class representing a command-line invocation of
37  *                     an application
38  * @see_also: #GApplication
39  *
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.
43  *
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).
49  *
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
54  * dropped).
55  *
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>
57  *
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>
59  **/
60
61 enum
62 {
63   PROP_NONE,
64   PROP_ARGUMENTS,
65   PROP_PLATFORM_DATA,
66   PROP_IS_REMOTE
67 };
68
69 struct _GApplicationCommandLinePrivate
70 {
71   GVariant *platform_data;
72   GVariant *arguments;
73   GVariant *cwd;
74   gint exit_status;
75 };
76
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)
80
81 static void
82 grok_platform_data (GApplicationCommandLine *cmdline)
83 {
84   GVariantIter iter;
85   const gchar *key;
86   GVariant *value;
87
88   g_variant_iter_init (&iter, cmdline->priv->platform_data);
89
90   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
91     if (strcmp (key, "cwd") == 0)
92       {
93         if (!cmdline->priv->cwd)
94           cmdline->priv->cwd = g_variant_ref (value);
95       }
96 }
97
98 static void
99 g_application_command_line_real_print_literal (GApplicationCommandLine *cmdline,
100                                                const gchar             *message)
101 {
102   g_print ("%s\n", message);
103 }
104
105 static void
106 g_application_command_line_real_printerr_literal (GApplicationCommandLine *cmdline,
107                                                   const gchar             *message)
108 {
109   g_printerr ("%s\n", message);
110 }
111
112 static void
113 g_application_command_line_get_property (GObject    *object,
114                                          guint       prop_id,
115                                          GValue     *value,
116                                          GParamSpec *pspec)
117 {
118   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
119
120   switch (prop_id)
121     {
122     case PROP_ARGUMENTS:
123       g_value_set_variant (value, cmdline->priv->arguments);
124       break;
125
126     case PROP_PLATFORM_DATA:
127       g_value_set_variant (value, cmdline->priv->platform_data);
128       break;
129
130     case PROP_IS_REMOTE:
131       g_value_set_boolean (value, IS_REMOTE (cmdline));
132       break;
133
134     default:
135       g_assert_not_reached ();
136     }
137 }
138
139 static void
140 g_application_command_line_set_property (GObject      *object,
141                                          guint         prop_id,
142                                          const GValue *value,
143                                          GParamSpec   *pspec)
144 {
145   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
146
147   switch (prop_id)
148     {
149     case PROP_ARGUMENTS:
150       g_assert (cmdline->priv->arguments == NULL);
151       cmdline->priv->arguments = g_value_dup_variant (value);
152       break;
153
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);
159       break;
160
161     default:
162       g_assert_not_reached ();
163     }
164 }
165
166 static void
167 g_application_command_line_finalize (GObject *object)
168 {
169   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
170
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);
177
178   G_OBJECT_CLASS (g_application_command_line_parent_class)
179     ->finalize (object);
180 }
181
182 static void
183 g_application_command_line_init (GApplicationCommandLine *cmdline)
184 {
185   cmdline->priv =
186     G_TYPE_INSTANCE_GET_PRIVATE (cmdline,
187                                  G_TYPE_APPLICATION_COMMAND_LINE,
188                                  GApplicationCommandLinePrivate);
189 }
190
191 static void
192 g_application_command_line_class_init (GApplicationCommandLineClass *class)
193 {
194   GObjectClass *object_class = G_OBJECT_CLASS (class);
195
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;
201
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));
209
210   g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
211     g_param_spec_variant ("platform-data",
212                           P_("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));
217
218   g_object_class_install_property (object_class, PROP_IS_REMOTE,
219     g_param_spec_boolean ("is-remote",
220                           P_("Is remote"),
221                           P_("TRUE if this is a remote commandline"),
222                           FALSE,
223                           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
224
225   g_type_class_add_private (class, sizeof (GApplicationCommandLinePrivate));
226 }
227
228
229 /**
230  * g_application_command_line_get_arguments:
231  * @cmdline: a #GApplicationCommandLine
232  * @argc: the length of the arguments array, or %NULL
233  *
234  * Gets the list of arguments that was passed on the command line.
235  *
236  * The strings in the array may contain non-utf8 data.
237  *
238  * The return value is %NULL-terminated and should be freed using
239  * g_strfreev().
240  *
241  * Returns: the string array containing the arguments (the argv)
242  *
243  * Since: 2.28
244  **/
245 gchar **
246 g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
247                                           int                     *argc)
248 {
249   gchar **argv;
250   gsize len;
251
252   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
253
254   argv = g_variant_dup_bytestring_array (cmdline->priv->arguments, &len);
255
256   if (argc)
257     *argc = len;
258
259   return argv;
260 }
261
262 /**
263  * g_application_command_line_get_cwd:
264  * @cmdline: a #GApplicationCommandLine
265  *
266  * Gets the working directory of the command line invocation.  The
267  * string may contain non-utf8 data.
268  *
269  * It is possible that the remote application did not send a working
270  * directory, so this may be %NULL.
271  *
272  * The return value should not be modified or freed and is valid for as
273  * long as @cmdline exists.
274  *
275  * Returns: the current directory, or %NULL
276  *
277  * Since: 2.28
278  **/
279 const gchar *
280 g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
281 {
282   if (cmdline->priv->cwd)
283     return g_variant_get_bytestring (cmdline->priv->cwd);
284   else
285     return NULL;
286 }
287
288 /**
289  * g_application_command_line_get_is_remote:
290  * @cmdline: a #GApplicationCommandLine
291  *
292  * Determines if @cmdline represents a remote invocation.
293  *
294  * Returns: %TRUE if the invocation was remote
295  *
296  * Since: 2.28
297  **/
298 gboolean
299 g_application_command_line_get_is_remote (GApplicationCommandLine *cmdline)
300 {
301   return IS_REMOTE (cmdline);
302 }
303
304 /**
305  * g_application_command_line_print:
306  * @cmdline: a #GApplicationCommandLine
307  * @format: a printf-style format string
308  * @...: arguments, as per @format
309  *
310  * Formats a message and prints it using the stdout print handler in the
311  * invoking process.
312  *
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.
316  *
317  * Since: 2.28
318  **/
319 void
320 g_application_command_line_print (GApplicationCommandLine *cmdline,
321                                   const gchar             *format,
322                                   ...)
323 {
324   gchar *message;
325   va_list ap;
326
327   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
328   g_return_if_fail (format != NULL);
329
330   va_start (ap, format);
331   message = g_strdup_vprintf (format, ap);
332   va_end (ap);
333
334   G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
335     ->print_literal (cmdline, message);
336   g_free (message);
337 }
338
339 /**
340  * g_application_command_line_printerr:
341  * @cmdline: a #GApplicationCommandLine
342  * @format: a printf-style format string
343  * @...: arguments, as per @format
344  *
345  * Formats a message and prints it using the stderr print handler in the
346  * invoking process.
347  *
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.
351  *
352  * Since: 2.28
353  **/
354 void
355 g_application_command_line_printerr (GApplicationCommandLine *cmdline,
356                                      const gchar             *format,
357                                      ...)
358 {
359   gchar *message;
360   va_list ap;
361
362   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
363   g_return_if_fail (format != NULL);
364
365   va_start (ap, format);
366   message = g_strdup_vprintf (format, ap);
367   va_end (ap);
368
369   G_APPLICATION_COMMAND_LINE_GET_CLASS (cmdline)
370     ->printerr_literal (cmdline, message);
371   g_free (message);
372 }
373
374 /**
375  * g_application_command_line_set_exit_status:
376  * @cmdline: a #GApplicationCommandLine
377  * @exit_status: the exit status
378  *
379  * Sets the exit status that will be used when the invoking process
380  * exits.
381  *
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.
385  *
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.
392  *
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.
400  *
401  * Since: 2.28
402  **/
403 void
404 g_application_command_line_set_exit_status (GApplicationCommandLine *cmdline,
405                                             int                      exit_status)
406 {
407   g_return_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline));
408
409   cmdline->priv->exit_status = exit_status;
410 }
411
412 /**
413  * g_application_command_line_get_exit_status:
414  * @cmdline: a #GApplicationCommandLine
415  *
416  * Gets the exit status of @cmdline.  See
417  * g_application_command_line_set_exit_status() for more information.
418  *
419  * Returns: the exit status
420  *
421  * Since: 2.28
422  **/
423 int
424 g_application_command_line_get_exit_status (GApplicationCommandLine *cmdline)
425 {
426   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), -1);
427
428   return cmdline->priv->exit_status;
429 }
430
431 /**
432  * g_application_command_line_get_platform_data:
433  * @cmdline: #GApplicationCommandLine
434  *
435  * Gets the platform data associated with the invocation of @cmdline.
436  *
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
440  * notification ID.
441  *
442  * For local invocation, it will be %NULL.
443  *
444  * Returns: the platform data, or %NULL
445  *
446  * Since: 2.28
447  **/
448 GVariant *
449 g_application_command_line_get_platform_data (GApplicationCommandLine *cmdline)
450 {
451   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
452
453   if (cmdline->priv->platform_data)
454     return g_variant_ref (cmdline->priv->platform_data);
455   else
456       return NULL;
457 }