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