2010-08-18 08:15:09 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2010 Codethink Limited
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published
|
|
|
|
* by the Free Software Foundation; either version 2 of the licence or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
|
|
|
* Public License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Authors: Ryan Lortie <desrt@desrt.ca>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gsimpleactiongroup.h"
|
2011-06-29 14:00:30 +02:00
|
|
|
|
|
|
|
#include "gsimpleaction.h"
|
2010-08-18 08:15:09 +02:00
|
|
|
#include "gaction.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gsimpleactiongroup
|
|
|
|
* @title: GSimpleActionGroup
|
2010-11-29 05:55:43 +01:00
|
|
|
* @short_description: A simple GActionGroup implementation
|
2010-08-18 08:15:09 +02:00
|
|
|
*
|
|
|
|
* #GSimpleActionGroup is a hash table filled with #GAction objects,
|
|
|
|
* implementing the #GActionGroup interface.
|
|
|
|
**/
|
|
|
|
|
|
|
|
struct _GSimpleActionGroupPrivate
|
|
|
|
{
|
|
|
|
GHashTable *table; /* string -> GAction */
|
|
|
|
};
|
|
|
|
|
2010-08-30 17:31:06 +02:00
|
|
|
static void g_simple_action_group_iface_init (GActionGroupInterface *);
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GSimpleActionGroup,
|
|
|
|
g_simple_action_group, G_TYPE_OBJECT,
|
|
|
|
G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
|
|
|
|
g_simple_action_group_iface_init))
|
2010-08-18 08:15:09 +02:00
|
|
|
|
|
|
|
static gchar **
|
|
|
|
g_simple_action_group_list_actions (GActionGroup *group)
|
|
|
|
{
|
|
|
|
GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
|
|
|
|
GHashTableIter iter;
|
|
|
|
gint n, i = 0;
|
|
|
|
gchar **keys;
|
|
|
|
gpointer key;
|
|
|
|
|
|
|
|
n = g_hash_table_size (simple->priv->table);
|
|
|
|
keys = g_new (gchar *, n + 1);
|
|
|
|
|
|
|
|
g_hash_table_iter_init (&iter, simple->priv->table);
|
|
|
|
while (g_hash_table_iter_next (&iter, &key, NULL))
|
|
|
|
keys[i++] = g_strdup (key);
|
|
|
|
g_assert_cmpint (i, ==, n);
|
|
|
|
keys[n] = NULL;
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-26 04:35:33 +01:00
|
|
|
g_simple_action_group_query_action (GActionGroup *group,
|
|
|
|
const gchar *action_name,
|
|
|
|
gboolean *enabled,
|
|
|
|
const GVariantType **parameter_type,
|
|
|
|
const GVariantType **state_type,
|
|
|
|
GVariant **state_hint,
|
|
|
|
GVariant **state)
|
2010-08-18 08:15:09 +02:00
|
|
|
{
|
|
|
|
GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
|
|
|
|
GAction *action;
|
|
|
|
|
|
|
|
action = g_hash_table_lookup (simple->priv->table, action_name);
|
|
|
|
|
|
|
|
if (action == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
2011-11-26 04:35:33 +01:00
|
|
|
if (enabled)
|
|
|
|
*enabled = g_action_get_enabled (action);
|
2010-08-18 08:15:09 +02:00
|
|
|
|
2011-11-26 04:35:33 +01:00
|
|
|
if (parameter_type)
|
|
|
|
*parameter_type = g_action_get_parameter_type (action);
|
2010-08-18 08:15:09 +02:00
|
|
|
|
2011-11-26 04:35:33 +01:00
|
|
|
if (state_type)
|
|
|
|
*state_type = g_action_get_state_type (action);
|
2010-08-18 08:15:09 +02:00
|
|
|
|
2011-11-26 04:35:33 +01:00
|
|
|
if (state_hint)
|
|
|
|
*state_hint = g_action_get_state_hint (action);
|
|
|
|
|
|
|
|
if (state)
|
|
|
|
*state = g_action_get_state (action);
|
2010-08-18 08:15:09 +02:00
|
|
|
|
2011-11-26 04:35:33 +01:00
|
|
|
return TRUE;
|
2010-08-18 08:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-29 11:11:59 +02:00
|
|
|
g_simple_action_group_change_state (GActionGroup *group,
|
|
|
|
const gchar *action_name,
|
|
|
|
GVariant *value)
|
2010-08-18 08:15:09 +02:00
|
|
|
{
|
|
|
|
GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
|
|
|
|
GAction *action;
|
|
|
|
|
|
|
|
action = g_hash_table_lookup (simple->priv->table, action_name);
|
|
|
|
|
|
|
|
if (action == NULL)
|
|
|
|
return;
|
|
|
|
|
2011-06-29 11:11:59 +02:00
|
|
|
g_action_change_state (action, value);
|
2010-08-18 08:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_simple_action_group_activate (GActionGroup *group,
|
|
|
|
const gchar *action_name,
|
|
|
|
GVariant *parameter)
|
|
|
|
{
|
|
|
|
GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
|
|
|
|
GAction *action;
|
|
|
|
|
|
|
|
action = g_hash_table_lookup (simple->priv->table, action_name);
|
|
|
|
|
|
|
|
if (action == NULL)
|
|
|
|
return;
|
|
|
|
|
2010-12-22 08:37:21 +01:00
|
|
|
g_action_activate (action, parameter);
|
2010-08-18 08:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-30 17:31:06 +02:00
|
|
|
action_enabled_notify (GAction *action,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
gpointer user_data)
|
2010-08-18 08:15:09 +02:00
|
|
|
{
|
|
|
|
g_action_group_action_enabled_changed (user_data,
|
|
|
|
g_action_get_name (action),
|
2010-08-30 17:31:06 +02:00
|
|
|
g_action_get_enabled (action));
|
2010-08-18 08:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-30 17:31:06 +02:00
|
|
|
action_state_notify (GAction *action,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
gpointer user_data)
|
2010-08-18 08:15:09 +02:00
|
|
|
{
|
2010-08-30 17:31:06 +02:00
|
|
|
GVariant *value;
|
|
|
|
|
|
|
|
value = g_action_get_state (action);
|
2010-08-18 08:15:09 +02:00
|
|
|
g_action_group_action_state_changed (user_data,
|
|
|
|
g_action_get_name (action),
|
|
|
|
value);
|
2010-08-30 17:31:06 +02:00
|
|
|
g_variant_unref (value);
|
2010-08-18 08:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_simple_action_group_disconnect (gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2010-08-30 17:31:06 +02:00
|
|
|
g_signal_handlers_disconnect_by_func (value, action_enabled_notify,
|
2010-08-18 08:15:09 +02:00
|
|
|
user_data);
|
2010-08-30 17:31:06 +02:00
|
|
|
g_signal_handlers_disconnect_by_func (value, action_state_notify,
|
2010-08-18 08:15:09 +02:00
|
|
|
user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_simple_action_group_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (object);
|
|
|
|
|
|
|
|
g_hash_table_foreach (simple->priv->table,
|
|
|
|
g_simple_action_group_disconnect,
|
|
|
|
simple);
|
|
|
|
g_hash_table_unref (simple->priv->table);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (g_simple_action_group_parent_class)
|
|
|
|
->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_simple_action_group_init (GSimpleActionGroup *simple)
|
|
|
|
{
|
|
|
|
simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (simple,
|
|
|
|
G_TYPE_SIMPLE_ACTION_GROUP,
|
|
|
|
GSimpleActionGroupPrivate);
|
|
|
|
simple->priv->table = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
g_free, g_object_unref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_simple_action_group_class_init (GSimpleActionGroupClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
object_class->finalize = g_simple_action_group_finalize;
|
|
|
|
|
|
|
|
g_type_class_add_private (class, sizeof (GSimpleActionGroupPrivate));
|
|
|
|
}
|
|
|
|
|
2010-08-30 17:31:06 +02:00
|
|
|
static void
|
|
|
|
g_simple_action_group_iface_init (GActionGroupInterface *iface)
|
|
|
|
{
|
|
|
|
iface->list_actions = g_simple_action_group_list_actions;
|
2011-11-26 04:35:33 +01:00
|
|
|
iface->query_action = g_simple_action_group_query_action;
|
2011-06-29 11:11:59 +02:00
|
|
|
iface->change_action_state = g_simple_action_group_change_state;
|
2010-10-11 16:45:51 +02:00
|
|
|
iface->activate_action = g_simple_action_group_activate;
|
2010-08-30 17:31:06 +02:00
|
|
|
}
|
|
|
|
|
2010-08-18 08:15:09 +02:00
|
|
|
/**
|
|
|
|
* g_simple_action_group_new:
|
|
|
|
*
|
|
|
|
* Creates a new, empty, #GSimpleActionGroup.
|
|
|
|
*
|
|
|
|
* Returns: a new #GSimpleActionGroup
|
|
|
|
*
|
2010-10-11 21:57:09 +02:00
|
|
|
* Since: 2.28
|
2010-08-18 08:15:09 +02:00
|
|
|
**/
|
|
|
|
GSimpleActionGroup *
|
|
|
|
g_simple_action_group_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (G_TYPE_SIMPLE_ACTION_GROUP, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_simple_action_group_lookup:
|
|
|
|
* @simple: a #GSimpleActionGroup
|
|
|
|
* @action_name: the name of an action
|
|
|
|
*
|
|
|
|
* Looks up the action with the name @action_name in the group.
|
|
|
|
*
|
|
|
|
* If no such action exists, returns %NULL.
|
|
|
|
*
|
|
|
|
* Returns: (transfer none): a #GAction, or %NULL
|
|
|
|
*
|
2010-10-11 21:57:09 +02:00
|
|
|
* Since: 2.28
|
2010-08-18 08:15:09 +02:00
|
|
|
**/
|
|
|
|
GAction *
|
|
|
|
g_simple_action_group_lookup (GSimpleActionGroup *simple,
|
|
|
|
const gchar *action_name)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple), NULL);
|
|
|
|
|
|
|
|
return g_hash_table_lookup (simple->priv->table, action_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_simple_action_group_insert:
|
|
|
|
* @simple: a #GSimpleActionGroup
|
|
|
|
* @action: a #GAction
|
|
|
|
*
|
|
|
|
* Adds an action to the action group.
|
|
|
|
*
|
|
|
|
* If the action group already contains an action with the same name as
|
|
|
|
* @action then the old action is dropped from the group.
|
|
|
|
*
|
|
|
|
* The action group takes its own reference on @action.
|
|
|
|
*
|
2010-10-11 21:57:09 +02:00
|
|
|
* Since: 2.28
|
2010-08-18 08:15:09 +02:00
|
|
|
**/
|
|
|
|
void
|
|
|
|
g_simple_action_group_insert (GSimpleActionGroup *simple,
|
|
|
|
GAction *action)
|
|
|
|
{
|
|
|
|
const gchar *action_name;
|
|
|
|
GAction *old_action;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
|
|
|
|
g_return_if_fail (G_IS_ACTION (action));
|
|
|
|
|
|
|
|
action_name = g_action_get_name (action);
|
|
|
|
old_action = g_hash_table_lookup (simple->priv->table, action_name);
|
|
|
|
|
|
|
|
if (old_action != action)
|
|
|
|
{
|
|
|
|
if (old_action != NULL)
|
|
|
|
{
|
|
|
|
g_action_group_action_removed (G_ACTION_GROUP (simple),
|
|
|
|
action_name);
|
|
|
|
g_simple_action_group_disconnect (NULL, old_action, simple);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_signal_connect (action, "notify::enabled",
|
2010-08-30 17:31:06 +02:00
|
|
|
G_CALLBACK (action_enabled_notify), simple);
|
2010-08-18 08:15:09 +02:00
|
|
|
|
|
|
|
if (g_action_get_state_type (action) != NULL)
|
|
|
|
g_signal_connect (action, "notify::state",
|
2010-08-30 17:31:06 +02:00
|
|
|
G_CALLBACK (action_state_notify), simple);
|
2010-08-18 08:15:09 +02:00
|
|
|
|
|
|
|
g_hash_table_insert (simple->priv->table,
|
|
|
|
g_strdup (action_name),
|
|
|
|
g_object_ref (action));
|
|
|
|
|
|
|
|
g_action_group_action_added (G_ACTION_GROUP (simple), action_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_simple_action_group_remove:
|
|
|
|
* @simple: a #GSimpleActionGroup
|
|
|
|
* @action_name: the name of the action
|
|
|
|
*
|
|
|
|
* Removes the named action from the action group.
|
|
|
|
*
|
|
|
|
* If no action of this name is in the group then nothing happens.
|
|
|
|
*
|
2010-10-11 21:57:09 +02:00
|
|
|
* Since: 2.28
|
2010-08-18 08:15:09 +02:00
|
|
|
**/
|
|
|
|
void
|
|
|
|
g_simple_action_group_remove (GSimpleActionGroup *simple,
|
|
|
|
const gchar *action_name)
|
|
|
|
{
|
|
|
|
GAction *action;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
|
|
|
|
|
|
|
|
action = g_hash_table_lookup (simple->priv->table, action_name);
|
|
|
|
|
|
|
|
if (action != NULL)
|
|
|
|
{
|
|
|
|
g_action_group_action_removed (G_ACTION_GROUP (simple), action_name);
|
|
|
|
g_simple_action_group_disconnect (NULL, action, simple);
|
|
|
|
g_hash_table_remove (simple->priv->table, action_name);
|
|
|
|
}
|
|
|
|
}
|
2011-06-29 14:00:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GActionEntry:
|
|
|
|
* @name: the name of the action
|
|
|
|
* @activate: the callback to connect to the "activate" signal of the
|
|
|
|
* action
|
|
|
|
* @parameter_type: the type of the parameter that must be passed to the
|
|
|
|
* activate function for this action, given as a single
|
|
|
|
* GVariant type string (or %NULL for no parameter)
|
|
|
|
* @state: the initial state for this action, given in GVariant text
|
|
|
|
* format. The state is parsed with no extra type information,
|
|
|
|
* so type tags must be added to the string if they are
|
|
|
|
* necessary.
|
2011-06-29 17:04:09 +02:00
|
|
|
* @change_state: the callback to connect to the "change-state" signal
|
|
|
|
* of the action
|
2011-06-29 14:00:30 +02:00
|
|
|
*
|
|
|
|
* This struct defines a single action. It is for use with
|
|
|
|
* g_simple_action_group_add_entries().
|
|
|
|
*
|
|
|
|
* The order of the items in the structure are intended to reflect
|
|
|
|
* frequency of use. It is permissible to use an incomplete initialiser
|
|
|
|
* in order to leave some of the later values as %NULL. All values
|
|
|
|
* after @name are optional. Additional optional fields may be added in
|
|
|
|
* the future.
|
2011-06-29 14:55:59 +02:00
|
|
|
*
|
|
|
|
* See g_simple_action_group_add_entries() for an example.
|
2011-06-29 14:00:30 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_simple_action_group_add_entries:
|
|
|
|
* @simple: a #GSimpleActionGroup
|
|
|
|
* @entries: a pointer to the first item in an array of #GActionEntry
|
|
|
|
* structs
|
|
|
|
* @n_entries: the length of @entries, or -1
|
|
|
|
* @user_data: the user data for signal connections
|
|
|
|
*
|
|
|
|
* A convenience function for creating multiple #GSimpleAction instances
|
|
|
|
* and adding them to the action group.
|
|
|
|
*
|
|
|
|
* Each action is constructed as per one #GActionEntry.
|
|
|
|
*
|
2011-06-29 14:55:59 +02:00
|
|
|
* <example>
|
|
|
|
* <title>Using g_simple_action_group_add_entries()</title>
|
|
|
|
* <programlisting>
|
|
|
|
* static void
|
|
|
|
* activate_quit (GSimpleAction *simple,
|
|
|
|
* GVariant *parameter,
|
|
|
|
* gpointer user_data)
|
|
|
|
* {
|
|
|
|
* exit (0);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* static void
|
|
|
|
* activate_print_string (GSimpleAction *simple,
|
|
|
|
* GVariant *parameter,
|
|
|
|
* gpointer user_data)
|
|
|
|
* {
|
|
|
|
* g_print ("%s\n", g_variant_get_string (parameter, NULL));
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* static GActionGroup *
|
|
|
|
* create_action_group (void)
|
|
|
|
* {
|
|
|
|
* const GActionEntry entries[] = {
|
|
|
|
* { "quit", activate_quit },
|
|
|
|
* { "print-string", activate_print_string, "s" }
|
|
|
|
* };
|
|
|
|
* GSimpleActionGroup *group;
|
|
|
|
*
|
|
|
|
* group = g_simple_action_group_new ();
|
|
|
|
* g_simple_action_group_add_entries (group, entries, G_N_ELEMENTS (entries), NULL);
|
|
|
|
*
|
|
|
|
* return G_ACTION_GROUP (group);
|
|
|
|
* }
|
|
|
|
* </programlisting>
|
|
|
|
* </example>
|
|
|
|
*
|
2011-06-29 14:00:30 +02:00
|
|
|
* Since: 2.30
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
g_simple_action_group_add_entries (GSimpleActionGroup *simple,
|
|
|
|
const GActionEntry *entries,
|
|
|
|
gint n_entries,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
|
|
|
|
g_return_if_fail (entries != NULL || n_entries == 0);
|
|
|
|
|
|
|
|
for (i = 0; n_entries == -1 ? entries[i].name != NULL : i < n_entries; i++)
|
|
|
|
{
|
|
|
|
const GActionEntry *entry = &entries[i];
|
|
|
|
const GVariantType *parameter_type;
|
|
|
|
GSimpleAction *action;
|
|
|
|
|
|
|
|
if (entry->parameter_type)
|
|
|
|
{
|
|
|
|
if (!g_variant_type_string_is_valid (entry->parameter_type))
|
|
|
|
{
|
|
|
|
g_critical ("g_simple_action_group_add_entries: the type "
|
|
|
|
"string '%s' given as the parameter type for "
|
|
|
|
"action '%s' is not a valid GVariant type "
|
|
|
|
"string. This action will not be added.",
|
|
|
|
entry->parameter_type, entry->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parameter_type = G_VARIANT_TYPE (entry->parameter_type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
parameter_type = NULL;
|
|
|
|
|
|
|
|
if (entry->state)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
GVariant *state;
|
|
|
|
|
|
|
|
state = g_variant_parse (NULL, entry->state, NULL, NULL, &error);
|
|
|
|
if (state == NULL)
|
|
|
|
{
|
|
|
|
g_critical ("g_simple_action_group_add_entries: GVariant could "
|
|
|
|
"not parse the state value given for action '%s' "
|
|
|
|
"('%s'): %s. This action will not be added.",
|
|
|
|
entry->name, entry->state, error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
action = g_simple_action_new_stateful (entry->name,
|
|
|
|
parameter_type,
|
|
|
|
state);
|
|
|
|
|
|
|
|
g_variant_unref (state);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
action = g_simple_action_new (entry->name,
|
|
|
|
parameter_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->activate != NULL)
|
|
|
|
g_signal_connect (action, "activate",
|
|
|
|
G_CALLBACK (entry->activate), user_data);
|
|
|
|
|
2011-06-29 17:04:09 +02:00
|
|
|
if (entry->change_state != NULL)
|
|
|
|
g_signal_connect (action, "change-state",
|
|
|
|
G_CALLBACK (entry->change_state), user_data);
|
|
|
|
|
2011-06-29 14:00:30 +02:00
|
|
|
g_simple_action_group_insert (simple, G_ACTION (action));
|
|
|
|
g_object_unref (action);
|
|
|
|
}
|
|
|
|
}
|