* Migrated both the new folder and rename folder dialogs to Fremantle UI: removed...
[modest] / src / modest-ui-dimming-manager.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include "modest-debug.h"
31 #include "modest-ui-dimming-manager.h"
32 #include "modest-dimming-rules-group-priv.h"
33
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
38 static void _process_all_rules (gpointer key, gpointer value, gpointer user_data);
39
40
41 typedef struct _ModestUIDimmingManagerPrivate ModestUIDimmingManagerPrivate;
42 struct _ModestUIDimmingManagerPrivate {
43         GHashTable *groups_map;
44         GHashTable *delayed_calls;
45 };
46
47 #define MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
48                                                    MODEST_TYPE_UI_DIMMING_MANAGER, \
49                                                    ModestUIDimmingManagerPrivate))
50
51 /* globals */
52 static GObjectClass *parent_class = NULL;
53
54 GType
55 modest_ui_dimming_manager_get_type (void)
56 {
57         static GType my_type = 0;
58         if (!my_type) {
59                 static const GTypeInfo my_info = {
60                         sizeof(ModestUIDimmingManagerClass),
61                         NULL,           /* base init */
62                         NULL,           /* base finalize */
63                         (GClassInitFunc) modest_ui_dimming_manager_class_init,
64                         NULL,           /* class finalize */
65                         NULL,           /* class data */
66                         sizeof(ModestUIDimmingManager),
67                         1,              /* n_preallocs */
68                         (GInstanceInitFunc) modest_ui_dimming_manager_init,
69                         NULL
70                 };
71                 my_type = g_type_register_static (G_TYPE_OBJECT,
72                                                   "ModestUIDimmingManager",
73                                                   &my_info, 0);
74         }
75         return my_type;
76 }
77
78 static void
79 modest_ui_dimming_manager_class_init (ModestUIDimmingManagerClass *klass)
80 {
81         GObjectClass *gobject_class;
82         gobject_class = (GObjectClass*) klass;
83
84         parent_class            = g_type_class_peek_parent (klass);
85         gobject_class->finalize = modest_ui_dimming_manager_finalize;
86
87         g_type_class_add_private (gobject_class, sizeof(ModestUIDimmingManagerPrivate));
88 }
89
90 static void
91 modest_ui_dimming_manager_init (ModestUIDimmingManager *obj)
92 {
93         ModestUIDimmingManagerPrivate *priv;
94
95         priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(obj);
96
97         priv->groups_map = g_hash_table_new_full ((GHashFunc) g_str_hash,
98                                                   (GEqualFunc) g_str_equal,
99                                                   (GDestroyNotify) g_free,
100                                                   (GDestroyNotify) g_object_unref);
101         priv->delayed_calls = g_hash_table_new_full (g_str_hash,
102                                                      g_str_equal,
103                                                      g_free,
104                                                      NULL);
105 }
106
107 static void
108 remove_all_timeouts (gpointer key, 
109                      gpointer value, 
110                      gpointer user_data)
111 {
112         if (GPOINTER_TO_INT (value) > 0)
113                 g_source_remove (GPOINTER_TO_INT (value));
114 }
115
116 static void
117 modest_ui_dimming_manager_finalize (GObject *obj)
118 {
119         ModestUIDimmingManagerPrivate *priv;
120
121         priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(obj);
122
123         if (priv->groups_map != NULL)
124                 g_hash_table_unref (priv->groups_map);
125
126         if (priv->delayed_calls != NULL) {
127                 /* Remove all pending calls */
128                 g_hash_table_foreach (priv->delayed_calls,
129                                       remove_all_timeouts,
130                                       NULL);
131
132                 g_hash_table_unref (priv->delayed_calls);
133         }
134
135         G_OBJECT_CLASS(parent_class)->finalize (obj);
136 }
137
138
139 ModestUIDimmingManager*
140 modest_ui_dimming_manager_new()
141 {
142         ModestUIDimmingManager *obj;
143                 
144         obj = MODEST_UI_DIMMING_MANAGER(g_object_new(MODEST_TYPE_UI_DIMMING_MANAGER, NULL));
145
146
147         return obj;
148 }
149
150 void
151 modest_ui_dimming_manager_insert_rules_group (ModestUIDimmingManager *self,
152                                               ModestDimmingRulesGroup *group)
153 {
154         ModestUIDimmingManagerPrivate *priv;
155         gchar *group_name = NULL;
156         gboolean unique = FALSE;
157         
158         priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(self);
159         
160         /* Get group name */
161         group_name = modest_dimming_rules_group_get_name (group);
162         
163         /* Check group name is unique */
164         unique = g_hash_table_lookup (priv->groups_map, group_name) == NULL;
165         if (!unique) {
166                 g_free(group_name);
167                 g_return_if_fail (unique);
168         }
169         
170         
171         /* Insert new dimming rules group */
172         g_hash_table_insert (priv->groups_map, group_name, g_object_ref(group));
173 }
174
175 void
176 modest_ui_dimming_manager_process_dimming_rules (ModestUIDimmingManager *self)
177 {
178         ModestUIDimmingManagerPrivate *priv;
179         
180         priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(self);
181
182         /* Peforms a full dimming tules checking */
183         g_hash_table_foreach (priv->groups_map, _process_all_rules, NULL);
184 }
185
186 typedef struct
187 {
188         ModestDimmingRulesGroup *group;
189         ModestUIDimmingManager *manager;
190         gchar *name;
191 } DelayedDimmingRules;
192
193 static gboolean
194 process_dimming_rules_delayed (gpointer data)
195 {
196         DelayedDimmingRules *helper = (DelayedDimmingRules *) data;
197         gpointer timeout_handler;
198         ModestUIDimmingManagerPrivate *priv;
199
200         /* We remove the timeout here because the execute action could
201            take too much time, and so this will be called again */
202         priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(helper->manager);
203         timeout_handler = g_hash_table_lookup (priv->delayed_calls, helper->name);
204
205         if (GPOINTER_TO_INT (timeout_handler) > 0) {
206                 g_source_remove (GPOINTER_TO_INT (timeout_handler));
207         }
208
209         gdk_threads_enter ();
210         modest_dimming_rules_group_execute (helper->group);
211         gdk_threads_leave ();
212
213         return FALSE;
214 }
215
216 static void
217 process_dimming_rules_delayed_destroyer (gpointer data)
218 {
219         DelayedDimmingRules *helper = (DelayedDimmingRules *) data;
220         ModestUIDimmingManagerPrivate *priv;
221
222         priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(helper->manager);
223         g_hash_table_remove (priv->delayed_calls, helper->name);
224         g_free (helper->name);
225         g_object_unref (helper->manager);
226         g_slice_free (DelayedDimmingRules, helper);
227 }
228
229 void
230 modest_ui_dimming_manager_process_dimming_rules_group (ModestUIDimmingManager *self,
231                                                        const gchar *group_name)
232 {
233         ModestDimmingRulesGroup *group = NULL;
234         ModestUIDimmingManagerPrivate *priv;
235         guint *handler, new_handler;
236         DelayedDimmingRules *helper;
237         
238         g_return_if_fail (group_name != NULL);
239
240         priv = MODEST_UI_DIMMING_MANAGER_GET_PRIVATE(self);
241
242         /* Search group by name */
243         group = MODEST_DIMMING_RULES_GROUP(g_hash_table_lookup (priv->groups_map, group_name));
244         g_return_if_fail (group != NULL);
245
246         /* If there was another pending dimming operation check then ignore this */
247         handler = g_hash_table_lookup (priv->delayed_calls, group_name);
248         if (!handler) {
249                 /* Create the helper and start the timeout */
250                 helper = g_slice_new (DelayedDimmingRules);
251                 helper->group = group;
252                 helper->manager = g_object_ref (self);
253                 helper->name = g_strdup (group_name);
254                 new_handler = g_timeout_add_full (G_PRIORITY_DEFAULT, 100, process_dimming_rules_delayed, 
255                                                   helper, process_dimming_rules_delayed_destroyer);
256                 g_hash_table_insert (priv->delayed_calls, g_strdup (group_name), GINT_TO_POINTER (new_handler));
257                 MODEST_DEBUG_BLOCK(g_print ("---------------------Adding %d\n", new_handler););
258         } else {
259                 MODEST_DEBUG_BLOCK(g_print ("---------------------Ignoring\n"););
260         }
261 }
262
263
264 static void
265 _process_all_rules (gpointer key, gpointer value, gpointer user_data)
266 {
267         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP (value));
268         
269         modest_dimming_rules_group_execute (MODEST_DIMMING_RULES_GROUP (value));
270 }
271