2 * Copyright © 2011 Canonical Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at 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 Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
22 #include "gactionmuxer.h"
24 #include "gactionobservable.h"
25 #include "gactionobserver.h"
30 * SECTION:gactionmuxer
31 * @short_description: Aggregate and monitor several action groups
33 * #GActionMuxer is a #GActionGroup and #GActionObservable that is
34 * capable of containing other #GActionGroup instances.
36 * The typical use is aggregating all of the actions applicable to a
37 * particular context into a single action group, with namespacing.
39 * Consider the case of two action groups -- one containing actions
40 * applicable to an entire application (such as 'quit') and one
41 * containing actions applicable to a particular window in the
42 * application (such as 'fullscreen').
44 * In this case, each of these action groups could be added to a
45 * #GActionMuxer with the prefixes "app" and "win", respectively. This
46 * would expose the actions as "app.quit" and "win.fullscreen" on the
47 * #GActionGroup interface presented by the #GActionMuxer.
49 * Activations and state change requests on the #GActionMuxer are wired
50 * through to the underlying action group in the expected way.
52 * This class is typically only used at the site of "consumption" of
53 * actions (eg: when displaying a menu that contains many actions on
57 static void g_action_muxer_group_iface_init (GActionGroupInterface *iface);
58 static void g_action_muxer_observable_iface_init (GActionObservableInterface *iface);
60 typedef GObjectClass GActionMuxerClass;
64 GObject parent_instance;
70 G_DEFINE_TYPE_WITH_CODE (GActionMuxer, g_action_muxer, G_TYPE_OBJECT,
71 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_action_muxer_group_iface_init)
72 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_OBSERVABLE, g_action_muxer_observable_iface_init))
86 gulong handler_ids[4];
90 g_action_muxer_list_actions (GActionGroup *action_group)
92 GActionMuxer *muxer = G_ACTION_MUXER (action_group);
94 return (gchar **) muxer->groups;
98 g_action_muxer_find_group (GActionMuxer *muxer,
105 dot = strchr (*name, '.');
110 prefix = g_strndup (*name, dot - *name);
111 group = g_hash_table_lookup (muxer->groups, prefix);
120 g_action_muxer_lookup_action (GActionMuxer *muxer,
122 const gchar *action_name,
127 *fullname = g_strconcat (prefix, ".", action_name, NULL);
128 action = g_hash_table_lookup (muxer->actions, *fullname);
134 g_action_muxer_action_enabled_changed (GActionGroup *action_group,
135 const gchar *action_name,
139 Group *group = user_data;
144 action = g_action_muxer_lookup_action (group->muxer, group->prefix, action_name, &fullname);
145 for (node = action ? action->watchers : NULL; node; node = node->next)
146 g_action_observer_action_enabled_changed (node->data, G_ACTION_OBSERVABLE (group->muxer), fullname, enabled);
147 g_action_group_action_enabled_changed (G_ACTION_GROUP (group->muxer), fullname, enabled);
152 g_action_muxer_action_state_changed (GActionGroup *action_group,
153 const gchar *action_name,
157 Group *group = user_data;
162 action = g_action_muxer_lookup_action (group->muxer, group->prefix, action_name, &fullname);
163 for (node = action ? action->watchers : NULL; node; node = node->next)
164 g_action_observer_action_state_changed (node->data, G_ACTION_OBSERVABLE (group->muxer), fullname, state);
165 g_action_group_action_state_changed (G_ACTION_GROUP (group->muxer), fullname, state);
170 g_action_muxer_action_added (GActionGroup *action_group,
171 const gchar *action_name,
174 const GVariantType *parameter_type;
175 Group *group = user_data;
179 if (g_action_group_query_action (group->group, action_name, &enabled, ¶meter_type, NULL, NULL, &state))
185 action = g_action_muxer_lookup_action (group->muxer, group->prefix, action_name, &fullname);
187 for (node = action ? action->watchers : NULL; node; node = node->next)
188 g_action_observer_action_added (node->data,
189 G_ACTION_OBSERVABLE (group->muxer),
190 fullname, parameter_type, enabled, state);
192 g_action_group_action_added (G_ACTION_GROUP (group->muxer), fullname);
195 g_variant_unref (state);
202 g_action_muxer_action_removed (GActionGroup *action_group,
203 const gchar *action_name,
206 Group *group = user_data;
211 action = g_action_muxer_lookup_action (group->muxer, group->prefix, action_name, &fullname);
212 for (node = action ? action->watchers : NULL; node; node = node->next)
213 g_action_observer_action_removed (node->data, G_ACTION_OBSERVABLE (group->muxer), fullname);
214 g_action_group_action_removed (G_ACTION_GROUP (group->muxer), fullname);
219 g_action_muxer_query_action (GActionGroup *action_group,
220 const gchar *action_name,
222 const GVariantType **parameter_type,
223 const GVariantType **state_type,
224 GVariant **state_hint,
227 GActionMuxer *muxer = G_ACTION_MUXER (action_group);
230 group = g_action_muxer_find_group (muxer, &action_name);
235 return g_action_group_query_action (group->group, action_name, enabled,
236 parameter_type, state_type, state_hint, state);
240 g_action_muxer_activate_action (GActionGroup *action_group,
241 const gchar *action_name,
244 GActionMuxer *muxer = G_ACTION_MUXER (action_group);
247 group = g_action_muxer_find_group (muxer, &action_name);
250 g_action_group_activate_action (group->group, action_name, parameter);
254 g_action_muxer_change_action_state (GActionGroup *action_group,
255 const gchar *action_name,
258 GActionMuxer *muxer = G_ACTION_MUXER (action_group);
261 group = g_action_muxer_find_group (muxer, &action_name);
264 g_action_group_change_action_state (group->group, action_name, state);
268 g_action_muxer_unregister_internal (Action *action,
271 GActionMuxer *muxer = action->muxer;
274 for (ptr = &action->watchers; *ptr; ptr = &(*ptr)->next)
275 if ((*ptr)->data == observer)
277 *ptr = g_slist_remove (*ptr, observer);
279 if (action->watchers == NULL)
281 g_hash_table_remove (muxer->actions, action->fullname);
282 g_free (action->fullname);
284 g_slice_free (Action, action);
286 g_object_unref (muxer);
294 g_action_muxer_weak_notify (gpointer data,
295 GObject *where_the_object_was)
297 Action *action = data;
299 g_action_muxer_unregister_internal (action, where_the_object_was);
303 g_action_muxer_register_observer (GActionObservable *observable,
305 GActionObserver *observer)
307 GActionMuxer *muxer = G_ACTION_MUXER (observable);
310 action = g_hash_table_lookup (muxer->actions, name);
314 action = g_slice_new (Action);
315 action->muxer = g_object_ref (muxer);
316 action->fullname = g_strdup (name);
317 action->watchers = NULL;
319 g_hash_table_insert (muxer->actions, action->fullname, action);
322 action->watchers = g_slist_prepend (action->watchers, observer);
323 g_object_weak_ref (G_OBJECT (observer), g_action_muxer_weak_notify, action);
327 g_action_muxer_unregister_observer (GActionObservable *observable,
329 GActionObserver *observer)
331 GActionMuxer *muxer = G_ACTION_MUXER (observable);
334 action = g_hash_table_lookup (muxer->actions, name);
335 g_object_weak_unref (G_OBJECT (observer), g_action_muxer_weak_notify, action);
336 g_action_muxer_unregister_internal (action, observer);
340 g_action_muxer_free_group (gpointer data)
344 g_object_unref (group->group);
345 g_free (group->prefix);
347 g_slice_free (Group, group);
351 g_action_muxer_finalize (GObject *object)
353 GActionMuxer *muxer = G_ACTION_MUXER (object);
355 g_assert_cmpint (g_hash_table_size (muxer->actions), ==, 0);
356 g_hash_table_unref (muxer->actions);
357 g_hash_table_unref (muxer->groups);
359 G_OBJECT_CLASS (g_action_muxer_parent_class)
364 g_action_muxer_init (GActionMuxer *muxer)
366 muxer->actions = g_hash_table_new (g_str_hash, g_str_equal);
367 muxer->groups = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_action_muxer_free_group);
371 g_action_muxer_observable_iface_init (GActionObservableInterface *iface)
373 iface->register_observer = g_action_muxer_register_observer;
374 iface->unregister_observer = g_action_muxer_unregister_observer;
378 g_action_muxer_group_iface_init (GActionGroupInterface *iface)
380 iface->list_actions = g_action_muxer_list_actions;
381 iface->query_action = g_action_muxer_query_action;
382 iface->activate_action = g_action_muxer_activate_action;
383 iface->change_action_state = g_action_muxer_change_action_state;
387 g_action_muxer_class_init (GObjectClass *class)
389 class->finalize = g_action_muxer_finalize;
393 * g_action_muxer_insert:
394 * @muxer: a #GActionMuxer
395 * @prefix: the prefix string for the action group
396 * @action_group: a #GActionGroup
398 * Adds the actions in @action_group to the list of actions provided by
399 * @muxer. @prefix is prefixed to each action name, such that for each
400 * action <varname>x</varname> in @action_group, there is an equivalent
401 * action @prefix<literal>.</literal><varname>x</varname> in @muxer.
403 * For example, if @prefix is "<literal>app</literal>" and @action_group
404 * contains an action called "<literal>quit</literal>", then @muxer will
405 * now contain an action called "<literal>app.quit</literal>".
407 * If any #GActionObservers are registered for actions in the group,
408 * "action_added" notifications will be emitted, as appropriate.
410 * @prefix must not contain a dot ('.').
413 g_action_muxer_insert (GActionMuxer *muxer,
415 GActionGroup *action_group)
421 /* TODO: diff instead of ripout and replace */
422 g_action_muxer_remove (muxer, prefix);
424 group = g_slice_new (Group);
425 group->muxer = muxer;
426 group->group = g_object_ref (action_group);
427 group->prefix = g_strdup (prefix);
429 g_hash_table_insert (muxer->groups, group->prefix, group);
431 actions = g_action_group_list_actions (group->group);
432 for (i = 0; actions[i]; i++)
433 g_action_muxer_action_added (group->group, actions[i], group);
434 g_strfreev (actions);
436 group->handler_ids[0] = g_signal_connect (group->group, "action-added",
437 G_CALLBACK (g_action_muxer_action_added), group);
438 group->handler_ids[1] = g_signal_connect (group->group, "action-removed",
439 G_CALLBACK (g_action_muxer_action_removed), group);
440 group->handler_ids[2] = g_signal_connect (group->group, "action-enabled-changed",
441 G_CALLBACK (g_action_muxer_action_enabled_changed), group);
442 group->handler_ids[3] = g_signal_connect (group->group, "action-state-changed",
443 G_CALLBACK (g_action_muxer_action_state_changed), group);
447 * g_action_muxer_remove:
448 * @muxer: a #GActionMuxer
449 * @prefix: the prefix of the action group to remove
451 * Removes a #GActionGroup from the #GActionMuxer.
453 * If any #GActionObservers are registered for actions in the group,
454 * "action_removed" notifications will be emitted, as appropriate.
457 g_action_muxer_remove (GActionMuxer *muxer,
462 group = g_hash_table_lookup (muxer->groups, prefix);
469 g_hash_table_steal (muxer->groups, prefix);
471 actions = g_action_group_list_actions (group->group);
472 for (i = 0; actions[i]; i++)
473 g_action_muxer_action_removed (group->group, actions[i], group);
474 g_strfreev (actions);
476 /* 'for loop' or 'four loop'? */
477 for (i = 0; i < 4; i++)
478 g_signal_handler_disconnect (group->group, group->handler_ids[i]);
480 g_action_muxer_free_group (group);
485 * g_action_muxer_new:
487 * Creates a new #GActionMuxer.
490 g_action_muxer_new (void)
492 return g_object_new (G_TYPE_ACTION_MUXER, NULL);