2 * Copyright © 2012 Canonical Limited
4 * This library is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * licence or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * 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 Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 * Authors: Ryan Lortie <desrt@desrt.ca>
22 #include "gsimpleactionobserver.h"
23 #include "gactionobservable.h"
25 typedef GObjectClass GSimpleActionObserverClass;
26 struct _GSimpleActionObserver
28 GObject parent_instance;
30 GActionGroup *action_group;
34 gboolean can_activate;
41 static void g_simple_action_observer_init_iface (GActionObserverInterface *iface);
42 G_DEFINE_TYPE_WITH_CODE (GSimpleActionObserver, g_simple_action_observer, G_TYPE_OBJECT,
43 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_OBSERVER, g_simple_action_observer_init_iface));
53 static GParamSpec *g_simple_action_observer_pspecs[N_PROPS];
56 g_simple_action_observer_action_added (GActionObserver *g_observer,
57 GActionObservable *observable,
58 const gchar *action_name,
59 const GVariantType *parameter_type,
63 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (g_observer);
66 /* we can only activate if we have the correct type of parameter */
67 observer->can_activate = (observer->target == NULL && parameter_type == NULL) ||
68 (observer->target != NULL && parameter_type != NULL &&
69 g_variant_is_of_type (observer->target, parameter_type));
71 if (observer->can_activate)
73 if (observer->target != NULL && state != NULL)
74 active = g_variant_equal (state, observer->target);
76 else if (state != NULL && g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
77 active = g_variant_get_boolean (state);
82 if (active != observer->active)
84 observer->active = active;
85 observer->reporting++;
86 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ACTIVE]);
87 observer->reporting--;
90 if (enabled != observer->enabled)
92 observer->enabled = enabled;
93 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ENABLED]);
99 g_simple_action_observer_action_enabled_changed (GActionObserver *g_observer,
100 GActionObservable *observable,
101 const gchar *action_name,
104 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (g_observer);
106 if (!observer->can_activate)
109 if (enabled != observer->enabled)
111 observer->enabled = enabled;
112 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ENABLED]);
117 g_simple_action_observer_action_state_changed (GActionObserver *g_observer,
118 GActionObservable *observable,
119 const gchar *action_name,
122 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (g_observer);
123 gboolean active = FALSE;
125 if (!observer->can_activate)
128 if (observer->target)
129 active = g_variant_equal (state, observer->target);
131 else if (g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
132 active = g_variant_get_boolean (state);
134 if (active != observer->active)
136 observer->active = active;
137 observer->reporting++;
138 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ACTIVE]);
139 observer->reporting--;
144 g_simple_action_observer_action_removed (GActionObserver *g_observer,
145 GActionObservable *observable,
146 const gchar *action_name)
148 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (g_observer);
150 if (!observer->can_activate)
153 observer->can_activate = FALSE;
155 if (observer->active)
157 observer->active = FALSE;
158 observer->reporting++;
159 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ACTIVE]);
160 observer->reporting--;
163 if (observer->enabled)
165 observer->enabled = FALSE;
166 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ENABLED]);
171 g_simple_action_observer_get_property (GObject *object, guint prop_id,
172 GValue *value, GParamSpec *pspec)
174 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (object);
179 g_value_set_boolean (value, observer->active);
183 g_value_set_boolean (value, observer->enabled);
187 g_assert_not_reached ();
192 g_simple_action_observer_finalize (GObject *object)
194 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (object);
196 g_object_unref (observer->action_group);
197 g_free (observer->action_name);
199 if (observer->target)
200 g_variant_unref (observer->target);
202 G_OBJECT_CLASS (g_simple_action_observer_parent_class)
207 g_simple_action_observer_init (GSimpleActionObserver *observer)
212 g_simple_action_observer_init_iface (GActionObserverInterface *iface)
214 iface->action_added = g_simple_action_observer_action_added;
215 iface->action_enabled_changed = g_simple_action_observer_action_enabled_changed;
216 iface->action_state_changed = g_simple_action_observer_action_state_changed;
217 iface->action_removed = g_simple_action_observer_action_removed;
221 g_simple_action_observer_class_init (GObjectClass *class)
223 class->get_property = g_simple_action_observer_get_property;
224 class->finalize = g_simple_action_observer_finalize;
226 g_simple_action_observer_pspecs[PROP_ACTIVE] = g_param_spec_boolean ("active", "active", "active", FALSE,
227 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
228 g_simple_action_observer_pspecs[PROP_ENABLED] = g_param_spec_boolean ("enabled", "enabled", "enabled", FALSE,
229 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
230 g_object_class_install_properties (class, N_PROPS, g_simple_action_observer_pspecs);
233 GSimpleActionObserver *
234 g_simple_action_observer_new (GActionObservable *observable,
235 const gchar *action_name,
238 GSimpleActionObserver *observer;
239 const GVariantType *type;
243 observer = g_object_new (G_TYPE_SIMPLE_ACTION_OBSERVER, NULL);
244 observer->action_group = g_object_ref (observable);
245 observer->action_name = g_strdup (action_name);
247 observer->target = g_variant_ref_sink (target);
249 g_action_observable_register_observer (observable, action_name, G_ACTION_OBSERVER (observer));
251 if (g_action_group_query_action (observer->action_group, action_name, &enabled, &type, NULL, NULL, &state))
253 g_simple_action_observer_action_added (G_ACTION_OBSERVER (observer), observable,
254 action_name, type, enabled, state);
256 g_variant_unref (state);
263 g_simple_action_observer_activate (GSimpleActionObserver *observer)
265 g_return_if_fail (G_IS_SIMPLE_ACTION_OBSERVER (observer));
267 if (observer->can_activate && !observer->reporting)
268 g_action_group_activate_action (G_ACTION_GROUP (observer->action_group),
269 observer->action_name, observer->target);
273 g_simple_action_observer_get_active (GSimpleActionObserver *observer)
275 g_return_val_if_fail (G_IS_SIMPLE_ACTION_OBSERVER (observer), FALSE);
277 return observer->active;
281 g_simple_action_observer_get_enabled (GSimpleActionObserver *observer)
283 g_return_val_if_fail (G_IS_SIMPLE_ACTION_OBSERVER (observer), FALSE);
285 return observer->enabled;