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>
28 G_DEFINE_INTERFACE (GAction, g_action, G_TYPE_OBJECT)
33 * @short_description: An action interface
35 * #GAction represents a single named action.
37 * The main interface to an action is that it can be activated with
38 * g_action_activate(). This results in the 'activate' signal being
39 * emitted. An activation has a #GVariant parameter (which may be
40 * %NULL). The correct type for the parameter is determined by a static
41 * parameter type (which is given at construction time).
43 * An action may optionally have a state, in which case the state may be
44 * set with g_action_change_state(). This call takes a #GVariant. The
45 * correct type for the state is determined by a static state type
46 * (which is given at construction time).
48 * The state may have a hint associated with it, specifying its valid
51 * #GAction is merely the interface to the concept of an action, as
52 * described above. Various implementations of actions exist, including
53 * #GSimpleAction and #GtkAction.
55 * In all cases, the implementing class is responsible for storing the
56 * name of the action, the parameter type, the enabled state, the
57 * optional state type and the state and emitting the appropriate
58 * signals when these change. The implementor responsible for filtering
59 * calls to g_action_activate() and g_action_change_state() for type
60 * safety and for the state being enabled.
62 * Probably the only useful thing to do with a #GAction is to put it
63 * inside of a #GSimpleActionGroup.
68 * @get_name: the virtual function pointer for g_action_get_name()
69 * @get_parameter_type: the virtual function pointer for g_action_get_parameter_type()
70 * @get_state_type: the virtual function pointer for g_action_get_state_type()
71 * @get_state_hint: the virtual function pointer for g_action_get_state_hint()
72 * @get_enabled: the virtual function pointer for g_action_get_enabled()
73 * @get_state: the virtual function pointer for g_action_get_state()
74 * @change_state: the virtual function pointer for g_action_change_state()
75 * @activate: the virtual function pointer for g_action_activate(). Note that #GAction does not have an
76 * 'activate' signal but that implementations of it may have one.
78 * The virtual function table for #GAction.
84 g_action_default_init (GActionInterface *iface)
89 * The name of the action. This is mostly meaningful for identifying
90 * the action once it has been added to a #GActionGroup.
94 g_object_interface_install_property (iface,
95 g_param_spec_string ("name",
97 P_("The name used to invoke the action"),
100 G_PARAM_STATIC_STRINGS));
103 * GAction:parameter-type:
105 * The type of the parameter that must be given when activating the
110 g_object_interface_install_property (iface,
111 g_param_spec_boxed ("parameter-type",
112 P_("Parameter Type"),
113 P_("The type of GVariant passed to activate()"),
116 G_PARAM_STATIC_STRINGS));
121 * If @action is currently enabled.
123 * If the action is disabled then calls to g_action_activate() and
124 * g_action_change_state() have no effect.
128 g_object_interface_install_property (iface,
129 g_param_spec_boolean ("enabled",
131 P_("If the action can be activated"),
134 G_PARAM_STATIC_STRINGS));
137 * GAction:state-type:
139 * The #GVariantType of the state that the action has, or %NULL if the
140 * action is stateless.
144 g_object_interface_install_property (iface,
145 g_param_spec_boxed ("state-type",
147 P_("The type of the state kept by the action"),
150 G_PARAM_STATIC_STRINGS));
155 * The state of the action, or %NULL if the action is stateless.
159 g_object_interface_install_property (iface,
160 g_param_spec_variant ("state",
162 P_("The state the action is in"),
166 G_PARAM_STATIC_STRINGS));
170 * g_action_change_state:
171 * @action: a #GAction
172 * @value: the new state
174 * Request for the state of @action to be changed to @value.
176 * The action must be stateful and @value must be of the correct type.
177 * See g_action_get_state_type().
179 * This call merely requests a change. The action may refuse to change
180 * its state or may change its state to something other than @value.
181 * See g_action_get_state_hint().
183 * If the @value GVariant is floating, it is consumed.
188 g_action_change_state (GAction *action,
191 const GVariantType *state_type;
193 g_return_if_fail (G_IS_ACTION (action));
194 g_return_if_fail (value != NULL);
195 state_type = g_action_get_state_type (action);
196 g_return_if_fail (state_type != NULL);
197 g_return_if_fail (g_variant_is_of_type (value, state_type));
199 g_variant_ref_sink (value);
201 G_ACTION_GET_IFACE (action)
202 ->change_state (action, value);
204 g_variant_unref (value);
208 * g_action_get_state:
209 * @action: a #GAction
211 * Queries the current state of @action.
213 * If the action is not stateful then %NULL will be returned. If the
214 * action is stateful then the type of the return value is the type
215 * given by g_action_get_state_type().
217 * The return value (if non-%NULL) should be freed with
218 * g_variant_unref() when it is no longer required.
220 * Returns: (transfer full): the current state of the action
225 g_action_get_state (GAction *action)
227 g_return_val_if_fail (G_IS_ACTION (action), NULL);
229 return G_ACTION_GET_IFACE (action)
230 ->get_state (action);
235 * @action: a #GAction
237 * Queries the name of @action.
239 * Returns: the name of the action
244 g_action_get_name (GAction *action)
246 g_return_val_if_fail (G_IS_ACTION (action), NULL);
248 return G_ACTION_GET_IFACE (action)
253 * g_action_get_parameter_type:
254 * @action: a #GAction
256 * Queries the type of the parameter that must be given when activating
259 * When activating the action using g_action_activate(), the #GVariant
260 * given to that function must be of the type returned by this function.
262 * In the case that this function returns %NULL, you must not give any
263 * #GVariant, but %NULL instead.
265 * Returns: (allow-none): the parameter type
270 g_action_get_parameter_type (GAction *action)
272 g_return_val_if_fail (G_IS_ACTION (action), NULL);
274 return G_ACTION_GET_IFACE (action)
275 ->get_parameter_type (action);
279 * g_action_get_state_type:
280 * @action: a #GAction
282 * Queries the type of the state of @action.
284 * If the action is stateful (e.g. created with
285 * g_simple_action_new_stateful()) then this function returns the
286 * #GVariantType of the state. This is the type of the initial value
287 * given as the state. All calls to g_action_change_state() must give a
288 * #GVariant of this type and g_action_get_state() will return a
289 * #GVariant of the same type.
291 * If the action is not stateful (e.g. created with g_simple_action_new())
292 * then this function will return %NULL. In that case, g_action_get_state()
293 * will return %NULL and you must not call g_action_change_state().
295 * Returns: (allow-none): the state type, if the action is stateful
300 g_action_get_state_type (GAction *action)
302 g_return_val_if_fail (G_IS_ACTION (action), NULL);
304 return G_ACTION_GET_IFACE (action)
305 ->get_state_type (action);
309 * g_action_get_state_hint:
310 * @action: a #GAction
312 * Requests a hint about the valid range of values for the state of
315 * If %NULL is returned it either means that the action is not stateful
316 * or that there is no hint about the valid range of values for the
317 * state of the action.
319 * If a #GVariant array is returned then each item in the array is a
320 * possible value for the state. If a #GVariant pair (ie: two-tuple) is
321 * returned then the tuple specifies the inclusive lower and upper bound
322 * of valid values for the state.
324 * In any case, the information is merely a hint. It may be possible to
325 * have a state value outside of the hinted range and setting a value
326 * within the range may fail.
328 * The return value (if non-%NULL) should be freed with
329 * g_variant_unref() when it is no longer required.
331 * Returns: (transfer full): the state range hint
336 g_action_get_state_hint (GAction *action)
338 g_return_val_if_fail (G_IS_ACTION (action), NULL);
340 return G_ACTION_GET_IFACE (action)
341 ->get_state_hint (action);
345 * g_action_get_enabled:
346 * @action: a #GAction
348 * Checks if @action is currently enabled.
350 * An action must be enabled in order to be activated or in order to
351 * have its state changed from outside callers.
353 * Returns: whether the action is enabled
358 g_action_get_enabled (GAction *action)
360 g_return_val_if_fail (G_IS_ACTION (action), FALSE);
362 return G_ACTION_GET_IFACE (action)
363 ->get_enabled (action);
368 * @action: a #GAction
369 * @parameter: (allow-none): the parameter to the activation
371 * Activates the action.
373 * @parameter must be the correct type of parameter for the action (ie:
374 * the parameter type given at construction time). If the parameter
375 * type was %NULL then @parameter must also be %NULL.
377 * If the @parameter GVariant is floating, it is consumed.
382 g_action_activate (GAction *action,
385 g_return_if_fail (G_IS_ACTION (action));
387 if (parameter != NULL)
388 g_variant_ref_sink (parameter);
390 G_ACTION_GET_IFACE (action)
391 ->activate (action, parameter);
393 if (parameter != NULL)
394 g_variant_unref (parameter);
398 * g_action_parse_detailed_name:
399 * @detailed_name: a detailed action name
400 * @action_name: (out): the action name
401 * @target_value: (out): the target value, or %NULL for no target
402 * @error: a pointer to a %NULL #GError, or %NULL
404 * Parses a detailed action name into its separate name and target
407 * Detailed action names can have three formats.
409 * The first format is used to represent an action name with no target
410 * value and consists of just an action name containing no whitespace
411 * nor the characters ':', '(' or ')'. For example: "app.action".
413 * The second format is used to represent an action with a string-typed
414 * target value. The action name and target value are separated by a
415 * double colon ("::"). For example: "app.action::target".
417 * The third format is used to represent an action with an
418 * arbitrarily-typed target value. The target value follows the action
419 * name, surrounded in parens. For example: "app.action(42)". The
420 * target value is parsed using g_variant_parse(). If a tuple-typed
421 * value is desired, it must be specified in the same way, resulting in
422 * two sets of parens, for example: "app.action((1,2,3))".
424 * Returns: %TRUE if successful, else %FALSE with @error set
429 g_action_parse_detailed_name (const gchar *detailed_name,
431 GVariant **target_value,
438 /* We decide which format we have based on which we see first between
442 if (*detailed_name == '\0' || *detailed_name == ' ')
445 base_len = strcspn (detailed_name, ": ()");
446 target = detailed_name + base_len;
447 target_len = strlen (target);
456 if (target[1] != ':')
459 *target_value = g_variant_ref_sink (g_variant_new_string (target + 2));
464 if (target[target_len - 1] != ')')
467 *target_value = g_variant_parse (NULL, target + 1, target + target_len - 1, NULL, error);
468 if (*target_value == NULL)
474 *target_value = NULL;
478 *action_name = g_strndup (detailed_name, base_len);
486 g_set_error (error, G_VARIANT_PARSE_ERROR, G_VARIANT_PARSE_ERROR_FAILED,
487 "Detailed action name '%s' has invalid format", detailed_name);
489 g_prefix_error (error, "Detailed action name '%s' has invalid format: ", detailed_name);