gsimpleactiongroup.c 11.6 KB
Newer Older
1 2 3
/*
 * Copyright © 2010 Codethink Limited
 *
4 5 6 7
 * This library 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.1 of the License, or (at your option) any later version.
8 9 10 11 12 13 14
 *
 * 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
15
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 17 18 19
 *
 * Authors: Ryan Lortie <desrt@desrt.ca>
 */

20 21
#include "config.h"

22
#include "gsimpleactiongroup.h"
23 24

#include "gsimpleaction.h"
25
#include "gactionmap.h"
26 27 28 29 30
#include "gaction.h"

/**
 * SECTION:gsimpleactiongroup
 * @title: GSimpleActionGroup
Matthias Clasen's avatar
Matthias Clasen committed
31
 * @short_description: A simple GActionGroup implementation
32
 * @include: gio/gio.h
33 34
 *
 * #GSimpleActionGroup is a hash table filled with #GAction objects,
35
 * implementing the #GActionGroup and #GActionMap interfaces.
36 37 38 39 40 41 42
 **/

struct _GSimpleActionGroupPrivate
{
  GHashTable *table;  /* string -> GAction */
};

43
static void g_simple_action_group_iface_init (GActionGroupInterface *);
44
static void g_simple_action_group_map_iface_init (GActionMapInterface *);
45 46
G_DEFINE_TYPE_WITH_CODE (GSimpleActionGroup,
  g_simple_action_group, G_TYPE_OBJECT,
47
  G_ADD_PRIVATE (GSimpleActionGroup)
48
  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
49
                         g_simple_action_group_iface_init);
50
  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP,
51
                         g_simple_action_group_map_iface_init))
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

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
75 76 77 78 79 80 81
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)
82 83 84 85 86 87 88 89 90
{
  GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
  GAction *action;

  action = g_hash_table_lookup (simple->priv->table, action_name);

  if (action == NULL)
    return FALSE;

91 92
  if (enabled)
    *enabled = g_action_get_enabled (action);
93

94 95
  if (parameter_type)
    *parameter_type = g_action_get_parameter_type (action);
96

97 98
  if (state_type)
    *state_type = g_action_get_state_type (action);
99

100 101 102 103 104
  if (state_hint)
    *state_hint = g_action_get_state_hint (action);

  if (state)
    *state = g_action_get_state (action);
105

106
  return TRUE;
107 108 109
}

static void
110 111 112
g_simple_action_group_change_state (GActionGroup *group,
                                    const gchar  *action_name,
                                    GVariant     *value)
113 114 115 116 117 118 119 120 121
{
  GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
  GAction *action;

  action = g_hash_table_lookup (simple->priv->table, action_name);

  if (action == NULL)
    return;

122
  g_action_change_state (action, value);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
}

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;

138
  g_action_activate (action, parameter);
139 140 141
}

static void
142 143 144
action_enabled_notify (GAction     *action,
                       GParamSpec  *pspec,
                       gpointer     user_data)
145 146 147
{
  g_action_group_action_enabled_changed (user_data,
                                         g_action_get_name (action),
148
                                         g_action_get_enabled (action));
149 150 151
}

static void
152 153 154
action_state_notify (GAction    *action,
                     GParamSpec *pspec,
                     gpointer    user_data)
155
{
156 157 158
  GVariant *value;

  value = g_action_get_state (action);
159 160 161
  g_action_group_action_state_changed (user_data,
                                       g_action_get_name (action),
                                       value);
162
  g_variant_unref (value);
163 164 165 166 167 168 169
}

static void
g_simple_action_group_disconnect (gpointer key,
                                  gpointer value,
                                  gpointer user_data)
{
170
  g_signal_handlers_disconnect_by_func (value, action_enabled_notify,
171
                                        user_data);
172
  g_signal_handlers_disconnect_by_func (value, action_state_notify,
173 174 175
                                        user_data);
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
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);
194 195 196 197 198 199 200
  if (action_name == NULL)
    {
      g_critical ("The supplied action has no name. You must set the "
                  "GAction:name property when creating an action.");
      return;
    }

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  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);
    }
}

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
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)
{
261
  simple->priv = g_simple_action_group_get_instance_private (simple);
262 263 264 265 266 267 268 269 270 271 272 273
  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;
}

274 275 276 277
static void
g_simple_action_group_iface_init (GActionGroupInterface *iface)
{
  iface->list_actions = g_simple_action_group_list_actions;
278
  iface->query_action = g_simple_action_group_query_action;
279
  iface->change_action_state = g_simple_action_group_change_state;
280
  iface->activate_action = g_simple_action_group_activate;
281 282
}

283 284 285 286 287 288 289 290
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;
}

291 292 293 294 295 296 297
/**
 * g_simple_action_group_new:
 *
 * Creates a new, empty, #GSimpleActionGroup.
 *
 * Returns: a new #GSimpleActionGroup
 *
298
 * Since: 2.28
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
 **/
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
 *
317
 * Since: 2.28
318 319
 *
 * Deprecated: 2.38: Use g_action_map_lookup_action()
320
 */
321 322 323 324 325 326
GAction *
g_simple_action_group_lookup (GSimpleActionGroup *simple,
                              const gchar        *action_name)
{
  g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple), NULL);

327
  return g_action_map_lookup_action (G_ACTION_MAP (simple), action_name);
328 329 330 331 332 333 334 335 336 337 338 339 340 341
}

/**
 * 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.
 *
342
 * Since: 2.28
343 344
 *
 * Deprecated: 2.38: Use g_action_map_add_action()
345 346 347 348 349 350 351
 **/
void
g_simple_action_group_insert (GSimpleActionGroup *simple,
                              GAction            *action)
{
  g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));

352
  g_action_map_add_action (G_ACTION_MAP (simple), action);
353 354 355 356 357 358 359 360 361 362 363
}

/**
 * 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.
 *
364
 * Since: 2.28
365 366
 *
 * Deprecated: 2.38: Use g_action_map_remove_action()
367 368 369 370 371 372 373
 **/
void
g_simple_action_group_remove (GSimpleActionGroup *simple,
                              const gchar        *action_name)
{
  g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));

374
  g_action_map_remove_action (G_ACTION_MAP (simple), action_name);
375
}
376 377 378 379 380


/**
 * g_simple_action_group_add_entries:
 * @simple: a #GSimpleActionGroup
381 382
 * @entries: (array length=n_entries): a pointer to the first item in
 *           an array of #GActionEntry structs
383 384 385 386 387 388 389
 * @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.
 *
 * Since: 2.30
390 391
 *
 * Deprecated: 2.38: Use g_action_map_add_action_entries()
392 393 394 395 396 397 398
 **/
void
g_simple_action_group_add_entries (GSimpleActionGroup *simple,
                                   const GActionEntry *entries,
                                   gint                n_entries,
                                   gpointer            user_data)
{
399
  g_action_map_add_action_entries (G_ACTION_MAP (simple), entries, n_entries, user_data);
400
}