2.2 <GActionGroup>

Interface: <GActionGroup>

<GActionGroup> represents a group of actions.

Actions can be used to expose functionality in a structured way, either from one part of a program to another, or to the outside world. Action groups are often used together with a <GMenuModel> that provides additional representation data for displaying the actions to the user, e.g. in a menu.

The main way to interact with the actions in a <GActionGroup> is to activate them with action-group:activate-action. Activating an action may require a <GVariant> parameter. The required type of the parameter can be inquired with action-group:get-action-parameter-type. Actions may be disabled, see action-group:get-action-enabled?. Activating a disabled action has no effect.

Actions may optionally have a state in the form of a <GVariant>. The current state of an action can be inquired with action-group:get-action-state. Activating a stateful action may change its state, but it is also possible to set the state by calling action-group:change-action-state.

As typical example, consider a text editing application which has an option to change the current font to ‘bold’. A good way to represent this would be a stateful action, with a boolean state. Activating the action would toggle the state.

Each action in the group has a unique name (which is a string). All method calls, except action-group:list-actions take the name of an action as an argument.

The <GActionGroup> API is meant to be the ‘public’ API to the action group. The calls here are exactly the interaction that ‘external forces’ (eg: UI, incoming D-Bus messages, etc.) are supposed to have with actions. ‘Internal’ APIs (ie: ones meant only to be accessed by the action group implementation) are found on subclasses. This is why you will find – for example – action-group:get-action-enabled? but not an equivalent set_action_enabled() method.

Signals are emitted on the action group in response to state changes on individual actions.

Implementations of <GActionGroup> should provide implementations for the virtual functions action-group:list-actions and action-group:query-action. The other virtual functions should not be implemented — their ‘wrappers’ are actually implemented with calls to action-group:query-action.

Procedures

Method: action-group:action-added self action-name
Method: action-added self action-name

Emits the action-group:action-added signal on self.

This function should only be called by <GActionGroup> implementations.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of an action in the group

Method: action-group:action-enabled-changed self action-name enabled
Method: action-enabled-changed self action-name enabled

Emits the action-group:action-enabled-changed signal on self.

This function should only be called by <GActionGroup> implementations.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of an action in the group

enabled

Passed: a boolean.

whether the action is now enabled

Method: action-group:action-removed self action-name
Method: action-removed self action-name

Emits the action-group:action-removed signal on self.

This function should only be called by <GActionGroup> implementations.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of an action in the group

Method: action-group:action-state-changed self action-name state
Method: action-state-changed self action-name state

Emits the action-group:action-state-changed signal on self.

This function should only be called by <GActionGroup> implementations.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of an action in the group

state

Passed: a <GVariant>.

the new state of the named action

Method: action-group:activate-action self action-name [parameter]
Method: activate-action self action-name [parameter]

Activate the named action within self.

If the action is expecting a parameter, then the correct type of parameter must be given as parameter. If the action is expecting no parameters then parameter must be NULL. See action-group:get-action-parameter-type.

If the <GActionGroup> implementation supports asynchronous remote activation over D-Bus, this call may return before the relevant D-Bus traffic has been sent, or any replies have been received. In order to block on such asynchronous activation calls, dbus-connection:flush should be called prior to the code, which depends on the result of the action activation. Without flushing the D-Bus connection, there is no guarantee that the action would have been activated.

The following code which runs in a remote app instance, shows an example of a ‘quit’ action being activated on the primary app instance over D-Bus. Here dbus-connection:flush is called before exit(). Without dbus-connection:flush, the ‘quit’ action may fail to be activated on the primary instance.

// call ‘quit’ action on primary instance
g_action_group_activate_action (G_ACTION_GROUP (app), "quit", NULL);

// make sure the action is activated now
g_dbus_connection_flush (…);

g_debug ("Application has been terminated. Exiting.");

exit (0);
self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to activate

parameter

