gio/tests/file: skip the file monitor tests if using GPollFileMonitor
[platform/upstream/glib.git] / gio / gactiongroupexporter.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 "gactiongroupexporter.h"
26
27 #include "gdbusmethodinvocation.h"
28 #include "gremoteactiongroup.h"
29 #include "gdbusintrospection.h"
30 #include "gdbusconnection.h"
31 #include "gactiongroup.h"
32 #include "gdbuserror.h"
33
34 /**
35  * SECTION:gactiongroupexporter
36  * @title: GActionGroup exporter
37  * @short_description: Export GActionGroups on D-Bus
38  * @see_also: #GActionGroup, #GDBusActionGroup
39  *
40  * These functions support exporting a #GActionGroup on D-Bus.
41  * The D-Bus interface that is used is a private implementation
42  * detail.
43  *
44  * To access an exported #GActionGroup remotely, use
45  * g_dbus_action_group_get() to obtain a #GDBusActionGroup.
46  */
47
48 G_GNUC_INTERNAL GVariant *
49 g_action_group_describe_action (GActionGroup *action_group,
50                                 const gchar  *name)
51 {
52   const GVariantType *type;
53   GVariantBuilder builder;
54   gboolean enabled;
55   GVariant *state;
56
57   g_variant_builder_init (&builder, G_VARIANT_TYPE ("(bgav)"));
58
59   enabled = g_action_group_get_action_enabled (action_group, name);
60   g_variant_builder_add (&builder, "b", enabled);
61
62   if ((type = g_action_group_get_action_parameter_type (action_group, name)))
63     {
64       gchar *str = g_variant_type_dup_string (type);
65       g_variant_builder_add (&builder, "g", str);
66       g_free (str);
67     }
68   else
69     g_variant_builder_add (&builder, "g", "");
70
71   g_variant_builder_open (&builder, G_VARIANT_TYPE ("av"));
72   if ((state = g_action_group_get_action_state (action_group, name)))
73     {
74       g_variant_builder_add (&builder, "v", state);
75       g_variant_unref (state);
76     }
77   g_variant_builder_close (&builder);
78
79   return g_variant_builder_end (&builder);
80 }
81
82 /* Using XML saves us dozens of relocations vs. using the introspection
83  * structure types.  We only need to burn cycles and memory if we
84  * actually use the exporter -- not in every single app using GIO.
85  *
86  * It's also a lot easier to read. :)
87  *
88  * For documentation of this interface, see
89  * http://live.gnome.org/GTK+/GApplication-dbus-apis
90  */
91 const char org_gtk_Actions_xml[] =
92   "<node>"
93   "  <interface name='org.gtk.Actions'>"
94   "    <method name='List'>"
95   "      <arg type='as' name='list' direction='out'/>"
96   "    </method>"
97   "    <method name='Describe'>"
98   "      <arg type='s' name='action_name' direction='in'/>"
99   "      <arg type='(bgav)' name='description' direction='out'/>"
100   "    </method>"
101   "    <method name='DescribeAll'>"
102   "      <arg type='a{s(bgav)}' name='descriptions' direction='out'/>"
103   "    </method>"
104   "    <method name='Activate'>"
105   "      <arg type='s' name='action_name' direction='in'/>"
106   "      <arg type='av' name='parameter' direction='in'/>"
107   "      <arg type='a{sv}' name='platform_data' direction='in'/>"
108   "    </method>"
109   "    <method name='SetState'>"
110   "      <arg type='s' name='action_name' direction='in'/>"
111   "      <arg type='v' name='value' direction='in'/>"
112   "      <arg type='a{sv}' name='platform_data' direction='in'/>"
113   "    </method>"
114   "    <signal name='Changed'>"
115   "      <arg type='as' name='removals'/>"
116   "      <arg type='a{sb}' name='enable_changes'/>"
117   "      <arg type='a{sv}' name='state_changes'/>"
118   "      <arg type='a{s(bgav)}' name='additions'/>"
119   "    </signal>"
120   "  </interface>"
121   "</node>";
122
123 static GDBusInterfaceInfo *org_gtk_Actions;
124
125 typedef struct
126 {
127   GActionGroup    *action_group;
128   GDBusConnection *connection;
129   GMainContext    *context;
130   gchar           *object_path;
131   GHashTable      *pending_changes;
132   GSource         *pending_source;
133 } GActionGroupExporter;
134
135 #define ACTION_ADDED_EVENT             (1u<<0)
136 #define ACTION_REMOVED_EVENT           (1u<<1)
137 #define ACTION_STATE_CHANGED_EVENT     (1u<<2)
138 #define ACTION_ENABLED_CHANGED_EVENT   (1u<<3)
139
140 static gboolean
141 g_action_group_exporter_dispatch_events (gpointer user_data)
142 {
143   GActionGroupExporter *exporter = user_data;
144   GVariantBuilder removes;
145   GVariantBuilder enabled_changes;
146   GVariantBuilder state_changes;
147   GVariantBuilder adds;
148   GHashTableIter iter;
149   gpointer value;
150   gpointer key;
151
152   g_variant_builder_init (&removes, G_VARIANT_TYPE_STRING_ARRAY);
153   g_variant_builder_init (&enabled_changes, G_VARIANT_TYPE ("a{sb}"));
154   g_variant_builder_init (&state_changes, G_VARIANT_TYPE ("a{sv}"));
155   g_variant_builder_init (&adds, G_VARIANT_TYPE ("a{s(bgav)}"));
156
157   g_hash_table_iter_init (&iter, exporter->pending_changes);
158   while (g_hash_table_iter_next (&iter, &key, &value))
159     {
160       guint events = GPOINTER_TO_INT (value);
161       const gchar *name = key;
162
163       /* Adds and removes are incompatible with enabled or state
164        * changes, but we must report at least one event.
165        */
166       g_assert (((events & (ACTION_ENABLED_CHANGED_EVENT | ACTION_STATE_CHANGED_EVENT)) == 0) !=
167                 ((events & (ACTION_REMOVED_EVENT | ACTION_ADDED_EVENT)) == 0));
168
169       if (events & ACTION_REMOVED_EVENT)
170         g_variant_builder_add (&removes, "s", name);
171
172       if (events & ACTION_ENABLED_CHANGED_EVENT)
173         {
174           gboolean enabled;
175
176           enabled = g_action_group_get_action_enabled (exporter->action_group, name);
177           g_variant_builder_add (&enabled_changes, "{sb}", name, enabled);
178         }
179
180       if (events & ACTION_STATE_CHANGED_EVENT)
181         {
182           GVariant *state;
183
184           state = g_action_group_get_action_state (exporter->action_group, name);
185           g_variant_builder_add (&state_changes, "{sv}", name, state);
186           g_variant_unref (state);
187         }
188
189       if (events & ACTION_ADDED_EVENT)
190         {
191           GVariant *description;
192
193           description = g_action_group_describe_action (exporter->action_group, name);
194           g_variant_builder_add (&adds, "{s@(bgav)}", name, description);
195         }
196     }
197
198   g_hash_table_remove_all (exporter->pending_changes);
199
200   g_dbus_connection_emit_signal (exporter->connection, NULL, exporter->object_path,
201                                  "org.gtk.Actions", "Changed",
202                                  g_variant_new ("(asa{sb}a{sv}a{s(bgav)})",
203                                                 &removes, &enabled_changes,
204                                                 &state_changes, &adds),
205                                  NULL);
206
207   exporter->pending_source = NULL;
208
209   return FALSE;
210 }
211
212 static guint
213 g_action_group_exporter_get_events (GActionGroupExporter *exporter,
214                                     const gchar          *name)
215 {
216   return (gsize) g_hash_table_lookup (exporter->pending_changes, name);
217 }
218
219 static void
220 g_action_group_exporter_set_events (GActionGroupExporter *exporter,
221                                     const gchar          *name,
222                                     guint                 events)
223 {
224   gboolean have_events;
225   gboolean is_queued;
226
227   if (events != 0)
228     g_hash_table_insert (exporter->pending_changes, g_strdup (name), GINT_TO_POINTER (events));
229   else
230     g_hash_table_remove (exporter->pending_changes, name);
231
232   have_events = g_hash_table_size (exporter->pending_changes) > 0;
233   is_queued = exporter->pending_source != NULL;
234
235   if (have_events && !is_queued)
236     {
237       GSource *source;
238
239       source = g_idle_source_new ();
240       exporter->pending_source = source;
241       g_source_set_callback (source, g_action_group_exporter_dispatch_events, exporter, NULL);
242       g_source_attach (source, exporter->context);
243       g_source_unref (source);
244     }
245
246   if (!have_events && is_queued)
247     {
248       g_source_destroy (exporter->pending_source);
249       exporter->pending_source = NULL;
250     }
251 }
252
253 static void
254 g_action_group_exporter_action_added (GActionGroup *action_group,
255                                       const gchar  *action_name,
256                                       gpointer      user_data)
257 {
258   GActionGroupExporter *exporter = user_data;
259   guint event_mask;
260
261   event_mask = g_action_group_exporter_get_events (exporter, action_name);
262
263   /* The action is new, so we should not have any pending
264    * enabled-changed or state-changed signals for it.
265    */
266   g_assert (~event_mask & (ACTION_STATE_CHANGED_EVENT |
267                            ACTION_ENABLED_CHANGED_EVENT));
268
269   event_mask |= ACTION_ADDED_EVENT;
270
271   g_action_group_exporter_set_events (exporter, action_name, event_mask);
272 }
273
274 static void
275 g_action_group_exporter_action_removed (GActionGroup *action_group,
276                                         const gchar  *action_name,
277                                         gpointer      user_data)
278 {
279   GActionGroupExporter *exporter = user_data;
280   guint event_mask;
281
282   event_mask = g_action_group_exporter_get_events (exporter, action_name);
283
284   /* If the add event for this is still queued then just cancel it since
285    * it's gone now.
286    *
287    * If the event was freshly added, there should not have been any
288    * enabled or state changed events.
289    */
290   if (event_mask & ACTION_ADDED_EVENT)
291     {
292       g_assert (~event_mask & ~(ACTION_STATE_CHANGED_EVENT | ACTION_ENABLED_CHANGED_EVENT));
293       event_mask &= ~ACTION_ADDED_EVENT;
294     }
295
296   /* Otherwise, queue a remove event.  Drop any state or enabled changes
297    * that were queued before the remove. */
298   else
299     {
300       event_mask |= ACTION_REMOVED_EVENT;
301       event_mask &= ~(ACTION_STATE_CHANGED_EVENT | ACTION_ENABLED_CHANGED_EVENT);
302     }
303
304   g_action_group_exporter_set_events (exporter, action_name, event_mask);
305 }
306
307 static void
308 g_action_group_exporter_action_state_changed (GActionGroup *action_group,
309                                               const gchar  *action_name,
310                                               GVariant     *value,
311                                               gpointer      user_data)
312 {
313   GActionGroupExporter *exporter = user_data;
314   guint event_mask;
315
316   event_mask = g_action_group_exporter_get_events (exporter, action_name);
317
318   /* If it was removed, it must have been added back.  Otherwise, why
319    * are we hearing about changes?
320    */
321   g_assert (~event_mask & ACTION_REMOVED_EVENT ||
322             event_mask & ACTION_ADDED_EVENT);
323
324   /* If it is freshly added, don't also bother with the state change
325    * signal since the updated state will be sent as part of the pending
326    * add message.
327    */
328   if (~event_mask & ACTION_ADDED_EVENT)
329     event_mask |= ACTION_STATE_CHANGED_EVENT;
330
331   g_action_group_exporter_set_events (exporter, action_name, event_mask);
332 }
333
334 static void
335 g_action_group_exporter_action_enabled_changed (GActionGroup *action_group,
336                                                 const gchar  *action_name,
337                                                 gboolean      enabled,
338                                                 gpointer      user_data)
339 {
340   GActionGroupExporter *exporter = user_data;
341   guint event_mask;
342
343   event_mask = g_action_group_exporter_get_events (exporter, action_name);
344
345   /* Reasoning as above. */
346   g_assert (~event_mask & ACTION_REMOVED_EVENT ||
347             event_mask & ACTION_ADDED_EVENT);
348
349   if (~event_mask & ACTION_ADDED_EVENT)
350     event_mask |= ACTION_ENABLED_CHANGED_EVENT;
351
352   g_action_group_exporter_set_events (exporter, action_name, event_mask);
353 }
354
355 static void
356 org_gtk_Actions_method_call (GDBusConnection       *connection,
357                              const gchar           *sender,
358                              const gchar           *object_path,
359                              const gchar           *interface_name,
360                              const gchar           *method_name,
361                              GVariant              *parameters,
362                              GDBusMethodInvocation *invocation,
363                              gpointer               user_data)
364 {
365   GActionGroupExporter *exporter = user_data;
366   GVariant *result = NULL;
367
368   if (g_str_equal (method_name, "List"))
369     {
370       gchar **list;
371
372       list = g_action_group_list_actions (exporter->action_group);
373       result = g_variant_new ("(^as)", list);
374       g_strfreev (list);
375     }
376
377   else if (g_str_equal (method_name, "Describe"))
378     {
379       const gchar *name;
380       GVariant *desc;
381
382       g_variant_get (parameters, "(&s)", &name);
383       desc = g_action_group_describe_action (exporter->action_group, name);
384       result = g_variant_new ("(@(bgav))", desc);
385     }
386
387   else if (g_str_equal (method_name, "DescribeAll"))
388     {
389       GVariantBuilder builder;
390       gchar **list;
391       gint i;
392
393       list = g_action_group_list_actions (exporter->action_group);
394       g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{s(bgav)}"));
395       for (i = 0; list[i]; i++)
396         {
397           const gchar *name = list[i];
398           GVariant *description;
399
400           description = g_action_group_describe_action (exporter->action_group, name);
401           g_variant_builder_add (&builder, "{s@(bgav)}", name, description);
402         }
403       result = g_variant_new ("(a{s(bgav)})", &builder);
404       g_strfreev (list);
405     }
406
407   else if (g_str_equal (method_name, "Activate"))
408     {
409       GVariant *parameter = NULL;
410       GVariant *platform_data;
411       GVariantIter *iter;
412       const gchar *name;
413
414       g_variant_get (parameters, "(&sav@a{sv})", &name, &iter, &platform_data);
415       g_variant_iter_next (iter, "v", &parameter);
416       g_variant_iter_free (iter);
417
418       if (G_IS_REMOTE_ACTION_GROUP (exporter->action_group))
419         g_remote_action_group_activate_action_full (G_REMOTE_ACTION_GROUP (exporter->action_group),
420                                                     name, parameter, platform_data);
421       else
422         g_action_group_activate_action (exporter->action_group, name, parameter);
423
424       if (parameter)
425         g_variant_unref (parameter);
426
427       g_variant_unref (platform_data);
428     }
429
430   else if (g_str_equal (method_name, "SetState"))
431     {
432       GVariant *platform_data;
433       const gchar *name;
434       GVariant *state;
435
436       g_variant_get (parameters, "(&sv@a{sv})", &name, &state, &platform_data);
437
438       if (G_IS_REMOTE_ACTION_GROUP (exporter->action_group))
439         g_remote_action_group_change_action_state_full (G_REMOTE_ACTION_GROUP (exporter->action_group),
440                                                         name, state, platform_data);
441       else
442         g_action_group_change_action_state (exporter->action_group, name, state);
443
444       g_variant_unref (platform_data);
445       g_variant_unref (state);
446     }
447
448   else
449     g_assert_not_reached ();
450
451   g_dbus_method_invocation_return_value (invocation, result);
452 }
453
454 static void
455 g_action_group_exporter_free (gpointer user_data)
456 {
457   GActionGroupExporter *exporter = user_data;
458
459   g_signal_handlers_disconnect_by_func (exporter->action_group,
460                                         g_action_group_exporter_action_added, exporter);
461   g_signal_handlers_disconnect_by_func (exporter->action_group,
462                                         g_action_group_exporter_action_enabled_changed, exporter);
463   g_signal_handlers_disconnect_by_func (exporter->action_group,
464                                         g_action_group_exporter_action_state_changed, exporter);
465   g_signal_handlers_disconnect_by_func (exporter->action_group,
466                                         g_action_group_exporter_action_removed, exporter);
467
468   g_hash_table_unref (exporter->pending_changes);
469   if (exporter->pending_source)
470     g_source_destroy (exporter->pending_source);
471
472   g_main_context_unref (exporter->context);
473   g_object_unref (exporter->connection);
474   g_object_unref (exporter->action_group);
475   g_free (exporter->object_path);
476
477   g_slice_free (GActionGroupExporter, exporter);
478 }
479
480 /**
481  * g_dbus_connection_export_action_group:
482  * @connection: a #GDBusConnection
483  * @object_path: a D-Bus object path
484  * @action_group: a #GActionGroup
485  * @error: a pointer to a %NULL #GError, or %NULL
486  *
487  * Exports @action_group on @connection at @object_path.
488  *
489  * The implemented D-Bus API should be considered private.  It is
490  * subject to change in the future.
491  *
492  * A given object path can only have one action group exported on it.
493  * If this constraint is violated, the export will fail and 0 will be
494  * returned (with @error set accordingly).
495  *
496  * You can unexport the action group using
497  * g_dbus_connection_unexport_action_group() with the return value of
498  * this function.
499  *
500  * The thread default main context is taken at the time of this call.
501  * All incoming action activations and state change requests are
502  * reported from this context.  Any changes on the action group that
503  * cause it to emit signals must also come from this same context.
504  * Since incoming action activations and state change requests are
505  * rather likely to cause changes on the action group, this effectively
506  * limits a given action group to being exported from only one main
507  * context.
508  *
509  * Returns: the ID of the export (never zero), or 0 in case of failure
510  *
511  * Since: 2.32
512  **/
513 guint
514 g_dbus_connection_export_action_group (GDBusConnection  *connection,
515                                        const gchar      *object_path,
516                                        GActionGroup     *action_group,
517                                        GError          **error)
518 {
519   const GDBusInterfaceVTable vtable = {
520     org_gtk_Actions_method_call
521   };
522   GActionGroupExporter *exporter;
523   guint id;
524
525   if G_UNLIKELY (org_gtk_Actions == NULL)
526     {
527       GError *error = NULL;
528       GDBusNodeInfo *info;
529
530       info = g_dbus_node_info_new_for_xml (org_gtk_Actions_xml, &error);
531       if G_UNLIKELY (info == NULL)
532         g_error ("%s", error->message);
533       org_gtk_Actions = g_dbus_node_info_lookup_interface (info, "org.gtk.Actions");
534       g_assert (org_gtk_Actions != NULL);
535       g_dbus_interface_info_ref (org_gtk_Actions);
536       g_dbus_node_info_unref (info);
537     }
538
539   exporter = g_slice_new (GActionGroupExporter);
540   id = g_dbus_connection_register_object (connection, object_path, org_gtk_Actions, &vtable,
541                                           exporter, g_action_group_exporter_free, error);
542
543   if (id == 0)
544     {
545       g_slice_free (GActionGroupExporter, exporter);
546       return 0;
547     }
548
549   exporter->context = g_main_context_ref_thread_default ();
550   exporter->pending_changes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
551   exporter->pending_source = NULL;
552   exporter->action_group = g_object_ref (action_group);
553   exporter->connection = g_object_ref (connection);
554   exporter->object_path = g_strdup (object_path);
555
556   g_signal_connect (action_group, "action-added",
557                     G_CALLBACK (g_action_group_exporter_action_added), exporter);
558   g_signal_connect (action_group, "action-removed",
559                     G_CALLBACK (g_action_group_exporter_action_removed), exporter);
560   g_signal_connect (action_group, "action-state-changed",
561                     G_CALLBACK (g_action_group_exporter_action_state_changed), exporter);
562   g_signal_connect (action_group, "action-enabled-changed",
563                     G_CALLBACK (g_action_group_exporter_action_enabled_changed), exporter);
564
565   return id;
566 }
567
568 /**
569  * g_dbus_connection_unexport_action_group:
570  * @connection: a #GDBusConnection
571  * @export_id: the ID from g_dbus_connection_export_action_group()
572  *
573  * Reverses the effect of a previous call to
574  * g_dbus_connection_export_action_group().
575  *
576  * It is an error to call this function with an ID that wasn't returned
577  * from g_dbus_connection_export_action_group() or to call it with the
578  * same ID more than once.
579  *
580  * Since: 2.32
581  **/
582 void
583 g_dbus_connection_unexport_action_group (GDBusConnection *connection,
584                                          guint            export_id)
585 {
586   g_dbus_connection_unregister_object (connection, export_id);
587 }