GSimpleAction: don't allow changing state type
[platform/upstream/glib.git] / gio / gsimpleaction.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
24 #include "gsimpleaction.h"
25
26 #include "gaction.h"
27 #include "glibintl.h"
28
29 /**
30  * SECTION:gsimpleaction
31  * @title: GSimpleAction
32  * @short_description: A simple GAction implementation
33  *
34  * A #GSimpleAction is the obvious simple implementation of the #GAction
35  * interface. This is the easiest way to create an action for purposes of
36  * adding it to a #GSimpleActionGroup.
37  *
38  * See also #GtkAction.
39  */
40 struct _GSimpleAction
41 {
42   GObject       parent_instance;
43
44   gchar        *name;
45   GVariantType *parameter_type;
46   gboolean      enabled;
47   GVariant     *state;
48   gboolean      state_set_already;
49 };
50
51 typedef GObjectClass GSimpleActionClass;
52
53 static void g_simple_action_iface_init (GActionInterface *iface);
54 G_DEFINE_TYPE_WITH_CODE (GSimpleAction, g_simple_action, G_TYPE_OBJECT,
55   G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_simple_action_iface_init))
56
57 enum
58 {
59   PROP_NONE,
60   PROP_NAME,
61   PROP_PARAMETER_TYPE,
62   PROP_ENABLED,
63   PROP_STATE_TYPE,
64   PROP_STATE
65 };
66
67 enum
68 {
69   SIGNAL_CHANGE_STATE,
70   SIGNAL_ACTIVATE,
71   NR_SIGNALS
72 };
73
74 static guint g_simple_action_signals[NR_SIGNALS];
75
76 static const gchar *
77 g_simple_action_get_name (GAction *action)
78 {
79   GSimpleAction *simple = G_SIMPLE_ACTION (action);
80
81   return simple->name;
82 }
83
84 static const GVariantType *
85 g_simple_action_get_parameter_type (GAction *action)
86 {
87   GSimpleAction *simple = G_SIMPLE_ACTION (action);
88
89   return simple->parameter_type;
90 }
91
92 static const GVariantType *
93 g_simple_action_get_state_type (GAction *action)
94 {
95   GSimpleAction *simple = G_SIMPLE_ACTION (action);
96
97   if (simple->state != NULL)
98     return g_variant_get_type (simple->state);
99   else
100     return NULL;
101 }
102
103 static GVariant *
104 g_simple_action_get_state_hint (GAction *action)
105 {
106   return NULL;
107 }
108
109 static gboolean
110 g_simple_action_get_enabled (GAction *action)
111 {
112   GSimpleAction *simple = G_SIMPLE_ACTION (action);
113
114   return simple->enabled;
115 }
116
117 static void
118 g_simple_action_change_state (GAction  *action,
119                               GVariant *value)
120 {
121   GSimpleAction *simple = G_SIMPLE_ACTION (action);
122
123   /* If the user connected a signal handler then they are responsible
124    * for handling state changes.
125    */
126   if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, TRUE))
127     g_signal_emit (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, value);
128
129   /* If not, then the default behaviour is to just set the state. */
130   else
131     g_simple_action_set_state (simple, value);
132 }
133
134 /**
135  * g_simple_action_set_state:
136  * @simple: a #GSimpleAction
137  * @value: the new #GVariant for the state
138  *
139  * Sets the state of the action.
140  *
141  * This directly updates the 'state' property to the given value.
142  *
143  * This should only be called by the implementor of the action.  Users
144  * of the action should not attempt to directly modify the 'state'
145  * property.  Instead, they should call g_action_change_state() to
146  * request the change.
147  *
148  * If the @value GVariant is floating, it is consumed.
149  *
150  * Since: 2.30
151  **/
152 void
153 g_simple_action_set_state (GSimpleAction *simple,
154                            GVariant      *value)
155 {
156   g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
157   g_return_if_fail (value != NULL);
158
159   {
160     const GVariantType *state_type;
161
162     state_type = simple->state ?
163                    g_variant_get_type (simple->state) : NULL;
164     g_return_if_fail (state_type != NULL);
165     g_return_if_fail (g_variant_is_of_type (value, state_type));
166   }
167
168   g_variant_ref_sink (value);
169
170   if (!simple->state || !g_variant_equal (simple->state, value))
171     {
172       if (simple->state)
173         g_variant_unref (simple->state);
174
175       simple->state = g_variant_ref (value);
176
177       g_object_notify (G_OBJECT (simple), "state");
178     }
179
180   g_variant_unref (value);
181 }
182
183 static GVariant *
184 g_simple_action_get_state (GAction *action)
185 {
186   GSimpleAction *simple = G_SIMPLE_ACTION (action);
187
188   return simple->state ? g_variant_ref (simple->state) : NULL;
189 }
190
191 static void
192 g_simple_action_activate (GAction  *action,
193                           GVariant *parameter)
194 {
195   GSimpleAction *simple = G_SIMPLE_ACTION (action);
196
197   g_return_if_fail (simple->parameter_type == NULL ?
198                       parameter == NULL :
199                     (parameter != NULL &&
200                      g_variant_is_of_type (parameter,
201                                            simple->parameter_type)));
202
203   if (parameter != NULL)
204     g_variant_ref_sink (parameter);
205
206   if (simple->enabled)
207     g_signal_emit (simple, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
208
209   if (parameter != NULL)
210     g_variant_unref (parameter);
211 }
212
213 static void
214 g_simple_action_set_property (GObject    *object,
215                               guint       prop_id,
216                               const GValue     *value,
217                               GParamSpec *pspec)
218 {
219   GSimpleAction *action = G_SIMPLE_ACTION (object);
220
221   switch (prop_id)
222     {
223     case PROP_NAME:
224       action->name = g_strdup (g_value_get_string (value));
225       break;
226
227     case PROP_PARAMETER_TYPE:
228       action->parameter_type = g_value_dup_boxed (value);
229       break;
230
231     case PROP_ENABLED:
232       action->enabled = g_value_get_boolean (value);
233       break;
234
235     case PROP_STATE:
236       /* The first time we see this (during construct) we should just
237        * take the state as it was handed to us.
238        *
239        * After that, we should make sure we go through the same checks
240        * as the C API.
241        */
242       if (!action->state_set_already)
243         {
244           action->state = g_value_dup_variant (value);
245           action->state_set_already = TRUE;
246         }
247       else
248         g_simple_action_set_state (action, g_value_get_variant (value));
249
250       break;
251
252     default:
253       g_assert_not_reached ();
254     }
255 }
256
257 static void
258 g_simple_action_get_property (GObject    *object,
259                               guint       prop_id,
260                               GValue     *value,
261                               GParamSpec *pspec)
262 {
263   GAction *action = G_ACTION (object);
264
265   switch (prop_id)
266     {
267     case PROP_NAME:
268       g_value_set_string (value, g_simple_action_get_name (action));
269       break;
270
271     case PROP_PARAMETER_TYPE:
272       g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
273       break;
274
275     case PROP_ENABLED:
276       g_value_set_boolean (value, g_simple_action_get_enabled (action));
277       break;
278
279     case PROP_STATE_TYPE:
280       g_value_set_boxed (value, g_simple_action_get_state_type (action));
281       break;
282
283     case PROP_STATE:
284       g_value_take_variant (value, g_simple_action_get_state (action));
285       break;
286
287     default:
288       g_assert_not_reached ();
289     }
290 }
291
292 static void
293 g_simple_action_finalize (GObject *object)
294 {
295   GSimpleAction *simple = G_SIMPLE_ACTION (object);
296
297   g_free (simple->name);
298   if (simple->parameter_type)
299     g_variant_type_free (simple->parameter_type);
300   if (simple->state)
301     g_variant_unref (simple->state);
302
303   G_OBJECT_CLASS (g_simple_action_parent_class)
304     ->finalize (object);
305 }
306
307 void
308 g_simple_action_init (GSimpleAction *simple)
309 {
310   simple->enabled = TRUE;
311 }
312
313 void
314 g_simple_action_iface_init (GActionInterface *iface)
315 {
316   iface->get_name = g_simple_action_get_name;
317   iface->get_parameter_type = g_simple_action_get_parameter_type;
318   iface->get_state_type = g_simple_action_get_state_type;
319   iface->get_state_hint = g_simple_action_get_state_hint;
320   iface->get_enabled = g_simple_action_get_enabled;
321   iface->get_state = g_simple_action_get_state;
322   iface->change_state = g_simple_action_change_state;
323   iface->activate = g_simple_action_activate;
324 }
325
326 void
327 g_simple_action_class_init (GSimpleActionClass *class)
328 {
329   GObjectClass *object_class = G_OBJECT_CLASS (class);
330
331   object_class->set_property = g_simple_action_set_property;
332   object_class->get_property = g_simple_action_get_property;
333   object_class->finalize = g_simple_action_finalize;
334
335   /**
336    * GSimpleAction::activate:
337    * @simple: the #GSimpleAction
338    * @parameter: (allow-none): the parameter to the activation
339    *
340    * Indicates that the action was just activated.
341    *
342    * @parameter will always be of the expected type.  In the event that
343    * an incorrect type was given, no signal will be emitted.
344    *
345    * Since: 2.28
346    */
347   g_simple_action_signals[SIGNAL_ACTIVATE] =
348     g_signal_new (I_("activate"),
349                   G_TYPE_SIMPLE_ACTION,
350                   G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
351                   0, NULL, NULL,
352                   g_cclosure_marshal_VOID__VARIANT,
353                   G_TYPE_NONE, 1,
354                   G_TYPE_VARIANT);
355
356   /**
357    * GSimpleAction::change-state:
358    * @simple: the #GSimpleAction
359    * @value: (allow-none): the requested value for the state
360    *
361    * Indicates that the action just received a request to change its
362    * state.
363    *
364    * @value will always be of the correct state type.  In the event that
365    * an incorrect type was given, no signal will be emitted.
366    *
367    * If no handler is connected to this signal then the default
368    * behaviour is to call g_simple_action_set_state() to set the state
369    * to the requested value.  If you connect a signal handler then no
370    * default action is taken.  If the state should change then you must
371    * call g_simple_action_set_state() from the handler.
372    *
373    * <example>
374    * <title>Example 'change-state' handler</title>
375    * <programlisting>
376    * static void
377    * change_volume_state (GSimpleAction *action,
378    *                      GVariant      *value,
379    *                      gpointer       user_data)
380    * {
381    *   gint requested;
382    *
383    *   requested = g_variant_get_int32 (value);
384    *
385    *   // Volume only goes from 0 to 10
386    *   if (0 <= requested && requested <= 10)
387    *     g_simple_action_set_state (action, value);
388    * }
389    * </programlisting>
390    * </example>
391    *
392    * The handler need not set the state to the requested value.  It
393    * could set it to any value at all, or take some other action.
394    *
395    * Since: 2.30
396    */
397   g_simple_action_signals[SIGNAL_CHANGE_STATE] =
398     g_signal_new (I_("change-state"),
399                   G_TYPE_SIMPLE_ACTION,
400                   G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
401                   0, NULL, NULL,
402                   g_cclosure_marshal_VOID__VARIANT,
403                   G_TYPE_NONE, 1,
404                   G_TYPE_VARIANT);
405
406   /**
407    * GSimpleAction:name:
408    *
409    * The name of the action.  This is mostly meaningful for identifying
410    * the action once it has been added to a #GSimpleActionGroup.
411    *
412    * Since: 2.28
413    **/
414   g_object_class_install_property (object_class, PROP_NAME,
415                                    g_param_spec_string ("name",
416                                                         P_("Action Name"),
417                                                         P_("The name used to invoke the action"),
418                                                         NULL,
419                                                         G_PARAM_READWRITE |
420                                                         G_PARAM_CONSTRUCT_ONLY |
421                                                         G_PARAM_STATIC_STRINGS));
422
423   /**
424    * GSimpleAction:parameter-type:
425    *
426    * The type of the parameter that must be given when activating the
427    * action.
428    *
429    * Since: 2.28
430    **/
431   g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
432                                    g_param_spec_boxed ("parameter-type",
433                                                        P_("Parameter Type"),
434                                                        P_("The type of GVariant passed to activate()"),
435                                                        G_TYPE_VARIANT_TYPE,
436                                                        G_PARAM_READWRITE |
437                                                        G_PARAM_CONSTRUCT_ONLY |
438                                                        G_PARAM_STATIC_STRINGS));
439
440   /**
441    * GSimpleAction:enabled:
442    *
443    * If @action is currently enabled.
444    *
445    * If the action is disabled then calls to g_action_activate() and
446    * g_action_change_state() have no effect.
447    *
448    * Since: 2.28
449    **/
450   g_object_class_install_property (object_class, PROP_ENABLED,
451                                    g_param_spec_boolean ("enabled",
452                                                          P_("Enabled"),
453                                                          P_("If the action can be activated"),
454                                                          TRUE,
455                                                          G_PARAM_READWRITE |
456                                                          G_PARAM_STATIC_STRINGS));
457
458   /**
459    * GSimpleAction:state-type:
460    *
461    * The #GVariantType of the state that the action has, or %NULL if the
462    * action is stateless.
463    *
464    * Since: 2.28
465    **/
466   g_object_class_install_property (object_class, PROP_STATE_TYPE,
467                                    g_param_spec_boxed ("state-type",
468                                                        P_("State Type"),
469                                                        P_("The type of the state kept by the action"),
470                                                        G_TYPE_VARIANT_TYPE,
471                                                        G_PARAM_READABLE |
472                                                        G_PARAM_STATIC_STRINGS));
473
474   /**
475    * GSimpleAction:state:
476    *
477    * The state of the action, or %NULL if the action is stateless.
478    *
479    * Since: 2.28
480    **/
481   g_object_class_install_property (object_class, PROP_STATE,
482                                    g_param_spec_variant ("state",
483                                                          P_("State"),
484                                                          P_("The state the action is in"),
485                                                          G_VARIANT_TYPE_ANY,
486                                                          NULL,
487                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
488                                                          G_PARAM_STATIC_STRINGS));
489 }
490
491 /**
492  * g_simple_action_set_enabled:
493  * @simple: a #GSimpleAction
494  * @enabled: whether the action is enabled
495  *
496  * Sets the action as enabled or not.
497  *
498  * An action must be enabled in order to be activated or in order to
499  * have its state changed from outside callers.
500  *
501  * This should only be called by the implementor of the action.  Users
502  * of the action should not attempt to modify its enabled flag.
503  *
504  * Since: 2.28
505  **/
506 void
507 g_simple_action_set_enabled (GSimpleAction *simple,
508                              gboolean       enabled)
509 {
510   g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
511
512   enabled = !!enabled;
513
514   if (simple->enabled != enabled)
515     {
516       simple->enabled = enabled;
517       g_object_notify (G_OBJECT (simple), "enabled");
518     }
519 }
520 /**
521  * g_simple_action_new:
522  * @name: the name of the action
523  * @parameter_type: (allow-none): the type of parameter to the activate function
524  *
525  * Creates a new action.
526  *
527  * The created action is stateless.  See g_simple_action_new_stateful().
528  *
529  * Returns: a new #GSimpleAction
530  *
531  * Since: 2.28
532  **/
533 GSimpleAction *
534 g_simple_action_new (const gchar        *name,
535                      const GVariantType *parameter_type)
536 {
537   return g_object_new (G_TYPE_SIMPLE_ACTION,
538                        "name", name,
539                        "parameter-type", parameter_type,
540                        NULL);
541 }
542
543 /**
544  * g_simple_action_new_stateful:
545  * @name: the name of the action
546  * @parameter_type: (allow-none): the type of the parameter to the activate function
547  * @state: the initial state of the action
548  *
549  * Creates a new stateful action.
550  *
551  * @state is the initial state of the action.  All future state values
552  * must have the same #GVariantType as the initial state.
553  *
554  * If the @state GVariant is floating, it is consumed.
555  *
556  * Returns: a new #GSimpleAction
557  *
558  * Since: 2.28
559  **/
560 GSimpleAction *
561 g_simple_action_new_stateful (const gchar        *name,
562                               const GVariantType *parameter_type,
563                               GVariant           *state)
564 {
565   return g_object_new (G_TYPE_SIMPLE_ACTION,
566                        "name", name,
567                        "parameter-type", parameter_type,
568                        "state", state,
569                        NULL);
570 }