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