GApplication: fix remote action states
[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
396   return appid_path;
397 }
398
399 void
400 g_application_impl_destroy (GApplicationImpl *impl)
401 {
402   if (impl->session_bus)
403     {
404       if (impl->object_id)
405         g_dbus_connection_unregister_object (impl->session_bus,
406                                              impl->object_id);
407
408       g_object_unref (impl->session_bus);
409       g_free (impl->object_path);
410     }
411   else
412     {
413       g_assert (impl->object_path == NULL);
414       g_assert (impl->object_id == 0);
415     }
416
417   g_slice_free (GApplicationImpl, impl);
418 }
419
420 static void
421 unwrap_fake_maybe (GVariant **value)
422 {
423   GVariant *tmp;
424
425   if (g_variant_n_children (*value))
426     g_variant_get_child (*value, 0, "v", &tmp);
427   else
428     tmp = NULL;
429
430   g_variant_unref (*value);
431   *value = tmp;
432 }
433
434 RemoteActionInfo *
435 remote_action_info_new_from_iter (GVariantIter *iter)
436 {
437   RemoteActionInfo *info;
438   GVariant *param_type;
439   gboolean enabled;
440   GVariant *state;
441   gchar *name;
442
443   if (!g_variant_iter_next (iter, "(s@avb@av)", &name,
444                             &param_type, &enabled, &state))
445     return NULL;
446
447   unwrap_fake_maybe (&param_type);
448   unwrap_fake_maybe (&state);
449
450   info = g_slice_new (RemoteActionInfo);
451   info->name = name;
452   info->enabled = enabled;
453   info->state = state;
454
455   if (param_type != NULL)
456     {
457       info->parameter_type = g_variant_type_copy (
458                                g_variant_type_element (
459                                  g_variant_get_type (param_type)));
460       g_variant_unref (param_type);
461     }
462   else
463     info->parameter_type = NULL;
464
465   return info;
466 }
467
468 static void
469 g_application_impl_action_signal (GDBusConnection *connection,
470                                   const gchar     *sender_name,
471                                   const gchar     *object_path,
472                                   const gchar     *interface_name,
473                                   const gchar     *signal_name,
474                                   GVariant        *parameters,
475                                   gpointer         user_data)
476 {
477   GApplicationImpl *impl = user_data;
478   GActionGroup *action_group;
479
480   action_group = G_ACTION_GROUP (impl->app);
481
482   if (strcmp (signal_name, "Added") == 0 &&
483       g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(a(savbav))")))
484     {
485       RemoteActionInfo *info;
486       GVariantIter *iter;
487
488       g_variant_get_child (parameters, 0, "a(savbav)", &iter);
489
490       while ((info = remote_action_info_new_from_iter (iter)))
491         {
492           g_hash_table_replace (impl->actions, info->name, info);
493           g_action_group_action_added (action_group, info->name);
494         }
495
496       g_variant_iter_free (iter);
497     }
498
499   else if (strcmp (signal_name, "Removed") == 0 &&
500            g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(as)")))
501     {
502       GVariantIter *iter;
503       const gchar *name;
504
505       g_variant_get_child (parameters, 0, "as", &iter);
506       while (g_variant_iter_next (iter, "&s", &name))
507         if (g_hash_table_remove (impl->actions, name))
508           g_action_group_action_removed (action_group, name);
509       g_variant_iter_free (iter);
510     }
511
512   else if (strcmp (signal_name, "EnabledChanged") == 0 &&
513            g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sb)")))
514     {
515       RemoteActionInfo *info;
516       const gchar *name;
517       gboolean enabled;
518
519       g_variant_get (parameters, "(&sb)", &name, &enabled);
520       info = g_hash_table_lookup (impl->actions, name);
521
522       if (info && enabled != info->enabled)
523         {
524           info->enabled = enabled;
525           g_action_group_action_enabled_changed (action_group,
526                                                  info->name,
527                                                  enabled);
528         }
529     }
530
531   else if (strcmp (signal_name, "StateChanged") == 0 &&
532            g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sv)")))
533     {
534       RemoteActionInfo *info;
535       const gchar *name;
536       GVariant *state;
537
538       g_variant_get (parameters, "(&sv)", &name, &state);
539       info = g_hash_table_lookup (impl->actions, name);
540       
541       if (info && info->state &&
542           g_variant_is_of_type (state, g_variant_get_type (info->state)) &&
543           !g_variant_equal (state, info->state))
544         {
545           g_variant_unref (info->state);
546           info->state = g_variant_ref (state);
547           g_action_group_action_state_changed (action_group,
548                                                info->name,
549                                                state);
550         }
551       g_variant_unref (state);
552     }
553 }
554
555 GApplicationImpl *
556 g_application_impl_register (GApplication       *application,
557                              const gchar        *appid,
558                              GApplicationFlags   flags,
559                              GHashTable        **remote_actions,
560                              GCancellable       *cancellable,
561                              GError            **error)
562 {
563   const static GDBusInterfaceVTable vtable = {
564     g_application_impl_method_call
565   };
566   const static GDBusInterfaceVTable actions_vtable = {
567     g_application_impl_actions_method_call
568   };
569   GApplicationImpl *impl;
570   GVariant *reply;
571   guint32 rval;
572
573   impl = g_slice_new (GApplicationImpl);
574
575   impl->app = application;
576   impl->bus_name = appid;
577
578   impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION,
579                                       cancellable, error);
580
581   if (impl->session_bus == NULL)
582     {
583       g_slice_free (GApplicationImpl, impl);
584       return NULL;
585     }
586
587   impl->object_path = application_path_from_appid (appid);
588
589   /* Only try to be the primary instance if
590    * G_APPLICATION_IS_LAUNCHER was not specified.
591    */
592   if (~flags & G_APPLICATION_IS_LAUNCHER)
593     {
594       /* Attempt to become primary instance. */
595       impl->object_id =
596         g_dbus_connection_register_object (impl->session_bus,
597                                            impl->object_path,
598                                            (GDBusInterfaceInfo *)
599                                              &org_gtk_Application,
600                                            &vtable, impl, NULL, error);
601
602       if (impl->object_id == 0)
603         {
604           g_object_unref (impl->session_bus);
605           g_free (impl->object_path);
606           impl->session_bus = NULL;
607           impl->object_path = NULL;
608
609           g_slice_free (GApplicationImpl, impl);
610           return NULL;
611         }
612
613       impl->action_id =
614         g_dbus_connection_register_object (impl->session_bus,
615                                            impl->object_path,
616                                            (GDBusInterfaceInfo *)
617                                              &org_gtk_Actions,
618                                            &actions_vtable,
619                                            impl, NULL, error);
620
621       if (impl->action_id == 0)
622         {
623           g_dbus_connection_unregister_object (impl->session_bus,
624                                                impl->object_id);
625
626           g_object_unref (impl->session_bus);
627           g_free (impl->object_path);
628           impl->session_bus = NULL;
629           impl->object_path = NULL;
630
631           g_slice_free (GApplicationImpl, impl);
632           return NULL;
633         }
634
635       /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
636       reply = g_dbus_connection_call_sync (impl->session_bus,
637                                            "org.freedesktop.DBus",
638                                            "/org/freedesktop/DBus",
639                                            "org.freedesktop.DBus",
640                                            "RequestName",
641                                            g_variant_new ("(su)",
642                                                           impl->bus_name,
643                                                           0x4),
644                                            G_VARIANT_TYPE ("(u)"),
645                                            0, -1, cancellable, error);
646
647       if (reply == NULL)
648         {
649           g_dbus_connection_unregister_object (impl->session_bus,
650                                                impl->object_id);
651           impl->object_id = 0;
652
653           g_object_unref (impl->session_bus);
654           g_free (impl->object_path);
655           impl->session_bus = NULL;
656           impl->object_path = NULL;
657
658           g_slice_free (GApplicationImpl, impl);
659           return NULL;
660         }
661
662       g_variant_get (reply, "(u)", &rval);
663       g_variant_unref (reply);
664
665       /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
666       if (rval != 3)
667         {
668           /* We are the primary instance. */
669           g_dbus_connection_emit_signal (impl->session_bus,
670                                          NULL,
671                                          impl->object_path,
672                                          "org.gtk.Application",
673                                          "Hello",
674                                          g_variant_new ("(s)",
675                                                         impl->bus_name),
676                                          NULL);
677           *remote_actions = NULL;
678           return impl;
679         }
680
681       /* We didn't make it.  Drop our service-side stuff. */
682       g_dbus_connection_unregister_object (impl->session_bus,
683                                            impl->object_id);
684       impl->object_id = 0;
685       g_dbus_connection_unregister_object (impl->session_bus,
686                                            impl->action_id);
687       impl->action_id = 0;
688
689       if (flags & G_APPLICATION_IS_SERVICE)
690         {
691           g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
692                        "Unable to acquire bus name `%s'", appid);
693           g_object_unref (impl->session_bus);
694           g_free (impl->object_path);
695
696           g_slice_free (GApplicationImpl, impl);
697           return NULL;
698         }
699     }
700
701   /* We are non-primary.  Try to get the primary's list of actions.
702    * This also serves as a mechanism to ensure that the primary exists
703    * (ie: DBus service files installed correctly, etc).
704    */
705   impl->signal_id =
706     g_dbus_connection_signal_subscribe (impl->session_bus, impl->bus_name,
707                                         "org.gtk.Actions", NULL,
708                                         impl->object_path, NULL,
709                                         G_DBUS_SIGNAL_FLAGS_NONE,
710                                         g_application_impl_action_signal,
711                                         impl, NULL);
712
713   reply = g_dbus_connection_call_sync (impl->session_bus, impl->bus_name,
714                                        impl->object_path, "org.gtk.Actions",
715                                        "DescribeAll", NULL,
716                                        G_VARIANT_TYPE ("(a(savbav))"),
717                                        G_DBUS_CALL_FLAGS_NONE, -1,
718                                        cancellable, error);
719
720   if (reply == NULL)
721     {
722       /* The primary appears not to exist.  Fail the registration. */
723       g_object_unref (impl->session_bus);
724       g_free (impl->object_path);
725       impl->session_bus = NULL;
726       impl->object_path = NULL;
727
728       g_slice_free (GApplicationImpl, impl);
729       return NULL;
730     }
731
732   /* Create and populate the hashtable */
733   {
734     RemoteActionInfo *info;
735     GVariant *descriptions;
736     GVariantIter iter;
737
738     *remote_actions = g_hash_table_new (g_str_hash, g_str_equal);
739     descriptions = g_variant_get_child_value (reply, 0);
740     g_variant_iter_init (&iter, descriptions);
741
742     while ((info = remote_action_info_new_from_iter (&iter)))
743       g_hash_table_insert (*remote_actions, info->name, info);
744
745     g_variant_unref (descriptions);
746   }
747
748
749   return impl;
750 }
751
752 void
753 g_application_impl_activate (GApplicationImpl *impl,
754                              GVariant         *platform_data)
755 {
756   g_dbus_connection_call (impl->session_bus,
757                           impl->bus_name,
758                           impl->object_path,
759                           "org.gtk.Application",
760                           "Activate",
761                           g_variant_new ("(@a{sv})", platform_data),
762                           NULL, 0, -1, NULL, NULL, NULL);
763 }
764
765 void
766 g_application_impl_open (GApplicationImpl  *impl,
767                          GFile            **files,
768                          gint               n_files,
769                          const gchar       *hint,
770                          GVariant          *platform_data)
771 {
772   GVariantBuilder builder;
773   gint i;
774
775   g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
776   g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
777   for (i = 0; i < n_files; i++)
778     {
779       gchar *uri = g_file_get_uri (files[i]);
780       g_variant_builder_add (&builder, "s", uri);
781       g_free (uri);
782     }
783   g_variant_builder_close (&builder);
784   g_variant_builder_add (&builder, "s", hint);
785   g_variant_builder_add_value (&builder, platform_data);
786
787   g_dbus_connection_call (impl->session_bus,
788                           impl->bus_name,
789                           impl->object_path,
790                           "org.gtk.Application",
791                           "Open",
792                           g_variant_builder_end (&builder),
793                           NULL, 0, -1, NULL, NULL, NULL);
794 }
795
796 static void
797 g_application_impl_cmdline_method_call (GDBusConnection       *connection,
798                                         const gchar           *sender,
799                                         const gchar           *object_path,
800                                         const gchar           *interface_name,
801                                         const gchar           *method_name,
802                                         GVariant              *parameters,
803                                         GDBusMethodInvocation *invocation,
804                                         gpointer               user_data)
805 {
806   const gchar *message;
807
808   g_variant_get_child (parameters, 0, "&s", &message);
809
810   if (strcmp (method_name, "Print") == 0)
811     g_print ("%s", message);
812   else if (strcmp (method_name, "PrintError") == 0)
813     g_printerr ("%s", message);
814   else
815     g_assert_not_reached ();
816
817   g_dbus_method_invocation_return_value (invocation, NULL);
818 }
819
820 typedef struct
821 {
822   GMainLoop *loop;
823   int status;
824 } CommandLineData;
825
826 static void
827 g_application_impl_cmdline_done (GObject      *source,
828                                  GAsyncResult *result,
829                                  gpointer      user_data)
830 {
831   CommandLineData *data = user_data;
832   GError *error = NULL;
833   GVariant *reply;
834
835   reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
836                                          result, &error);
837
838   if (reply != NULL)
839     {
840       g_variant_get (reply, "(i)", &data->status);
841       g_variant_unref (reply);
842     }
843
844   else
845     {
846       g_printerr ("%s\n", error->message);
847       g_error_free (error);
848       data->status = 1;
849     }
850
851   g_main_loop_quit (data->loop);
852 }
853
854 int
855 g_application_impl_command_line (GApplicationImpl  *impl,
856                                  gchar            **arguments,
857                                  GVariant          *platform_data)
858 {
859   const static GDBusInterfaceVTable vtable = {
860     g_application_impl_cmdline_method_call
861   };
862   const gchar *object_path = "/org/gtk/Application/CommandLine";
863   GMainContext *context;
864   CommandLineData data;
865   guint object_id;
866
867   context = g_main_context_new ();
868   data.loop = g_main_loop_new (context, FALSE);
869   g_main_context_push_thread_default (context);
870
871   object_id = g_dbus_connection_register_object (impl->session_bus,
872                                                  object_path,
873                                                  (GDBusInterfaceInfo *)
874                                                    &org_gtk_private_Cmdline,
875                                                  &vtable, &data, NULL, NULL);
876   /* In theory we should try other paths... */
877   g_assert (object_id != 0);
878
879   g_dbus_connection_call (impl->session_bus,
880                           impl->bus_name,
881                           impl->object_path,
882                           "org.gtk.Application",
883                           "CommandLine",
884                           g_variant_new ("(o^aay@a{sv})", object_path,
885                                          arguments, platform_data),
886                           G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
887                           g_application_impl_cmdline_done, &data);
888
889   g_main_loop_run (data.loop);
890
891   g_main_context_pop_thread_default (context);
892   g_main_context_unref (context);
893   g_main_loop_unref (data.loop);
894
895   return data.status;
896 }
897
898 void
899 g_application_impl_change_action_state (GApplicationImpl *impl,
900                                         const gchar      *action_name,
901                                         GVariant         *value,
902                                         GVariant         *platform_data)
903 {
904   g_dbus_connection_call (impl->session_bus,
905                           impl->bus_name,
906                           impl->object_path,
907                           "org.gtk.Actions",
908                           "SetState",
909                           g_variant_new ("(sv@a{sv})", action_name,
910                                          value, platform_data),
911                           NULL, 0, -1, NULL, NULL, NULL);
912 }
913
914 void
915 g_application_impl_activate_action (GApplicationImpl *impl,
916                                     const gchar      *action_name,
917                                     GVariant         *parameter,
918                                     GVariant         *platform_data)
919 {
920   GVariant *param;
921
922   if (parameter)
923     parameter = g_variant_new_variant (parameter);
924
925   param = g_variant_new_array (G_VARIANT_TYPE_VARIANT,
926                                &parameter, parameter != NULL);
927
928   g_dbus_connection_call (impl->session_bus,
929                           impl->bus_name,
930                           impl->object_path,
931                           "org.gtk.Actions",
932                           "Activate",
933                           g_variant_new ("(s@av@a{sv})", action_name,
934                                          param, platform_data),
935                           NULL, 0, -1, NULL, NULL, NULL);
936 }
937
938 void
939 g_application_impl_flush (GApplicationImpl *impl)
940 {
941   g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
942 }
943
944
945 /* GDBusCommandLine implementation {{{1 */
946
947 typedef GApplicationCommandLineClass GDBusCommandLineClass;
948 static GType g_dbus_command_line_get_type (void);
949 typedef struct
950 {
951   GApplicationCommandLine  parent_instance;
952   GDBusMethodInvocation   *invocation;
953
954   GDBusConnection *connection;
955   const gchar     *bus_name;
956   const gchar     *object_path;
957 } GDBusCommandLine;
958
959
960 G_DEFINE_TYPE (GDBusCommandLine,
961                g_dbus_command_line,
962                G_TYPE_APPLICATION_COMMAND_LINE)
963
964 static void
965 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
966                                    const gchar             *message)
967 {
968   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
969
970   g_dbus_connection_call (gdbcl->connection,
971                           gdbcl->bus_name,
972                           gdbcl->object_path,
973                           "org.gtk.private.CommandLine", "Print",
974                           g_variant_new ("(s)", message),
975                           NULL, 0, -1, NULL, NULL, NULL);
976 }
977
978 static void
979 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
980                                       const gchar             *message)
981 {
982   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
983
984   g_dbus_connection_call (gdbcl->connection,
985                           gdbcl->bus_name,
986                           gdbcl->object_path,
987                           "org.gtk.private.CommandLine", "PrintError",
988                           g_variant_new ("(s)", message),
989                           NULL, 0, -1, NULL, NULL, NULL);
990 }
991
992 static void
993 g_dbus_command_line_finalize (GObject *object)
994 {
995   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
996   GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
997   gint status;
998
999   status = g_application_command_line_get_exit_status (cmdline);
1000
1001   g_dbus_method_invocation_return_value (gdbcl->invocation,
1002                                          g_variant_new ("(i)", status));
1003   g_object_unref (gdbcl->invocation);
1004
1005   G_OBJECT_CLASS (g_dbus_command_line_parent_class)
1006     ->finalize (object);
1007 }
1008
1009 static void
1010 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
1011 {
1012 }
1013
1014 static void
1015 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
1016 {
1017   GObjectClass *object_class = G_OBJECT_CLASS (class);
1018
1019   object_class->finalize = g_dbus_command_line_finalize;
1020   class->printerr_literal = g_dbus_command_line_printerr_literal;
1021   class->print_literal = g_dbus_command_line_print_literal;
1022 }
1023
1024 static GApplicationCommandLine *
1025 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
1026 {
1027   GDBusCommandLine *gdbcl;
1028   GVariant *args;
1029
1030   args = g_dbus_method_invocation_get_parameters (invocation);
1031
1032   gdbcl = g_object_new (g_dbus_command_line_get_type (),
1033                         "arguments", g_variant_get_child_value (args, 1),
1034                         "platform-data", g_variant_get_child_value (args, 2),
1035                         NULL);
1036   gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
1037   gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
1038   g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
1039   gdbcl->invocation = g_object_ref (invocation);
1040
1041   return G_APPLICATION_COMMAND_LINE (gdbcl);
1042 }
1043
1044 /* Epilogue {{{1 */
1045
1046 /* vim:set foldmethod=marker: */