Revert "GIOScheduler: Avoid constant iteration over pending job list"
[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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  */
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  *
33  * #GSimpleActionGroup is a hash table filled with #GAction objects,
34  * implementing the #GActionGroup and #GActionMap interfaces.
35  **/
36
37 struct _GSimpleActionGroupPrivate
38 {
39   GHashTable *table;  /* string -> GAction */
40 };
41
42 static void g_simple_action_group_iface_init (GActionGroupInterface *);
43 static void g_simple_action_group_map_iface_init (GActionMapInterface *);
44 G_DEFINE_TYPE_WITH_CODE (GSimpleActionGroup,
45   g_simple_action_group, G_TYPE_OBJECT,
46   G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
47                          g_simple_action_group_iface_init);
48   G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP,
49                          g_simple_action_group_map_iface_init))
50
51 static gchar **
52 g_simple_action_group_list_actions (GActionGroup *group)
53 {
54   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
55   GHashTableIter iter;
56   gint n, i = 0;
57   gchar **keys;
58   gpointer key;
59
60   n = g_hash_table_size (simple->priv->table);
61   keys = g_new (gchar *, n + 1);
62
63   g_hash_table_iter_init (&iter, simple->priv->table);
64   while (g_hash_table_iter_next (&iter, &key, NULL))
65     keys[i++] = g_strdup (key);
66   g_assert_cmpint (i, ==, n);
67   keys[n] = NULL;
68
69   return keys;
70 }
71
72 static gboolean
73 g_simple_action_group_query_action (GActionGroup        *group,
74                                     const gchar         *action_name,
75                                     gboolean            *enabled,
76                                     const GVariantType **parameter_type,
77                                     const GVariantType **state_type,
78                                     GVariant           **state_hint,
79                                     GVariant           **state)
80 {
81   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
82   GAction *action;
83
84   action = g_hash_table_lookup (simple->priv->table, action_name);
85
86   if (action == NULL)
87     return FALSE;
88
89   if (enabled)
90     *enabled = g_action_get_enabled (action);
91
92   if (parameter_type)
93     *parameter_type = g_action_get_parameter_type (action);
94
95   if (state_type)
96     *state_type = g_action_get_state_type (action);
97
98   if (state_hint)
99     *state_hint = g_action_get_state_hint (action);
100
101   if (state)
102     *state = g_action_get_state (action);
103
104   return TRUE;
105 }
106
107 static void
108 g_simple_action_group_change_state (GActionGroup *group,
109                                     const gchar  *action_name,
110                                     GVariant     *value)
111 {
112   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
113   GAction *action;
114
115   action = g_hash_table_lookup (simple->priv->table, action_name);
116
117   if (action == NULL)
118     return;
119
120   g_action_change_state (action, value);
121 }
122
123 static void
124 g_simple_action_group_activate (GActionGroup *group,
125                                 const gchar  *action_name,
126                                 GVariant     *parameter)
127 {
128   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
129   GAction *action;
130
131   action = g_hash_table_lookup (simple->priv->table, action_name);
132
133   if (action == NULL)
134     return;
135
136   g_action_activate (action, parameter);
137 }
138
139 static void
140 action_enabled_notify (GAction     *action,
141                        GParamSpec  *pspec,
142                        gpointer     user_data)
143 {
144   g_action_group_action_enabled_changed (user_data,
145                                          g_action_get_name (action),
146                                          g_action_get_enabled (action));
147 }
148
149 static void
150 action_state_notify (GAction    *action,
151                      GParamSpec *pspec,
152                      gpointer    user_data)
153 {
154   GVariant *value;
155
156   value = g_action_get_state (action);
157   g_action_group_action_state_changed (user_data,
158                                        g_action_get_name (action),
159                                        value);
160   g_variant_unref (value);
161 }
162
163 static void
164 g_simple_action_group_disconnect (gpointer key,
165                                   gpointer value,
166                                   gpointer user_data)
167 {
168   g_signal_handlers_disconnect_by_func (value, action_enabled_notify,
169                                         user_data);
170   g_signal_handlers_disconnect_by_func (value, action_state_notify,
171                                         user_data);
172 }
173
174 static GAction *
175 g_simple_action_group_lookup_action (GActionMap *action_map,
176                                      const gchar        *action_name)
177 {
178   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
179
180   return g_hash_table_lookup (simple->priv->table, action_name);
181 }
182
183 static void
184 g_simple_action_group_add_action (GActionMap *action_map,
185                                   GAction    *action)
186 {
187   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
188   const gchar *action_name;
189   GAction *old_action;
190
191   action_name = g_action_get_name (action);
192   old_action = g_hash_table_lookup (simple->priv->table, action_name);
193
194   if (old_action != action)
195     {
196       if (old_action != NULL)
197         {
198           g_action_group_action_removed (G_ACTION_GROUP (simple),
199                                          action_name);
200           g_simple_action_group_disconnect (NULL, old_action, simple);
201         }
202
203       g_signal_connect (action, "notify::enabled",
204                         G_CALLBACK (action_enabled_notify), simple);
205
206       if (g_action_get_state_type (action) != NULL)
207         g_signal_connect (action, "notify::state",
208                           G_CALLBACK (action_state_notify), simple);
209
210       g_hash_table_insert (simple->priv->table,
211                            g_strdup (action_name),
212                            g_object_ref (action));
213
214       g_action_group_action_added (G_ACTION_GROUP (simple), action_name);
215     }
216 }
217
218 static void
219 g_simple_action_group_remove_action (GActionMap  *action_map,
220                                      const gchar *action_name)
221 {
222   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
223   GAction *action;
224
225   action = g_hash_table_lookup (simple->priv->table, action_name);
226
227   if (action != NULL)
228     {
229       g_action_group_action_removed (G_ACTION_GROUP (simple), action_name);
230       g_simple_action_group_disconnect (NULL, action, simple);
231       g_hash_table_remove (simple->priv->table, action_name);
232     }
233 }
234
235 static void
236 g_simple_action_group_finalize (GObject *object)
237 {
238   GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (object);
239
240   g_hash_table_foreach (simple->priv->table,
241                         g_simple_action_group_disconnect,
242                         simple);
243   g_hash_table_unref (simple->priv->table);
244
245   G_OBJECT_CLASS (g_simple_action_group_parent_class)
246     ->finalize (object);
247 }
248
249 static void
250 g_simple_action_group_init (GSimpleActionGroup *simple)
251 {
252   simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (simple,
253                                               G_TYPE_SIMPLE_ACTION_GROUP,
254                                               GSimpleActionGroupPrivate);
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   g_type_class_add_private (class, sizeof (GSimpleActionGroupPrivate));
267 }
268
269 static void
270 g_simple_action_group_iface_init (GActionGroupInterface *iface)
271 {
272   iface->list_actions = g_simple_action_group_list_actions;
273   iface->query_action = g_simple_action_group_query_action;
274   iface->change_action_state = g_simple_action_group_change_state;
275   iface->activate_action = g_simple_action_group_activate;
276 }
277
278 static void
279 g_simple_action_group_map_iface_init (GActionMapInterface *iface)
280 {
281   iface->add_action = g_simple_action_group_add_action;
282   iface->remove_action = g_simple_action_group_remove_action;
283   iface->lookup_action = g_simple_action_group_lookup_action;
284 }
285
286 /**
287  * g_simple_action_group_new:
288  *
289  * Creates a new, empty, #GSimpleActionGroup.
290  *
291  * Returns: a new #GSimpleActionGroup
292  *
293  * Since: 2.28
294  **/
295 GSimpleActionGroup *
296 g_simple_action_group_new (void)
297 {
298   return g_object_new (G_TYPE_SIMPLE_ACTION_GROUP, NULL);
299 }
300
301 /**
302  * g_simple_action_group_lookup:
303  * @simple: a #GSimpleActionGroup
304  * @action_name: the name of an action
305  *
306  * Looks up the action with the name @action_name in the group.
307  *
308  * If no such action exists, returns %NULL.
309  *
310  * Returns: (transfer none): a #GAction, or %NULL
311  *
312  * Since: 2.28
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 void
338 g_simple_action_group_insert (GSimpleActionGroup *simple,
339                               GAction            *action)
340 {
341   g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
342
343   g_action_map_add_action (G_ACTION_MAP (simple), action);
344 }
345
346 /**
347  * g_simple_action_group_remove:
348  * @simple: a #GSimpleActionGroup
349  * @action_name: the name of the action
350  *
351  * Removes the named action from the action group.
352  *
353  * If no action of this name is in the group then nothing happens.
354  *
355  * Since: 2.28
356  **/
357 void
358 g_simple_action_group_remove (GSimpleActionGroup *simple,
359                               const gchar        *action_name)
360 {
361   g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
362
363   g_action_map_remove_action (G_ACTION_MAP (simple), action_name);
364 }
365
366
367 /**
368  * g_simple_action_group_add_entries:
369  * @simple: a #GSimpleActionGroup
370  * @entries: (array length=n_entries): a pointer to the first item in
371  *           an array of #GActionEntry structs
372  * @n_entries: the length of @entries, or -1
373  * @user_data: the user data for signal connections
374  *
375  * A convenience function for creating multiple #GSimpleAction instances
376  * and adding them to the action group.
377  *
378  * Since: 2.30
379  **/
380 void
381 g_simple_action_group_add_entries (GSimpleActionGroup *simple,
382                                    const GActionEntry *entries,
383                                    gint                n_entries,
384                                    gpointer            user_data)
385 {
386   g_action_map_add_action_entries (G_ACTION_MAP (simple), entries, n_entries, user_data);
387 }