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