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