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