Passed, or left out: a <GVariant>, or #f.

parameters to the activation

Method: action-group:change-action-state self action-name value
Method: change-action-state self action-name value

Request for the state of the named action within self to be changed to value.

The action must be stateful and value must be of the correct type. See action-group:get-action-state-type.

This call merely requests a change. The action may refuse to change its state or may change its state to something other than value. See action-group:get-action-state-hint.

If the value GVariant is floating, it is consumed.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to request the change on

value

Passed: a <GVariant>.

the new state

Method: action-group:get-action-enabled? self action-name ⇒ %return
Method: get-action-enabled? self action-name ⇒ %return

Checks if the named action within self is currently enabled.

An action must be enabled in order to be activated or in order to have its state changed from outside callers.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to query

%return

Returned: a boolean.

whether the action is currently enabled

Method: action-group:get-action-parameter-type self action-name ⇒ %return
Method: get-action-parameter-type self action-name ⇒ %return

Queries the type of the parameter that must be given when activating the named action within self.

When activating the action using action-group:activate-action, the <GVariant> given to that function must be of the type returned by this function.

In the case that this function returns NULL, you must not give any <GVariant>, but NULL instead.

The parameter type of a particular action will never change but it is possible for an action to be removed and for a new action to be added with the same name but a different parameter type.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to query

%return

Returned: a <GVariantType>, or #f.

the parameter type

Method: action-group:get-action-state self action-name ⇒ %return
Method: get-action-state self action-name ⇒ %return

Queries the current state of the named action within self.

If the action is not stateful then NULL will be returned. If the action is stateful then the type of the return value is the type given by action-group:get-action-state-type.

The return value (if non-NULL) should be freed with variant:unref when it is no longer required.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to query

%return

Returned: a <GVariant>, or #f.

the current state of the action

Method: action-group:get-action-state-hint self action-name ⇒ %return
Method: get-action-state-hint self action-name ⇒ %return

Requests a hint about the valid range of values for the state of the named action within self.

If NULL is returned it either means that the action is not stateful or that there is no hint about the valid range of values for the state of the action.

If a <GVariant> array is returned then each item in the array is a possible value for the state. If a <GVariant> pair (ie: two-tuple) is returned then the tuple specifies the inclusive lower and upper bound of valid values for the state.

In any case, the information is merely a hint. It may be possible to have a state value outside of the hinted range and setting a value within the range may fail.

The return value (if non-NULL) should be freed with variant:unref when it is no longer required.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to query

%return

Returned: a <GVariant>, or #f.

the state range hint

Method: action-group:get-action-state-type self action-name ⇒ %return
Method: get-action-state-type self action-name ⇒ %return

Queries the type of the state of the named action within self.

If the action is stateful then this function returns the <GVariantType> of the state. All calls to action-group:change-action-state must give a <GVariant> of this type and action-group:get-action-state will return a <GVariant> of the same type.

If the action is not stateful then this function will return NULL. In that case, action-group:get-action-state will return NULL and you must not call action-group:change-action-state.

The state type of a particular action will never change but it is possible for an action to be removed and for a new action to be added with the same name but a different state type.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to query

%return

Returned: a <GVariantType>, or #f.

the state type, if the action is stateful

Method: action-group:has-action? self action-name ⇒ %return
Method: has-action? self action-name ⇒ %return

Checks if the named action exists within self.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to check for

%return

Returned: a boolean.

whether the named action exists

Method: action-group:list-actions self ⇒ %return
Method: list-actions self ⇒ %return

Lists the actions contained within self.

The caller is responsible for freeing the list with strfreev when it is no longer required.

self

Passed: a <GActionGroup>.

a <GActionGroup>

%return

Returned: a vector, each a string.

a NULL-terminated array of the names of the actions in the group

Method: action-group:query-action self action-name ⇒ %return enabled parameter-type state-type state-hint state
Method: query-action self action-name ⇒ %return enabled parameter-type state-type state-hint state

