add GActionMap interface

This is an interface to represent GSimpleActionGroup-like objects (ie:
those GActionGroups that operate by containing a number of named GAction
instances).
This commit is contained in:
Ryan Lortie 2011-11-30 11:36:08 -05:00
parent 02b001f8f6
commit 76527e5cd5
7 changed files with 392 additions and 194 deletions

View File

@ -126,6 +126,7 @@ endif
application_headers = \
gactiongroup.h \
gactionmap.h \
gsimpleactiongroup.h \
gaction.h \
gsimpleaction.h \
@ -142,6 +143,7 @@ application_headers = \
application_sources = \
gactiongroup.c \
gactionmap.c \
gsimpleactiongroup.c \
gaction.c \
gsimpleaction.c \

211
gio/gactionmap.c Normal file
View File

@ -0,0 +1,211 @@
/*
* 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 "config.h"
#include "gsimpleaction.h"
#include "gactiongroup.h"
#include "gactionmap.h"
#include "gaction.h"
G_DEFINE_INTERFACE (GActionMap, g_action_map, G_TYPE_ACTION_GROUP)
static void
g_action_map_default_init (GActionMapInterface *iface)
{
}
GAction *
g_action_map_lookup_action (GActionMap *action_map,
const gchar *action_name)
{
return G_ACTION_MAP_GET_IFACE (action_map)
->lookup_action (action_map, action_name);
}
void
g_action_map_add_action (GActionMap *action_map,
GAction *action)
{
return G_ACTION_MAP_GET_IFACE (action_map)
->add_action (action_map, action);
}
void
g_action_map_remove_action (GActionMap *action_map,
const gchar *action_name)
{
return G_ACTION_MAP_GET_IFACE (action_map)
->remove_action (action_map, action_name);
}
/**
* 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.
* @change_state: the callback to connect to the "change-state" signal
* of the action
*
* This struct defines a single action. It is for use with
* g_action_map_add_action_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.
*
* See g_action_map_add_action_entries() for an example.
**/
/**
* g_action_map_add_action_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 a #GActionMap.
*
* Each action is constructed as per one #GActionEntry.
*
* <example>
* <title>Using g_action_map_add_action_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_action_map_add_action_entries (G_ACTION_MAP (group), entries, G_N_ELEMENTS (entries), NULL);
*
* return G_ACTION_GROUP (group);
* }
* </programlisting>
* </example>
*
* Since: 2.30
**/
void
g_action_map_add_action_entries (GActionMap *action_map,
const GActionEntry *entries,
gint n_entries,
gpointer user_data)
{
gint i;
g_return_if_fail (G_IS_ACTION_MAP (action_map));
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);
if (entry->change_state != NULL)
g_signal_connect (action, "change-state",
G_CALLBACK (entry->change_state), user_data);
g_action_map_add_action (action_map, G_ACTION (action));
g_object_unref (action);
}
}

94
gio/gactionmap.h Normal file
View File

@ -0,0 +1,94 @@
/*
* 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>
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#ifndef __G_ACTION_MAP_H__
#define __G_ACTION_MAP_H__
#include <gio/giotypes.h>
G_BEGIN_DECLS
#define G_TYPE_ACTION_MAP (g_action_map_get_type ())
#define G_ACTION_MAP(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
G_TYPE_ACTION_MAP, GActionMap))
#define G_IS_ACTION_MAP(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
G_TYPE_ACTION_MAP))
#define G_ACTION_MAP_GET_IFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), \
G_TYPE_ACTION_MAP, GActionMapInterface))
typedef struct _GActionMapInterface GActionMapInterface;
typedef struct _GActionEntry GActionEntry;
struct _GActionMapInterface
{
GTypeInterface g_iface;
GAction * (* lookup_action) (GActionMap *action_map,
const gchar *action_name);
void (* add_action) (GActionMap *action_map,
GAction *action);
void (* remove_action) (GActionMap *action_map,
const gchar *action_name);
};
struct _GActionEntry
{
const gchar *name;
void (* activate) (GSimpleAction *action,
GVariant *parameter,
gpointer user_data);
const gchar *parameter_type;
const gchar *state;
void (* change_state) (GSimpleAction *action,
GVariant *value,
gpointer user_data);
/*< private >*/
gsize padding[3];
};
GType g_action_map_get_type (void) G_GNUC_CONST;
GAction * g_action_map_lookup_action (GActionMap *action_map,
const gchar *action_name);
void g_action_map_add_action (GActionMap *action_map,
GAction *action);
void g_action_map_remove_action (GActionMap *action_map,
const gchar *action_name);
void g_action_map_add_action_entries (GActionMap *action_map,
const GActionEntry *entries,
gint n_entries,
gpointer user_data);
G_END_DECLS
#endif /* __G_ACTION_MAP_H__ */

View File

@ -1412,6 +1412,11 @@ g_action_group_get_type
g_action_group_has_action
g_action_group_list_actions
g_action_group_query_action
g_action_map_add_action
g_action_map_add_action_entries
g_action_map_get_type
g_action_map_lookup_action
g_action_map_remove_action
g_action_group_change_action_state
g_action_group_dbus_export_start
g_action_group_dbus_export_query

View File

