Make 4 incompatible changes to the GAction API
[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
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 void
65 g_action_default_init (GActionInterface *iface)
66 {
67   /**
68    * GAction:name:
69    *
70    * The name of the action.  This is mostly meaningful for identifying
71    * the action once it has been added to a #GActionGroup.
72    *
73    * Since: 2.28
74    **/
75   g_object_interface_install_property (iface,
76                                        g_param_spec_string ("name",
77                                                             P_("Action Name"),
78                                                             P_("The name used to invoke the action"),
79                                                             NULL,
80                                                             G_PARAM_READWRITE |
81                                                             G_PARAM_CONSTRUCT_ONLY |
82                                                             G_PARAM_STATIC_STRINGS));
83
84   /**
85    * GAction:parameter-type:
86    *
87    * The type of the parameter that must be given when activating the
88    * action.
89    *
90    * Since: 2.28
91    **/
92   g_object_interface_install_property (iface,
93                                        g_param_spec_boxed ("parameter-type",
94                                                            P_("Parameter Type"),
95                                                            P_("The type of GVariant passed to activate()"),
96                                                            G_TYPE_VARIANT_TYPE,
97                                                            G_PARAM_READWRITE |
98                                                            G_PARAM_CONSTRUCT_ONLY |
99                                                            G_PARAM_STATIC_STRINGS));
100
101   /**
102    * GAction:enabled:
103    *
104    * If @action is currently enabled.
105    *
106    * If the action is disabled then calls to g_action_activate() and
107    * g_action_change_state() have no effect.
108    *
109    * Since: 2.28
110    **/
111   g_object_interface_install_property (iface,
112                                        g_param_spec_boolean ("enabled",
113                                                              P_("Enabled"),
114                                                              P_("If the action can be activated"),
115                                                              TRUE,
116                                                              G_PARAM_CONSTRUCT |
117                                                              G_PARAM_READWRITE |
118                                                              G_PARAM_STATIC_STRINGS));
119
120   /**
121    * GAction:state-type:
122    *
123    * The #GVariantType of the state that the action has, or %NULL if the
124    * action is stateless.
125    *
126    * Since: 2.28
127    **/
128   g_object_interface_install_property (iface,
129                                        g_param_spec_boxed ("state-type",
130                                                            P_("State Type"),
131                                                            P_("The type of the state kept by the action"),
132                                                            G_TYPE_VARIANT_TYPE,
133                                                            G_PARAM_READABLE |
134                                                            G_PARAM_STATIC_STRINGS));
135
136   /**
137    * GAction:state:
138    *
139    * The state of the action, or %NULL if the action is stateless.
140    *
141    * Since: 2.28
142    **/
143   g_object_interface_install_property (iface,
144                                        g_param_spec_variant ("state",
145                                                              P_("State"),
146                                                              P_("The state the action is in"),
147                                                              G_VARIANT_TYPE_ANY,
148                                                              NULL,
149                                                              G_PARAM_READABLE |
150                                                              G_PARAM_STATIC_STRINGS));
151 }
152
153 /**
154  * g_action_change_state:
155  * @action: a #GAction
156  * @value: the new state
157  *
158  * Request for the state of @action to be changed to @value.
159  *
160  * The action must be stateful and @value must be of the correct type.
161  * See g_action_get_state_type().
162  *
163  * This call merely requests a change.  The action may refuse to change
164  * its state or may change its state to something other than @value.
165  * See g_action_get_state_hint().
166  *
167  * If the @value GVariant is floating, it is consumed.
168  *
169  * Since: 2.30
170  **/
171 void
172 g_action_change_state (GAction  *action,
173                        GVariant *value)
174 {
175   const GVariantType *state_type;
176
177   g_return_if_fail (G_IS_ACTION (action));
178   g_return_if_fail (value != NULL);
179   state_type = g_action_get_state_type (action);
180   g_return_if_fail (state_type != NULL);
181   g_return_if_fail (g_variant_is_of_type (value, state_type));
182
183   g_variant_ref_sink (value);
184
185   G_ACTION_GET_IFACE (action)
186     ->change_state (action, value);
187
188   g_variant_unref (value);
189 }
190
191 /**
192  * g_action_get_state:
193  * @action: a #GAction
194  *
195  * Queries the current state of @action.
196  *
197  * If the action is not stateful then %NULL will be returned.  If the
198  * action is stateful then the type of the return value is the type
199  * given by g_action_get_state_type().
200  *
201  * The return value (if non-%NULL) should be freed with
202  * g_variant_unref() when it is no longer required.
203  *
204  * Returns: (transfer full): the current state of the action
205  *
206  * Since: 2.28
207  **/
208 GVariant *
209 g_action_get_state (GAction *action)
210 {
211   g_return_val_if_fail (G_IS_ACTION (action), NULL);
212
213   return G_ACTION_GET_IFACE (action)
214     ->get_state (action);
215 }
216
217 /**
218  * g_action_get_name:
219  * @action: a #GAction
220  *
221  * Queries the name of @action.
222  *
223  * Returns: the name of the action
224  *
225  * Since: 2.28
226  **/
227 const gchar *
228 g_action_get_name (GAction *action)
229 {
230   g_return_val_if_fail (G_IS_ACTION (action), NULL);
231
232   return G_ACTION_GET_IFACE (action)
233     ->get_name (action);
234 }
235
236 /**
237  * g_action_get_parameter_type:
238  * @action: a #GAction
239  *
240  * Queries the type of the parameter that must be given when activating
241  * @action.
242  *
243  * When activating the action using g_action_activate(), the #GVariant
244  * given to that function must be of the type returned by this function.
245  *
246  * In the case that this function returns %NULL, you must not give any
247  * #GVariant, but %NULL instead.
248  *
249  * Returns: (allow-none): the parameter type
250  *
251  * Since: 2.28
252  **/
253 const GVariantType *
254 g_action_get_parameter_type (GAction *action)
255 {
256   g_return_val_if_fail (G_IS_ACTION (action), NULL);
257
258   return G_ACTION_GET_IFACE (action)
259     ->get_parameter_type (action);
260 }
261
262 /**
263  * g_action_get_state_type:
264  * @action: a #GAction
265  *
266  * Queries the type of the state of @action.
267  *
268  * If the action is stateful (e.g. created with
269  * g_simple_action_new_stateful()) then this function returns the
270  * #GVariantType of the state.  This is the type of the initial value
271  * given as the state. All calls to g_action_change_state() must give a
272  * #GVariant of this type and g_action_get_state() will return a
273  * #GVariant of the same type.
274  *
275  * If the action is not stateful (e.g. created with g_simple_action_new())
276  * then this function will return %NULL. In that case, g_action_get_state()
277  * will return %NULL and you must not call g_action_change_state().
278  *
279  * Returns: (allow-none): the state type, if the action is stateful
280  *
281  * Since: 2.28
282  **/
283 const GVariantType *
284 g_action_get_state_type (GAction *action)
285 {
286   g_return_val_if_fail (G_IS_ACTION (action), NULL);
287
288   return G_ACTION_GET_IFACE (action)
289     ->get_state_type (action);
290 }
291
292 /**
293  * g_action_get_state_hint:
294  * @action: a #GAction
295  *
296  * Requests a hint about the valid range of values for the state of
297  * @action.
298  *
299  * If %NULL is returned it either means that the action is not stateful
300  * or that there is no hint about the valid range of values for the
301  * state of the action.
302  *
303  * If a #GVariant array is returned then each item in the array is a
304  * possible value for the state.  If a #GVariant pair (ie: two-tuple) is
305  * returned then the tuple specifies the inclusive lower and upper bound
306  * of valid values for the state.
307  *
308  * In any case, the information is merely a hint.  It may be possible to
309  * have a state value outside of the hinted range and setting a value
310  * within the range may fail.
311  *
312  * The return value (if non-%NULL) should be freed with
313  * g_variant_unref() when it is no longer required.
314  *
315  * Returns: (transfer full): the state range hint
316  *
317  * Since: 2.28
318  **/
319 GVariant *
320 g_action_get_state_hint (GAction *action)
321 {
322   g_return_val_if_fail (G_IS_ACTION (action), NULL);
323
324   return G_ACTION_GET_IFACE (action)
325     ->get_state_hint (action);
326 }
327
328 /**
329  * g_action_get_enabled:
330  * @action: a #GAction
331  *
332  * Checks if @action is currently enabled.
333  *
334  * An action must be enabled in order to be activated or in order to
335  * have its state changed from outside callers.
336  *
337  * Returns: whether the action is enabled
338  *
339  * Since: 2.28
340  **/
341 gboolean
342 g_action_get_enabled (GAction *action)
343 {
344   g_return_val_if_fail (G_IS_ACTION (action), FALSE);
345
346   return G_ACTION_GET_IFACE (action)
347     ->get_enabled (action);
348 }
349
350 /**
351  * g_action_activate:
352  * @action: a #GAction
353  * @parameter: (allow-none): the parameter to the activation
354  *
355  * Activates the action.
356  *
357  * @parameter must be the correct type of parameter for the action (ie:
358  * the parameter type given at construction time).  If the parameter
359  * type was %NULL then @parameter must also be %NULL.
360  *
361  * Since: 2.28
362  **/
363 void
364 g_action_activate (GAction  *action,
365                    GVariant *parameter)
366 {
367   g_return_if_fail (G_IS_ACTION (action));
368
369   if (parameter != NULL)
370     g_variant_ref_sink (parameter);
371
372   G_ACTION_GET_IFACE (action)
373     ->activate (action, parameter);
374
375   if (parameter != NULL)
376     g_variant_unref (parameter);
377 }