Queries all aspects of the named action within an self.

This function acquires the information available from action-group:has-action?, action-group:get-action-enabled?, action-group:get-action-parameter-type, action-group:get-action-state-type, action-group:get-action-state-hint and action-group:get-action-state with a single function call.

This provides two main benefits.

The first is the improvement in efficiency that comes with not having to perform repeated lookups of the action in order to discover different things about it. The second is that implementing <GActionGroup> can now be done by only overriding this one virtual function.

The interface provides a default implementation of this function that calls the individual functions, as required, to fetch the information. The interface also provides default implementations of those functions that call this function. All implementations, therefore, must override either this function or all of the others.

If the action exists, TRUE is returned and any of the requested fields (as indicated by having a non-NULL reference passed in) are filled. If the action doesn’t exist, FALSE is returned and the fields may or may not have been modified.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of an action in the group

%return

Returned: a boolean.

TRUE if the action exists, else FALSE

enabled

Returned: a boolean.

if the action is presently enabled

parameter-type

Returned: a <GVariantType>.

the parameter type, or NULL if none needed

state-type

Returned: a <GVariantType>.

the state type, or NULL if stateless

state-hint

Returned: a <GVariant>.

the state hint, or NULL if none

state

Returned: a <GVariant>.

the current state, or NULL if stateless

Signals

Signal: action-group:action-added

Connect a handler with (connect object action-group:action-added handler). It is called as (handler self action-name).

Signals that a new action was just added to the group.

This signal is emitted after the action has been added and is now visible.

self

Passed: a <GActionGroup>.

action-name

Passed: a string.

the name of the action in action-group

Signal: action-group:action-enabled-changed

Connect a handler with (connect object action-group:action-enabled-changed handler). It is called as (handler self action-name enabled).

Signals that the enabled status of the named action has changed.

self

Passed: a <GActionGroup>.

action-name

Passed: a string.

the name of the action in action-group

enabled

Passed: a boolean.

whether the action is enabled

Signal: action-group:action-removed

Connect a handler with (connect object action-group:action-removed handler). It is called as (handler self action-name).

Signals that an action is just about to be removed from the group.

This signal is emitted before the action is removed, so the action is still visible and can be queried from the signal handler.

self

Passed: a <GActionGroup>.

action-name

Passed: a string.

the name of the action in action-group

Signal: action-group:action-state-changed

Connect a handler with (connect object action-group:action-state-changed handler). It is called as (handler self action-name value).

Signals that the state of the named action has changed.

self

Passed: a <GActionGroup>.

action-name

Passed: a string.

the name of the action in action-group

value

Passed: a <GVariant>.

the new value of the state

Virtual methods

Virtual method: action-group:do-action-added implementor self action-name

Emits the action-group:action-added signal on self.

This function should only be called by <GActionGroup> implementations.

To override it in a new class, give register-type #:vfuncs `((action-added . ,procedure)). The procedure is called as (procedure self action-name).

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of an action in the group

Virtual method: action-group:do-action-enabled-changed implementor self action-name enabled

Emits the action-group:action-enabled-changed signal on self.

This function should only be called by <GActionGroup> implementations.

To override it in a new class, give register-type #:vfuncs `((action-enabled-changed . ,procedure)). The procedure is called as (procedure self action-name enabled).

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of an action in the group

enabled

Passed: a boolean.

whether the action is now enabled

Virtual method: action-group:do-action-removed implementor self action-name

Emits the action-group:action-removed signal on self.

This function should only be called by <GActionGroup> implementations.

To override it in a new class, give register-type #:vfuncs `((action-removed . ,procedure)). The procedure is called as (procedure self action-name).

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of an action in the group

Virtual method: action-group:do-action-state-changed implementor self action-name state

Emits the action-group:action-state-changed signal on self.

This function should only be called by <GActionGroup> implementations.

