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