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