2 * Copyright © 2013 Canonical Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
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.
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/>.
17 * Authors: Ryan Lortie <desrt@desrt.ca>
22 #include "gpropertyaction.h"
24 #include "gsettings-mapping.h"
29 * SECTION:gpropertyaction
30 * @title: GPropertyAction
31 * @short_description: A GAction reflecting a GObject property
34 * A #GPropertyAction is a way to get a #GAction with a state value
35 * reflecting and controlling the value of a #GObject property.
37 * The state of the action will correspond to the value of the property.
38 * Changing it will change the property (assuming the requested value
39 * matches the requirements as specified in the #GParamSpec).
41 * Only the most common types are presently supported. Booleans are
42 * mapped to booleans, strings to strings, signed/unsigned integers to
43 * int32/uint32 and floats and doubles to doubles.
45 * If the property is an enum then the state will be string-typed and
46 * conversion will automatically be performed between the enum value and
47 * "nick" string as per the #GEnumValue table.
49 * Flags types are not currently supported.
51 * Properties of object types, boxed types and pointer types are not
52 * supported and probably never will be.
54 * Properties of #GVariant types are not currently supported.
56 * If the property is boolean-valued then the action will have a NULL
57 * parameter type, and activating the action (with no parameter) will
58 * toggle the value of the property.
60 * In all other cases, the parameter type will correspond to the type of
63 * The general idea here is to reduce the number of locations where a
64 * particular piece of state is kept (and therefore has to be synchronised
65 * between). #GPropertyAction does not have a separate state that is kept
66 * in sync with the property value -- its state is the property value.
68 * For example, it might be useful to create a #GAction corresponding to
69 * the "visible-child-name" property of a #GtkStack so that the current
70 * page can be switched from a menu. The active radio indication in the
71 * menu is then directly determined from the active page of the
74 * An anti-example would be binding the "active-id" property on a
75 * #GtkComboBox. This is because the state of the combobox itself is
76 * probably uninteresting and is actually being used to control
79 * Another anti-example would be to bind to the "visible-child-name"
80 * property of a #GtkStack if this value is actually stored in
81 * #GSettings. In that case, the real source of the value is
82 * #GSettings. If you want a #GAction to control a setting stored in
83 * #GSettings, see g_settings_create_action() instead, and possibly
84 * combine its use with g_settings_bind().
88 struct _GPropertyAction
90 GObject parent_instance;
95 const GVariantType *state_type;
96 gboolean invert_boolean;
102 * This type is opaque.
107 typedef GObjectClass GPropertyActionClass;
109 static void g_property_action_iface_init (GActionInterface *iface);
110 G_DEFINE_TYPE_WITH_CODE (GPropertyAction, g_property_action, G_TYPE_OBJECT,
111 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_property_action_iface_init))
127 g_property_action_get_invert_boolean (GAction *action)
129 GPropertyAction *paction = G_PROPERTY_ACTION (action);
131 return paction->invert_boolean;
135 g_property_action_get_name (GAction *action)
137 GPropertyAction *paction = G_PROPERTY_ACTION (action);
139 return paction->name;
142 static const GVariantType *
143 g_property_action_get_parameter_type (GAction *action)
145 GPropertyAction *paction = G_PROPERTY_ACTION (action);
147 return paction->pspec->value_type == G_TYPE_BOOLEAN ? NULL : paction->state_type;
150 static const GVariantType *
151 g_property_action_get_state_type (GAction *action)
153 GPropertyAction *paction = G_PROPERTY_ACTION (action);
155 return paction->state_type;
159 g_property_action_get_state_hint (GAction *action)
161 GPropertyAction *paction = G_PROPERTY_ACTION (action);
163 if (paction->pspec->value_type == G_TYPE_INT)
165 GParamSpecInt *pspec = (GParamSpecInt *)paction->pspec;
166 return g_variant_new ("(ii)", pspec->minimum, pspec->maximum);
168 else if (paction->pspec->value_type == G_TYPE_UINT)
170 GParamSpecUInt *pspec = (GParamSpecUInt *)paction->pspec;
171 return g_variant_new ("(uu)", pspec->minimum, pspec->maximum);
173 else if (paction->pspec->value_type == G_TYPE_FLOAT)
175 GParamSpecFloat *pspec = (GParamSpecFloat *)paction->pspec;
176 return g_variant_new ("(dd)", (double)pspec->minimum, (double)pspec->maximum);
178 else if (paction->pspec->value_type == G_TYPE_DOUBLE)
180 GParamSpecDouble *pspec = (GParamSpecDouble *)paction->pspec;
181 return g_variant_new ("(dd)", pspec->minimum, pspec->maximum);
188 g_property_action_get_enabled (GAction *action)
194 g_property_action_set_state (GPropertyAction *paction,
197 GValue value = G_VALUE_INIT;
199 g_value_init (&value, paction->pspec->value_type);
200 g_settings_get_mapping (&value, variant, NULL);
202 if (paction->pspec->value_type == G_TYPE_BOOLEAN && paction->invert_boolean)
203 g_value_set_boolean (&value, !g_value_get_boolean (&value));
205 g_object_set_property (paction->object, paction->pspec->name, &value);
206 g_value_unset (&value);
210 g_property_action_change_state (GAction *action,
213 GPropertyAction *paction = G_PROPERTY_ACTION (action);
215 g_return_if_fail (g_variant_is_of_type (value, paction->state_type));
217 g_property_action_set_state (paction, value);
221 g_property_action_get_state (GAction *action)
223 GPropertyAction *paction = G_PROPERTY_ACTION (action);
224 GValue value = G_VALUE_INIT;
227 g_value_init (&value, paction->pspec->value_type);
228 g_object_get_property (paction->object, paction->pspec->name, &value);
230 if (paction->pspec->value_type == G_TYPE_BOOLEAN && paction->invert_boolean)
231 g_value_set_boolean (&value, !g_value_get_boolean (&value));
233 result = g_settings_set_mapping (&value, paction->state_type, NULL);
234 g_value_unset (&value);
236 return g_variant_ref_sink (result);
240 g_property_action_activate (GAction *action,
243 GPropertyAction *paction = G_PROPERTY_ACTION (action);
245 if (paction->pspec->value_type == G_TYPE_BOOLEAN)
249 g_return_if_fail (paction->pspec->value_type == G_TYPE_BOOLEAN && parameter == NULL);
251 g_object_get (paction->object, paction->pspec->name, &value, NULL);
253 g_object_set (paction->object, paction->pspec->name, value, NULL);
257 g_return_if_fail (parameter != NULL && g_variant_is_of_type (parameter, paction->state_type));
259 g_property_action_set_state (paction, parameter);
263 static const GVariantType *
264 g_property_action_determine_type (GParamSpec *pspec)
266 if (G_TYPE_IS_ENUM (pspec->value_type))
267 return G_VARIANT_TYPE_STRING;
269 switch (pspec->value_type)
272 return G_VARIANT_TYPE_BOOLEAN;
275 return G_VARIANT_TYPE_INT32;
278 return G_VARIANT_TYPE_UINT32;
282 return G_VARIANT_TYPE_DOUBLE;
285 return G_VARIANT_TYPE_STRING;
288 g_critical ("Unable to use GPropertyAction with property '%s::%s' of type '%s'",
289 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->value_type));
295 g_property_action_notify (GObject *object,
299 GPropertyAction *paction = user_data;
301 g_assert (object == paction->object);
302 g_assert (pspec == paction->pspec);
304 g_object_notify (G_OBJECT (paction), "state");
308 g_property_action_set_property_name (GPropertyAction *paction,
309 const gchar *property_name)
314 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (paction->object), property_name);
318 g_critical ("Attempted to use non-existent property '%s::%s' for GPropertyAction",
319 G_OBJECT_TYPE_NAME (paction->object), property_name);
323 if (~pspec->flags & G_PARAM_READABLE || ~pspec->flags & G_PARAM_WRITABLE || pspec->flags & G_PARAM_CONSTRUCT_ONLY)
325 g_critical ("Property '%s::%s' used with GPropertyAction must be readable, writable, and not construct-only",
326 G_OBJECT_TYPE_NAME (paction->object), property_name);
330 paction->pspec = pspec;
332 detailed = g_strconcat ("notify::", paction->pspec->name, NULL);
333 paction->state_type = g_property_action_determine_type (paction->pspec);
334 g_signal_connect (paction->object, detailed, G_CALLBACK (g_property_action_notify), paction);
339 g_property_action_set_property (GObject *object,
344 GPropertyAction *paction = G_PROPERTY_ACTION (object);
349 paction->name = g_value_dup_string (value);
353 paction->object = g_value_dup_object (value);
356 case PROP_PROPERTY_NAME:
357 g_property_action_set_property_name (paction, g_value_get_string (value));
360 case PROP_INVERT_BOOLEAN:
361 paction->invert_boolean = g_value_get_boolean (value);
365 g_assert_not_reached ();
370 g_property_action_get_property (GObject *object,
375 GAction *action = G_ACTION (object);
380 g_value_set_string (value, g_property_action_get_name (action));
383 case PROP_PARAMETER_TYPE:
384 g_value_set_boxed (value, g_property_action_get_parameter_type (action));
388 g_value_set_boolean (value, g_property_action_get_enabled (action));
391 case PROP_STATE_TYPE:
392 g_value_set_boxed (value, g_property_action_get_state_type (action));
396 g_value_take_variant (value, g_property_action_get_state (action));
399 case PROP_INVERT_BOOLEAN:
400 g_value_set_boolean (value, g_property_action_get_invert_boolean (action));
404 g_assert_not_reached ();
409 g_property_action_finalize (GObject *object)
411 GPropertyAction *paction = G_PROPERTY_ACTION (object);
413 g_signal_handlers_disconnect_by_func (paction->object, g_property_action_notify, paction);
414 g_object_unref (paction->object);
415 g_free (paction->name);
417 G_OBJECT_CLASS (g_property_action_parent_class)
422 g_property_action_init (GPropertyAction *property)
427 g_property_action_iface_init (GActionInterface *iface)
429 iface->get_name = g_property_action_get_name;
430 iface->get_parameter_type = g_property_action_get_parameter_type;
431 iface->get_state_type = g_property_action_get_state_type;
432 iface->get_state_hint = g_property_action_get_state_hint;
433 iface->get_enabled = g_property_action_get_enabled;
434 iface->get_state = g_property_action_get_state;
435 iface->change_state = g_property_action_change_state;
436 iface->activate = g_property_action_activate;
440 g_property_action_class_init (GPropertyActionClass *class)
442 GObjectClass *object_class = G_OBJECT_CLASS (class);
444 object_class->set_property = g_property_action_set_property;
445 object_class->get_property = g_property_action_get_property;
446 object_class->finalize = g_property_action_finalize;
449 * GPropertyAction:name:
451 * The name of the action. This is mostly meaningful for identifying
452 * the action once it has been added to a #GActionMap.
456 g_object_class_install_property (object_class, PROP_NAME,
457 g_param_spec_string ("name",
459 P_("The name used to invoke the action"),
462 G_PARAM_CONSTRUCT_ONLY |
463 G_PARAM_STATIC_STRINGS));
466 * GPropertyAction:parameter-type:
468 * The type of the parameter that must be given when activating the
473 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
474 g_param_spec_boxed ("parameter-type",
475 P_("Parameter Type"),
476 P_("The type of GVariant passed to activate()"),
479 G_PARAM_STATIC_STRINGS));
482 * GPropertyAction:enabled:
484 * If @action is currently enabled.
486 * If the action is disabled then calls to g_action_activate() and
487 * g_action_change_state() have no effect.
491 g_object_class_install_property (object_class, PROP_ENABLED,
492 g_param_spec_boolean ("enabled",
494 P_("If the action can be activated"),
497 G_PARAM_STATIC_STRINGS));
500 * GPropertyAction:state-type:
502 * The #GVariantType of the state that the action has, or %NULL if the
503 * action is stateless.
507 g_object_class_install_property (object_class, PROP_STATE_TYPE,
508 g_param_spec_boxed ("state-type",
510 P_("The type of the state kept by the action"),
513 G_PARAM_STATIC_STRINGS));
516 * GPropertyAction:state:
518 * The state of the action, or %NULL if the action is stateless.
522 g_object_class_install_property (object_class, PROP_STATE,
523 g_param_spec_variant ("state",
525 P_("The state the action is in"),
529 G_PARAM_STATIC_STRINGS));
532 * GPropertyAction:object:
534 * The object to wrap a property on.
536 * The object must be a non-%NULL #GObject with properties.
540 g_object_class_install_property (object_class, PROP_OBJECT,
541 g_param_spec_object ("object",
543 P_("The object with the property to wrap"),
546 G_PARAM_CONSTRUCT_ONLY |
547 G_PARAM_STATIC_STRINGS));
550 * GPropertyAction:property-name:
552 * The name of the property to wrap on the object.
554 * The property must exist on the passed-in object and it must be
555 * readable and writable (and not construct-only).
559 g_object_class_install_property (object_class, PROP_PROPERTY_NAME,
560 g_param_spec_string ("property-name",
562 P_("The name of the property to wrap"),
565 G_PARAM_CONSTRUCT_ONLY |
566 G_PARAM_STATIC_STRINGS));
569 * GPropertyAction:invert-boolean:
571 * If %TRUE, the state of the action will be the negation of the
572 * property value, provided the property is boolean.
576 g_object_class_install_property (object_class, PROP_INVERT_BOOLEAN,
577 g_param_spec_boolean ("invert-boolean",
578 P_("Invert boolean"),
579 P_("Whether to invert the value of a boolean property"),
582 G_PARAM_CONSTRUCT_ONLY |
583 G_PARAM_STATIC_STRINGS));
587 * g_property_action_new:
588 * @name: the name of the action to create
589 * @object: (type GObject.Object): the object that has the property
591 * @property_name: the name of the property
593 * Creates a #GAction corresponding to the value of property
594 * @property_name on @object.
596 * The property must be existent and readable and writable (and not
599 * This function takes a reference on @object and doesn't release it
600 * until the action is destroyed.
602 * Returns: a new #GPropertyAction
607 g_property_action_new (const gchar *name,
609 const gchar *property_name)
611 g_return_val_if_fail (name != NULL, NULL);
612 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
613 g_return_val_if_fail (property_name != NULL, NULL);
615 return g_object_new (G_TYPE_PROPERTY_ACTION,
618 "property-name", property_name,