[kdbus] Enable adding bloom filters items. Signals subscription refactoring.
[platform/upstream/glib.git] / gio / gsimpleactiongroup.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the licence or (at
7  * 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, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Ryan Lortie <desrt@desrt.ca>
18  */
19
20 #include "config.h"
21
22 #include "gsimpleactiongroup.h"
23
24 #include "gsimpleaction.h"
25 #include "gactionmap.h"
26 #include "gaction.h"
27
28 /**
29  * SECTION:gsimpleactiongroup
30  * @title: GSimpleActionGroup
31  * @short_description: A simple GActionGroup implementation
32  * @include: gio/gio.h
33  *
34  * #GSimpleActionGroup is a hash table filled with #GAction objects,
35  * implementing the #GActionGroup and #GActionMap interfaces.
36  **/
37
38 struct _GSimpleActionGroupPrivate
39 {
40   GHashTable *table;  /* string -> GAction */
41 };
42
43 static void g_simple_action_group_iface_init (GActionGroupInterface *);
44 static void g_simple_action_group_map_iface_init (GActionMapInterface *);
45 G_DEFINE_TYPE_WITH_CODE (GSimpleActionGroup,
46   g_simple_action_group, G_TYPE_OBJECT,
47   G_ADD_PRIVATE (GSimpleActionGroup)
48   G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
49                          g_simple_action_group_iface_init);
50   G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP,
51                          g_simple_action_group_map_iface_init))
52
53 static gchar **
54 g_simple_action_group_list_actions (GActionGroup *group)
55 {
56   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
57   GHashTableIter iter;
58   gint n, i = 0;
59   gchar **keys;
60   gpointer key;
61
62   n = g_hash_table_size (simple->priv->table);
63   keys = g_new (gchar *, n + 1);
64
65   g_hash_table_iter_init (&iter, simple->priv->table);
66   while (g_hash_table_iter_next (&iter, &key, NULL))
67     keys[i++] = g_strdup (key);
68   g_assert_cmpint (i, ==, n);
69   keys[n] = NULL;
70
71   return keys;
72 }
73
74 static gboolean
75 g_simple_action_group_query_action (GActionGroup        *group,
76                                     const gchar         *action_name,
77                                     gboolean            *enabled,
78                                     const GVariantType **parameter_type,
79                                     const GVariantType **state_type,
80                                     GVariant           **state_hint,
81                                     GVariant           **state)
82 {
83   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
84   GAction *action;
85
86   action = g_hash_table_lookup (simple->priv->table, action_name);
87
88   if (action == NULL)
89     return FALSE;
90
91   if (enabled)
92     *enabled = g_action_get_enabled (action);
93
94   if (parameter_type)
95     *parameter_type = g_action_get_parameter_type (action);
96
97   if (state_type)
98     *state_type = g_action_get_state_type (action);
99
100   if (state_hint)
101     *state_hint = g_action_get_state_hint (action);
102
103   if (state)
104     *state = g_action_get_state (action);
105
106   return TRUE;
107 }
108
109 static void
110 g_simple_action_group_change_state (GActionGroup *group,
111                                     const gchar  *action_name,
112                                     GVariant     *value)
113 {
114   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
115   GAction *action;
116
117   action = g_hash_table_lookup (simple->priv->table, action_name);
118
119   if (action == NULL)
120     return;
121
122   g_action_change_state (action, value);
123 }
124
125 static void
126 g_simple_action_group_activate (GActionGroup *group,
127                                 const gchar  *action_name,
128                                 GVariant     *parameter)
129 {
130   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
131   GAction *action;
132
133   action = g_hash_table_lookup (simple->priv->table, action_name);
134
135   if (action == NULL)
136     return;
137
138   g_action_activate (action, parameter);
139 }
140
141 static void
142 action_enabled_notify (GAction     *action,
143                        GParamSpec  *pspec,
144                        gpointer     user_data)
145 {
146   g_action_group_action_enabled_changed (user_data,
147                                          g_action_get_name (action),
148                                          g_action_get_enabled (action));
149 }
150
151 static void
152 action_state_notify (GAction    *action,
153                      GParamSpec *pspec,
154                      gpointer    user_data)
155 {
156   GVariant *value;
157
158   value = g_action_get_state (action);
159   g_action_group_action_state_changed (user_data,
160                                        g_action_get_name (action),
161                                        value);
162   g_variant_unref (value);
163 }
164
165 static void
166 g_simple_action_group_disconnect (gpointer key,
167                                   gpointer value,
168                                   gpointer user_data)
169 {
170   g_signal_handlers_disconnect_by_func (value, action_enabled_notify,
171                                         user_data);
172   g_signal_handlers_disconnect_by_func (value, action_state_notify,
173                                         user_data);
174 }
175
176 static GAction *
177 g_simple_action_group_lookup_action (GActionMap *action_map,
178                                      const gchar        *action_name)
179 {
180   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
181
182   return g_hash_table_lookup (simple->priv->table, action_name);
183 }
184
185 static void
186 g_simple_action_group_add_action (GActionMap *action_map,
187                                   GAction    *action)
188 {
189   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
190   const gchar *action_name;
191   GAction *old_action;
192
193   action_name = g_action_get_name (action);
194   old_action = g_hash_table_lookup (simple->priv->table, action_name);
195
196   if (old_action != action)
197     {
198       if (old_action != NULL)
199         {
200           g_action_group_action_removed (G_ACTION_GROUP (simple),
201                                          action_name);
202           g_simple_action_group_disconnect (NULL, old_action, simple);
203         }
204
205       g_signal_connect (action, "notify::enabled",
206                         G_CALLBACK (action_enabled_notify), simple);
207
208       if (g_action_get_state_type (action) != NULL)
209         g_signal_connect (action, "notify::state",
210                           G_CALLBACK (action_state_notify), simple);
211
212       g_hash_table_insert (simple->priv->table,
213                            g_strdup (action_name),
214                            g_object_ref (action));
215
216       g_action_group_action_added (G_ACTION_GROUP (simple), action_name);
217     }
218 }
219
220 static void
221 g_simple_action_group_remove_action (GActionMap  *action_map,
222                                      const gchar *action_name)
223 {
224   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
225   GAction *action;
226
227   action = g_hash_table_lookup (simple->priv->table, action_name);
228
229   if (action != NULL)
230     {
231       g_action_group_action_removed (G_ACTION_GROUP (simple), action_name);
232       g_simple_action_group_disconnect (NULL, action, simple);
233       g_hash_table_remove (simple->priv->table, action_name);
234     }
235 }
236
237 static void
238 g_simple_action_group_finalize (GObject *object)
239 {
240   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (object);
241
242   g_hash_table_foreach (simple->priv->table,
243                         g_simple_action_group_disconnect,
244                         simple);
245   g_hash_table_unref (simple->priv->table);
246
247   G_OBJECT_CLASS (g_simple_action_group_parent_class)
248     ->finalize (object);
249 }
250
251 static void
252 g_simple_action_group_init (GSimpleActionGroup *simple)
253 {
254   simple->priv = g_simple_action_group_get_instance_private (simple);
255   simple->priv->table = g_hash_table_new_full (g_str_hash, g_str_equal,
256                                                g_free, g_object_unref);
257 }
258
259 static void
260 g_simple_action_group_class_init (GSimpleActionGroupClass *class)
261 {
262   GObjectClass *object_class = G_OBJECT_CLASS (class);
263
264   object_class->finalize = g_simple_action_group_finalize;
265 }
266
267 static void
268 g_simple_action_group_iface_init (GActionGroupInterface *iface)
269 {
270   iface->list_actions = g_simple_action_group_list_actions;
271   iface->query_action = g_simple_action_group_query_action;
272   iface->change_action_state = g_simple_action_group_change_state;
273   iface->activate_action = g_simple_action_group_activate;
274 }
275
276 static void
277 g_simple_action_group_map_iface_init (GActionMapInterface *iface)
278 {
279   iface->add_action = g_simple_action_group_add_action;
280   iface->remove_action = g_simple_action_group_remove_action;
281   iface->lookup_action = g_simple_action_group_lookup_action;
282 }
283
284 /**
285  * g_simple_action_group_new:
286  *
287  * Creates a new, empty, #GSimpleActionGroup.
288  *
289  * Returns: a new #GSimpleActionGroup
290  *
291  * Since: 2.28
292  **/
293 GSimpleActionGroup *
294 g_simple_action_group_new (void)
295 {
296   return g_object_new (G_TYPE_SIMPLE_ACTION_GROUP, NULL);
297 }
298
299 /**
300  * g_simple_action_group_lookup:
301  * @simple: a #GSimpleActionGroup
302  * @action_name: the name of an action
303  *
304  * Looks up the action with the name @action_name in the group.
305  *
306  * If no such action exists, returns %NULL.
307  *
308  * Returns: (transfer none): a #GAction, or %NULL
309  *
310  * Since: 2.28
311  *
312  * Deprecated: 2.38: Use g_action_map_lookup_action()
313  */
314 GAction *
315 g_simple_action_group_lookup (GSimpleActionGroup *simple,
316                               const gchar        *action_name)
317 {
318   g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple), NULL);
319
320   return g_action_map_lookup_action (G_ACTION_MAP (simple), action_name);
321 }
322
323 /**
324  * g_simple_action_group_insert:
325  * @simple: a #GSimpleActionGroup
326  * @action: a #GAction
327  *
328  * Adds an action to the action group.
329  *
330  * If the action group already contains an action with the same name as
331  * @action then the old action is dropped from the group.
332  *
333  * The action group takes its own reference on @action.
334  *
335  * Since: 2.28
336  *
337  * Deprecated: 2.38: Use g_action_map_add_action()
338  **/
339 void
340 g_simple_action_group_insert (GSimpleActionGroup *simple,
341                               GAction            *action)
342 {
343   g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
344
345   g_action_map_add_action (G_ACTION_MAP (simple), action);
346 }
347
348 /**
349  * g_simple_action_group_remove:
350  * @simple: a #GSimpleActionGroup
351  * @action_name: the name of the action
352  *
353  * Removes the named action from the action group.
354  *
355  * If no action of this name is in the group then nothing happens.
356  *
357  * Since: 2.28
358  *
359  * Deprecated: 2.38: Use g_action_map_remove_action()
360  **/
361 void
362 g_simple_action_group_remove (GSimpleActionGroup *simple,
363                               const gchar        *action_name)
364 {
365   g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
366
367   g_action_map_remove_action (G_ACTION_MAP (simple), action_name);
368 }
369
370
371 /**
372  * g_simple_action_group_add_entries:
373  * @simple: a #GSimpleActionGroup
374  * @entries: (array length=n_entries): a pointer to the first item in
375  *           an array of #GActionEntry structs
376  * @n_entries: the length of @entries, or -1
377  * @user_data: the user data for signal connections
378  *
379  * A convenience function for creating multiple #GSimpleAction instances
380  * and adding them to the action group.
381  *
382  * Since: 2.30
383  *
384  * Deprecated: 2.38: Use g_action_map_add_action_entries()
385  **/
386 void
387 g_simple_action_group_add_entries (GSimpleActionGroup *simple,
388                                    const GActionEntry *entries,
389                                    gint                n_entries,
390                                    gpointer            user_data)
391 {
392   g_action_map_add_action_entries (G_ACTION_MAP (simple), entries, n_entries, user_data);
393 }