2 * Copyright © 2010 Codethink Limited
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.
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.
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.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
22 #include "gsimpleactiongroup.h"
24 #include "gsimpleaction.h"
28 * SECTION:gsimpleactiongroup
29 * @title: GSimpleActionGroup
30 * @short_description: A simple GActionGroup implementation
32 * #GSimpleActionGroup is a hash table filled with #GAction objects,
33 * implementing the #GActionGroup interface.
36 struct _GSimpleActionGroupPrivate
38 GHashTable *table; /* string -> GAction */
41 static void g_simple_action_group_iface_init (GActionGroupInterface *);
42 G_DEFINE_TYPE_WITH_CODE (GSimpleActionGroup,
43 g_simple_action_group, G_TYPE_OBJECT,
44 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
45 g_simple_action_group_iface_init))
48 g_simple_action_group_list_actions (GActionGroup *group)
50 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
56 n = g_hash_table_size (simple->priv->table);
57 keys = g_new (gchar *, n + 1);
59 g_hash_table_iter_init (&iter, simple->priv->table);
60 while (g_hash_table_iter_next (&iter, &key, NULL))
61 keys[i++] = g_strdup (key);
62 g_assert_cmpint (i, ==, n);
69 g_simple_action_group_has_action (GActionGroup *group,
70 const gchar *action_name)
72 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
74 return g_hash_table_lookup (simple->priv->table, action_name) != NULL;
77 static const GVariantType *
78 g_simple_action_group_get_parameter_type (GActionGroup *group,
79 const gchar *action_name)
81 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
84 action = g_hash_table_lookup (simple->priv->table, action_name);
89 return g_action_get_parameter_type (action);
92 static const GVariantType *
93 g_simple_action_group_get_state_type (GActionGroup *group,
94 const gchar *action_name)
96 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
99 action = g_hash_table_lookup (simple->priv->table, action_name);
104 return g_action_get_state_type (action);
108 g_simple_action_group_get_state_hint (GActionGroup *group,
109 const gchar *action_name)
111 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
114 action = g_hash_table_lookup (simple->priv->table, action_name);
119 return g_action_get_state_hint (action);
123 g_simple_action_group_get_enabled (GActionGroup *group,
124 const gchar *action_name)
126 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
129 action = g_hash_table_lookup (simple->priv->table, action_name);
134 return g_action_get_enabled (action);
138 g_simple_action_group_get_state (GActionGroup *group,
139 const gchar *action_name)
141 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
144 action = g_hash_table_lookup (simple->priv->table, action_name);
149 return g_action_get_state (action);
153 g_simple_action_group_change_state (GActionGroup *group,
154 const gchar *action_name,
157 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
160 action = g_hash_table_lookup (simple->priv->table, action_name);
165 g_action_change_state (action, value);
169 g_simple_action_group_activate (GActionGroup *group,
170 const gchar *action_name,
173 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
176 action = g_hash_table_lookup (simple->priv->table, action_name);
181 g_action_activate (action, parameter);
185 action_enabled_notify (GAction *action,
189 g_action_group_action_enabled_changed (user_data,
190 g_action_get_name (action),
191 g_action_get_enabled (action));
195 action_state_notify (GAction *action,
201 value = g_action_get_state (action);
202 g_action_group_action_state_changed (user_data,
203 g_action_get_name (action),
205 g_variant_unref (value);
209 g_simple_action_group_disconnect (gpointer key,
213 g_signal_handlers_disconnect_by_func (value, action_enabled_notify,
215 g_signal_handlers_disconnect_by_func (value, action_state_notify,
220 g_simple_action_group_finalize (GObject *object)
222 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (object);
224 g_hash_table_foreach (simple->priv->table,
225 g_simple_action_group_disconnect,
227 g_hash_table_unref (simple->priv->table);
229 G_OBJECT_CLASS (g_simple_action_group_parent_class)
234 g_simple_action_group_init (GSimpleActionGroup *simple)
236 simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (simple,
237 G_TYPE_SIMPLE_ACTION_GROUP,
238 GSimpleActionGroupPrivate);
239 simple->priv->table = g_hash_table_new_full (g_str_hash, g_str_equal,
240 g_free, g_object_unref);
244 g_simple_action_group_class_init (GSimpleActionGroupClass *class)
246 GObjectClass *object_class = G_OBJECT_CLASS (class);
248 object_class->finalize = g_simple_action_group_finalize;
250 g_type_class_add_private (class, sizeof (GSimpleActionGroupPrivate));
254 g_simple_action_group_iface_init (GActionGroupInterface *iface)
256 iface->list_actions = g_simple_action_group_list_actions;
257 iface->has_action = g_simple_action_group_has_action;
258 iface->get_action_parameter_type = g_simple_action_group_get_parameter_type;
259 iface->get_action_state_type = g_simple_action_group_get_state_type;
260 iface->get_action_state_hint = g_simple_action_group_get_state_hint;
261 iface->get_action_enabled = g_simple_action_group_get_enabled;
262 iface->get_action_state = g_simple_action_group_get_state;
263 iface->change_action_state = g_simple_action_group_change_state;
264 iface->activate_action = g_simple_action_group_activate;
268 * g_simple_action_group_new:
270 * Creates a new, empty, #GSimpleActionGroup.
272 * Returns: a new #GSimpleActionGroup
277 g_simple_action_group_new (void)
279 return g_object_new (G_TYPE_SIMPLE_ACTION_GROUP, NULL);
283 * g_simple_action_group_lookup:
284 * @simple: a #GSimpleActionGroup
285 * @action_name: the name of an action
287 * Looks up the action with the name @action_name in the group.
289 * If no such action exists, returns %NULL.
291 * Returns: (transfer none): a #GAction, or %NULL
296 g_simple_action_group_lookup (GSimpleActionGroup *simple,
297 const gchar *action_name)
299 g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple), NULL);
301 return g_hash_table_lookup (simple->priv->table, action_name);
305 * g_simple_action_group_insert:
306 * @simple: a #GSimpleActionGroup
307 * @action: a #GAction
309 * Adds an action to the action group.
311 * If the action group already contains an action with the same name as
312 * @action then the old action is dropped from the group.
314 * The action group takes its own reference on @action.
319 g_simple_action_group_insert (GSimpleActionGroup *simple,
322 const gchar *action_name;
325 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
326 g_return_if_fail (G_IS_ACTION (action));
328 action_name = g_action_get_name (action);
329 old_action = g_hash_table_lookup (simple->priv->table, action_name);
331 if (old_action != action)
333 if (old_action != NULL)
335 g_action_group_action_removed (G_ACTION_GROUP (simple),
337 g_simple_action_group_disconnect (NULL, old_action, simple);
340 g_signal_connect (action, "notify::enabled",
341 G_CALLBACK (action_enabled_notify), simple);
343 if (g_action_get_state_type (action) != NULL)
344 g_signal_connect (action, "notify::state",
345 G_CALLBACK (action_state_notify), simple);
347 g_hash_table_insert (simple->priv->table,
348 g_strdup (action_name),
349 g_object_ref (action));
351 g_action_group_action_added (G_ACTION_GROUP (simple), action_name);
356 * g_simple_action_group_remove:
357 * @simple: a #GSimpleActionGroup
358 * @action_name: the name of the action
360 * Removes the named action from the action group.
362 * If no action of this name is in the group then nothing happens.
367 g_simple_action_group_remove (GSimpleActionGroup *simple,
368 const gchar *action_name)
372 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
374 action = g_hash_table_lookup (simple->priv->table, action_name);
378 g_action_group_action_removed (G_ACTION_GROUP (simple), action_name);
379 g_simple_action_group_disconnect (NULL, action, simple);
380 g_hash_table_remove (simple->priv->table, action_name);
386 * @name: the name of the action
387 * @activate: the callback to connect to the "activate" signal of the
389 * @parameter_type: the type of the parameter that must be passed to the
390 * activate function for this action, given as a single
391 * GVariant type string (or %NULL for no parameter)
392 * @state: the initial state for this action, given in GVariant text
393 * format. The state is parsed with no extra type information,
394 * so type tags must be added to the string if they are
396 * @change_state: the callback to connect to the "change-state" signal
399 * This struct defines a single action. It is for use with
400 * g_simple_action_group_add_entries().
402 * The order of the items in the structure are intended to reflect
403 * frequency of use. It is permissible to use an incomplete initialiser
404 * in order to leave some of the later values as %NULL. All values
405 * after @name are optional. Additional optional fields may be added in
408 * See g_simple_action_group_add_entries() for an example.
412 * g_simple_action_group_add_entries:
413 * @simple: a #GSimpleActionGroup
414 * @entries: a pointer to the first item in an array of #GActionEntry
416 * @n_entries: the length of @entries, or -1
417 * @user_data: the user data for signal connections
419 * A convenience function for creating multiple #GSimpleAction instances
420 * and adding them to the action group.
422 * Each action is constructed as per one #GActionEntry.
425 * <title>Using g_simple_action_group_add_entries()</title>
428 * activate_quit (GSimpleAction *simple,
429 * GVariant *parameter,
430 * gpointer user_data)
436 * activate_print_string (GSimpleAction *simple,
437 * GVariant *parameter,
438 * gpointer user_data)
440 * g_print ("%s\n", g_variant_get_string (parameter, NULL));
443 * static GActionGroup *
444 * create_action_group (void)
446 * const GActionEntry entries[] = {
447 * { "quit", activate_quit },
448 * { "print-string", activate_print_string, "s" }
450 * GSimpleActionGroup *group;
452 * group = g_simple_action_group_new ();
453 * g_simple_action_group_add_entries (group, entries, G_N_ELEMENTS (entries), NULL);
455 * return G_ACTION_GROUP (group);
463 g_simple_action_group_add_entries (GSimpleActionGroup *simple,
464 const GActionEntry *entries,
470 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
471 g_return_if_fail (entries != NULL || n_entries == 0);
473 for (i = 0; n_entries == -1 ? entries[i].name != NULL : i < n_entries; i++)
475 const GActionEntry *entry = &entries[i];
476 const GVariantType *parameter_type;
477 GSimpleAction *action;
479 if (entry->parameter_type)
481 if (!g_variant_type_string_is_valid (entry->parameter_type))
483 g_critical ("g_simple_action_group_add_entries: the type "
484 "string '%s' given as the parameter type for "
485 "action '%s' is not a valid GVariant type "
486 "string. This action will not be added.",
487 entry->parameter_type, entry->name);
491 parameter_type = G_VARIANT_TYPE (entry->parameter_type);
494 parameter_type = NULL;
498 GError *error = NULL;
501 state = g_variant_parse (NULL, entry->state, NULL, NULL, &error);
504 g_critical ("g_simple_action_group_add_entries: GVariant could "
505 "not parse the state value given for action '%s' "
506 "('%s'): %s. This action will not be added.",
507 entry->name, entry->state, error->message);
508 g_error_free (error);
512 action = g_simple_action_new_stateful (entry->name,
516 g_variant_unref (state);
520 action = g_simple_action_new (entry->name,
524 if (entry->activate != NULL)
525 g_signal_connect (action, "activate",
526 G_CALLBACK (entry->activate), user_data);
528 if (entry->change_state != NULL)
529 g_signal_connect (action, "change-state",
530 G_CALLBACK (entry->change_state), user_data);
532 g_simple_action_group_insert (simple, G_ACTION (action));
533 g_object_unref (action);