add testcase for GAction
[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 "gaction.h"
23
24 G_DEFINE_TYPE (GAction, g_action, G_TYPE_OBJECT)
25
26 /**
27  * SECTION:gaction
28  * @title: GAction
29  * @short_description: an action
30  *
31  * #GAction represents a single named action.
32  *
33  * The main interface to an action is that it can be activated with
34  * g_action_activate().  This results in the 'activate' signal being
35  * emitted.  An activation has a #GVariant parameter (which may be
36  * %NULL).  The correct type for the parameter is determined by a static
37  * parameter type (which is given at construction time).
38  *
39  * An action may optionally have a state, in which case the state may be
40  * set with g_action_set_state().  This call takes a #GVariant.  The
41  * correct type for the state is determined by a static state type
42  * (which is given at construction time).
43  *
44  * The state may have a hint associated with it, specifying its valid
45  * range.
46  *
47  * #GAction is intended to be used both as a simple action class and as
48  * a base class for more complicated action types.  The base class
49  * itself supports activation and state.  Not supported are state hints
50  * and filtering requests to set the state based on the requested value.
51  * You should subclass if you require either of these abilities.
52  *
53  * In all cases, the base class is responsible for storing the name of
54  * the action, the parameter type, the enabled state, the optional state
55  * type and the state and emitting the appropriate signals when these
56  * change.  The base class is also responsbile for filtering calls to
57  * g_action_activate() and g_action_set_state() for type safety and for
58  * 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 struct _GActionPrivate
65 {
66   gchar        *name;
67   GVariantType *parameter_type;
68   gboolean      enabled;
69
70   GVariantType *state_type;
71   GVariant     *state;
72 };
73
74 enum
75 {
76   PROP_NONE,
77   PROP_NAME,
78   PROP_PARAMETER_TYPE,
79   PROP_ENABLED,
80   PROP_STATE_TYPE,
81   PROP_STATE
82 };
83
84 enum
85 {
86   SIGNAL_ACTIVATE,
87   NR_SIGNALS
88 };
89
90 static guint g_action_signals[NR_SIGNALS];
91
92 static void
93 g_action_real_set_state (GAction  *action,
94                          GVariant *value)
95 {
96   if (action->priv->state == value)
97     return;
98
99   if (!action->priv->state || !g_variant_equal (action->priv->state, value))
100     {
101       if (action->priv->state)
102         g_variant_unref (action->priv->state);
103
104       action->priv->state = g_variant_ref (value);
105
106       g_object_notify (G_OBJECT (action), "state");
107     }
108 }
109
110 static GVariant *
111 g_action_real_get_state_hint (GAction *action)
112 {
113   return NULL;
114 }
115
116 static void
117 g_action_set_property (GObject *object, guint prop_id,
118                        const GValue *value, GParamSpec *pspec)
119 {
120   GAction *action = G_ACTION (object);
121
122   switch (prop_id)
123     {
124     case PROP_NAME:
125       g_assert (action->priv->name == NULL);
126       action->priv->name = g_value_dup_string (value);
127       break;
128
129     case PROP_PARAMETER_TYPE:
130       g_assert (action->priv->parameter_type == NULL);
131       action->priv->parameter_type = g_value_dup_boxed (value);
132       break;
133
134     case PROP_ENABLED:
135       g_action_set_enabled (action, g_value_get_boolean (value));
136       break;
137
138     case PROP_STATE_TYPE:
139       g_assert (action->priv->state_type == NULL);
140       action->priv->state_type = g_value_dup_boxed (value);
141       break;
142
143     case PROP_STATE:
144       if (g_value_get_variant (value))
145         g_action_set_state (action, g_value_get_variant (value));
146       break;
147
148     default:
149       g_assert_not_reached ();
150     }
151 }
152
153 static void
154 g_action_get_property (GObject *object, guint prop_id,
155                        GValue *value, GParamSpec *pspec)
156 {
157   GAction *action = G_ACTION (object);
158
159   switch (prop_id)
160     {
161     case PROP_NAME:
162       g_value_set_string (value, g_action_get_name (action));
163       break;
164
165     case PROP_PARAMETER_TYPE:
166       g_value_set_boxed (value, g_action_get_parameter_type (action));
167       break;
168
169     case PROP_ENABLED:
170       g_value_set_boolean (value, g_action_get_enabled (action));
171       break;
172
173     case PROP_STATE_TYPE:
174       g_value_set_boxed (value, g_action_get_state_type (action));
175       break;
176
177     case PROP_STATE:
178       g_value_set_variant (value, g_action_get_state (action));
179       break;
180
181     default:
182       g_assert_not_reached ();
183     }
184 }
185
186 static void
187 g_action_finalize (GObject *object)
188 {
189   GAction *action = G_ACTION (object);
190
191   g_free (action->priv->name);
192   if (action->priv->parameter_type)
193     g_variant_type_free (action->priv->parameter_type);
194   if (action->priv->state_type)
195     g_variant_type_free (action->priv->state_type);
196   if (action->priv->state)
197     g_variant_unref (action->priv->state);
198
199   G_OBJECT_CLASS (g_action_parent_class)
200     ->finalize (object);
201 }
202
203 void
204 g_action_init (GAction *action)
205 {
206   action->priv = G_TYPE_INSTANCE_GET_PRIVATE (action,
207                                               G_TYPE_ACTION,
208                                               GActionPrivate);
209 }
210
211 void
212 g_action_class_init (GActionClass *class)
213 {
214   GObjectClass *object_class = G_OBJECT_CLASS (class);
215
216   class->get_state_hint = g_action_real_get_state_hint;
217   class->set_state = g_action_real_set_state;
218
219   object_class->get_property = g_action_get_property;
220   object_class->set_property = g_action_set_property;
221   object_class->finalize = g_action_finalize;
222
223   /**
224    * GAction::activate:
225    * @action: the #GAction
226    * @parameter: (allow-none): the parameter to the activation
227    *
228    * Indicates that the action was just activated.
229    *
230    * @parameter will always be of the expected type.  In the event that
231    * an incorrect type was given, no signal will be emitted.
232    **/
233   g_action_signals[SIGNAL_ACTIVATE] =
234     g_signal_new ("activate", G_TYPE_ACTION, G_SIGNAL_RUN_LAST,
235                   G_STRUCT_OFFSET (GActionClass, activate),
236                   NULL, NULL, g_cclosure_marshal_VOID__VARIANT,
237                   G_TYPE_NONE, 1, G_TYPE_VARIANT);
238
239   /**
240    * GAction:name:
241    *
242    * The name of the action.  This is mostly meaningful for identifying
243    * the action once it has been added to a #GActionGroup.
244    **/
245   g_object_class_install_property (object_class, PROP_NAME,
246     g_param_spec_string ("name", "action name",
247                          "the name used to invoke the action",
248                          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
249                          G_PARAM_STATIC_STRINGS));
250
251   /**
252    * GAction:parameter-type:
253    *
254    * The type of the parameter that must be given when activating the
255    * action.
256    **/
257   g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
258     g_param_spec_boxed ("parameter-type", "parameter type",
259                         "the type of GVariant passed to activate()",
260                         G_TYPE_VARIANT_TYPE, G_PARAM_READWRITE |
261                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
262
263   /**
264    * GAction:enabled:
265    *
266    * If @action is currently enabled.
267    *
268    * If the action is disabled then calls to g_action_activate() and
269    * g_action_set_state() have no effect.
270    **/
271   g_object_class_install_property (object_class, PROP_ENABLED,
272     g_param_spec_boolean ("enabled", "enabled",
273                           "if the action can be activated", TRUE,
274                           G_PARAM_CONSTRUCT | G_PARAM_READWRITE |
275                           G_PARAM_STATIC_STRINGS));
276
277   /**
278    * GAction:state-type:
279    *
280    * The #GVariantType of the state that the action has, or %NULL if the
281    * action is stateless.
282    **/
283   g_object_class_install_property (object_class, PROP_STATE_TYPE,
284     g_param_spec_boxed ("state-type", "state type",
285                         "the type of the state kept by the action",
286                         G_TYPE_VARIANT_TYPE, G_PARAM_READWRITE |
287                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
288
289   /**
290    * GAction:state:
291    *
292    * The state of the action, or %NULL if the action is stateless.
293    **/
294   g_object_class_install_property (object_class, PROP_STATE,
295     g_param_spec_variant ("state", "state", "the state the action is in",
296                           G_VARIANT_TYPE_ANY, NULL,
297                           G_PARAM_CONSTRUCT | G_PARAM_READWRITE |
298                           G_PARAM_STATIC_STRINGS));
299
300   g_type_class_add_private (class, sizeof (GActionPrivate));
301 }
302
303 /**
304  * g_action_set_state:
305  * @action: a #GAction
306  * @value: the new state
307  *
308  * Request for the state of @action to be changed to @value.
309  *
310  * The action must be stateful and @value must be of the correct type.
311  * See g_action_get_state_type().
312  *
313  * This call merely requests a change.  The action may refuse to change
314  * its state or may change its state to something other than @value.
315  * See g_action_get_state_hint().
316  *
317  * Since: 2.26
318  **/
319 void
320 g_action_set_state (GAction  *action,
321                     GVariant *value)
322 {
323   g_return_if_fail (G_IS_ACTION (action));
324   g_return_if_fail ((action->priv->state_type == NULL && value == NULL) ||
325                     g_variant_is_of_type (value, action->priv->state_type));
326
327   g_variant_ref_sink (value);
328
329   if (action->priv->enabled)
330     G_ACTION_GET_CLASS (action)
331       ->set_state (action, value);
332
333   g_variant_unref (value);
334 }
335
336 /**
337  * g_action_get_state:
338  * @action: a #GAction
339  *
340  * Queries the current state of @action.
341  *
342  * If the action is not stateful then %NULL will be returned.  If the
343  * action is stateful then the type of the return value is the type
344  * given by g_action_get_state_type().
345  *
346  * The return value (if non-%NULL) should be freed with
347  * g_variant_unref() when it is no longer required.
348  *
349  * Returns: (allow-none): the current state of the action
350  *
351  * Since: 2.26
352  **/
353 GVariant *
354 g_action_get_state (GAction *action)
355 {
356   g_return_val_if_fail (G_IS_ACTION (action), NULL);
357
358   return action->priv->state;
359 }
360
361 /**
362  * g_action_get_name:
363  * @action: a #GAction
364  *
365  * Queries the name of @action.
366  *
367  * Returns: the name of the action
368  *
369  * Since: 2.26
370  **/
371 const gchar *
372 g_action_get_name (GAction *action)
373 {
374   g_return_val_if_fail (G_IS_ACTION (action), NULL);
375
376   return action->priv->name;
377 }
378
379 /**
380  * g_action_get_parameter_type:
381  * @action: a #GAction
382  *
383  * Queries the type of the parameter that must be given when activating
384  * @action.
385  *
386  * When activating the action using g_action_activate(), the #GVariant
387  * given to that function must be of the type returned by this function.
388  *
389  * In the case that this function returns %NULL, you must not give any
390  * #GVariant, but %NULL instead.
391  *
392  * Returns: (allow-none): the parameter type
393  *
394  * Since: 2.26
395  **/
396 const GVariantType *
397 g_action_get_parameter_type (GAction *action)
398 {
399   g_return_val_if_fail (G_IS_ACTION (action), NULL);
400
401   return action->priv->parameter_type;
402 }
403
404 /**
405  * g_action_get_state_type:
406  * @action: a #GAction
407  *
408  * Queries the type of the state of @action.
409  *
410  * If the action is stateful (ie: was created with
411  * g_action_new_stateful()) then this function returns the #GVariantType
412  * of the state.  This is the type of the initial value given as the
413  * state.  All calls to g_action_set_state() must give a #GVariant of
414  * this type and g_action_get_state() will return a #GVariant of the
415  * same type.
416  *
417  * If the action is not stateful (ie: created with g_action_new()) then
418  * this function will return %NULL.  In that case, g_action_get_state()
419  * will return %NULL and you must not call g_action_set_state().
420  *
421  * Returns: (allow-none): the state type, if the action is stateful
422  *
423  * Since: 2.26
424  **/
425 const GVariantType *
426 g_action_get_state_type (GAction *action)
427 {
428   g_return_val_if_fail (G_IS_ACTION (action), NULL);
429
430   return action->priv->state_type;
431 }
432
433 /**
434  * g_action_get_state_hint:
435  * @action: a #GAction
436  *
437  * Requests a hint about the valid range of values for the state of
438  * @action.
439  *
440  * If %NULL is returned it either means that the action is not stateful
441  * or that there is no hint about the valid range of values for the
442  * state of the action.
443  *
444  * If a #GVariant array is returned then each item in the array is a
445  * possible value for the state.  If a #GVariant pair (ie: two-tuple) is
446  * returned then the tuple specifies the inclusive lower and upper bound
447  * of valid values for the state.
448  *
449  * In any case, the information is merely a hint.  It may be possible to
450  * have a state value outside of the hinted range and setting a value
451  * within the range may fail.
452  *
453  * The return value (if non-%NULL) should be freed with
454  * g_variant_unref() when it is no longer required.
455  *
456  * Returns: (allow-none): the state range hint
457  *
458  * Since: 2.26
459  **/
460 GVariant *
461 g_action_get_state_hint (GAction *action)
462 {
463   g_return_val_if_fail (G_IS_ACTION (action), NULL);
464
465   return G_ACTION_GET_CLASS (action)
466     ->get_state_hint (action);
467 }
468
469 /**
470  * g_action_get_enabled:
471  * @action: a #GAction
472  *
473  * Checks if @action is currently enabled.
474  *
475  * An action must be enabled in order to be activated or in order to
476  * have its state changed from outside callers.
477  *
478  * Returns: whether the action is enabled
479  *
480  * Since: 2.26
481  **/
482 gboolean
483 g_action_get_enabled (GAction *action)
484 {
485   g_return_val_if_fail (G_IS_ACTION (action), FALSE);
486
487   return action->priv->enabled;
488 }
489
490 /**
491  * g_action_set_enabled:
492  * @action: a #GAction
493  * @enabled: whether the action is enabled
494  *
495  * Sets the action as enabled or not.
496  *
497  * An action must be enabled in order to be activated or in order to
498  * have its state changed from outside callers.
499  *
500  * Since: 2.26
501  **/
502 void
503 g_action_set_enabled (GAction  *action,
504                       gboolean  enabled)
505 {
506   g_return_if_fail (G_IS_ACTION (action));
507
508   enabled = !!enabled;
509
510   if (action->priv->enabled != enabled)
511     {
512       action->priv->enabled = enabled;
513       g_object_notify (G_OBJECT (action), "enabled");
514     }
515 }
516
517 /**
518  * g_action_activate:
519  * @action: a #GAction
520  * @parameter: the parameter to the activation
521  *
522  * Activates the action.
523  *
524  * @parameter must be the correct type of parameter for the action (ie:
525  * the parameter type given at construction time).  If the parameter
526  * type was %NULL then @parameter must also be %NULL.
527  *
528  * Since: 2.26
529  **/
530 void
531 g_action_activate (GAction  *action,
532                    GVariant *parameter)
533 {
534   g_return_if_fail (G_IS_ACTION (action));
535
536   g_return_if_fail (action->priv->parameter_type == NULL ?
537                       parameter == NULL :
538                     (parameter != NULL &&
539                      g_variant_is_of_type (parameter,
540                                            action->priv->parameter_type)));
541
542   if (parameter)
543     g_variant_ref_sink (parameter);
544
545   if (action->priv->enabled)
546     g_signal_emit (action, g_action_signals[SIGNAL_ACTIVATE], 0, parameter);
547
548   if (parameter)
549     g_variant_unref (parameter);
550 }
551
552 /**
553  * g_action_new:
554  * @name: the name of the action
555  * @parameter_type: the type of parameter to the activate function
556  *
557  * Creates a new action.
558  *
559  * The created action is stateless.  See g_action_new_stateful().
560  *
561  * Returns: a new #GAction
562  *
563  * Since: 2.26
564  **/
565 GAction *
566 g_action_new (const gchar        *name,
567               const GVariantType *parameter_type)
568 {
569   return g_object_new (G_TYPE_ACTION,
570                        "name", name,
571                        "parameter-type", parameter_type,
572                        NULL);
573 }
574
575 /**
576  * g_action_new_stateful:
577  * @name: the name of the action
578  * @parameter_type: the type of the parameter to the activate function
579  * @state: the initial state of the action
580  *
581  * Creates a new stateful action.
582  *
583  * @state is the initial state of the action.  All future state values
584  * must have the same #GVariantType as the initial state.
585  *
586  * Returns: a new #GAction
587  *
588  * Since: 2.26
589  **/
590 GAction *
591 g_action_new_stateful (const gchar        *name,
592                        const GVariantType *parameter_type,
593                        GVariant           *state)
594 {
595   return g_object_new (G_TYPE_ACTION,
596                        "name", name,
597                        "parameter-type", parameter_type,
598                        "state", state,
599                        NULL);
600 }