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