To override it in a new class, give register-type #:vfuncs `((action-state-changed . ,procedure)). The procedure is called as (procedure self action-name state).

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of an action in the group

state

Passed: a <GVariant>.

the new state of the named action

Virtual method: action-group:do-activate-action implementor self action-name [parameter]

Activate the named action within self.

If the action is expecting a parameter, then the correct type of parameter must be given as parameter. If the action is expecting no parameters then parameter must be NULL. See action-group:get-action-parameter-type.

If the <GActionGroup> implementation supports asynchronous remote activation over D-Bus, this call may return before the relevant D-Bus traffic has been sent, or any replies have been received. In order to block on such asynchronous activation calls, dbus-connection:flush should be called prior to the code, which depends on the result of the action activation. Without flushing the D-Bus connection, there is no guarantee that the action would have been activated.

The following code which runs in a remote app instance, shows an example of a ‘quit’ action being activated on the primary app instance over D-Bus. Here dbus-connection:flush is called before exit(). Without dbus-connection:flush, the ‘quit’ action may fail to be activated on the primary instance.

// call ‘quit’ action on primary instance
g_action_group_activate_action (G_ACTION_GROUP (app), "quit", NULL);

// make sure the action is activated now
g_dbus_connection_flush (…);

g_debug ("Application has been terminated. Exiting.");

exit (0);

To override it in a new class, give register-type #:vfuncs `((activate-action . ,procedure)). The procedure is called as (procedure self action-name parameter).

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to activate

parameter

Passed, or left out: a <GVariant>, or #f.

parameters to the activation

Virtual method: action-group:do-change-action-state implementor self action-name value

Request for the state of the named action within self to be changed to value.

The action must be stateful and value must be of the correct type. See action-group:get-action-state-type.

This call merely requests a change. The action may refuse to change its state or may change its state to something other than value. See action-group:get-action-state-hint.

If the value GVariant is floating, it is consumed.

To override it in a new class, give register-type #:vfuncs `((change-action-state . ,procedure)). The procedure is called as (procedure self action-name value).

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to request the change on

value

Passed: a <GVariant>.

the new state

Virtual method: action-group:do-get-action-enabled implementor self action-name ⇒ %return

Checks if the named action within self is currently enabled.

An action must be enabled in order to be activated or in order to have its state changed from outside callers.

To override it in a new class, give register-type #:vfuncs `((get-action-enabled . ,procedure)). The procedure is called as (procedure self action-name) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to query

%return

Returned: a boolean.

whether the action is currently enabled

Virtual method: action-group:do-get-action-parameter-type implementor self action-name ⇒ %return

Queries the type of the parameter that must be given when activating the named action within self.

When activating the action using action-group:activate-action, the <GVariant> given to that function must be of the type returned by this function.

In the case that this function returns NULL, you must not give any <GVariant>, but NULL instead.

The parameter type of a particular action will never change but it is possible for an action to be removed and for a new action to be added with the same name but a different parameter type.

To override it in a new class, give register-type #:vfuncs `((get-action-parameter-type . ,procedure)). The procedure is called as (procedure self action-name) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to query

%return

Returned: a <GVariantType>, or #f.

the parameter type

Virtual method: action-group:do-get-action-state implementor self action-name ⇒ %return

Queries the current state of the named action within self.

If the action is not stateful then NULL will be returned. If the action is stateful then the type of the return value is the type given by action-group:get-action-state-type.

The return value (if non-NULL) should be freed with variant:unref when it is no longer required.

To override it in a new class, give register-type #:vfuncs `((get-action-state . ,procedure)). The procedure is called as (procedure self action-name) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to query

%return

Returned: a <GVariant>, or #f.

the current state of the action

Virtual method: action-group:do-get-action-state-hint implementor self action-name ⇒ %return

Requests a hint about the valid range of values for the state of the named action within self.

If NULL is returned it either means that the action is not stateful or that there is no hint about the valid range of values for the state of the action.