@ -49,6 +49,7 @@ typedef struct _GZlibDecompressor GZlibDecompressor;
typedef struct _GSimpleActionGroup GSimpleActionGroup;
typedef struct _GDBusActionGroup GDBusActionGroup;
typedef struct _GActionMap GActionMap;
typedef struct _GActionGroup GActionGroup;
typedef struct _GSimpleAction GSimpleAction;
typedef struct _GAction GAction;

View File

@ -22,6 +22,7 @@
#include "gsimpleactiongroup.h"
#include "gsimpleaction.h"
#include "gactionmap.h"
#include "gaction.h"
/**
@ -39,10 +40,13 @@ struct _GSimpleActionGroupPrivate
};
static void g_simple_action_group_iface_init (GActionGroupInterface *);
static void g_simple_action_group_map_iface_init (GActionMapInterface *);
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))
g_simple_action_group_iface_init);
G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP,
g_simple_action_group_map_iface_init))
static gchar **
g_simple_action_group_list_actions (GActionGroup *group)
@ -167,6 +171,67 @@ g_simple_action_group_disconnect (gpointer key,
user_data);
}
static GAction *
g_simple_action_group_lookup_action (GActionMap *action_map,
const gchar *action_name)
{
GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
return g_hash_table_lookup (simple->priv->table, action_name);
}
static void
g_simple_action_group_add_action (GActionMap *action_map,
GAction *action)
{
GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
const gchar *action_name;
GAction *old_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",
G_CALLBACK (action_enabled_notify), simple);
if (g_action_get_state_type (action) != NULL)
g_signal_connect (action, "notify::state",
G_CALLBACK (action_state_notify), simple);
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);
}
}
static void
g_simple_action_group_remove_action (GActionMap *action_map,
const gchar *action_name)
{
GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
GAction *action;
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);
}
}
static void
g_simple_action_group_finalize (GObject *object)
{
@ -210,6 +275,14 @@ g_simple_action_group_iface_init (GActionGroupInterface *iface)
iface->activate_action = g_simple_action_group_activate;
}
static void
g_simple_action_group_map_iface_init (GActionMapInterface *iface)
{
iface->add_action = g_simple_action_group_add_action;
iface->remove_action = g_simple_action_group_remove_action;
iface->lookup_action = g_simple_action_group_lookup_action;
}
/**
* g_simple_action_group_new:
*
@ -244,7 +317,7 @@ g_simple_action_group_lookup (GSimpleActionGroup *simple,
{
g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple), NULL);
return g_hash_table_lookup (simple->priv->table, action_name);
return g_action_map_lookup_action (G_ACTION_MAP (simple), action_name);
}
/**
@ -265,37 +338,9 @@ 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",
G_CALLBACK (action_enabled_notify), simple);
if (g_action_get_state_type (action) != NULL)
g_signal_connect (action, "notify::state",
G_CALLBACK (action_state_notify), simple);
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_action_map_add_action (G_ACTION_MAP (simple), action);
}
/**
@ -313,46 +358,11 @@ 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);
}
g_action_map_remove_action (G_ACTION_MAP (simple), action_name);
}
/**
* 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.
* @change_state: the callback to connect to the "change-state" signal
* of the action
*
* 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.
*
* See g_simple_action_group_add_entries() for an example.
**/
/**
* g_simple_action_group_add_entries:
@ -365,44 +375,6 @@ g_simple_action_group_remove (GSimpleActionGroup *simple,
* A convenience function for creating multiple #GSimpleAction instances
* and adding them to the action group.
*
* Each action is constructed as per one #GActionEntry.
*
* <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>
*
* Since: 2.30
**/
void
@ -411,71 +383,5 @@ g_simple_action_group_add_entries (GSimpleActionGroup *simple,
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);
if (entry->change_state != NULL)
g_signal_connect (action, "change-state",
G_CALLBACK (entry->change_state), user_data);
g_simple_action_group_insert (simple, G_ACTION (action));
g_object_unref (action);
}
g_action_map_add_action_entries (G_ACTION_MAP (simple), entries, n_entries, user_data);
}

View File

@ -27,6 +27,7 @@
#define __G_SIMPLE_ACTION_GROUP_H__
#include "gactiongroup.h"
#include "gactionmap.h"
G_BEGIN_DECLS
@ -45,8 +46,6 @@ G_BEGIN_DECLS
typedef struct _GSimpleActionGroupPrivate GSimpleActionGroupPrivate;
typedef struct _GSimpleActionGroupClass GSimpleActionGroupClass;
typedef struct _GActionEntry GActionEntry;
/**
* GSimpleActionGroup:
*
@ -84,26 +83,6 @@ void g_simple_action_group_insert (GSimple
void g_simple_action_group_remove (GSimpleActionGroup *simple,
const gchar *action_name);
struct _GActionEntry
{
const gchar *name;
void (* activate) (GSimpleAction *action,
GVariant *parameter,
gpointer user_data);
const gchar *parameter_type;
const gchar *state;
void (* change_state) (GSimpleAction *action,
GVariant *value,
gpointer user_data);
/*< private >*/
gsize padding[3];
};
void g_simple_action_group_add_entries (GSimpleActionGroup *simple,
const GActionEntry *entries,
gint n_entries,