GSocketControlMessage: clean up confusing code
[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 static void g_simple_action_iface_init (GActionInterface *iface);
30 G_DEFINE_TYPE_WITH_CODE (GSimpleAction, g_simple_action, G_TYPE_OBJECT,
31   G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_simple_action_iface_init))
32
33 /**
34  * SECTION:gsimpleaction
35  * @title: GSimpleAction
36  * @short_description: a simple #GSimpleAction
37  *
38  * A #GSimpleAction is the obvious simple implementation of the #GSimpleAction
39  * interface.  This is the easiest way to create an action for purposes of
40  * adding it to a #GSimpleActionGroup.
41  *
42  * See also #GtkAction.
43  **/
44
45 struct _GSimpleActionPrivate
46 {
47   gchar        *name;
48   GVariantType *parameter_type;
49   guint         enabled : 1;
50   guint         state_set : 1;
51   GVariant     *state;
52 };
53
54 enum
55 {
56   PROP_NONE,
57   PROP_NAME,
58   PROP_PARAMETER_TYPE,
59   PROP_ENABLED,
60   PROP_STATE_TYPE,
61   PROP_STATE
62 };
63
64 enum
65 {
66   SIGNAL_ACTIVATE,
67   NR_SIGNALS
68 };
69
70 static guint g_simple_action_signals[NR_SIGNALS];
71
72 static const gchar *
73 g_simple_action_get_name (GAction *action)
74 {
75   GSimpleAction *simple = G_SIMPLE_ACTION (action);
76
77   return simple->priv->name;
78 }
79
80 const GVariantType *
81 g_simple_action_get_parameter_type (GAction *action)
82 {
83   GSimpleAction *simple = G_SIMPLE_ACTION (action);
84
85   return simple->priv->parameter_type;
86 }
87
88 static const GVariantType *
89 g_simple_action_get_state_type (GAction *action)
90 {
91   GSimpleAction *simple = G_SIMPLE_ACTION (action);
92
93   if (simple->priv->state != NULL)
94     return g_variant_get_type (simple->priv->state);
95   else
96     return NULL;
97 }
98
99 static GVariant *
100 g_simple_action_get_state_hint (GAction *action)
101 {
102   return NULL;
103 }
104
105 static gboolean
106 g_simple_action_get_enabled (GAction *action)
107 {
108   GSimpleAction *simple = G_SIMPLE_ACTION (action);
109
110   return simple->priv->enabled;
111 }
112
113 static void
114 g_simple_action_set_state (GAction  *action,
115                            GVariant *value)
116 {
117   GSimpleAction *simple = G_SIMPLE_ACTION (action);
118
119   g_return_if_fail (value != NULL);
120
121   {
122     const GVariantType *state_type;
123
124     state_type = simple->priv->state ?
125                    g_variant_get_type (simple->priv->state) : NULL;
126     g_return_if_fail (state_type != NULL);
127     g_return_if_fail (g_variant_is_of_type (value, state_type));
128   }
129
130   g_variant_ref_sink (value);
131
132   if (!g_variant_equal (simple->priv->state, value))
133     {
134       if (simple->priv->state)
135         g_variant_unref (simple->priv->state);
136
137       simple->priv->state = g_variant_ref (value);
138
139       g_object_notify (G_OBJECT (simple), "state");
140     }
141
142   g_variant_unref (value);
143 }
144
145 static GVariant *
146 g_simple_action_get_state (GAction *action)
147 {
148   GSimpleAction *simple = G_SIMPLE_ACTION (action);
149
150   return simple->priv->state ? g_variant_ref (simple->priv->state) : NULL;
151 }
152
153 static void
154 g_simple_action_activate (GAction  *action,
155                           GVariant *parameter)
156 {
157   GSimpleAction *simple = G_SIMPLE_ACTION (action);
158
159   g_return_if_fail (simple->priv->parameter_type == NULL ?
160                       parameter == NULL :
161                     (parameter != NULL &&
162                      g_variant_is_of_type (parameter,
163                                            simple->priv->parameter_type)));
164
165   if (parameter != NULL)
166     g_variant_ref_sink (parameter);
167
168   if (simple->priv->enabled)
169     g_signal_emit (simple, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
170
171   if (parameter != NULL)
172     g_variant_unref (parameter);
173 }
174
175 static void
176 g_simple_action_set_property (GObject      *object,
177                               guint         prop_id,
178                               const GValue *value,
179                               GParamSpec   *pspec)
180 {
181   GSimpleAction *simple = G_SIMPLE_ACTION (object);
182
183   switch (prop_id)
184     {
185     case PROP_NAME:
186       g_assert (simple->priv->name == NULL);
187       simple->priv->name = g_value_dup_string (value);
188       break;
189
190     case PROP_PARAMETER_TYPE:
191       g_assert (simple->priv->parameter_type == NULL);
192       simple->priv->parameter_type = g_value_dup_boxed (value);
193       break;
194
195     case PROP_ENABLED:
196       g_simple_action_set_enabled (simple, g_value_get_boolean (value));
197       break;
198
199     case PROP_STATE:
200       /* PROP_STATE is marked as G_PARAM_CONSTRUCT so we always get a
201        * call during object construction, even if it is NULL.  We treat
202        * that first call differently, for a number of reasons.
203        *
204        * First, we don't want the value to be rejected by the
205        * possibly-overridden .set_state() function.  Second, we don't
206        * want to be tripped by the assertions in g_simple_action_set_state()
207        * that would enforce the catch22 that we only provide a value of
208        * the same type as the existing value (when there is not yet an
209        * existing value).
210        */
211       if (simple->priv->state_set)
212         g_simple_action_set_state (G_ACTION (simple),
213                                    g_value_get_variant (value));
214
215       else /* this is the special case */
216         {
217           /* only do it the first time. */
218           simple->priv->state_set = TRUE;
219
220           /* blindly set it. */
221           simple->priv->state = g_value_dup_variant (value);
222         }
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->priv->name);
271   if (simple->priv->parameter_type)
272     g_variant_type_free (simple->priv->parameter_type);
273   if (simple->priv->state)
274     g_variant_unref (simple->priv->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   simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (simple,
284                                               G_TYPE_SIMPLE_ACTION,
285                                               GSimpleActionPrivate);
286 }
287
288 void
289 g_simple_action_iface_init (GActionInterface *iface)
290 {
291   iface->get_name = g_simple_action_get_name;
292   iface->get_parameter_type = g_simple_action_get_parameter_type;
293   iface->get_state_type = g_simple_action_get_state_type;
294   iface->get_state_hint = g_simple_action_get_state_hint;
295   iface->get_enabled = g_simple_action_get_enabled;
296   iface->get_state = g_simple_action_get_state;
297   iface->set_state = g_simple_action_set_state;
298   iface->activate = g_simple_action_activate;
299 }
300
301 void
302 g_simple_action_class_init (GSimpleActionClass *class)
303 {
304   GObjectClass *object_class = G_OBJECT_CLASS (class);
305
306
307   object_class->get_property = g_simple_action_get_property;
308   object_class->set_property = g_simple_action_set_property;
309   object_class->finalize = g_simple_action_finalize;
310
311   /**
312    * GSimpleAction::activate:
313    * @simple: the #GSimpleAction
314    * @parameter: (allow-none): the parameter to the activation
315    *
316    * Indicates that the action was just activated.
317    *
318    * @parameter will always be of the expected type.  In the event that
319    * an incorrect type was given, no signal will be emitted.
320    *
321    * Since: 2.26
322    */
323   g_simple_action_signals[SIGNAL_ACTIVATE] =
324     g_signal_new (I_("activate"),
325                   G_TYPE_SIMPLE_ACTION,
326                   G_SIGNAL_RUN_LAST,
327                   G_STRUCT_OFFSET (GSimpleActionClass, activate),
328                   NULL, NULL,
329                   g_cclosure_marshal_VOID__VARIANT,
330                   G_TYPE_NONE, 1,
331                   G_TYPE_VARIANT);
332
333   /**
334    * GSimpleAction:name:
335    *
336    * The name of the action.  This is mostly meaningful for identifying
337    * the action once it has been added to a #GSimpleActionGroup.
338    *
339    * Since: 2.26
340    **/
341   g_object_class_install_property (object_class, PROP_NAME,
342                                    g_param_spec_string ("name",
343                                                         P_("Action Name"),
344                                                         P_("The name used to invoke the action"),
345                                                         NULL,
346                                                         G_PARAM_READWRITE |
347                                                         G_PARAM_CONSTRUCT_ONLY |
348                                                         G_PARAM_STATIC_STRINGS));
349
350   /**
351    * GSimpleAction:parameter-type:
352    *
353    * The type of the parameter that must be given when activating the
354    * action.
355    *
356    * Since: 2.26
357    **/
358   g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
359                                    g_param_spec_boxed ("parameter-type",
360                                                        P_("Parameter Type"),
361                                                        P_("The type of GVariant passed to activate()"),
362                                                        G_TYPE_VARIANT_TYPE,
363                                                        G_PARAM_READWRITE |
364                                                        G_PARAM_CONSTRUCT_ONLY |
365                                                        G_PARAM_STATIC_STRINGS));
366
367   /**
368    * GSimpleAction:enabled:
369    *
370    * If @action is currently enabled.
371    *
372    * If the action is disabled then calls to g_simple_action_activate() and
373    * g_simple_action_set_state() have no effect.
374    *
375    * Since: 2.26
376    **/
377   g_object_class_install_property (object_class, PROP_ENABLED,
378                                    g_param_spec_boolean ("enabled",
379                                                          P_("Enabled"),
380                                                          P_("If the action can be activated"),
381                                                          TRUE,
382                                                          G_PARAM_CONSTRUCT |
383                                                          G_PARAM_READWRITE |
384                                                          G_PARAM_STATIC_STRINGS));
385
386   /**
387    * GSimpleAction:state-type:
388    *
389    * The #GVariantType of the state that the action has, or %NULL if the
390    * action is stateless.
391    *
392    * Since: 2.26
393    **/
394   g_object_class_install_property (object_class, PROP_STATE_TYPE,
395                                    g_param_spec_boxed ("state-type",
396                                                        P_("State Type"),
397                                                        P_("The type of the state kept by the action"),
398                                                        G_TYPE_VARIANT_TYPE,
399                                                        G_PARAM_READABLE |
400                                                        G_PARAM_STATIC_STRINGS));
401
402   /**
403    * GSimpleAction:state:
404    *
405    * The state of the action, or %NULL if the action is stateless.
406    *
407    * Since: 2.26
408    **/
409   g_object_class_install_property (object_class, PROP_STATE,
410                                    g_param_spec_variant ("state",
411                                                          P_("State"),
412                                                          P_("The state the action is in"),
413                                                          G_VARIANT_TYPE_ANY,
414                                                          NULL,
415                                                          G_PARAM_CONSTRUCT |
416                                                          G_PARAM_READWRITE |
417                                                          G_PARAM_STATIC_STRINGS));
418
419   g_type_class_add_private (class, sizeof (GSimpleActionPrivate));
420 }
421
422 /**
423  * g_simple_action_set_enabled:
424  * @simple: a #GSimpleAction
425  * @enabled: whether the action is enabled
426  *
427  * Sets the action as enabled or not.
428  *
429  * An action must be enabled in order to be activated or in order to
430  * have its state changed from outside callers.
431  *
432  * Since: 2.26
433  **/
434 void
435 g_simple_action_set_enabled (GSimpleAction *simple,
436                              gboolean       enabled)
437 {
438   g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
439
440   enabled = !!enabled;
441
442   if (simple->priv->enabled != enabled)
443     {
444       simple->priv->enabled = enabled;
445       g_object_notify (G_OBJECT (simple), "enabled");
446     }
447 }
448 /**
449  * g_simple_action_new:
450  * @name: the name of the action
451  * @parameter_type: (allow-none): the type of parameter to the activate function
452  *
453  * Creates a new action.
454  *
455  * The created action is stateless.  See g_simple_action_new_stateful().
456  *
457  * Returns: a new #GSimpleAction
458  *
459  * Since: 2.26
460  **/
461 GSimpleAction *
462 g_simple_action_new (const gchar        *name,
463                      const GVariantType *parameter_type)
464 {
465   return g_object_new (G_TYPE_SIMPLE_ACTION,
466                        "name", name,
467                        "parameter-type", parameter_type,
468                        NULL);
469 }
470
471 /**
472  * g_simple_action_new_stateful:
473  * @name: the name of the action
474  * @parameter_type: (allow-none): the type of the parameter to the activate function
475  * @state: the initial state of the action
476  *
477  * Creates a new stateful action.
478  *
479  * @state is the initial state of the action.  All future state values
480  * must have the same #GVariantType as the initial state.
481  *
482  * If the @state GVariant is floating, it is consumed.
483  *
484  * Returns: a new #GSimpleAction
485  *
486  * Since: 2.26
487  **/
488 GSimpleAction *
489 g_simple_action_new_stateful (const gchar        *name,
490                               const GVariantType *parameter_type,
491                               GVariant           *state)
492 {
493   return g_object_new (G_TYPE_SIMPLE_ACTION,
494                        "name", name,
495                        "parameter-type", parameter_type,
496                        "state", state,
497                        NULL);
498 }