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