GApplication dbus: publish the menus again
[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,
530                                           G_DBUS_ACTION_GROUP_FLAGS_NONE, cancellable, error);
531
532   if (actions == NULL)
533     {
534       /* The primary appears not to exist.  Fail the registration. */
535       g_application_impl_destroy (impl);
536
537       return NULL;
538     }
539
540   *remote_actions = G_ACTION_GROUP (actions);
541
542   return impl;
543 }
544
545 void
546 g_application_impl_activate (GApplicationImpl *impl,
547                              GVariant         *platform_data)
548 {
549   g_dbus_connection_call (impl->session_bus,
550                           impl->bus_name,
551                           impl->object_path,
552                           "org.gtk.Application",
553                           "Activate",
554                           g_variant_new ("(@a{sv})", platform_data),
555                           NULL, 0, -1, NULL, NULL, NULL);
556 }
557
558 void
559 g_application_impl_open (GApplicationImpl  *impl,
560                          GFile            **files,
561                          gint               n_files,
562                          const gchar       *hint,
563                          GVariant          *platform_data)
564 {
565   GVariantBuilder builder;
566   gint i;
567
568   g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
569   g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
570   for (i = 0; i < n_files; i++)
571     {
572       gchar *uri = g_file_get_uri (files[i]);
573       g_variant_builder_add (&builder, "s", uri);
574       g_free (uri);
575     }
576   g_variant_builder_close (&builder);
577   g_variant_builder_add (&builder, "s", hint);
578   g_variant_builder_add_value (&builder, platform_data);
579
580   g_dbus_connection_call (impl->session_bus,
581                           impl->bus_name,
582                           impl->object_path,
583                           "org.gtk.Application",
584                           "Open",
585                           g_variant_builder_end (&builder),
586                           NULL, 0, -1, NULL, NULL, NULL);
587 }
588
589 static void
590 g_application_impl_cmdline_method_call (GDBusConnection       *connection,
591                                         const gchar           *sender,
592                                         const gchar           *object_path,
593                                         const gchar           *interface_name,
594                                         const gchar           *method_name,
595                                         GVariant              *parameters,
596                                         GDBusMethodInvocation *invocation,
597                                         gpointer               user_data)
598 {
599   const gchar *message;
600
601   g_variant_get_child (parameters, 0, "&s", &message);
602
603   if (strcmp (method_name, "Print") == 0)
604     g_print ("%s", message);
605   else if (strcmp (method_name, "PrintError") == 0)
606     g_printerr ("%s", message);
607   else
608     g_assert_not_reached ();
609
610   g_dbus_method_invocation_return_value (invocation, NULL);
611 }
612
613 typedef struct
614 {
615   GMainLoop *loop;
616   int status;
617 } CommandLineData;
618
619 static void
620 g_application_impl_cmdline_done (GObject      *source,
621                                  GAsyncResult *result,
622                                  gpointer      user_data)
623 {
624   CommandLineData *data = user_data;
625   GError *error = NULL;
626   GVariant *reply;
627
628   reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
629                                          result, &error);
630
631   if (reply != NULL)
632     {
633       g_variant_get (reply, "(i)", &data->status);
634       g_variant_unref (reply);
635     }
636
637   else
638     {
639       g_printerr ("%s\n", error->message);
640       g_error_free (error);
641       data->status = 1;
642     }
643
644   g_main_loop_quit (data->loop);
645 }
646
647 int
648 g_application_impl_command_line (GApplicationImpl  *impl,
649                                  gchar            **arguments,
650                                  GVariant          *platform_data)
651 {
652   const static GDBusInterfaceVTable vtable = {
653     g_application_impl_cmdline_method_call
654   };
655   const gchar *object_path = "/org/gtk/Application/CommandLine";
656   GMainContext *context;
657   CommandLineData data;
658   guint object_id;
659
660   context = g_main_context_new ();
661   data.loop = g_main_loop_new (context, FALSE);
662   g_main_context_push_thread_default (context);
663
664   if (org_gtk_private_CommandLine == NULL)
665     {
666       GError *error = NULL;
667       GDBusNodeInfo *info;
668
669       info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
670       if G_UNLIKELY (info == NULL)
671         g_error ("%s", error->message);
672       org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
673       g_assert (org_gtk_private_CommandLine != NULL);
674       g_dbus_interface_info_ref (org_gtk_private_CommandLine);
675       g_dbus_node_info_unref (info);
676     }
677
678   object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
679                                                  org_gtk_private_CommandLine,
680                                                  &vtable, &data, NULL, NULL);
681   /* In theory we should try other paths... */
682   g_assert (object_id != 0);
683
684   g_dbus_connection_call (impl->session_bus,
685                           impl->bus_name,
686                           impl->object_path,
687                           "org.gtk.Application",
688                           "CommandLine",
689                           g_variant_new ("(o^aay@a{sv})", object_path,
690                                          arguments, platform_data),
691                           G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
692                           g_application_impl_cmdline_done, &data);
693
694   g_main_loop_run (data.loop);
695
696   g_main_context_pop_thread_default (context);
697   g_main_context_unref (context);
698   g_main_loop_unref (data.loop);
699
700   return data.status;
701 }
702
703 void
704 g_application_impl_flush (GApplicationImpl *impl)
705 {
706   if (impl->session_bus)
707     g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
708 }
709
710
711 /* GDBusCommandLine implementation {{{1 */
712
713 typedef GApplicationCommandLineClass GDBusCommandLineClass;
714 static GType g_dbus_command_line_get_type (void);
715 typedef struct
716 {
717   GApplicationCommandLine  parent_instance;
718   GDBusMethodInvocation   *invocation;
719
720   GDBusConnection *connection;
721   const gchar     *bus_name;
722   const gchar     *object_path;
723 } GDBusCommandLine;
724
725
726 G_DEFINE_TYPE (GDBusCommandLine,
727                g_dbus_command_line,
728                G_TYPE_APPLICATION_COMMAND_LINE)
729
730 static void
731 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
732                                    const gchar             *message)
733 {
734   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
735
736   g_dbus_connection_call (gdbcl->connection,
737                           gdbcl->bus_name,
738                           gdbcl->object_path,
739                           "org.gtk.private.CommandLine", "Print",
740                           g_variant_new ("(s)", message),
741                           NULL, 0, -1, NULL, NULL, NULL);
742 }
743
744 static void
745 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
746                                       const gchar             *message)
747 {
748   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
749
750   g_dbus_connection_call (gdbcl->connection,
751                           gdbcl->bus_name,
752                           gdbcl->object_path,
753                           "org.gtk.private.CommandLine", "PrintError",
754                           g_variant_new ("(s)", message),
755                           NULL, 0, -1, NULL, NULL, NULL);
756 }
757
758 static void
759 g_dbus_command_line_finalize (GObject *object)
760 {
761   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
762   GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
763   gint status;
764
765   status = g_application_command_line_get_exit_status (cmdline);
766
767   g_dbus_method_invocation_return_value (gdbcl->invocation,
768                                          g_variant_new ("(i)", status));
769   g_object_unref (gdbcl->invocation);
770
771   G_OBJECT_CLASS (g_dbus_command_line_parent_class)
772     ->finalize (object);
773 }
774
775 static void
776 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
777 {
778 }
779
780 static void
781 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
782 {
783   GObjectClass *object_class = G_OBJECT_CLASS (class);
784
785   object_class->finalize = g_dbus_command_line_finalize;
786   class->printerr_literal = g_dbus_command_line_printerr_literal;
787   class->print_literal = g_dbus_command_line_print_literal;
788 }
789
790 static GApplicationCommandLine *
791 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
792 {
793   GDBusCommandLine *gdbcl;
794   GVariant *args;
795
796   args = g_dbus_method_invocation_get_parameters (invocation);
797
798   gdbcl = g_object_new (g_dbus_command_line_get_type (),
799                         "arguments", g_variant_get_child_value (args, 1),
800                         "platform-data", g_variant_get_child_value (args, 2),
801                         NULL);
802   gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
803   gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
804   g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
805   gdbcl->invocation = g_object_ref (invocation);
806
807   return G_APPLICATION_COMMAND_LINE (gdbcl);
808 }
809
810 /* Epilogue {{{1 */
811
812 /* vim:set foldmethod=marker: */