2 * Copyright © 2012 Canonical Limited
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
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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
19 * Authors: Ryan Lortie <desrt@desrt.ca>
24 #include "gsimpleactionobserver.h"
25 #include "gactionobservable.h"
27 typedef GObjectClass GSimpleActionObserverClass;
28 struct _GSimpleActionObserver
30 GObject parent_instance;
32 GActionGroup *action_group;
36 gboolean can_activate;
43 static void g_simple_action_observer_init_iface (GActionObserverInterface *iface);
44 G_DEFINE_TYPE_WITH_CODE (GSimpleActionObserver, g_simple_action_observer, G_TYPE_OBJECT,
45 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_OBSERVER, g_simple_action_observer_init_iface));
55 static GParamSpec *g_simple_action_observer_pspecs[N_PROPS];
58 g_simple_action_observer_action_added (GActionObserver *g_observer,
59 GActionObservable *observable,
60 const gchar *action_name,
61 const GVariantType *parameter_type,
65 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (g_observer);
68 /* we can only activate if we have the correct type of parameter */
69 observer->can_activate = (observer->target == NULL && parameter_type == NULL) ||
70 (observer->target != NULL && parameter_type != NULL &&
71 g_variant_is_of_type (observer->target, parameter_type));
73 if (observer->can_activate)
75 if (observer->target != NULL && state != NULL)
76 active = g_variant_equal (state, observer->target);
78 else if (state != NULL && g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
79 active = g_variant_get_boolean (state);
84 if (active != observer->active)
86 observer->active = active;
87 observer->reporting++;
88 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ACTIVE]);
89 observer->reporting--;
92 if (enabled != observer->enabled)
94 observer->enabled = enabled;
95 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ENABLED]);
101 g_simple_action_observer_action_enabled_changed (GActionObserver *g_observer,
102 GActionObservable *observable,
103 const gchar *action_name,
106 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (g_observer);
108 if (!observer->can_activate)
111 if (enabled != observer->enabled)
113 observer->enabled = enabled;
114 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ENABLED]);
119 g_simple_action_observer_action_state_changed (GActionObserver *g_observer,
120 GActionObservable *observable,
121 const gchar *action_name,
124 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (g_observer);
125 gboolean active = FALSE;
127 if (!observer->can_activate)
130 if (observer->target)
131 active = g_variant_equal (state, observer->target);
133 else if (g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
134 active = g_variant_get_boolean (state);
136 if (active != observer->active)
138 observer->active = active;
139 observer->reporting++;
140 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ACTIVE]);
141 observer->reporting--;
146 g_simple_action_observer_action_removed (GActionObserver *g_observer,
147 GActionObservable *observable,
148 const gchar *action_name)
150 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (g_observer);
152 if (!observer->can_activate)
155 observer->can_activate = FALSE;
157 if (observer->active)
159 observer->active = FALSE;
160 observer->reporting++;
161 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ACTIVE]);
162 observer->reporting--;
165 if (observer->enabled)
167 observer->enabled = FALSE;
168 g_object_notify_by_pspec (G_OBJECT (observer), g_simple_action_observer_pspecs[PROP_ENABLED]);
173 g_simple_action_observer_get_property (GObject *object, guint prop_id,
174 GValue *value, GParamSpec *pspec)
176 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (object);
181 g_value_set_boolean (value, observer->active);
185 g_value_set_boolean (value, observer->enabled);
189 g_assert_not_reached ();
194 g_simple_action_observer_finalize (GObject *object)
196 GSimpleActionObserver *observer = G_SIMPLE_ACTION_OBSERVER (object);
198 g_object_unref (observer->action_group);
199 g_free (observer->action_name);
201 if (observer->target)
202 g_variant_unref (observer->target);
204 G_OBJECT_CLASS (g_simple_action_observer_parent_class)
209 g_simple_action_observer_init (GSimpleActionObserver *observer)
214 g_simple_action_observer_init_iface (GActionObserverInterface *iface)
216 iface->action_added = g_simple_action_observer_action_added;
217 iface->action_enabled_changed = g_simple_action_observer_action_enabled_changed;
218 iface->action_state_changed = g_simple_action_observer_action_state_changed;
219 iface->action_removed = g_simple_action_observer_action_removed;
223 g_simple_action_observer_class_init (GObjectClass *class)
225 class->get_property = g_simple_action_observer_get_property;
226 class->finalize = g_simple_action_observer_finalize;
228 g_simple_action_observer_pspecs[PROP_ACTIVE] = g_param_spec_boolean ("active", "active", "active", FALSE,
229 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
230 g_simple_action_observer_pspecs[PROP_ENABLED] = g_param_spec_boolean ("enabled", "enabled", "enabled", FALSE,
231 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
232 g_object_class_install_properties (class, N_PROPS, g_simple_action_observer_pspecs);
235 GSimpleActionObserver *
236 g_simple_action_observer_new (GActionObservable *observable,
237 const gchar *action_name,
240 GSimpleActionObserver *observer;
241 const GVariantType *type;
245 observer = g_object_new (G_TYPE_SIMPLE_ACTION_OBSERVER, NULL);
246 observer->action_group = g_object_ref (observable);
247 observer->action_name = g_strdup (action_name);
249 observer->target = g_variant_ref_sink (target);
251 g_action_observable_register_observer (observable, action_name, G_ACTION_OBSERVER (observer));
253 if (g_action_group_query_action (observer->action_group, action_name, &enabled, &type, NULL, NULL, &state))
255 g_simple_action_observer_action_added (G_ACTION_OBSERVER (observer), observable,
256 action_name, type, enabled, state);
258 g_variant_unref (state);
265 g_simple_action_observer_activate (GSimpleActionObserver *observer)
267 g_return_if_fail (G_IS_SIMPLE_ACTION_OBSERVER (observer));
269 if (observer->can_activate && !observer->reporting)
270 g_action_group_activate_action (G_ACTION_GROUP (observer->action_group),
271 observer->action_name, observer->target);
275 g_simple_action_observer_get_active (GSimpleActionObserver *observer)
277 g_return_val_if_fail (G_IS_SIMPLE_ACTION_OBSERVER (observer), FALSE);
279 return observer->active;
283 g_simple_action_observer_get_enabled (GSimpleActionObserver *observer)
285 g_return_val_if_fail (G_IS_SIMPLE_ACTION_OBSERVER (observer), FALSE);
287 return observer->enabled;