If a <GVariant> array is returned then each item in the array is a possible value for the state. If a <GVariant> pair (ie: two-tuple) is returned then the tuple specifies the inclusive lower and upper bound of valid values for the state.

In any case, the information is merely a hint. It may be possible to have a state value outside of the hinted range and setting a value within the range may fail.

The return value (if non-NULL) should be freed with variant:unref when it is no longer required.

To override it in a new class, give register-type #:vfuncs `((get-action-state-hint . ,procedure)). The procedure is called as (procedure self action-name) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to query

%return

Returned: a <GVariant>, or #f.

the state range hint

Virtual method: action-group:do-get-action-state-type implementor self action-name ⇒ %return

Queries the type of the state of the named action within self.

If the action is stateful then this function returns the <GVariantType> of the state. All calls to action-group:change-action-state must give a <GVariant> of this type and action-group:get-action-state will return a <GVariant> of the same type.

If the action is not stateful then this function will return NULL. In that case, action-group:get-action-state will return NULL and you must not call action-group:change-action-state.

The state type of a particular action will never change but it is possible for an action to be removed and for a new action to be added with the same name but a different state type.

To override it in a new class, give register-type #:vfuncs `((get-action-state-type . ,procedure)). The procedure is called as (procedure self action-name) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to query

%return

Returned: a <GVariantType>, or #f.

the state type, if the action is stateful

Virtual method: action-group:do-has-action implementor self action-name ⇒ %return

Checks if the named action exists within self.

To override it in a new class, give register-type #:vfuncs `((has-action . ,procedure)). The procedure is called as (procedure self action-name) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of the action to check for

%return

Returned: a boolean.

whether the named action exists

Virtual method: action-group:do-list-actions implementor self ⇒ %return

Lists the actions contained within self.

The caller is responsible for freeing the list with strfreev when it is no longer required.

To override it in a new class, give register-type #:vfuncs `((list-actions . ,procedure)). The procedure is called as (procedure self) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

%return

Returned: a vector, each a string.

a NULL-terminated array of the names of the actions in the group

Virtual method: action-group:do-query-action implementor self action-name ⇒ %return enabled parameter-type state-type state-hint state

Queries all aspects of the named action within an self.

This function acquires the information available from action-group:has-action?, action-group:get-action-enabled?, action-group:get-action-parameter-type, action-group:get-action-state-type, action-group:get-action-state-hint and action-group:get-action-state with a single function call.

This provides two main benefits.

The first is the improvement in efficiency that comes with not having to perform repeated lookups of the action in order to discover different things about it. The second is that implementing <GActionGroup> can now be done by only overriding this one virtual function.

The interface provides a default implementation of this function that calls the individual functions, as required, to fetch the information. The interface also provides default implementations of those functions that call this function. All implementations, therefore, must override either this function or all of the others.

If the action exists, TRUE is returned and any of the requested fields (as indicated by having a non-NULL reference passed in) are filled. If the action doesn’t exist, FALSE is returned and the fields may or may not have been modified.

To override it in a new class, give register-type #:vfuncs `((query-action . ,procedure)). The procedure is called as (procedure self action-name) ⇒ %return enabled parameter-type state-type state-hint state.

implementor

Passed: the class whose implementation to call: a class that implements <GActionGroup>, or its GType number.

self

Passed: a <GActionGroup>.

a <GActionGroup>

action-name

Passed: a string.

the name of an action in the group

%return

Returned: a boolean.

TRUE if the action exists, else FALSE

enabled

Returned: a boolean.

if the action is presently enabled

parameter-type

Returned: a <GVariantType>.

the parameter type, or NULL if none needed

state-type

Returned: a <GVariantType>.

the state type, or NULL if stateless

state-hint

Returned: a <GVariant>.

the state hint, or NULL if none

state

Returned: a <GVariant>.

the current state, or NULL if stateless