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