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