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