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