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