Use 'dumb quotes' rather than `really dumb quotes'
[platform/upstream/glib.git] / gio / gapplicationimpl-dbus.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but 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
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include "gapplicationimpl.h"
25
26 #include "gactiongroup.h"
27 #include "gactiongroupexporter.h"
28 #include "gremoteactiongroup.h"
29 #include "gdbusactiongroup-private.h"
30 #include "gapplication.h"
31 #include "gfile.h"
32 #include "gdbusconnection.h"
33 #include "gdbusintrospection.h"
34 #include "gdbuserror.h"
35 #include "glib/gstdio.h"
36
37 #include <string.h>
38 #include <stdio.h>
39
40 #include "gapplicationcommandline.h"
41 #include "gdbusmethodinvocation.h"
42
43 #ifdef G_OS_UNIX
44 #include "gunixinputstream.h"
45 #include "gunixfdlist.h"
46 #endif
47
48 /* DBus Interface definition {{{1 */
49
50 /* For documentation of these interfaces, see
51  * http://live.gnome.org/GTK+/GApplication-dbus-apis
52  */
53 static const gchar org_gtk_Application_xml[] =
54   "<node>"
55     "<interface name='org.gtk.Application'>"
56       "<method name='Activate'>"
57         "<arg type='a{sv}' name='platform-data' direction='in'/>"
58       "</method>"
59       "<method name='Open'>"
60         "<arg type='as' name='uris' direction='in'/>"
61         "<arg type='s' name='hint' direction='in'/>"
62         "<arg type='a{sv}' name='platform-data' direction='in'/>"
63       "</method>"
64       "<method name='CommandLine'>"
65         "<arg type='o' name='path' direction='in'/>"
66         "<arg type='aay' name='arguments' direction='in'/>"
67         "<arg type='a{sv}' name='platform-data' direction='in'/>"
68         "<arg type='i' name='exit-status' direction='out'/>"
69       "</method>"
70     "<property name='Busy' type='b' access='read'/>"
71     "</interface>"
72   "</node>";
73
74 static GDBusInterfaceInfo *org_gtk_Application;
75
76 static const gchar org_gtk_private_CommandLine_xml[] =
77   "<node>"
78     "<interface name='org.gtk.private.CommandLine'>"
79       "<method name='Print'>"
80         "<arg type='s' name='message' direction='in'/>"
81       "</method>"
82       "<method name='PrintError'>"
83         "<arg type='s' name='message' direction='in'/>"
84       "</method>"
85     "</interface>"
86   "</node>";
87
88 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
89
90 /* GApplication implementation {{{1 */
91 struct _GApplicationImpl
92 {
93   GDBusConnection *session_bus;
94   GActionGroup    *exported_actions;
95   const gchar     *bus_name;
96
97   gchar           *object_path;
98   guint            object_id;
99   guint            actions_id;
100
101   gboolean         properties_live;
102   gboolean         primary;
103   gboolean         busy;
104   GApplication    *app;
105 };
106
107
108 static GApplicationCommandLine *
109 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
110
111 static GVariant *
112 g_application_impl_get_property (GDBusConnection *connection,
113                                  const gchar  *sender,
114                                  const gchar  *object_path,
115                                  const gchar  *interface_name,
116                                  const gchar  *property_name,
117                                  GError      **error,
118                                  gpointer      user_data)
119 {
120   GApplicationImpl *impl = user_data;
121
122   if (strcmp (property_name, "Busy") == 0)
123     return g_variant_new_boolean (impl->busy);
124
125   g_assert_not_reached ();
126
127   return NULL;
128 }
129
130 static void
131 send_property_change (GApplicationImpl *impl)
132 {
133   GVariantBuilder builder;
134
135   g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
136   g_variant_builder_add (&builder,
137                          "{sv}",
138                          "Busy", g_variant_new_boolean (impl->busy));
139
140   g_dbus_connection_emit_signal (impl->session_bus,
141                                  NULL,
142                                  impl->object_path,
143                                  "org.freedesktop.DBus.Properties",
144                                  "PropertiesChanged",
145                                  g_variant_new ("(sa{sv}as)",
146                                                 "org.gtk.Application",
147                                                 &builder,
148                                                 NULL),
149                                  NULL);
150 }
151
152 static void
153 g_application_impl_method_call (GDBusConnection       *connection,
154                                 const gchar           *sender,
155                                 const gchar           *object_path,
156                                 const gchar           *interface_name,
157                                 const gchar           *method_name,
158                                 GVariant              *parameters,
159                                 GDBusMethodInvocation *invocation,
160                                 gpointer               user_data)
161 {
162   GApplicationImpl *impl = user_data;
163   GApplicationClass *class;
164
165   class = G_APPLICATION_GET_CLASS (impl->app);
166
167   if (strcmp (method_name, "Activate") == 0)
168     {
169       GVariant *platform_data;
170
171       g_variant_get (parameters, "(@a{sv})", &platform_data);
172       class->before_emit (impl->app, platform_data);
173       g_signal_emit_by_name (impl->app, "activate");
174       class->after_emit (impl->app, platform_data);
175       g_variant_unref (platform_data);
176
177       g_dbus_method_invocation_return_value (invocation, NULL);
178     }
179
180   else if (strcmp (method_name, "Open") == 0)
181     {
182       GVariant *platform_data;
183       const gchar *hint;
184       GVariant *array;
185       GFile **files;
186       gint n, i;
187
188       g_variant_get (parameters, "(@as&s@a{sv})",
189                      &array, &hint, &platform_data);
190
191       n = g_variant_n_children (array);
192       files = g_new (GFile *, n + 1);
193
194       for (i = 0; i < n; i++)
195         {
196           const gchar *uri;
197
198           g_variant_get_child (array, i, "&s", &uri);
199           files[i] = g_file_new_for_uri (uri);
200         }
201       g_variant_unref (array);
202       files[n] = NULL;
203
204       class->before_emit (impl->app, platform_data);
205       g_signal_emit_by_name (impl->app, "open", files, n, hint);
206       class->after_emit (impl->app, platform_data);
207
208       g_variant_unref (platform_data);
209
210       for (i = 0; i < n; i++)
211         g_object_unref (files[i]);
212       g_free (files);
213
214       g_dbus_method_invocation_return_value (invocation, NULL);
215     }
216
217   else if (strcmp (method_name, "CommandLine") == 0)
218     {
219       GApplicationCommandLine *cmdline;
220       GVariant *platform_data;
221       int status;
222
223       cmdline = g_dbus_command_line_new (invocation);
224       platform_data = g_variant_get_child_value (parameters, 2);
225       class->before_emit (impl->app, platform_data);
226       g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
227       g_application_command_line_set_exit_status (cmdline, status);
228       class->after_emit (impl->app, platform_data);
229       g_variant_unref (platform_data);
230       g_object_unref (cmdline);
231     }
232   else
233     g_assert_not_reached ();
234 }
235
236 static gchar *
237 application_path_from_appid (const gchar *appid)
238 {
239   gchar *appid_path, *iter;
240
241   if (appid == NULL)
242     /* this is a private implementation detail */
243     return g_strdup ("/org/gtk/Application/anonymous");
244
245   appid_path = g_strconcat ("/", appid, NULL);
246   for (iter = appid_path; *iter; iter++)
247     {
248       if (*iter == '.')
249         *iter = '/';
250
251       if (*iter == '-')
252         *iter = '_';
253     }
254
255   return appid_path;
256 }
257
258 /* Attempt to become the primary instance.
259  *
260  * Returns %TRUE if everything went OK, regardless of if we became the
261  * primary instance or not.  %FALSE is reserved for when something went
262  * seriously wrong (and @error will be set too, in that case).
263  *
264  * After a %TRUE return, impl->primary will be TRUE if we were
265  * successful.
266  */
267 static gboolean
268 g_application_impl_attempt_primary (GApplicationImpl  *impl,
269                                     GCancellable      *cancellable,
270                                     GError           **error)
271 {
272   const static GDBusInterfaceVTable vtable = {
273     g_application_impl_method_call,
274     g_application_impl_get_property,
275     NULL /* set_property */
276   };
277   GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
278   GVariant *reply;
279   guint32 rval;
280
281   if (org_gtk_Application == NULL)
282     {
283       GError *error = NULL;
284       GDBusNodeInfo *info;
285
286       info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
287       if G_UNLIKELY (info == NULL)
288         g_error ("%s", error->message);
289       org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
290       g_assert (org_gtk_Application != NULL);
291       g_dbus_interface_info_ref (org_gtk_Application);
292       g_dbus_node_info_unref (info);
293     }
294
295   /* We could possibly have been D-Bus activated as a result of incoming
296    * requests on either the application or actiongroup interfaces.
297    * Because of how GDBus dispatches messages, we need to ensure that
298    * both of those things are registered before we attempt to request
299    * our name.
300    *
301    * The action group need not be populated yet, as long as it happens
302    * before we return to the mainloop.  The reason for that is because
303    * GDBus does the check to make sure the object exists from the worker
304    * thread but doesn't actually dispatch the action invocation until we
305    * hit the mainloop in this thread.  There is also no danger of
306    * receiving 'activate' or 'open' signals until after 'startup' runs,
307    * for the same reason.
308    */
309   impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
310                                                        org_gtk_Application, &vtable, impl, NULL, error);
311
312   if (impl->object_id == 0)
313     return FALSE;
314
315   impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
316                                                             impl->exported_actions, error);
317
318   if (impl->actions_id == 0)
319     return FALSE;
320
321   if (!app_class->dbus_register (impl->app,
322                                  impl->session_bus,
323                                  impl->object_path,
324                                  error))
325     return FALSE;
326
327   if (impl->bus_name == NULL)
328     {
329       /* If this is a non-unique application then it is sufficient to
330        * have our object paths registered. We can return now.
331        *
332        * Note: non-unique applications always act as primary-instance.
333        */
334       impl->primary = TRUE;
335       return TRUE;
336     }
337
338   /* If this is a unique application then we need to attempt to own
339    * the well-known name and fall back to remote mode (!is_primary)
340    * in the case that we can't do that.
341    */
342   /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
343   reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
344                                        "org.freedesktop.DBus", "RequestName",
345                                        g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
346                                        0, -1, cancellable, error);
347
348   if (reply == NULL)
349     return FALSE;
350
351   g_variant_get (reply, "(u)", &rval);
352   g_variant_unref (reply);
353
354   /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
355   impl->primary = (rval != 3);
356
357   return TRUE;
358 }
359
360 /* Stop doing the things that the primary instance does.
361  *
362  * This should be called if attempting to become the primary instance
363  * failed (in order to clean up any partial success) and should also
364  * be called when freeing the GApplication.
365  *
366  * It is safe to call this multiple times.
367  */
368 static void
369 g_application_impl_stop_primary (GApplicationImpl *impl)
370 {
371   GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
372
373   app_class->dbus_unregister (impl->app,
374                               impl->session_bus,
375                               impl->object_path);
376
377   if (impl->object_id)
378     {
379       g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
380       impl->object_id = 0;
381     }
382
383   if (impl->actions_id)
384     {
385       g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
386       impl->actions_id = 0;
387     }
388
389   if (impl->primary && impl->bus_name)
390     {
391       g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
392                               "/org/freedesktop/DBus", "org.freedesktop.DBus",
393                               "ReleaseName", g_variant_new ("(s)", impl->bus_name),
394                               NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
395       impl->primary = FALSE;
396     }
397 }
398
399 void
400 g_application_impl_set_busy_state (GApplicationImpl *impl,
401                                    gboolean          busy)
402 {
403   if (impl->busy != busy)
404     {
405       impl->busy = busy;
406       send_property_change (impl);
407     }
408 }
409
410 void
411 g_application_impl_destroy (GApplicationImpl *impl)
412 {
413   g_application_impl_stop_primary (impl);
414
415   if (impl->session_bus)
416     g_object_unref (impl->session_bus);
417
418   g_free (impl->object_path);
419
420   g_slice_free (GApplicationImpl, impl);
421 }
422
423 GApplicationImpl *
424 g_application_impl_register (GApplication        *application,
425                              const gchar         *appid,
426                              GApplicationFlags    flags,
427                              GActionGroup        *exported_actions,
428                              GRemoteActionGroup **remote_actions,
429                              GCancellable        *cancellable,
430                              GError             **error)
431 {
432   GDBusActionGroup *actions;
433   GApplicationImpl *impl;
434
435   g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
436
437   impl = g_slice_new0 (GApplicationImpl);
438
439   impl->app = application;
440   impl->exported_actions = exported_actions;
441
442   /* non-unique applications do not attempt to acquire a bus name */
443   if (~flags & G_APPLICATION_NON_UNIQUE)
444     impl->bus_name = appid;
445
446   impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
447
448   if (impl->session_bus == NULL)
449     {
450       /* If we can't connect to the session bus, proceed as a normal
451        * non-unique application.
452        */
453       *remote_actions = NULL;
454       return impl;
455     }
456
457   impl->object_path = application_path_from_appid (appid);
458
459   /* Only try to be the primary instance if
460    * G_APPLICATION_IS_LAUNCHER was not specified.
461    */
462   if (~flags & G_APPLICATION_IS_LAUNCHER)
463     {
464       if (!g_application_impl_attempt_primary (impl, cancellable, error))
465         {
466           g_application_impl_destroy (impl);
467           return NULL;
468         }
469
470       if (impl->primary)
471         return impl;
472
473       /* We didn't make it.  Drop our service-side stuff. */
474       g_application_impl_stop_primary (impl);
475
476       if (flags & G_APPLICATION_IS_SERVICE)
477         {
478           g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
479                        "Unable to acquire bus name '%s'", appid);
480           g_application_impl_destroy (impl);
481
482           return NULL;
483         }
484     }
485
486   /* We are non-primary.  Try to get the primary's list of actions.
487    * This also serves as a mechanism to ensure that the primary exists
488    * (ie: DBus service files installed correctly, etc).
489    */
490   actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
491   if (!g_dbus_action_group_sync (actions, cancellable, error))
492     {
493       /* The primary appears not to exist.  Fail the registration. */
494       g_application_impl_destroy (impl);
495       g_object_unref (actions);
496
497       return NULL;
498     }
499
500   *remote_actions = G_REMOTE_ACTION_GROUP (actions);
501
502   return impl;
503 }
504
505 void
506 g_application_impl_activate (GApplicationImpl *impl,
507                              GVariant         *platform_data)
508 {
509   g_dbus_connection_call (impl->session_bus,
510                           impl->bus_name,
511                           impl->object_path,
512                           "org.gtk.Application",
513                           "Activate",
514                           g_variant_new ("(@a{sv})", platform_data),
515                           NULL, 0, -1, NULL, NULL, NULL);
516 }
517
518 void
519 g_application_impl_open (GApplicationImpl  *impl,
520                          GFile            **files,
521                          gint               n_files,
522                          const gchar       *hint,
523                          GVariant          *platform_data)
524 {
525   GVariantBuilder builder;
526   gint i;
527
528   g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
529   g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
530   for (i = 0; i < n_files; i++)
531     {
532       gchar *uri = g_file_get_uri (files[i]);
533       g_variant_builder_add (&builder, "s", uri);
534       g_free (uri);
535     }
536   g_variant_builder_close (&builder);
537   g_variant_builder_add (&builder, "s", hint);
538   g_variant_builder_add_value (&builder, platform_data);
539
540   g_dbus_connection_call (impl->session_bus,
541                           impl->bus_name,
542                           impl->object_path,
543                           "org.gtk.Application",
544                           "Open",
545                           g_variant_builder_end (&builder),
546                           NULL, 0, -1, NULL, NULL, NULL);
547 }
548
549 static void
550 g_application_impl_cmdline_method_call (GDBusConnection       *connection,
551                                         const gchar           *sender,
552                                         const gchar           *object_path,
553                                         const gchar           *interface_name,
554                                         const gchar           *method_name,
555                                         GVariant              *parameters,
556                                         GDBusMethodInvocation *invocation,
557                                         gpointer               user_data)
558 {
559   const gchar *message;
560
561   g_variant_get_child (parameters, 0, "&s", &message);
562
563   if (strcmp (method_name, "Print") == 0)
564     g_print ("%s", message);
565   else if (strcmp (method_name, "PrintError") == 0)
566     g_printerr ("%s", message);
567   else
568     g_assert_not_reached ();
569
570   g_dbus_method_invocation_return_value (invocation, NULL);
571 }
572
573 typedef struct
574 {
575   GMainLoop *loop;
576   int status;
577 } CommandLineData;
578
579 static void
580 g_application_impl_cmdline_done (GObject      *source,
581                                  GAsyncResult *result,
582                                  gpointer      user_data)
583 {
584   CommandLineData *data = user_data;
585   GError *error = NULL;
586   GVariant *reply;
587
588 #ifdef G_OS_UNIX
589   reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (source), NULL, result, &error);
590 #else
591   reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
592 #endif
593
594
595   if (reply != NULL)
596     {
597       g_variant_get (reply, "(i)", &data->status);
598       g_variant_unref (reply);
599     }
600
601   else
602     {
603       g_printerr ("%s\n", error->message);
604       g_error_free (error);
605       data->status = 1;
606     }
607
608   g_main_loop_quit (data->loop);
609 }
610
611 int
612 g_application_impl_command_line (GApplicationImpl  *impl,
613                                  gchar            **arguments,
614                                  GVariant          *platform_data)
615 {
616   const static GDBusInterfaceVTable vtable = {
617     g_application_impl_cmdline_method_call
618   };
619   const gchar *object_path = "/org/gtk/Application/CommandLine";
620   GMainContext *context;
621   CommandLineData data;
622   guint object_id;
623
624   context = g_main_context_new ();
625   data.loop = g_main_loop_new (context, FALSE);
626   g_main_context_push_thread_default (context);
627
628   if (org_gtk_private_CommandLine == NULL)
629     {
630       GError *error = NULL;
631       GDBusNodeInfo *info;
632
633       info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
634       if G_UNLIKELY (info == NULL)
635         g_error ("%s", error->message);
636       org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
637       g_assert (org_gtk_private_CommandLine != NULL);
638       g_dbus_interface_info_ref (org_gtk_private_CommandLine);
639       g_dbus_node_info_unref (info);
640     }
641
642   object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
643                                                  org_gtk_private_CommandLine,
644                                                  &vtable, &data, NULL, NULL);
645   /* In theory we should try other paths... */
646   g_assert (object_id != 0);
647
648 #ifdef G_OS_UNIX
649   {
650     GError *error = NULL;
651     GUnixFDList *fd_list;
652
653     /* send along the stdin in case
654      * g_application_command_line_get_stdin_data() is called
655      */
656     fd_list = g_unix_fd_list_new ();
657     g_unix_fd_list_append (fd_list, 0, &error);
658     g_assert_no_error (error);
659
660     g_dbus_connection_call_with_unix_fd_list (impl->session_bus, impl->bus_name, impl->object_path,
661                                               "org.gtk.Application", "CommandLine",
662                                               g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
663                                               G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL,
664                                               g_application_impl_cmdline_done, &data);
665   }
666 #else
667   g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path,
668                           "org.gtk.Application", "CommandLine",
669                           g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
670                           G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
671                           g_application_impl_cmdline_done, &data);
672 #endif
673
674   g_main_loop_run (data.loop);
675
676   g_main_context_pop_thread_default (context);
677   g_main_context_unref (context);
678   g_main_loop_unref (data.loop);
679
680   return data.status;
681 }
682
683 void
684 g_application_impl_flush (GApplicationImpl *impl)
685 {
686   if (impl->session_bus)
687     g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
688 }
689
690 GDBusConnection *
691 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
692 {
693   return impl->session_bus;
694 }
695
696 const gchar *
697 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
698 {
699   return impl->object_path;
700 }
701
702 /* GDBusCommandLine implementation {{{1 */
703
704 typedef GApplicationCommandLineClass GDBusCommandLineClass;
705 static GType g_dbus_command_line_get_type (void);
706 typedef struct
707 {
708   GApplicationCommandLine  parent_instance;
709   GDBusMethodInvocation   *invocation;
710
711   GDBusConnection *connection;
712   const gchar     *bus_name;
713   const gchar     *object_path;
714 } GDBusCommandLine;
715
716
717 G_DEFINE_TYPE (GDBusCommandLine,
718                g_dbus_command_line,
719                G_TYPE_APPLICATION_COMMAND_LINE)
720
721 static void
722 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
723                                    const gchar             *message)
724 {
725   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
726
727   g_dbus_connection_call (gdbcl->connection,
728                           gdbcl->bus_name,
729                           gdbcl->object_path,
730                           "org.gtk.private.CommandLine", "Print",
731                           g_variant_new ("(s)", message),
732                           NULL, 0, -1, NULL, NULL, NULL);
733 }
734
735 static void
736 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
737                                       const gchar             *message)
738 {
739   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
740
741   g_dbus_connection_call (gdbcl->connection,
742                           gdbcl->bus_name,
743                           gdbcl->object_path,
744                           "org.gtk.private.CommandLine", "PrintError",
745                           g_variant_new ("(s)", message),
746                           NULL, 0, -1, NULL, NULL, NULL);
747 }
748
749 static GInputStream *
750 g_dbus_command_line_get_stdin (GApplicationCommandLine *cmdline)
751 {
752 #ifdef G_OS_UNIX
753   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
754   GInputStream *result = NULL;
755   GDBusMessage *message;
756   GUnixFDList *fd_list;
757
758   message = g_dbus_method_invocation_get_message (gdbcl->invocation);
759   fd_list = g_dbus_message_get_unix_fd_list (message);
760
761   if (fd_list && g_unix_fd_list_get_length (fd_list))
762     {
763       gint *fds, n_fds, i;
764
765       fds = g_unix_fd_list_steal_fds (fd_list, &n_fds);
766       result = g_unix_input_stream_new (fds[0], TRUE);
767       for (i = 1; i < n_fds; i++)
768         (void) g_close (fds[i], NULL);
769       g_free (fds);
770     }
771
772   return result;
773 #else
774   return NULL;
775 #endif
776 }
777
778 static void
779 g_dbus_command_line_finalize (GObject *object)
780 {
781   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
782   GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
783   gint status;
784
785   status = g_application_command_line_get_exit_status (cmdline);
786
787   g_dbus_method_invocation_return_value (gdbcl->invocation,
788                                          g_variant_new ("(i)", status));
789   g_object_unref (gdbcl->invocation);
790
791   G_OBJECT_CLASS (g_dbus_command_line_parent_class)
792     ->finalize (object);
793 }
794
795 static void
796 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
797 {
798 }
799
800 static void
801 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
802 {
803   GObjectClass *object_class = G_OBJECT_CLASS (class);
804
805   object_class->finalize = g_dbus_command_line_finalize;
806   class->printerr_literal = g_dbus_command_line_printerr_literal;
807   class->print_literal = g_dbus_command_line_print_literal;
808   class->get_stdin = g_dbus_command_line_get_stdin;
809 }
810
811 static GApplicationCommandLine *
812 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
813 {
814   GDBusCommandLine *gdbcl;
815   GVariant *args;
816
817   args = g_dbus_method_invocation_get_parameters (invocation);
818
819   gdbcl = g_object_new (g_dbus_command_line_get_type (),
820                         "arguments", g_variant_get_child_value (args, 1),
821                         "platform-data", g_variant_get_child_value (args, 2),
822                         NULL);
823   gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
824   gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
825   g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
826   gdbcl->invocation = g_object_ref (invocation);
827
828   return G_APPLICATION_COMMAND_LINE (gdbcl);
829 }
830
831 /* Epilogue {{{1 */
832
833 /* vim:set foldmethod=marker: */