GAction: back out changes to property flags
[platform/upstream/glib.git] / gio / gaction.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23 #include "gaction.h"
24 #include "glibintl.h"
25
26 G_DEFINE_INTERFACE (GAction, g_action, G_TYPE_OBJECT)
27
28 /**
29  * SECTION:gaction
30  * @title: GAction
31  * @short_description: An action interface
32  *
33  * #GAction represents a single named action.
34  *
35  * The main interface to an action is that it can be activated with
36  * g_action_activate().  This results in the 'activate' signal being
37  * emitted.  An activation has a #GVariant parameter (which may be
38  * %NULL).  The correct type for the parameter is determined by a static
39  * parameter type (which is given at construction time).
40  *
41  * An action may optionally have a state, in which case the state may be
42  * set with g_action_change_state().  This call takes a #GVariant.  The
43  * correct type for the state is determined by a static state type
44  * (which is given at construction time).
45  *
46  * The state may have a hint associated with it, specifying its valid
47  * range.
48  *
49  * #GAction is merely the interface to the concept of an action, as
50  * described above.  Various implementations of actions exist, including
51  * #GSimpleAction and #GtkAction.
52  *
53  * In all cases, the implementing class is responsible for storing the
54  * name of the action, the parameter type, the enabled state, the
55  * optional state type and the state and emitting the appropriate
56  * signals when these change.  The implementor responsible for filtering
57  * calls to g_action_activate() and g_action_change_state() for type
58  * safety and for the state being enabled.
59  *
60  * Probably the only useful thing to do with a #GAction is to put it
61  * inside of a #GSimpleActionGroup.
62  **/
63
64 /**
65  * GActionInterface:
66  * @get_name: the virtual function pointer for g_action_get_name()
67  * @get_parameter_type: the virtual function pointer for g_action_get_parameter_type()
68  * @get_state_type: the virtual function pointer for g_action_get_state_type()
69  * @get_state_hint: the virtual function pointer for g_action_get_state_hint()
70  * @get_enabled: the virtual function pointer for g_action_get_enabled()
71  * @get_state: the virtual function pointer for g_action_get_state()
72  * @change_state: the virtual function pointer for g_action_change_state()
73  * @activate: the virtual function pointer for g_action_activate().  Note that #GAction does not have an
74  *            'activate' signal but that implementations of it may have one.
75  *
76  * The virtual function table for #GAction.
77  *
78  * Since: 2.28
79  */
80
81 void
82 g_action_default_init (GActionInterface *iface)
83 {
84   /**
85    * GAction:name:
86    *
87    * The name of the action.  This is mostly meaningful for identifying
88    * the action once it has been added to a #GActionGroup.
89    *
90    * Since: 2.28
91    **/
92   g_object_interface_install_property (iface,
93                                        g_param_spec_string ("name",
94                                                             P_("Action Name"),
95                                                             P_("The name used to invoke the action"),
96                                                             NULL,
97                                                             G_PARAM_READABLE |
98                                                             G_PARAM_STATIC_STRINGS));
99
100   /**
101    * GAction:parameter-type:
102    *
103    * The type of the parameter that must be given when activating the
104    * action.
105    *
106    * Since: 2.28
107    **/
108   g_object_interface_install_property (iface,
109                                        g_param_spec_boxed ("parameter-type",
110                                                            P_("Parameter Type"),
111                                                            P_("The type of GVariant passed to activate()"),
112                                                            G_TYPE_VARIANT_TYPE,
113                                                            G_PARAM_READABLE |
114                                                            G_PARAM_STATIC_STRINGS));
115
116   /**
117    * GAction:enabled:
118    *
119    * If @action is currently enabled.
120    *
121    * If the action is disabled then calls to g_action_activate() and
122    * g_action_change_state() have no effect.
123    *
124    * Since: 2.28
125    **/
126   g_object_interface_install_property (iface,
127                                        g_param_spec_boolean ("enabled",
128                                                              P_("Enabled"),
129                                                              P_("If the action can be activated"),
130                                                              TRUE,
131                                                              G_PARAM_READABLE |
132                                                              G_PARAM_STATIC_STRINGS));
133
134   /**
135    * GAction:state-type:
136    *
137    * The #GVariantType of the state that the action has, or %NULL if the
138    * action is stateless.
139    *
140    * Since: 2.28
141    **/
142   g_object_interface_install_property (iface,
143                                        g_param_spec_boxed ("state-type",
144                                                            P_("State Type"),
145                                                            P_("The type of the state kept by the action"),
146                                                            G_TYPE_VARIANT_TYPE,
147                                                            G_PARAM_READABLE |
148                                                            G_PARAM_STATIC_STRINGS));
149
150   /**
151    * GAction:state:
152    *
153    * The state of the action, or %NULL if the action is stateless.
154    *
155    * Since: 2.28
156    **/
157   g_object_interface_install_property (iface,
158                                        g_param_spec_variant ("state",
159                                                              P_("State"),
160                                                              P_("The state the action is in"),
161                                                              G_VARIANT_TYPE_ANY,
162                                                              NULL,
163                                                              G_PARAM_READABLE |
164                                                              G_PARAM_STATIC_STRINGS));
165 }
166
167 /**
168  * g_action_change_state:
169  * @action: a #GAction
170  * @value: the new state
171  *
172  * Request for the state of @action to be changed to @value.
173  *
174  * The action must be stateful and @value must be of the correct type.
175  * See g_action_get_state_type().
176  *
177  * This call merely requests a change.  The action may refuse to change
178  * its state or may change its state to something other than @value.
179  * See g_action_get_state_hint().
180  *
181  * If the @value GVariant is floating, it is consumed.
182  *
183  * Since: 2.30
184  **/
185 void
186 g_action_change_state (GAction  *action,
187                        GVariant *value)
188 {
189   const GVariantType *state_type;
190
191   g_return_if_fail (G_IS_ACTION (action));
192   g_return_if_fail (value != NULL);
193   state_type = g_action_get_state_type (action);
194   g_return_if_fail (state_type != NULL);
195   g_return_if_fail (g_variant_is_of_type (value, state_type));
196
197   g_variant_ref_sink (value);
198
199   G_ACTION_GET_IFACE (action)
200     ->change_state (action, value);
201
202   g_variant_unref (value);
203 }
204
205 /**
206  * g_action_get_state:
207  * @action: a #GAction
208  *
209  * Queries the current state of @action.
210  *
211  * If the action is not stateful then %NULL will be returned.  If the
212  * action is stateful then the type of the return value is the type
213  * given by g_action_get_state_type().
214  *
215  * The return value (if non-%NULL) should be freed with
216  * g_variant_unref() when it is no longer required.
217  *
218  * Returns: (transfer full): the current state of the action
219  *
220  * Since: 2.28
221  **/
222 GVariant *
223 g_action_get_state (GAction *action)
224 {
225   g_return_val_if_fail (G_IS_ACTION (action), NULL);
226
227   return G_ACTION_GET_IFACE (action)
228     ->get_state (action);
229 }
230
231 /**
232  * g_action_get_name:
233  * @action: a #GAction
234  *
235  * Queries the name of @action.
236  *
237  * Returns: the name of the action
238  *
239  * Since: 2.28
240  **/
241 const gchar *
242 g_action_get_name (GAction *action)
243 {
244   g_return_val_if_fail (G_IS_ACTION (action), NULL);
245
246   return G_ACTION_GET_IFACE (action)
247     ->get_name (action);
248 }
249
250 /**
251  * g_action_get_parameter_type:
252  * @action: a #GAction
253  *
254  * Queries the type of the parameter that must be given when activating
255  * @action.
256  *
257  * When activating the action using g_action_activate(), the #GVariant
258  * given to that function must be of the type returned by this function.
259  *
260  * In the case that this function returns %NULL, you must not give any
261  * #GVariant, but %NULL instead.
262  *
263  * Returns: (allow-none): the parameter type
264  *
265  * Since: 2.28
266  **/
267 const GVariantType *
268 g_action_get_parameter_type (GAction *action)
269 {
270   g_return_val_if_fail (G_IS_ACTION (action), NULL);
271
272   return G_ACTION_GET_IFACE (action)
273     ->get_parameter_type (action);
274 }
275
276 /**
277  * g_action_get_state_type:
278  * @action: a #GAction
279  *
280  * Queries the type of the state of @action.
281  *
282  * If the action is stateful (e.g. created with
283  * g_simple_action_new_stateful()) then this function returns the
284  * #GVariantType of the state.  This is the type of the initial value
285  * given as the state. All calls to g_action_change_state() must give a
286  * #GVariant of this type and g_action_get_state() will return a
287  * #GVariant of the same type.
288  *
289  * If the action is not stateful (e.g. created with g_simple_action_new())
290  * then this function will return %NULL. In that case, g_action_get_state()
291  * will return %NULL and you must not call g_action_change_state().
292  *
293  * Returns: (allow-none): the state type, if the action is stateful
294  *
295  * Since: 2.28
296  **/
297 const GVariantType *
298 g_action_get_state_type (GAction *action)
299 {
300   g_return_val_if_fail (G_IS_ACTION (action), NULL);
301
302   return G_ACTION_GET_IFACE (action)
303     ->get_state_type (action);
304 }
305
306 /**
307  * g_action_get_state_hint:
308  * @action: a #GAction
309  *
310  * Requests a hint about the valid range of values for the state of
311  * @action.
312  *
313  * If %NULL is returned it either means that the action is not stateful
314  * or that there is no hint about the valid range of values for the
315  * state of the action.
316  *
317  * If a #GVariant array is returned then each item in the array is a
318  * possible value for the state.  If a #GVariant pair (ie: two-tuple) is
319  * returned then the tuple specifies the inclusive lower and upper bound
320  * of valid values for the state.
321  *
322  * In any case, the information is merely a hint.  It may be possible to
323  * have a state value outside of the hinted range and setting a value
324  * within the range may fail.
325  *
326  * The return value (if non-%NULL) should be freed with
327  * g_variant_unref() when it is no longer required.
328  *
329  * Returns: (transfer full): the state range hint
330  *
331  * Since: 2.28
332  **/
333 GVariant *
334 g_action_get_state_hint (GAction *action)
335 {
336   g_return_val_if_fail (G_IS_ACTION (action), NULL);
337
338   return G_ACTION_GET_IFACE (action)
339     ->get_state_hint (action);
340 }
341
342 /**
343  * g_action_get_enabled:
344  * @action: a #GAction
345  *
346  * Checks if @action is currently enabled.
347  *
348  * An action must be enabled in order to be activated or in order to
349  * have its state changed from outside callers.
350  *
351  * Returns: whether the action is enabled
352  *
353  * Since: 2.28
354  **/
355 gboolean
356 g_action_get_enabled (GAction *action)
357 {
358   g_return_val_if_fail (G_IS_ACTION (action), FALSE);
359
360   return G_ACTION_GET_IFACE (action)
361     ->get_enabled (action);
362 }
363
364 /**
365  * g_action_activate:
366  * @action: a #GAction
367  * @parameter: (allow-none): the parameter to the activation
368  *
369  * Activates the action.
370  *
371  * @parameter must be the correct type of parameter for the action (ie:
372  * the parameter type given at construction time).  If the parameter
373  * type was %NULL then @parameter must also be %NULL.
374  *
375  * Since: 2.28
376  **/
377 void
378 g_action_activate (GAction  *action,
379                    GVariant *parameter)
380 {
381   g_return_if_fail (G_IS_ACTION (action));
382
383   if (parameter != NULL)
384     g_variant_ref_sink (parameter);
385
386   G_ACTION_GET_IFACE (action)
387     ->activate (action, parameter);
388
389   if (parameter != NULL)
390     g_variant_unref (parameter);
391 }