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