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