allow NULL state in g_simple_action_new_stateful
[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 GSimpleAction
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 };
49
50 typedef GObjectClass GSimpleActionClass;
51
52 static void g_simple_action_iface_init (GActionInterface *iface);
53 G_DEFINE_TYPE_WITH_CODE (GSimpleAction, g_simple_action, G_TYPE_OBJECT,
54   G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_simple_action_iface_init))
55
56 enum
57 {
58   PROP_NONE,
59   PROP_NAME,
60   PROP_PARAMETER_TYPE,
61   PROP_ENABLED,
62   PROP_STATE_TYPE,
63   PROP_STATE
64 };
65
66 enum
67 {
68   SIGNAL_ACTIVATE,
69   NR_SIGNALS
70 };
71
72 static guint g_simple_action_signals[NR_SIGNALS];
73
74 static const gchar *
75 g_simple_action_get_name (GAction *action)
76 {
77   GSimpleAction *simple = G_SIMPLE_ACTION (action);
78
79   return simple->name;
80 }
81
82 const GVariantType *
83 g_simple_action_get_parameter_type (GAction *action)
84 {
85   GSimpleAction *simple = G_SIMPLE_ACTION (action);
86
87   return simple->parameter_type;
88 }
89
90 static const GVariantType *
91 g_simple_action_get_state_type (GAction *action)
92 {
93   GSimpleAction *simple = G_SIMPLE_ACTION (action);
94
95   if (simple->state != NULL)
96     return g_variant_get_type (simple->state);
97   else
98     return NULL;
99 }
100
101 static GVariant *
102 g_simple_action_get_state_hint (GAction *action)
103 {
104   return NULL;
105 }
106
107 static gboolean
108 g_simple_action_get_enabled (GAction *action)
109 {
110   GSimpleAction *simple = G_SIMPLE_ACTION (action);
111
112   return simple->enabled;
113 }
114
115 static void
116 g_simple_action_change_state (GAction  *action,
117                               GVariant *value)
118 {
119   GSimpleAction *simple = G_SIMPLE_ACTION (action);
120
121   g_simple_action_set_state (simple, value);
122 }
123
124 /**
125  * g_simple_action_set_state:
126  * @simple: a #GSimpleAction
127  * @value: the new #GVariant for the state
128  *
129  * Sets the state of the action.
130  *
131  * This directly updates the 'state' property to the given value.
132  *
133  * This should only be called by the implementor of the action.  Users
134  * of the action should not attempt to directly modify the 'state'
135  * property.  Instead, they should call g_action_change_state() to
136  * request the change.
137  *
138  * Since: 2.30
139  **/
140 void
141 g_simple_action_set_state (GSimpleAction *simple,
142                            GVariant      *value)
143 {
144   g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
145   g_return_if_fail (value != NULL);
146
147   {
148     const GVariantType *state_type;
149
150     state_type = simple->state ?
151                    g_variant_get_type (simple->state) : NULL;
152     g_return_if_fail (state_type != NULL);
153     g_return_if_fail (g_variant_is_of_type (value, state_type));
154   }
155
156   g_variant_ref_sink (value);
157
158   if (!simple->state || !g_variant_equal (simple->state, value))
159     {
160       if (simple->state)
161         g_variant_unref (simple->state);
162
163       simple->state = g_variant_ref (value);
164
165       g_object_notify (G_OBJECT (simple), "state");
166     }
167
168   g_variant_unref (value);
169 }
170
171 static GVariant *
172 g_simple_action_get_state (GAction *action)
173 {
174   GSimpleAction *simple = G_SIMPLE_ACTION (action);
175
176   return simple->state ? g_variant_ref (simple->state) : NULL;
177 }
178
179 static void
180 g_simple_action_activate (GAction  *action,
181                           GVariant *parameter)
182 {
183   GSimpleAction *simple = G_SIMPLE_ACTION (action);
184
185   g_return_if_fail (simple->parameter_type == NULL ?
186                       parameter == NULL :
187                     (parameter != NULL &&
188                      g_variant_is_of_type (parameter,
189                                            simple->parameter_type)));
190
191   if (parameter != NULL)
192     g_variant_ref_sink (parameter);
193
194   if (simple->enabled)
195     g_signal_emit (simple, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
196
197   if (parameter != NULL)
198     g_variant_unref (parameter);
199 }
200
201 static void
202 g_simple_action_set_property (GObject      *object,
203                               guint         prop_id,
204                               const GValue *value,
205                               GParamSpec   *pspec)
206 {
207   GSimpleAction *simple = G_SIMPLE_ACTION (object);
208
209   switch (prop_id)
210     {
211     case PROP_NAME:
212       g_assert (simple->name == NULL);
213       simple->name = g_value_dup_string (value);
214       break;
215
216     case PROP_PARAMETER_TYPE:
217       g_assert (simple->parameter_type == NULL);
218       simple->parameter_type = g_value_dup_boxed (value);
219       break;
220
221     case PROP_ENABLED:
222       g_simple_action_set_enabled (simple, g_value_get_boolean (value));
223       break;
224
225     default:
226       g_assert_not_reached ();
227     }
228 }
229
230 static void
231 g_simple_action_get_property (GObject    *object,
232                               guint       prop_id,
233                               GValue     *value,
234                               GParamSpec *pspec)
235 {
236   GAction *action = G_ACTION (object);
237
238   switch (prop_id)
239     {
240     case PROP_NAME:
241       g_value_set_string (value, g_simple_action_get_name (action));
242       break;
243
244     case PROP_PARAMETER_TYPE:
245       g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
246       break;
247
248     case PROP_ENABLED:
249       g_value_set_boolean (value, g_simple_action_get_enabled (action));
250       break;
251
252     case PROP_STATE_TYPE:
253       g_value_set_boxed (value, g_simple_action_get_state_type (action));
254       break;
255
256     case PROP_STATE:
257       g_value_take_variant (value, g_simple_action_get_state (action));
258       break;
259
260     default:
261       g_assert_not_reached ();
262     }
263 }
264
265 static void
266 g_simple_action_finalize (GObject *object)
267 {
268   GSimpleAction *simple = G_SIMPLE_ACTION (object);
269
270   g_free (simple->name);
271   if (simple->parameter_type)
272     g_variant_type_free (simple->parameter_type);
273   if (simple->state)
274     g_variant_unref (simple->state);
275
276   G_OBJECT_CLASS (g_simple_action_parent_class)
277     ->finalize (object);
278 }
279
280 void
281 g_simple_action_init (GSimpleAction *simple)
282 {
283 }
284
285 void
286 g_simple_action_iface_init (GActionInterface *iface)
287 {
288   iface->get_name = g_simple_action_get_name;
289   iface->get_parameter_type = g_simple_action_get_parameter_type;
290   iface->get_state_type = g_simple_action_get_state_type;
291   iface->get_state_hint = g_simple_action_get_state_hint;
292   iface->get_enabled = g_simple_action_get_enabled;
293   iface->get_state = g_simple_action_get_state;
294   iface->change_state = g_simple_action_change_state;
295   iface->activate = g_simple_action_activate;
296 }
297
298 void
299 g_simple_action_class_init (GSimpleActionClass *class)
300 {
301   GObjectClass *object_class = G_OBJECT_CLASS (class);
302
303   object_class->get_property = g_simple_action_get_property;
304   object_class->set_property = g_simple_action_set_property;
305   object_class->finalize = g_simple_action_finalize;
306
307   /**
308    * GSimpleAction::activate:
309    * @simple: the #GSimpleAction
310    * @parameter: (allow-none): the parameter to the activation
311    *
312    * Indicates that the action was just activated.
313    *
314    * @parameter will always be of the expected type.  In the event that
315    * an incorrect type was given, no signal will be emitted.
316    *
317    * Since: 2.28
318    */
319   g_simple_action_signals[SIGNAL_ACTIVATE] =
320     g_signal_new (I_("activate"),
321                   G_TYPE_SIMPLE_ACTION,
322                   G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
323                   0, NULL, NULL,
324                   g_cclosure_marshal_VOID__VARIANT,
325                   G_TYPE_NONE, 1,
326                   G_TYPE_VARIANT);
327
328   /**
329    * GSimpleAction:name:
330    *
331    * The name of the action.  This is mostly meaningful for identifying
332    * the action once it has been added to a #GSimpleActionGroup.
333    *
334    * Since: 2.28
335    **/
336   g_object_class_install_property (object_class, PROP_NAME,
337                                    g_param_spec_string ("name",
338                                                         P_("Action Name"),
339                                                         P_("The name used to invoke the action"),
340                                                         NULL,
341                                                         G_PARAM_READWRITE |
342                                                         G_PARAM_CONSTRUCT_ONLY |
343                                                         G_PARAM_STATIC_STRINGS));
344
345   /**
346    * GSimpleAction:parameter-type:
347    *
348    * The type of the parameter that must be given when activating the
349    * action.
350    *
351    * Since: 2.28
352    **/
353   g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
354                                    g_param_spec_boxed ("parameter-type",
355                                                        P_("Parameter Type"),
356                                                        P_("The type of GVariant passed to activate()"),
357                                                        G_TYPE_VARIANT_TYPE,
358                                                        G_PARAM_READWRITE |
359                                                        G_PARAM_CONSTRUCT_ONLY |
360                                                        G_PARAM_STATIC_STRINGS));
361
362   /**
363    * GSimpleAction:enabled:
364    *
365    * If @action is currently enabled.
366    *
367    * If the action is disabled then calls to g_simple_action_activate() and
368    * g_simple_action_change_state() have no effect.
369    *
370    * Since: 2.28
371    **/
372   g_object_class_install_property (object_class, PROP_ENABLED,
373                                    g_param_spec_boolean ("enabled",
374                                                          P_("Enabled"),
375                                                          P_("If the action can be activated"),
376                                                          TRUE,
377                                                          G_PARAM_CONSTRUCT |
378                                                          G_PARAM_READWRITE |
379                                                          G_PARAM_STATIC_STRINGS));
380
381   /**
382    * GSimpleAction:state-type:
383    *
384    * The #GVariantType of the state that the action has, or %NULL if the
385    * action is stateless.
386    *
387    * Since: 2.28
388    **/
389   g_object_class_install_property (object_class, PROP_STATE_TYPE,
390                                    g_param_spec_boxed ("state-type",
391                                                        P_("State Type"),
392                                                        P_("The type of the state kept by the action"),
393                                                        G_TYPE_VARIANT_TYPE,
394                                                        G_PARAM_READABLE |
395                                                        G_PARAM_STATIC_STRINGS));
396
397   /**
398    * GSimpleAction:state:
399    *
400    * The state of the action, or %NULL if the action is stateless.
401    *
402    * Since: 2.28
403    **/
404   g_object_class_install_property (object_class, PROP_STATE,
405                                    g_param_spec_variant ("state",
406                                                          P_("State"),
407                                                          P_("The state the action is in"),
408                                                          G_VARIANT_TYPE_ANY,
409                                                          NULL,
410                                                          G_PARAM_READABLE |
411                                                          G_PARAM_STATIC_STRINGS));
412 }
413
414 /**
415  * g_simple_action_set_enabled:
416  * @simple: a #GSimpleAction
417  * @enabled: whether the action is enabled
418  *
419  * Sets the action as enabled or not.
420  *
421  * An action must be enabled in order to be activated or in order to
422  * have its state changed from outside callers.
423  *
424  * This should only be called by the implementor of the action.  Users
425  * of the action should not attempt to modify its enabled flag.
426  *
427  * Since: 2.28
428  **/
429 void
430 g_simple_action_set_enabled (GSimpleAction *simple,
431                              gboolean       enabled)
432 {
433   g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
434
435   enabled = !!enabled;
436
437   if (simple->enabled != enabled)
438     {
439       simple->enabled = enabled;
440       g_object_notify (G_OBJECT (simple), "enabled");
441     }
442 }
443 /**
444  * g_simple_action_new:
445  * @name: the name of the action
446  * @parameter_type: (allow-none): the type of parameter to the activate function
447  *
448  * Creates a new action.
449  *
450  * The created action is stateless.  See g_simple_action_new_stateful().
451  *
452  * Returns: a new #GSimpleAction
453  *
454  * Since: 2.28
455  **/
456 GSimpleAction *
457 g_simple_action_new (const gchar        *name,
458                      const GVariantType *parameter_type)
459 {
460   return g_object_new (G_TYPE_SIMPLE_ACTION,
461                        "name", name,
462                        "parameter-type", parameter_type,
463                        NULL);
464 }
465
466 /**
467  * g_simple_action_new_stateful:
468  * @name: the name of the action
469  * @parameter_type: (allow-none): the type of the parameter to the activate function
470  * @state: the initial state of the action
471  *
472  * Creates a new stateful action.
473  *
474  * @state is the initial state of the action.  All future state values
475  * must have the same #GVariantType as the initial state.
476  *
477  * If the @state GVariant is floating, it is consumed.
478  *
479  * Returns: a new #GSimpleAction
480  *
481  * Since: 2.28
482  **/
483 GSimpleAction *
484 g_simple_action_new_stateful (const gchar        *name,
485                               const GVariantType *parameter_type,
486                               GVariant           *state)
487 {
488   GSimpleAction *simple;
489
490   simple = g_object_new (G_TYPE_SIMPLE_ACTION,
491                          "name", name,
492                          "parameter-type", parameter_type,
493                          NULL);
494
495   if (state)
496     simple->state = g_variant_ref_sink (state);
497
498   return simple;
499 }