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