GApplication: use GActionGroup exporter
[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 "gapplication.h"
27 #include "gfile.h"
28 #include "gdbusconnection.h"
29 #include "gdbusintrospection.h"
30 #include "gdbuserror.h"
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "gapplicationcommandline.h"
36 #include "gdbusmethodinvocation.h"
37
38 /* DBus Interface definition {{{1 */
39 static const GDBusArgInfo platform_data_arg = { -1, (gchar *) "platform_data", (gchar *) "a{sv}" };
40
41 static const GDBusArgInfo open_uris_arg = { -1, (gchar *) "uris", (gchar *) "as" };
42 static const GDBusArgInfo open_hint_arg = { -1, (gchar *) "hint", (gchar *) "s" };
43
44 static const GDBusArgInfo invoke_action_name_arg = { -1, (gchar *) "name", (gchar *) "s" };
45 static const GDBusArgInfo invoke_action_args_arg = { -1, (gchar *) "args", (gchar *) "v" };
46
47 static const GDBusArgInfo cmdline_path_arg = { -1, (gchar *) "path", (gchar *) "o" };
48 static const GDBusArgInfo cmdline_arguments_arg = { -1, (gchar *) "arguments", (gchar *) "aay" };
49 static const GDBusArgInfo cmdline_exit_status_arg = { -1, (gchar *) "exit_status", (gchar *) "i" };
50
51 static const GDBusArgInfo *activate_in[] = { &platform_data_arg, NULL };
52 static const GDBusArgInfo *activate_out[] = { NULL };
53
54 static const GDBusArgInfo *open_in[] = { &open_uris_arg, &open_hint_arg, &platform_data_arg, NULL };
55 static const GDBusArgInfo *open_out[] = { NULL };
56
57 static const GDBusArgInfo *cmdline_in[] = { &cmdline_path_arg, &cmdline_arguments_arg, &platform_data_arg, NULL };
58 static const GDBusArgInfo *cmdline_out[] = { &cmdline_exit_status_arg, NULL };
59
60 static const GDBusMethodInfo activate_method = {
61   -1, (gchar *) "Activate",
62   (GDBusArgInfo **) activate_in,
63   (GDBusArgInfo **) activate_out
64 };
65
66 static const GDBusMethodInfo open_method = {
67   -1, (gchar *) "Open",
68   (GDBusArgInfo **) open_in,
69   (GDBusArgInfo **) open_out
70 };
71
72 static const GDBusMethodInfo command_line_method = {
73   -1, (gchar *) "CommandLine",
74   (GDBusArgInfo **) cmdline_in,
75   (GDBusArgInfo **) cmdline_out
76 };
77
78 static const GDBusMethodInfo *application_methods[] = {
79   &activate_method, &open_method, &command_line_method, NULL
80 };
81
82 const GDBusInterfaceInfo org_gtk_Application = {
83   -1, (gchar *) "org.gtk.Application",
84   (GDBusMethodInfo **) application_methods
85 };
86
87 static const GDBusArgInfo message_arg = { -1, (gchar *) "message", (gchar *) "s" };
88 static const GDBusArgInfo *print_in[] = { &message_arg, NULL };
89 static const GDBusArgInfo *print_out[] = { NULL };
90
91 static const GDBusMethodInfo stdout_method = {
92   -1, (gchar *) "Print",
93   (GDBusArgInfo **) print_in,
94   (GDBusArgInfo **) print_out
95 };
96
97 static const GDBusMethodInfo stderr_method = {
98   -1, (gchar *) "PrintError",
99   (GDBusArgInfo **) print_in,
100   (GDBusArgInfo **) print_out
101 };
102
103 static const GDBusMethodInfo *cmdline_methods[] = {
104   &stdout_method, &stderr_method, NULL
105 };
106
107 const GDBusInterfaceInfo org_gtk_private_Cmdline = {
108   -1, (gchar *) "org.gtk.private.CommandLine",
109   (GDBusMethodInfo **) cmdline_methods
110 };
111
112 /* GApplication implementation {{{1 */
113 struct _GApplicationImpl
114 {
115   GDBusConnection *session_bus;
116   const gchar     *bus_name;
117   gchar           *object_path;
118   guint            object_id;
119   gboolean         actions_exported;
120   gpointer         app;
121
122   GHashTable      *actions;
123   guint            signal_id;
124 };
125
126
127 static GApplicationCommandLine *
128 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
129
130
131 static void
132 g_application_impl_method_call (GDBusConnection       *connection,
133                                 const gchar           *sender,
134                                 const gchar           *object_path,
135                                 const gchar           *interface_name,
136                                 const gchar           *method_name,
137                                 GVariant              *parameters,
138                                 GDBusMethodInvocation *invocation,
139                                 gpointer               user_data)
140 {
141   GApplicationImpl *impl = user_data;
142   GApplicationClass *class;
143
144   class = G_APPLICATION_GET_CLASS (impl->app);
145
146   if (strcmp (method_name, "Activate") == 0)
147     {
148       GVariant *platform_data;
149
150       g_variant_get (parameters, "(@a{sv})", &platform_data);
151       class->before_emit (impl->app, platform_data);
152       g_signal_emit_by_name (impl->app, "activate");
153       class->after_emit (impl->app, platform_data);
154       g_variant_unref (platform_data);
155
156       g_dbus_method_invocation_return_value (invocation, NULL);
157     }
158
159   else if (strcmp (method_name, "Open") == 0)
160     {
161       GVariant *platform_data;
162       const gchar *hint;
163       GVariant *array;
164       GFile **files;
165       gint n, i;
166
167       g_variant_get (parameters, "(@ass@a{sv})",
168                      &array, &hint, &platform_data);
169
170       n = g_variant_n_children (array);
171       files = g_new (GFile *, n + 1);
172
173       for (i = 0; i < n; i++)
174         {
175           const gchar *uri;
176
177           g_variant_get_child (array, i, "&s", &uri);
178           files[i] = g_file_new_for_uri (uri);
179         }
180       g_variant_unref (array);
181       files[n] = NULL;
182
183       class->before_emit (impl->app, platform_data);
184       g_signal_emit_by_name (impl->app, "open", files, n, hint);
185       class->after_emit (impl->app, platform_data);
186
187       g_variant_unref (platform_data);
188
189       for (i = 0; i < n; i++)
190         g_object_unref (files[i]);
191       g_free (files);
192
193       g_dbus_method_invocation_return_value (invocation, NULL);
194     }
195
196   else if (strcmp (method_name, "CommandLine") == 0)
197     {
198       GApplicationCommandLine *cmdline;
199       GVariant *platform_data;
200       int status;
201
202       cmdline = g_dbus_command_line_new (invocation);
203       platform_data = g_variant_get_child_value (parameters, 2);
204       class->before_emit (impl->app, platform_data);
205       g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
206       g_application_command_line_set_exit_status (cmdline, status);
207       class->after_emit (impl->app, platform_data);
208       g_variant_unref (platform_data);
209       g_object_unref (cmdline);
210     }
211   else
212     g_assert_not_reached ();
213 }
214
215 static gchar *
216 application_path_from_appid (const gchar *appid)
217 {
218   gchar *appid_path, *iter;
219
220   appid_path = g_strconcat ("/", appid, NULL);
221   for (iter = appid_path; *iter; iter++)
222     {
223       if (*iter == '.')
224         *iter = '/';
225
226       if (*iter == '-')
227         *iter = '_';
228     }
229
230   return appid_path;
231 }
232
233 void
234 g_application_impl_destroy (GApplicationImpl *impl)
235 {
236   if (impl->session_bus)
237     {
238       if (impl->object_id)
239         g_dbus_connection_unregister_object (impl->session_bus,
240                                              impl->object_id);
241       if (impl->actions_exported)
242         g_action_group_exporter_stop (impl->app);
243
244       g_dbus_connection_call (impl->session_bus,
245                               "org.freedesktop.DBus",
246                               "/org/freedesktop/DBus",
247                               "org.freedesktop.DBus",
248                               "ReleaseName",
249                               g_variant_new ("(s)",
250                                              impl->bus_name),
251                               NULL,
252                               G_DBUS_CALL_FLAGS_NONE,
253                               -1, NULL, NULL, NULL);
254
255       g_object_unref (impl->session_bus);
256       g_free (impl->object_path);
257     }
258   else
259     {
260       g_assert (impl->object_path == NULL);
261       g_assert (impl->object_id == 0);
262     }
263
264   g_slice_free (GApplicationImpl, impl);
265 }
266
267 static void
268 unwrap_fake_maybe (GVariant **value)
269 {
270   GVariant *tmp;
271
272   if (g_variant_n_children (*value))
273     g_variant_get_child (*value, 0, "v", &tmp);
274   else
275     tmp = NULL;
276
277   g_variant_unref (*value);
278   *value = tmp;
279 }
280
281 static RemoteActionInfo *
282 remote_action_info_new_from_iter (GVariantIter *iter)
283 {
284   RemoteActionInfo *info;
285   GVariant *param_type;
286   gboolean enabled;
287   GVariant *state;
288   gchar *name;
289
290   if (!g_variant_iter_next (iter, "(s@avb@av)", &name,
291                             &param_type, &enabled, &state))
292     return NULL;
293
294   unwrap_fake_maybe (&param_type);
295   unwrap_fake_maybe (&state);
296
297   info = g_slice_new (RemoteActionInfo);
298   info->name = name;
299   info->enabled = enabled;
300   info->state = state;
301
302   if (param_type != NULL)
303     {
304       info->parameter_type = g_variant_type_copy (
305                                g_variant_type_element (
306                                  g_variant_get_type (param_type)));
307       g_variant_unref (param_type);
308     }
309   else
310     info->parameter_type = NULL;
311
312   return info;
313 }
314
315 static void
316 g_application_impl_action_signal (GDBusConnection *connection,
317                                   const gchar     *sender_name,
318                                   const gchar     *object_path,
319                                   const gchar     *interface_name,
320                                   const gchar     *signal_name,
321                                   GVariant        *parameters,
322                                   gpointer         user_data)
323 {
324   GApplicationImpl *impl = user_data;
325   GActionGroup *action_group;
326
327   action_group = G_ACTION_GROUP (impl->app);
328
329   if (strcmp (signal_name, "Added") == 0 &&
330       g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(a(savbav))")))
331     {
332       RemoteActionInfo *info;
333       GVariantIter *iter;
334
335       g_variant_get_child (parameters, 0, "a(savbav)", &iter);
336
337       while ((info = remote_action_info_new_from_iter (iter)))
338         {
339           g_hash_table_replace (impl->actions, info->name, info);
340           g_action_group_action_added (action_group, info->name);
341         }
342
343       g_variant_iter_free (iter);
344     }
345
346   else if (strcmp (signal_name, "Removed") == 0 &&
347            g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(as)")))
348     {
349       GVariantIter *iter;
350       const gchar *name;
351
352       g_variant_get_child (parameters, 0, "as", &iter);
353       while (g_variant_iter_next (iter, "&s", &name))
354         if (g_hash_table_remove (impl->actions, name))
355           g_action_group_action_removed (action_group, name);
356       g_variant_iter_free (iter);
357     }
358
359   else if (strcmp (signal_name, "EnabledChanged") == 0 &&
360            g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sb)")))
361     {
362       RemoteActionInfo *info;
363       const gchar *name;
364       gboolean enabled;
365
366       g_variant_get (parameters, "(&sb)", &name, &enabled);
367       info = g_hash_table_lookup (impl->actions, name);
368
369       if (info && enabled != info->enabled)
370         {
371           info->enabled = enabled;
372           g_action_group_action_enabled_changed (action_group,
373                                                  info->name,
374                                                  enabled);
375         }
376     }
377
378   else if (strcmp (signal_name, "StateChanged") == 0 &&
379            g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sv)")))
380     {
381       RemoteActionInfo *info;
382       const gchar *name;
383       GVariant *state;
384
385       g_variant_get (parameters, "(&sv)", &name, &state);
386       info = g_hash_table_lookup (impl->actions, name);
387
388       if (info && info->state &&
389           g_variant_is_of_type (state, g_variant_get_type (info->state)) &&
390           !g_variant_equal (state, info->state))
391         {
392           g_variant_unref (info->state);
393           info->state = g_variant_ref (state);
394           g_action_group_action_state_changed (action_group,
395                                                info->name,
396                                                state);
397         }
398       g_variant_unref (state);
399     }
400 }
401
402 GApplicationImpl *
403 g_application_impl_register (GApplication       *application,
404                              const gchar        *appid,
405                              GApplicationFlags   flags,
406                              GHashTable        **remote_actions,
407                              GCancellable       *cancellable,
408                              GError            **error)
409 {
410   const static GDBusInterfaceVTable vtable = {
411     g_application_impl_method_call
412   };
413   GApplicationImpl *impl;
414   GVariant *reply;
415   guint32 rval;
416
417   impl = g_slice_new0 (GApplicationImpl);
418
419   impl->app = application;
420   impl->bus_name = appid;
421
422   impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
423
424   if (impl->session_bus == NULL)
425     {
426       /* If we can't connect to the session bus, proceed as a normal
427        * non-unique application.
428        */
429       *remote_actions = NULL;
430       return impl;
431     }
432
433   impl->object_path = application_path_from_appid (appid);
434
435   /* Only try to be the primary instance if
436    * G_APPLICATION_IS_LAUNCHER was not specified.
437    */
438   if (~flags & G_APPLICATION_IS_LAUNCHER)
439     {
440       /* Attempt to become primary instance. */
441       impl->object_id =
442         g_dbus_connection_register_object (impl->session_bus,
443                                            impl->object_path,
444                                            (GDBusInterfaceInfo *)
445                                              &org_gtk_Application,
446                                            &vtable, impl, NULL, error);
447
448       if (impl->object_id == 0)
449         {
450           g_object_unref (impl->session_bus);
451           g_free (impl->object_path);
452           impl->session_bus = NULL;
453           impl->object_path = NULL;
454
455           g_slice_free (GApplicationImpl, impl);
456           return NULL;
457         }
458
459       if (!g_action_group_exporter_export (impl->session_bus,
460                                            impl->object_path,
461                                            impl->app, error))
462         {
463           g_dbus_connection_unregister_object (impl->session_bus,
464                                                impl->object_id);
465
466           g_object_unref (impl->session_bus);
467           g_free (impl->object_path);
468           impl->session_bus = NULL;
469           impl->object_path = NULL;
470
471           g_slice_free (GApplicationImpl, impl);
472           return NULL;
473         }
474       impl->actions_exported = TRUE;
475
476       /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
477       reply = g_dbus_connection_call_sync (impl->session_bus,
478                                            "org.freedesktop.DBus",
479                                            "/org/freedesktop/DBus",
480                                            "org.freedesktop.DBus",
481                                            "RequestName",
482                                            g_variant_new ("(su)",
483                                                           impl->bus_name,
484                                                           0x4),
485                                            G_VARIANT_TYPE ("(u)"),
486                                            0, -1, cancellable, error);
487
488       if (reply == NULL)
489         {
490           g_dbus_connection_unregister_object (impl->session_bus,
491                                                impl->object_id);
492           impl->object_id = 0;
493
494           g_action_group_exporter_stop (impl->app);
495           impl->actions_exported = FALSE;
496
497           g_object_unref (impl->session_bus);
498           g_free (impl->object_path);
499           impl->session_bus = NULL;
500           impl->object_path = NULL;
501
502           g_slice_free (GApplicationImpl, impl);
503           return NULL;
504         }
505
506       g_variant_get (reply, "(u)", &rval);
507       g_variant_unref (reply);
508
509       /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
510       if (rval != 3)
511         {
512           /* We are the primary instance. */
513           g_dbus_connection_emit_signal (impl->session_bus,
514                                          NULL,
515                                          impl->object_path,
516                                          "org.gtk.Application",
517                                          "Hello",
518                                          g_variant_new ("(s)",
519                                                         impl->bus_name),
520                                          NULL);
521           *remote_actions = NULL;
522           return impl;
523         }
524
525       /* We didn't make it.  Drop our service-side stuff. */
526       g_dbus_connection_unregister_object (impl->session_bus,
527                                            impl->object_id);
528       impl->object_id = 0;
529       g_action_group_exporter_stop (impl->app);
530       impl->actions_exported = FALSE;
531
532       if (flags & G_APPLICATION_IS_SERVICE)
533         {
534           g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
535                        "Unable to acquire bus name `%s'", appid);
536           g_object_unref (impl->session_bus);
537           g_free (impl->object_path);
538
539           g_slice_free (GApplicationImpl, impl);
540           return NULL;
541         }
542     }
543
544   /* We are non-primary.  Try to get the primary's list of actions.
545    * This also serves as a mechanism to ensure that the primary exists
546    * (ie: DBus service files installed correctly, etc).
547    */
548   impl->signal_id =
549     g_dbus_connection_signal_subscribe (impl->session_bus, impl->bus_name,
550                                         "org.gtk.Actions", NULL,
551                                         impl->object_path, NULL,
552                                         G_DBUS_SIGNAL_FLAGS_NONE,
553                                         g_application_impl_action_signal,
554                                         impl, NULL);
555
556   reply = g_dbus_connection_call_sync (impl->session_bus, impl->bus_name,
557                                        impl->object_path, "org.gtk.Actions",
558                                        "DescribeAll", NULL,
559                                        G_VARIANT_TYPE ("(a(savbav))"),
560                                        G_DBUS_CALL_FLAGS_NONE, -1,
561                                        cancellable, error);
562
563   if (reply == NULL)
564     {
565       /* The primary appears not to exist.  Fail the registration. */
566       g_object_unref (impl->session_bus);
567       g_free (impl->object_path);
568       impl->session_bus = NULL;
569       impl->object_path = NULL;
570
571       g_slice_free (GApplicationImpl, impl);
572       return NULL;
573     }
574
575   /* Create and populate the hashtable */
576   {
577     RemoteActionInfo *info;
578     GVariant *descriptions;
579     GVariantIter iter;
580
581     *remote_actions = g_hash_table_new (g_str_hash, g_str_equal);
582     descriptions = g_variant_get_child_value (reply, 0);
583     g_variant_iter_init (&iter, descriptions);
584
585     while ((info = remote_action_info_new_from_iter (&iter)))
586       g_hash_table_insert (*remote_actions, info->name, info);
587
588     g_variant_unref (descriptions);
589   }
590
591
592   return impl;
593 }
594
595 void
596 g_application_impl_activate (GApplicationImpl *impl,
597                              GVariant         *platform_data)
598 {
599   g_dbus_connection_call (impl->session_bus,
600                           impl->bus_name,
601                           impl->object_path,
602                           "org.gtk.Application",
603                           "Activate",
604                           g_variant_new ("(@a{sv})", platform_data),
605                           NULL, 0, -1, NULL, NULL, NULL);
606 }
607
608 void
609 g_application_impl_open (GApplicationImpl  *impl,
610                          GFile            **files,
611                          gint               n_files,
612                          const gchar       *hint,
613                          GVariant          *platform_data)
614 {
615   GVariantBuilder builder;
616   gint i;
617
618   g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
619   g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
620   for (i = 0; i < n_files; i++)
621     {
622       gchar *uri = g_file_get_uri (files[i]);
623       g_variant_builder_add (&builder, "s", uri);
624       g_free (uri);
625     }
626   g_variant_builder_close (&builder);
627   g_variant_builder_add (&builder, "s", hint);
628   g_variant_builder_add_value (&builder, platform_data);
629
630   g_dbus_connection_call (impl->session_bus,
631                           impl->bus_name,
632                           impl->object_path,
633                           "org.gtk.Application",
634                           "Open",
635                           g_variant_builder_end (&builder),
636                           NULL, 0, -1, NULL, NULL, NULL);
637 }
638
639 static void
640 g_application_impl_cmdline_method_call (GDBusConnection       *connection,
641                                         const gchar           *sender,
642                                         const gchar           *object_path,
643                                         const gchar           *interface_name,
644                                         const gchar           *method_name,
645                                         GVariant              *parameters,
646                                         GDBusMethodInvocation *invocation,
647                                         gpointer               user_data)
648 {
649   const gchar *message;
650
651   g_variant_get_child (parameters, 0, "&s", &message);
652
653   if (strcmp (method_name, "Print") == 0)
654     g_print ("%s", message);
655   else if (strcmp (method_name, "PrintError") == 0)
656     g_printerr ("%s", message);
657   else
658     g_assert_not_reached ();
659
660   g_dbus_method_invocation_return_value (invocation, NULL);
661 }
662
663 typedef struct
664 {
665   GMainLoop *loop;
666   int status;
667 } CommandLineData;
668
669 static void
670 g_application_impl_cmdline_done (GObject      *source,
671                                  GAsyncResult *result,
672                                  gpointer      user_data)
673 {
674   CommandLineData *data = user_data;
675   GError *error = NULL;
676   GVariant *reply;
677
678   reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
679                                          result, &error);
680
681   if (reply != NULL)
682     {
683       g_variant_get (reply, "(i)", &data->status);
684       g_variant_unref (reply);
685     }
686
687   else
688     {
689       g_printerr ("%s\n", error->message);
690       g_error_free (error);
691       data->status = 1;
692     }
693
694   g_main_loop_quit (data->loop);
695 }
696
697 int
698 g_application_impl_command_line (GApplicationImpl  *impl,
699                                  gchar            **arguments,
700                                  GVariant          *platform_data)
701 {
702   const static GDBusInterfaceVTable vtable = {
703     g_application_impl_cmdline_method_call
704   };
705   const gchar *object_path = "/org/gtk/Application/CommandLine";
706   GMainContext *context;
707   CommandLineData data;
708   guint object_id;
709
710   context = g_main_context_new ();
711   data.loop = g_main_loop_new (context, FALSE);
712   g_main_context_push_thread_default (context);
713
714   object_id = g_dbus_connection_register_object (impl->session_bus,
715                                                  object_path,
716                                                  (GDBusInterfaceInfo *)
717                                                    &org_gtk_private_Cmdline,
718                                                  &vtable, &data, NULL, NULL);
719   /* In theory we should try other paths... */
720   g_assert (object_id != 0);
721
722   g_dbus_connection_call (impl->session_bus,
723                           impl->bus_name,
724                           impl->object_path,
725                           "org.gtk.Application",
726                           "CommandLine",
727                           g_variant_new ("(o^aay@a{sv})", object_path,
728                                          arguments, platform_data),
729                           G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
730                           g_application_impl_cmdline_done, &data);
731
732   g_main_loop_run (data.loop);
733
734   g_main_context_pop_thread_default (context);
735   g_main_context_unref (context);
736   g_main_loop_unref (data.loop);
737
738   return data.status;
739 }
740
741 void
742 g_application_impl_change_action_state (GApplicationImpl *impl,
743                                         const gchar      *action_name,
744                                         GVariant         *value,
745                                         GVariant         *platform_data)
746 {
747   g_dbus_connection_call (impl->session_bus,
748                           impl->bus_name,
749                           impl->object_path,
750                           "org.gtk.Actions",
751                           "SetState",
752                           g_variant_new ("(sv@a{sv})", action_name,
753                                          value, platform_data),
754                           NULL, 0, -1, NULL, NULL, NULL);
755 }
756
757 void
758 g_application_impl_activate_action (GApplicationImpl *impl,
759                                     const gchar      *action_name,
760                                     GVariant         *parameter,
761                                     GVariant         *platform_data)
762 {
763   GVariant *param;
764
765   if (parameter)
766     parameter = g_variant_new_variant (parameter);
767
768   param = g_variant_new_array (G_VARIANT_TYPE_VARIANT,
769                                &parameter, parameter != NULL);
770
771   g_dbus_connection_call (impl->session_bus,
772                           impl->bus_name,
773                           impl->object_path,
774                           "org.gtk.Actions",
775                           "Activate",
776                           g_variant_new ("(s@av@a{sv})", action_name,
777                                          param, platform_data),
778                           NULL, 0, -1, NULL, NULL, NULL);
779 }
780
781 void
782 g_application_impl_flush (GApplicationImpl *impl)
783 {
784   if (impl->session_bus)
785     g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
786 }
787
788
789 /* GDBusCommandLine implementation {{{1 */
790
791 typedef GApplicationCommandLineClass GDBusCommandLineClass;
792 static GType g_dbus_command_line_get_type (void);
793 typedef struct
794 {
795   GApplicationCommandLine  parent_instance;
796   GDBusMethodInvocation   *invocation;
797
798   GDBusConnection *connection;
799   const gchar     *bus_name;
800   const gchar     *object_path;
801 } GDBusCommandLine;
802
803
804 G_DEFINE_TYPE (GDBusCommandLine,
805                g_dbus_command_line,
806                G_TYPE_APPLICATION_COMMAND_LINE)
807
808 static void
809 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
810                                    const gchar             *message)
811 {
812   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
813
814   g_dbus_connection_call (gdbcl->connection,
815                           gdbcl->bus_name,
816                           gdbcl->object_path,
817                           "org.gtk.private.CommandLine", "Print",
818                           g_variant_new ("(s)", message),
819                           NULL, 0, -1, NULL, NULL, NULL);
820 }
821
822 static void
823 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
824                                       const gchar             *message)
825 {
826   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
827
828   g_dbus_connection_call (gdbcl->connection,
829                           gdbcl->bus_name,
830                           gdbcl->object_path,
831                           "org.gtk.private.CommandLine", "PrintError",
832                           g_variant_new ("(s)", message),
833                           NULL, 0, -1, NULL, NULL, NULL);
834 }
835
836 static void
837 g_dbus_command_line_finalize (GObject *object)
838 {
839   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
840   GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
841   gint status;
842
843   status = g_application_command_line_get_exit_status (cmdline);
844
845   g_dbus_method_invocation_return_value (gdbcl->invocation,
846                                          g_variant_new ("(i)", status));
847   g_object_unref (gdbcl->invocation);
848
849   G_OBJECT_CLASS (g_dbus_command_line_parent_class)
850     ->finalize (object);
851 }
852
853 static void
854 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
855 {
856 }
857
858 static void
859 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
860 {
861   GObjectClass *object_class = G_OBJECT_CLASS (class);
862
863   object_class->finalize = g_dbus_command_line_finalize;
864   class->printerr_literal = g_dbus_command_line_printerr_literal;
865   class->print_literal = g_dbus_command_line_print_literal;
866 }
867
868 static GApplicationCommandLine *
869 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
870 {
871   GDBusCommandLine *gdbcl;
872   GVariant *args;
873
874   args = g_dbus_method_invocation_get_parameters (invocation);
875
876   gdbcl = g_object_new (g_dbus_command_line_get_type (),
877                         "arguments", g_variant_get_child_value (args, 1),
878                         "platform-data", g_variant_get_child_value (args, 2),
879                         NULL);
880   gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
881   gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
882   g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
883   gdbcl->invocation = g_object_ref (invocation);
884
885   return G_APPLICATION_COMMAND_LINE (gdbcl);
886 }
887
888 /* Epilogue {{{1 */
889
890 /* vim:set foldmethod=marker: */