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>
23 #include "gactiongroup.h"
28 * SECTION:gactiongroup
29 * @title: GActionGroup
30 * @short_description: A group of actions
32 * #GActionGroup represents a group of actions.
34 * Each action in the group has a unique name (which is a string). All
35 * method calls, except g_action_group_list_actions() take the name of
36 * an action as an argument.
38 * Action groups are often used together with a #GMenuModel that provides
39 * additional representation data for the actions.
41 * The #GActionGroup API is meant to be the 'public' API to the action
42 * group. The calls here are exactly the interaction that 'external
43 * forces' (eg: UI, incoming D-Bus messages, etc.) are supposed to have
44 * with actions. 'Internal' APIs (ie: ones meant only to be accessed by
45 * the action group implementation) are found on subclasses. This is
46 * why you will find - for example - g_action_group_get_action_enabled()
47 * but not an equivalent <function>set()</function> call.
49 * Signals are emitted on the action group in response to state changes
50 * on individual actions.
52 * Implementations of #GActionGroup should provide implementations for
53 * the virtual functions g_action_group_list_actions() and
54 * g_action_group_query_action(). The other virtual functions should
55 * not be implemented - their "wrappers" are actually implemented with
56 * calls to g_action_group_query_action().
59 G_DEFINE_INTERFACE (GActionGroup
, g_action_group
, G_TYPE_OBJECT
)
64 SIGNAL_ACTION_REMOVED
,
65 SIGNAL_ACTION_ENABLED_CHANGED
,
66 SIGNAL_ACTION_STATE_CHANGED
,
70 static guint g_action_group_signals
[NR_SIGNALS
];
73 g_action_group_real_has_action (GActionGroup
*action_group
,
74 const gchar
*action_name
)
76 return g_action_group_query_action (action_group
, action_name
, NULL
, NULL
, NULL
, NULL
, NULL
);
80 g_action_group_real_get_action_enabled (GActionGroup
*action_group
,
81 const gchar
*action_name
)
83 gboolean enabled
= FALSE
;
85 g_action_group_query_action (action_group
, action_name
, &enabled
, NULL
, NULL
, NULL
, NULL
);
90 static const GVariantType
*
91 g_action_group_real_get_action_parameter_type (GActionGroup
*action_group
,
92 const gchar
*action_name
)
94 const GVariantType
*type
= NULL
;
96 g_action_group_query_action (action_group
, action_name
, NULL
, &type
, NULL
, NULL
, NULL
);
101 static const GVariantType
*
102 g_action_group_real_get_action_state_type (GActionGroup
*action_group
,
103 const gchar
*action_name
)
105 const GVariantType
*type
= NULL
;
107 g_action_group_query_action (action_group
, action_name
, NULL
, NULL
, &type
, NULL
, NULL
);
113 g_action_group_real_get_action_state_hint (GActionGroup
*action_group
,
114 const gchar
*action_name
)
116 GVariant
*hint
= NULL
;
118 g_action_group_query_action (action_group
, action_name
, NULL
, NULL
, NULL
, &hint
, NULL
);
124 g_action_group_real_get_action_state (GActionGroup
*action_group
,
125 const gchar
*action_name
)
127 GVariant
*state
= NULL
;
129 g_action_group_query_action (action_group
, action_name
, NULL
, NULL
, NULL
, NULL
, &state
);
135 g_action_group_real_query_action (GActionGroup
*action_group
,
136 const gchar
*action_name
,
138 const GVariantType
**parameter_type
,
139 const GVariantType
**state_type
,
140 GVariant
**state_hint
,
143 GActionGroupInterface
*iface
= G_ACTION_GROUP_GET_IFACE (action_group
);
145 /* we expect implementations to override this method, but we also
146 * allow for implementations that existed before this method was
147 * introduced to override the individual accessors instead.
149 * detect the case that neither has happened and report it.
151 if G_UNLIKELY (iface
->has_action
== g_action_group_real_has_action
||
152 iface
->get_action_enabled
== g_action_group_real_get_action_enabled
||
153 iface
->get_action_parameter_type
== g_action_group_real_get_action_parameter_type
||
154 iface
->get_action_state_type
== g_action_group_real_get_action_state_type
||
155 iface
->get_action_state_hint
== g_action_group_real_get_action_state_hint
||
156 iface
->get_action_state
== g_action_group_real_get_action_state
)
158 g_critical ("Class '%s' implements GActionGroup interface without overriding "
159 "query_action() method -- bailing out to avoid infinite recursion.",
160 G_OBJECT_TYPE_NAME (action_group
));
164 if (!(* iface
->has_action
) (action_group
, action_name
))
168 *enabled
= (* iface
->get_action_enabled
) (action_group
, action_name
);
170 if (parameter_type
!= NULL
)
171 *parameter_type
= (* iface
->get_action_parameter_type
) (action_group
, action_name
);
173 if (state_type
!= NULL
)
174 *state_type
= (* iface
->get_action_state_type
) (action_group
, action_name
);
176 if (state_hint
!= NULL
)
177 *state_hint
= (* iface
->get_action_state_hint
) (action_group
, action_name
);
180 *state
= (* iface
->get_action_state
) (action_group
, action_name
);
186 g_action_group_default_init (GActionGroupInterface
*iface
)
188 iface
->has_action
= g_action_group_real_has_action
;
189 iface
->get_action_enabled
= g_action_group_real_get_action_enabled
;
190 iface
->get_action_parameter_type
= g_action_group_real_get_action_parameter_type
;
191 iface
->get_action_state_type
= g_action_group_real_get_action_state_type
;
192 iface
->get_action_state_hint
= g_action_group_real_get_action_state_hint
;
193 iface
->get_action_state
= g_action_group_real_get_action_state
;
194 iface
->query_action
= g_action_group_real_query_action
;
197 * GActionGroup::action-added:
198 * @action_group: the #GActionGroup that changed
199 * @action_name: the name of the action in @action_group
201 * Signals that a new action was just added to the group.
202 * This signal is emitted after the action has been added
203 * and is now visible.
207 g_action_group_signals
[SIGNAL_ACTION_ADDED
] =
208 g_signal_new (I_("action-added"),
210 G_SIGNAL_RUN_LAST
| G_SIGNAL_DETAILED
,
211 G_STRUCT_OFFSET (GActionGroupInterface
, action_added
),
213 g_cclosure_marshal_VOID__STRING
,
218 * GActionGroup::action-removed:
219 * @action_group: the #GActionGroup that changed
220 * @action_name: the name of the action in @action_group
222 * Signals that an action is just about to be removed from the group.
223 * This signal is emitted before the action is removed, so the action
224 * is still visible and can be queried from the signal handler.
228 g_action_group_signals
[SIGNAL_ACTION_REMOVED
] =
229 g_signal_new (I_("action-removed"),
231 G_SIGNAL_RUN_LAST
| G_SIGNAL_DETAILED
,
232 G_STRUCT_OFFSET (GActionGroupInterface
, action_removed
),
234 g_cclosure_marshal_VOID__STRING
,
240 * GActionGroup::action-enabled-changed:
241 * @action_group: the #GActionGroup that changed
242 * @action_name: the name of the action in @action_group
243 * @enabled: whether the action is enabled or not
245 * Signals that the enabled status of the named action has changed.
249 g_action_group_signals
[SIGNAL_ACTION_ENABLED_CHANGED
] =
250 g_signal_new (I_("action-enabled-changed"),
252 G_SIGNAL_RUN_LAST
| G_SIGNAL_DETAILED
,
253 G_STRUCT_OFFSET (GActionGroupInterface
,
254 action_enabled_changed
),
262 * GActionGroup::action-state-changed:
263 * @action_group: the #GActionGroup that changed
264 * @action_name: the name of the action in @action_group
265 * @value: the new value of the state
267 * Signals that the state of the named action has changed.
271 g_action_group_signals
[SIGNAL_ACTION_STATE_CHANGED
] =
272 g_signal_new (I_("action-state-changed"),
276 G_SIGNAL_MUST_COLLECT
,
277 G_STRUCT_OFFSET (GActionGroupInterface
,
278 action_state_changed
),
287 * g_action_group_list_actions:
288 * @action_group: a #GActionGroup
290 * Lists the actions contained within @action_group.
292 * The caller is responsible for freeing the list with g_strfreev() when
293 * it is no longer required.
295 * Returns: (transfer full): a %NULL-terminated array of the names of the
296 * actions in the groupb
301 g_action_group_list_actions (GActionGroup
*action_group
)
303 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), NULL
);
305 return G_ACTION_GROUP_GET_IFACE (action_group
)
306 ->list_actions (action_group
);
310 * g_action_group_has_action:
311 * @action_group: a #GActionGroup
312 * @action_name: the name of the action to check for
314 * Checks if the named action exists within @action_group.
316 * Returns: whether the named action exists
321 g_action_group_has_action (GActionGroup
*action_group
,
322 const gchar
*action_name
)
324 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), FALSE
);
326 return G_ACTION_GROUP_GET_IFACE (action_group
)
327 ->has_action (action_group
, action_name
);
331 * g_action_group_get_action_parameter_type:
332 * @action_group: a #GActionGroup
333 * @action_name: the name of the action to query
335 * Queries the type of the parameter that must be given when activating
336 * the named action within @action_group.
338 * When activating the action using g_action_group_activate_action(),
339 * the #GVariant given to that function must be of the type returned
342 * In the case that this function returns %NULL, you must not give any
343 * #GVariant, but %NULL instead.
345 * The parameter type of a particular action will never change but it is
346 * possible for an action to be removed and for a new action to be added
347 * with the same name but a different parameter type.
349 * Return value: the parameter type
354 g_action_group_get_action_parameter_type (GActionGroup
*action_group
,
355 const gchar
*action_name
)
357 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), NULL
);
359 return G_ACTION_GROUP_GET_IFACE (action_group
)
360 ->get_action_parameter_type (action_group
, action_name
);
364 * g_action_group_get_action_state_type:
365 * @action_group: a #GActionGroup
366 * @action_name: the name of the action to query
368 * Queries the type of the state of the named action within
371 * If the action is stateful then this function returns the
372 * #GVariantType of the state. All calls to
373 * g_action_group_change_action_state() must give a #GVariant of this
374 * type and g_action_group_get_action_state() will return a #GVariant
377 * If the action is not stateful then this function will return %NULL.
378 * In that case, g_action_group_get_action_state() will return %NULL
379 * and you must not call g_action_group_change_action_state().
381 * The state type of a particular action will never change but it is
382 * possible for an action to be removed and for a new action to be added
383 * with the same name but a different state type.
385 * Returns: (transfer full): the state type, if the action is stateful
390 g_action_group_get_action_state_type (GActionGroup
*action_group
,
391 const gchar
*action_name
)
393 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), NULL
);
395 return G_ACTION_GROUP_GET_IFACE (action_group
)
396 ->get_action_state_type (action_group
, action_name
);
400 * g_action_group_get_action_state_hint:
401 * @action_group: a #GActionGroup
402 * @action_name: the name of the action to query
404 * Requests a hint about the valid range of values for the state of the
405 * named action within @action_group.
407 * If %NULL is returned it either means that the action is not stateful
408 * or that there is no hint about the valid range of values for the
409 * state of the action.
411 * If a #GVariant array is returned then each item in the array is a
412 * possible value for the state. If a #GVariant pair (ie: two-tuple) is
413 * returned then the tuple specifies the inclusive lower and upper bound
414 * of valid values for the state.
416 * In any case, the information is merely a hint. It may be possible to
417 * have a state value outside of the hinted range and setting a value
418 * within the range may fail.
420 * The return value (if non-%NULL) should be freed with
421 * g_variant_unref() when it is no longer required.
423 * Return value: (transfer full): the state range hint
428 g_action_group_get_action_state_hint (GActionGroup
*action_group
,
429 const gchar
*action_name
)
431 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), NULL
);
433 return G_ACTION_GROUP_GET_IFACE (action_group
)
434 ->get_action_state_hint (action_group
, action_name
);
438 * g_action_group_get_action_enabled:
439 * @action_group: a #GActionGroup
440 * @action_name: the name of the action to query
442 * Checks if the named action within @action_group is currently enabled.
444 * An action must be enabled in order to be activated or in order to
445 * have its state changed from outside callers.
447 * Return value: whether or not the action is currently enabled
452 g_action_group_get_action_enabled (GActionGroup
*action_group
,
453 const gchar
*action_name
)
455 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), FALSE
);
457 return G_ACTION_GROUP_GET_IFACE (action_group
)
458 ->get_action_enabled (action_group
, action_name
);
462 * g_action_group_get_action_state:
463 * @action_group: a #GActionGroup
464 * @action_name: the name of the action to query
466 * Queries the current state of the named action within @action_group.
468 * If the action is not stateful then %NULL will be returned. If the
469 * action is stateful then the type of the return value is the type
470 * given by g_action_group_get_action_state_type().
472 * The return value (if non-%NULL) should be freed with
473 * g_variant_unref() when it is no longer required.
475 * Return value: (allow-none): the current state of the action
480 g_action_group_get_action_state (GActionGroup
*action_group
,
481 const gchar
*action_name
)
483 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), NULL
);
485 return G_ACTION_GROUP_GET_IFACE (action_group
)
486 ->get_action_state (action_group
, action_name
);
490 * g_action_group_change_action_state:
491 * @action_group: a #GActionGroup
492 * @action_name: the name of the action to request the change on
493 * @value: the new state
495 * Request for the state of the named action within @action_group to be
498 * The action must be stateful and @value must be of the correct type.
499 * See g_action_group_get_action_state_type().
501 * This call merely requests a change. The action may refuse to change
502 * its state or may change its state to something other than @value.
503 * See g_action_group_get_action_state_hint().
505 * If the @value GVariant is floating, it is consumed.
510 g_action_group_change_action_state (GActionGroup
*action_group
,
511 const gchar
*action_name
,
514 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
515 g_return_if_fail (action_name
!= NULL
);
516 g_return_if_fail (value
!= NULL
);
518 G_ACTION_GROUP_GET_IFACE (action_group
)
519 ->change_action_state (action_group
, action_name
, value
);
523 * g_action_group_activate_action:
524 * @action_group: a #GActionGroup
525 * @action_name: the name of the action to activate
526 * @parameter: (allow-none): parameters to the activation
528 * Activate the named action within @action_group.
530 * If the action is expecting a parameter, then the correct type of
531 * parameter must be given as @parameter. If the action is expecting no
532 * parameters then @parameter must be %NULL. See
533 * g_action_group_get_action_parameter_type().
538 g_action_group_activate_action (GActionGroup
*action_group
,
539 const gchar
*action_name
,
542 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
543 g_return_if_fail (action_name
!= NULL
);
545 G_ACTION_GROUP_GET_IFACE (action_group
)
546 ->activate_action (action_group
, action_name
, parameter
);
550 * g_action_group_action_added:
551 * @action_group: a #GActionGroup
552 * @action_name: the name of an action in the group
554 * Emits the #GActionGroup::action-added signal on @action_group.
556 * This function should only be called by #GActionGroup implementations.
561 g_action_group_action_added (GActionGroup
*action_group
,
562 const gchar
*action_name
)
564 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
565 g_return_if_fail (action_name
!= NULL
);
567 g_signal_emit (action_group
,
568 g_action_group_signals
[SIGNAL_ACTION_ADDED
],
569 g_quark_try_string (action_name
),
574 * g_action_group_action_removed:
575 * @action_group: a #GActionGroup
576 * @action_name: the name of an action in the group
578 * Emits the #GActionGroup::action-removed signal on @action_group.
580 * This function should only be called by #GActionGroup implementations.
585 g_action_group_action_removed (GActionGroup
*action_group
,
586 const gchar
*action_name
)
588 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
589 g_return_if_fail (action_name
!= NULL
);
591 g_signal_emit (action_group
,
592 g_action_group_signals
[SIGNAL_ACTION_REMOVED
],
593 g_quark_try_string (action_name
),
598 * g_action_group_action_enabled_changed:
599 * @action_group: a #GActionGroup
600 * @action_name: the name of an action in the group
601 * @enabled: whether or not the action is now enabled
603 * Emits the #GActionGroup::action-enabled-changed signal on @action_group.
605 * This function should only be called by #GActionGroup implementations.
610 g_action_group_action_enabled_changed (GActionGroup
*action_group
,
611 const gchar
*action_name
,
614 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
615 g_return_if_fail (action_name
!= NULL
);
619 g_signal_emit (action_group
,
620 g_action_group_signals
[SIGNAL_ACTION_ENABLED_CHANGED
],
621 g_quark_try_string (action_name
),
627 * g_action_group_action_state_changed:
628 * @action_group: a #GActionGroup
629 * @action_name: the name of an action in the group
630 * @state: the new state of the named action
632 * Emits the #GActionGroup::action-state-changed signal on @action_group.
634 * This function should only be called by #GActionGroup implementations.
639 g_action_group_action_state_changed (GActionGroup
*action_group
,
640 const gchar
*action_name
,
643 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
644 g_return_if_fail (action_name
!= NULL
);
646 g_signal_emit (action_group
,
647 g_action_group_signals
[SIGNAL_ACTION_STATE_CHANGED
],
648 g_quark_try_string (action_name
),
654 * g_action_group_query_action:
655 * @action_group: a #GActionGroup
656 * @action_name: the name of an action in the group
657 * @enabled: (out): if the action is presently enabled
658 * @parameter_type: (out): the parameter type, or %NULL if none needed
659 * @state_type: (out): the state type, or %NULL if stateless
660 * @state_hint: (out): the state hint, or %NULL if none
661 * @state: (out): the current state, or %NULL if stateless
663 * Queries all aspects of the named action within an @action_group.
665 * This function acquires the information available from
666 * g_action_group_has_action(), g_action_group_get_action_enabled(),
667 * g_action_group_get_action_parameter_type(),
668 * g_action_group_get_action_state_type(),
669 * g_action_group_get_action_state_hint() and
670 * g_action_group_get_state() with a single function call.
672 * This provides two main benefits.
674 * The first is the improvement in efficiency that comes with not having
675 * to perform repeated lookups of the action in order to discover
676 * different things about it. The second is that implementing
677 * #GActionGroup can now be done by only overriding this one virtual
680 * The interface provides a default implementation of this function that
681 * calls the individual functions, as required, to fetch the
682 * information. The interface also provides default implementations of
683 * those functions that call this function. All implementations,
684 * therefore, must override either this function or all of the others.
686 * If the action exists, %TRUE is returned and any of the requested
687 * fields (as indicated by having a non-%NULL reference passed in) are
688 * filled. If the action doesn't exist, %FALSE is returned and the
689 * fields may or may not have been modified.
691 * Returns: %TRUE if the action exists, else %FALSE
696 g_action_group_query_action (GActionGroup
*action_group
,
697 const gchar
*action_name
,
699 const GVariantType
**parameter_type
,
700 const GVariantType
**state_type
,
701 GVariant
**state_hint
,
704 return G_ACTION_GROUP_GET_IFACE (action_group
)
705 ->query_action (action_group
, action_name
, enabled
, parameter_type
, state_type
, state_hint
, state
);