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