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