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