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