1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "modest-debug.h"
31 #include "modest-ui-dimming-manager.h"
32 #include "modest-dimming-rules-group-priv.h"
34 static void modest_ui_dimming_manager_class_init (ModestUIDimmingManagerClass *klass);
35 static void modest_ui_dimming_manager_init (ModestUIDimmingManager *obj);
36 static void modest_ui_dimming_manager_finalize (GObject *obj);
37 static void modest_ui_dimming_manager_dispose (GObject *obj);
39 static void _process_all_rules (gpointer key, gpointer value, gpointer user_data);
42 typedef struct _ModestUIDimmingManagerPrivate ModestUIDimmingManagerPrivate;
43 struct _ModestUIDimmingManagerPrivate {
44 GHashTable *groups_map;
45 GHashTable *delayed_calls;
48 #define MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
49 MODEST_TYPE_UI_DIMMING_MANAGER, \
50 ModestUIDimmingManagerPrivate))
53 static GObjectClass *parent_class = NULL;
56 modest_ui_dimming_manager_get_type (void)
58 static GType my_type = 0;
60 static const GTypeInfo my_info = {
61 sizeof(ModestUIDimmingManagerClass),
63 NULL, /* base finalize */
64 (GClassInitFunc) modest_ui_dimming_manager_class_init,
65 NULL, /* class finalize */
66 NULL, /* class data */
67 sizeof(ModestUIDimmingManager),
69 (GInstanceInitFunc) modest_ui_dimming_manager_init,
72 my_type = g_type_register_static (G_TYPE_OBJECT,
73 "ModestUIDimmingManager",
80 modest_ui_dimming_manager_class_init (ModestUIDimmingManagerClass *klass)
82 GObjectClass *gobject_class;
83 gobject_class = (GObjectClass*) klass;
85 parent_class = g_type_class_peek_parent (klass);
86 gobject_class->finalize = modest_ui_dimming_manager_finalize;
87 gobject_class->dispose = modest_ui_dimming_manager_dispose;
89 g_type_class_add_private (gobject_class, sizeof(ModestUIDimmingManagerPrivate));
93 modest_ui_dimming_manager_init (ModestUIDimmingManager *obj)
95 ModestUIDimmingManagerPrivate *priv;
97 priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(obj);
99 priv->groups_map = g_hash_table_new_full ((GHashFunc) g_str_hash,
100 (GEqualFunc) g_str_equal,
101 (GDestroyNotify) g_free,
102 (GDestroyNotify) g_object_unref);
103 priv->delayed_calls = g_hash_table_new_full (g_str_hash,
110 remove_all_timeouts (gpointer key,
114 if (GPOINTER_TO_INT (value) > 0)
115 g_source_remove (GPOINTER_TO_INT (value));
119 modest_ui_dimming_manager_finalize (GObject *obj)
121 ModestUIDimmingManagerPrivate *priv;
123 priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(obj);
125 if (priv->groups_map != NULL)
126 g_hash_table_unref (priv->groups_map);
128 if (priv->delayed_calls != NULL) {
129 /* Remove all pending calls */
130 g_hash_table_foreach (priv->delayed_calls,
134 g_hash_table_unref (priv->delayed_calls);
137 G_OBJECT_CLASS(parent_class)->finalize (obj);
141 modest_ui_dimming_manager_dispose (GObject *obj)
143 ModestUIDimmingManagerPrivate *priv;
145 priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(obj);
147 if (priv->delayed_calls != NULL && (g_hash_table_size (priv->delayed_calls) > 0)) {
148 /* Remove all pending calls */
149 g_hash_table_foreach (priv->delayed_calls,
152 g_hash_table_remove_all (priv->delayed_calls);
155 G_OBJECT_CLASS(parent_class)->dispose (obj);
159 ModestUIDimmingManager*
160 modest_ui_dimming_manager_new()
162 ModestUIDimmingManager *obj;
164 obj = MODEST_UI_DIMMING_MANAGER(g_object_new(MODEST_TYPE_UI_DIMMING_MANAGER, NULL));
171 modest_ui_dimming_manager_insert_rules_group (ModestUIDimmingManager *self,
172 ModestDimmingRulesGroup *group)
174 ModestUIDimmingManagerPrivate *priv;
175 gchar *group_name = NULL;
176 gboolean unique = FALSE;
178 priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(self);
181 group_name = modest_dimming_rules_group_get_name (group);
183 /* Check group name is unique */
184 unique = g_hash_table_lookup (priv->groups_map, group_name) == NULL;
187 g_return_if_fail (unique);
191 /* Insert new dimming rules group */
192 g_hash_table_insert (priv->groups_map, group_name, g_object_ref(group));
196 modest_ui_dimming_manager_process_dimming_rules (ModestUIDimmingManager *self)
198 ModestUIDimmingManagerPrivate *priv;
200 priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(self);
202 /* Peforms a full dimming tules checking */
203 g_hash_table_foreach (priv->groups_map, _process_all_rules, NULL);
208 ModestDimmingRulesGroup *group;
209 ModestUIDimmingManager *manager;
212 } DelayedDimmingRules;
215 process_dimming_rules_delayed (gpointer data)
217 DelayedDimmingRules *helper = (DelayedDimmingRules *) data;
218 gpointer timeout_handler;
219 ModestUIDimmingManagerPrivate *priv;
221 /* Let the destroyer remove it from the hash table */
222 helper->delete = TRUE;
224 /* We remove the timeout here because the execute action could
225 take too much time, and so this will be called again */
226 priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(helper->manager);
227 timeout_handler = g_hash_table_lookup (priv->delayed_calls, helper->name);
229 if (GPOINTER_TO_INT (timeout_handler) > 0) {
230 g_source_remove (GPOINTER_TO_INT (timeout_handler));
233 gdk_threads_enter ();
234 modest_dimming_rules_group_execute (helper->group);
235 gdk_threads_leave ();
241 process_dimming_rules_delayed_destroyer (gpointer data)
243 DelayedDimmingRules *helper = (DelayedDimmingRules *) data;
244 ModestUIDimmingManagerPrivate *priv;
246 priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(helper->manager);
248 /* We can only destroy it if we had really executed it. If the
249 source is removed because the manager is finalized then we
250 cannot remove it because it removes the sources in a
251 foreach, that does not allow you to modify the hash table
254 g_hash_table_remove (priv->delayed_calls, helper->name);
256 g_free (helper->name);
257 g_object_unref (helper->manager);
258 g_slice_free (DelayedDimmingRules, helper);
262 modest_ui_dimming_manager_process_dimming_rules_group (ModestUIDimmingManager *self,
263 const gchar *group_name)
265 ModestDimmingRulesGroup *group = NULL;
266 ModestUIDimmingManagerPrivate *priv;
267 guint *handler, new_handler;
268 DelayedDimmingRules *helper;
270 g_return_if_fail (group_name != NULL);
272 priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(self);
274 /* Search group by name */
275 group = MODEST_DIMMING_RULES_GROUP(g_hash_table_lookup (priv->groups_map, group_name));
276 g_return_if_fail (group != NULL);
278 /* If there was another pending dimming operation check then ignore this */
279 handler = g_hash_table_lookup (priv->delayed_calls, group_name);
281 /* Create the helper and start the timeout */
282 helper = g_slice_new (DelayedDimmingRules);
283 helper->group = group;
284 helper->manager = g_object_ref (self);
285 helper->name = g_strdup (group_name);
286 helper->delete = FALSE;
287 new_handler = g_timeout_add_full (G_PRIORITY_DEFAULT, 100,
288 process_dimming_rules_delayed,
289 helper, process_dimming_rules_delayed_destroyer);
290 g_hash_table_insert (priv->delayed_calls,
291 g_strdup (group_name),
292 GINT_TO_POINTER (new_handler));
293 MODEST_DEBUG_BLOCK(g_print ("---------------------Adding %d\n", new_handler););
295 MODEST_DEBUG_BLOCK(g_print ("---------------------Ignoring\n"););
301 _process_all_rules (gpointer key, gpointer value, gpointer user_data)
303 g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP (value));
305 modest_dimming_rules_group_execute (MODEST_DIMMING_RULES_GROUP (value));