4e08d1002f91bdca38f5e19e092c2ab1b54576b4
[platform/upstream/glib.git] / gio / gdbusactiongroup.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  * Copyright © 2011 Canonical Limited
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation; either version 2 of the licence or (at
8  * your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Ryan Lortie <desrt@desrt.ca>
21  */
22
23 #include "config.h"
24
25 #include "gdbusactiongroup.h"
26
27 #include "gdbusconnection.h"
28 #include "gactiongroup.h"
29
30 /**
31  * SECTION:gdbusactiongroup
32  * @title: GDBusActionGroup
33  * @short_description: A D-Bus GActionGroup implementation
34  * @see_also: <link linkend="gio-GActionGroup-exporter">GActionGroup exporter</link>
35  *
36  * #GDBusActionGroup is an implementation of the #GActionGroup
37  * interface that can be used as a proxy for an action group
38  * that is exported over D-Bus with g_dbus_connection_export_action_group().
39  */
40
41 struct _GDBusActionGroup
42 {
43   GObject parent_instance;
44
45   GDBusConnection *connection;
46   gchar           *bus_name;
47   gchar           *object_path;
48   guint            subscription_id;
49   GHashTable      *actions;
50
51   /* The 'strict' flag indicates that the non-existence of at least one
52    * action has potentially been observed through the API.  This means
53    * that we should always emit 'action-added' signals for all new
54    * actions.
55    *
56    * The user can observe the non-existence of an action by listing the
57    * actions or by performing a query (such as parameter type) on a
58    * non-existent action.
59    *
60    * If the user has no way of knowing that a given action didn't
61    * already exist then we can skip emitting 'action-added' signals
62    * since they have no way of knowing that it wasn't there from the
63    * start.
64    */
65   gboolean         strict;
66 };
67
68 typedef GObjectClass GDBusActionGroupClass;
69
70 typedef struct
71 {
72   gchar        *name;
73   GVariantType *parameter_type;
74   gboolean      enabled;
75   GVariant     *state;
76 } ActionInfo;
77
78 static void
79 action_info_free (gpointer user_data)
80 {
81   ActionInfo *info = user_data;
82
83   g_free (info->name);
84
85   if (info->state)
86     g_variant_unref (info->state);
87
88   if (info->parameter_type)
89     g_variant_type_free (info->parameter_type);
90
91   g_slice_free (ActionInfo, info);
92 }
93
94 ActionInfo *
95 action_info_new_from_iter (GVariantIter *iter)
96 {
97   const gchar *param_str;
98   ActionInfo *info;
99   gboolean enabled;
100   GVariant *state;
101   gchar *name;
102
103   if (!g_variant_iter_next (iter, "{s(b&g@av)}", &name,
104                             &enabled, &param_str, &state))
105     return NULL;
106
107   info = g_slice_new (ActionInfo);
108   info->name = name;
109   info->enabled = enabled;
110
111   if (g_variant_n_children (state))
112     g_variant_get_child (state, 0, "v", &info->state);
113   else
114     info->state = NULL;
115   g_variant_unref (state);
116
117   if (param_str[0])
118     info->parameter_type = g_variant_type_copy ((GVariantType *) param_str);
119   else
120     info->parameter_type = NULL;
121
122   return info;
123 }
124
125 static void g_dbus_action_group_iface_init (GActionGroupInterface *);
126 G_DEFINE_TYPE_WITH_CODE (GDBusActionGroup, g_dbus_action_group, G_TYPE_OBJECT,
127   G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_dbus_action_group_iface_init))
128
129 static void
130 g_dbus_action_group_changed (GDBusConnection *connection,
131                              const gchar     *sender,
132                              const gchar     *object_path,
133                              const gchar     *interface_name,
134                              const gchar     *signal_name,
135                              GVariant        *parameters,
136                              gpointer         user_data)
137 {
138   GDBusActionGroup *group = user_data;
139   GActionGroup *g_group = user_data;
140
141   /* make sure that we've been fully initialised */
142   if (group->actions == NULL)
143     return;
144
145   if (g_str_equal (signal_name, "Changed") &&
146       g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(asa{sb}a{sv}a{s(bgav)})")))
147     {
148       /* Removes */
149       {
150         GVariantIter *iter;
151         const gchar *name;
152
153         g_variant_get_child (parameters, 0, "as", &iter);
154         while (g_variant_iter_next (iter, "&s", &name))
155           {
156             if (g_hash_table_lookup (group->actions, name))
157               {
158                 g_hash_table_remove (group->actions, name);
159                 g_action_group_action_removed (g_group, name);
160               }
161           }
162         g_variant_iter_free (iter);
163       }
164
165       /* Enable changes */
166       {
167         GVariantIter *iter;
168         const gchar *name;
169         gboolean enabled;
170
171         g_variant_get_child (parameters, 1, "a{sb}", &iter);
172         while (g_variant_iter_next (iter, "{&sb}", &name, &enabled))
173           {
174             ActionInfo *info;
175
176             info = g_hash_table_lookup (group->actions, name);
177
178             if (info && info->enabled != enabled)
179               {
180                 info->enabled = enabled;
181                 g_action_group_action_enabled_changed (g_group, name, enabled);
182               }
183           }
184         g_variant_iter_free (iter);
185       }
186
187       /* State changes */
188       {
189         GVariantIter *iter;
190         const gchar *name;
191         GVariant *state;
192
193         g_variant_get_child (parameters, 2, "a{sv}", &iter);
194         while (g_variant_iter_next (iter, "{&sv}", &name, &state))
195           {
196             ActionInfo *info;
197
198             info = g_hash_table_lookup (group->actions, name);
199
200             if (info && info->state && !g_variant_equal (state, info->state) &&
201                 g_variant_is_of_type (state, g_variant_get_type (info->state)))
202               {
203                 g_variant_unref (info->state);
204                 info->state = g_variant_ref (state);
205
206                 g_action_group_action_state_changed (g_group, name, state);
207               }
208
209             g_variant_unref (state);
210           }
211         g_variant_iter_free (iter);
212       }
213
214       /* Additions */
215       {
216         GVariantIter *iter;
217         ActionInfo *info;
218
219         g_variant_get_child (parameters, 3, "a{s(bgav)}", &iter);
220         while ((info = action_info_new_from_iter (iter)))
221           {
222             if (!g_hash_table_lookup (group->actions, info->name))
223               {
224                 g_hash_table_insert (group->actions, info->name, info);
225
226                 if (group->strict)
227                   g_action_group_action_added (g_group, info->name);
228               }
229             else
230               action_info_free (info);
231           }
232         g_variant_iter_free (iter);
233       }
234     }
235 }
236
237
238 static void
239 g_dbus_action_group_describe_all_done (GObject      *source,
240                                        GAsyncResult *result,
241                                        gpointer      user_data)
242 {
243   GDBusActionGroup *group= user_data;
244   GVariant *reply;
245
246   g_assert (group->actions == NULL);
247   group->actions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, action_info_free);
248
249   g_assert (group->connection == (gpointer) source);
250   reply = g_dbus_connection_call_finish (group->connection, result, NULL);
251
252   if (reply != NULL)
253     {
254       GVariantIter *iter;
255       ActionInfo *action;
256
257       g_variant_get (reply, "(a{s(bgav)})", &iter);
258       while ((action = action_info_new_from_iter (iter)))
259         {
260           g_hash_table_insert (group->actions, action->name, action);
261
262           if (group->strict)
263             g_action_group_action_added (G_ACTION_GROUP (group), action->name);
264         }
265       g_variant_iter_free (iter);
266       g_variant_unref (reply);
267     }
268 }
269
270
271 static void
272 g_dbus_action_group_async_init (GDBusActionGroup *group)
273 {
274   if (group->subscription_id != 0)
275     return;
276
277   group->subscription_id =
278     g_dbus_connection_signal_subscribe (group->connection, group->bus_name, "org.gtk.Actions", "Changed", group->object_path,
279                                         NULL, G_DBUS_SIGNAL_FLAGS_NONE, g_dbus_action_group_changed, group, NULL);
280
281   g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "DescribeAll", NULL,
282                           G_VARIANT_TYPE ("(a{s(bgav)})"), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
283                           g_dbus_action_group_describe_all_done, group);
284 }
285
286 static gchar **
287 g_dbus_action_group_list_actions (GActionGroup *g_group)
288 {
289   GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
290   gchar **keys;
291
292   if (group->actions != NULL)
293     {
294       GHashTableIter iter;
295       gint n, i = 0;
296       gpointer key;
297
298       n = g_hash_table_size (group->actions);
299       keys = g_new (gchar *, n + 1);
300
301       g_hash_table_iter_init (&iter, group->actions);
302       while (g_hash_table_iter_next (&iter, &key, NULL))
303         keys[i++] = g_strdup (key);
304       g_assert_cmpint (i, ==, n);
305       keys[n] = NULL;
306     }
307   else
308     {
309       g_dbus_action_group_async_init (group);
310       keys = g_new0 (gchar *, 1);
311     }
312
313   group->strict = TRUE;
314
315   return keys;
316 }
317
318 static gboolean
319 g_dbus_action_group_query_action (GActionGroup        *g_group,
320                                   const gchar         *action_name,
321                                   gboolean            *enabled,
322                                   const GVariantType **parameter_type,
323                                   const GVariantType **state_type,
324                                   GVariant           **state_hint,
325                                   GVariant           **state)
326 {
327   GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
328   ActionInfo *info;
329
330   if (group->actions != NULL)
331     {
332       info = g_hash_table_lookup (group->actions, action_name);
333
334       if (info == NULL)
335         {
336           group->strict = TRUE;
337           return FALSE;
338         }
339
340       if (enabled)
341         *enabled = info->enabled;
342
343       if (parameter_type)
344         *parameter_type = info->parameter_type;
345
346       if (state_type)
347         *state_type = info->state ? g_variant_get_type (info->state) : NULL;
348
349       if (state_hint)
350         *state_hint = NULL;
351
352       if (state)
353         *state = info->state ? g_variant_ref (info->state) : NULL;
354
355       return TRUE;
356     }
357   else
358     {
359       g_dbus_action_group_async_init (group);
360
361       return FALSE;
362     }
363 }
364
365 static void
366 g_dbus_action_group_change_state (GActionGroup *g_group,
367                                   const gchar  *action_name,
368                                   GVariant     *value)
369 {
370   GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
371
372   g_dbus_action_group_change_action_state_full (group, action_name, value, g_variant_new ("a{sv}", NULL));
373 }
374
375 static void
376 g_dbus_action_group_activate (GActionGroup *g_group,
377                               const gchar  *action_name,
378                               GVariant     *parameter)
379 {
380   GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
381
382   g_dbus_action_group_activate_action_full (group, action_name, parameter, g_variant_new ("a{sv}", NULL));
383 }
384
385 static void
386 g_dbus_action_group_finalize (GObject *object)
387 {
388   GDBusActionGroup *group = G_DBUS_ACTION_GROUP (object);
389
390   if (group->subscription_id)
391     g_dbus_connection_signal_unsubscribe (group->connection, group->subscription_id);
392
393   if (group->actions)
394     g_hash_table_unref (group->actions);
395
396   g_object_unref (group->connection);
397   g_free (group->object_path);
398   g_free (group->bus_name);
399
400   G_OBJECT_CLASS (g_dbus_action_group_parent_class)
401     ->finalize (object);
402 }
403
404 static void
405 g_dbus_action_group_init (GDBusActionGroup *group)
406 {
407 }
408
409 static void
410 g_dbus_action_group_class_init (GDBusActionGroupClass *class)
411 {
412   GObjectClass *object_class = G_OBJECT_CLASS (class);
413
414   object_class->finalize = g_dbus_action_group_finalize;
415 }
416
417 static void
418 g_dbus_action_group_iface_init (GActionGroupInterface *iface)
419 {
420   iface->list_actions = g_dbus_action_group_list_actions;
421   iface->query_action = g_dbus_action_group_query_action;
422   iface->change_action_state = g_dbus_action_group_change_state;
423   iface->activate_action = g_dbus_action_group_activate;
424 }
425
426 /**
427  * g_dbus_action_group_get:
428  * @connection: A #GDBusConnection
429  * @bus_name: the bus name which exports the action group
430  * @object_path: the object path at which the action group is exported
431  *
432  * Obtains a #GDBusActionGroup for the action group which is exported at
433  * the given @bus_name and @object_path.
434  *
435  * The thread default main context is taken at the time of this call.
436  * All signals on the menu model (and any linked models) are reported
437  * with respect to this context.  All calls on the returned menu model
438  * (and linked models) must also originate from this same context, with
439  * the thread default main context unchanged.
440  *
441  * This call is non-blocking.  The returned action group may or may not
442  * already be filled in.  The correct thing to do is connect the signals
443  * for the action group to monitor for changes and then to call
444  * g_action_group_list_actions() to get the initial list.
445  *
446  * Returns: (transfer full): a #GDBusActionGroup
447  *
448  * Since: 2.32
449  */
450 GDBusActionGroup *
451 g_dbus_action_group_get (GDBusConnection *connection,
452                          const gchar     *bus_name,
453                          const gchar     *object_path)
454 {
455   GDBusActionGroup *group;
456
457   group = g_object_new (G_TYPE_DBUS_ACTION_GROUP, NULL);
458   group->connection = g_object_ref (connection);
459   group->bus_name = g_strdup (bus_name);
460   group->object_path = g_strdup (object_path);
461
462   return group;
463 }
464
465 G_GNUC_INTERNAL gboolean
466 g_dbus_action_group_sync (GDBusActionGroup  *group,
467                           GCancellable      *cancellable,
468                           GError           **error)
469 {
470   GVariant *reply;
471
472   g_assert (group->subscription_id == 0);
473
474   group->subscription_id =
475     g_dbus_connection_signal_subscribe (group->connection, group->bus_name, "org.gtk.Actions", "Changed", group->object_path,
476                                         NULL, G_DBUS_SIGNAL_FLAGS_NONE, g_dbus_action_group_changed, group, NULL);
477
478   reply = g_dbus_connection_call_sync (group->connection, group->bus_name, group->object_path, "org.gtk.Actions",
479                                        "DescribeAll", NULL, G_VARIANT_TYPE ("(a{s(bgav)})"),
480                                        G_DBUS_CALL_FLAGS_NONE, -1, cancellable, error);
481
482   if (reply != NULL)
483     {
484       GVariantIter *iter;
485       ActionInfo *action;
486
487       g_assert (group->actions == NULL);
488       group->actions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, action_info_free);
489
490       g_variant_get (reply, "(a{s(bgav)})", &iter);
491       while ((action = action_info_new_from_iter (iter)))
492         g_hash_table_insert (group->actions, action->name, action);
493       g_variant_iter_free (iter);
494       g_variant_unref (reply);
495     }
496
497   return reply != NULL;
498 }
499
500 /**
501  * g_dbus_action_group_activate_action_full:
502  * @action_group: a #GDBusActionGroup
503  * @action_name: the name of the action to activate
504  * @parameter: (allow none): the optional parameter to the activation
505  * @platform_data: the platform data to send
506  *
507  * Activates the remote action.
508  *
509  * This is the same as g_action_group_activate_action() except that it
510  * allows for provision of "platform data" to be sent along with the
511  * activation request.  This typically contains details such as the user
512  * interaction timestamp or startup notification information.
513  *
514  * @platform_data must be non-%NULL and must have the type
515  * %G_VARIANT_TYPE_VARDICT.  If it is floating, it will be consumed.
516  *
517  * Since: 2.32
518  **/
519 void
520 g_dbus_action_group_activate_action_full (GDBusActionGroup *action_group,
521                                           const gchar      *action_name,
522                                           GVariant         *parameter,
523                                           GVariant         *platform_data)
524 {
525   GVariantBuilder builder;
526
527   g_return_if_fail (G_IS_DBUS_ACTION_GROUP (action_group));
528   g_return_if_fail (action_name != NULL);
529   g_return_if_fail (platform_data != NULL);
530
531   g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
532
533   if (parameter)
534     g_variant_builder_add (&builder, "v", parameter);
535
536   g_dbus_connection_call (action_group->connection, action_group->bus_name, action_group->object_path,
537                           "org.gtk.Actions", "Activate",
538                           g_variant_new ("(sav@a{sv})", action_name, &builder, platform_data),
539                           NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
540 }
541
542 /**
543  * g_dbus_action_group_activate_action_full:
544  * @action_group: a #GDBusActionGroup
545  * @action_name: the name of the action to change the state of
546  * @value: the new requested value for the state
547  * @platform_data: the platform data to send
548  *
549  * Changes the state of a remote action.
550  *
551  * This is the same as g_action_group_change_action_state() except that
552  * it allows for provision of "platform data" to be sent along with the
553  * state change request.  This typically contains details such as the
554  * user interaction timestamp or startup notification information.
555  *
556  * @platform_data must be non-%NULL and must have the type
557  * %G_VARIANT_TYPE_VARDICT.  If it is floating, it will be consumed.
558  *
559  * Since: 2.32
560  **/
561 void
562 g_dbus_action_group_change_action_state_full (GDBusActionGroup *action_group,
563                                               const gchar      *action_name,
564                                               GVariant         *value,
565                                               GVariant         *platform_data)
566 {
567   g_return_if_fail (G_IS_DBUS_ACTION_GROUP (action_group));
568   g_return_if_fail (action_name != NULL);
569   g_return_if_fail (value != NULL);
570   g_return_if_fail (platform_data != NULL);
571
572   g_dbus_connection_call (action_group->connection, action_group->bus_name, action_group->object_path,
573                           "org.gtk.Actions", "SetState",
574                           g_variant_new ("(sv@a{sv})", action_name, value, platform_data),
575                           NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
576 }