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