* Fixes NB#91689. fixes a wrong check for ASCII
[modest] / src / modest-dimming-rules-group.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-dimming-rules-group.h"
31 #include "modest-dimming-rules-group-priv.h"
32 #include "modest-dimming-rule.h"
33 #include "modest-platform.h"
34 #include "modest-ui-dimming-rules.h"
35
36 static void modest_dimming_rules_group_class_init (ModestDimmingRulesGroupClass *klass);
37 static void modest_dimming_rules_group_init       (ModestDimmingRulesGroup *obj);
38 static void modest_dimming_rules_group_finalize   (GObject *obj);
39
40 #ifndef MODEST_TOOLKIT_GTK
41 static void _insensitive_press_callback (GtkWidget *widget, gpointer user_data);
42 #endif
43
44 static void on_window_destroy (gpointer data,
45                                GObject *object);
46
47 static void _add_rule (ModestDimmingRulesGroup *self,
48                        ModestDimmingRule *rule,
49                        ModestWindow *window);
50
51 typedef struct _ModestDimmingRulesGroupPrivate ModestDimmingRulesGroupPrivate;
52 struct _ModestDimmingRulesGroupPrivate {        
53         ModestWindow *window;
54         gchar *name;
55         gboolean notifications_enabled;
56         GHashTable *rules_map;
57         GSList *widget_rules;
58 };
59
60
61 #define MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
62                                                    MODEST_TYPE_DIMMING_RULES_GROUP, \
63                                                    ModestDimmingRulesGroupPrivate))
64
65 /* globals */
66 static GObjectClass *parent_class = NULL;
67
68 static void _execute_dimming_rule (gpointer key, gpointer value, gpointer user_data);
69 static void _execute_widget_dimming_rule (gpointer data, gpointer user_data);
70
71 GType
72 modest_dimming_rules_group_get_type (void)
73 {
74         static GType my_type = 0;
75         if (!my_type) {
76                 static const GTypeInfo my_info = {
77                         sizeof(ModestDimmingRulesGroupClass),
78                         NULL,           /* base init */
79                         NULL,           /* base finalize */
80                         (GClassInitFunc) modest_dimming_rules_group_class_init,
81                         NULL,           /* class finalize */
82                         NULL,           /* class data */
83                         sizeof(ModestDimmingRulesGroup),
84                         1,              /* n_preallocs */
85                         (GInstanceInitFunc) modest_dimming_rules_group_init,
86                         NULL
87                 };
88                 my_type = g_type_register_static (G_TYPE_OBJECT,
89                                                   "ModestDimmingRulesGroup",
90                                                   &my_info, 0);
91         }
92         return my_type;
93 }
94
95 static void
96 modest_dimming_rules_group_class_init (ModestDimmingRulesGroupClass *klass)
97 {
98         GObjectClass *gobject_class;
99         gobject_class = (GObjectClass*) klass;
100
101         parent_class            = g_type_class_peek_parent (klass);
102         gobject_class->finalize = modest_dimming_rules_group_finalize;
103
104         g_type_class_add_private (gobject_class, sizeof(ModestDimmingRulesGroupPrivate));
105 }
106
107 static void
108 modest_dimming_rules_group_init (ModestDimmingRulesGroup *obj)
109 {
110         ModestDimmingRulesGroupPrivate *priv;
111
112         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(obj);
113
114         priv->name = NULL;
115         priv->window = NULL;
116         priv->notifications_enabled = FALSE;
117         priv->rules_map = g_hash_table_new_full ((GHashFunc) g_str_hash,
118                                                  (GEqualFunc) g_str_equal,
119                                                  (GDestroyNotify) g_free,
120                                                  (GDestroyNotify) g_object_unref);
121         priv->widget_rules = NULL;
122 }
123
124 static void
125 modest_dimming_rules_group_finalize (GObject *obj)
126 {
127         ModestDimmingRulesGroupPrivate *priv;
128
129         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(obj);
130
131         if (priv->name != NULL)
132                 g_free(priv->name);
133
134         if (priv->rules_map != NULL)
135                 g_hash_table_destroy (priv->rules_map);
136
137         if (priv->widget_rules != NULL)
138                 g_slist_foreach (priv->widget_rules, (GFunc) g_object_unref, NULL);
139
140         G_OBJECT_CLASS(parent_class)->finalize (obj);
141 }
142
143
144 ModestDimmingRulesGroup*
145 modest_dimming_rules_group_new(const gchar *group_name,
146                                gboolean notifications_enabled)
147 {
148         ModestDimmingRulesGroup *obj;
149         ModestDimmingRulesGroupPrivate *priv;
150                 
151         g_return_val_if_fail (group_name != NULL, NULL);
152
153         obj = MODEST_DIMMING_RULES_GROUP(g_object_new(MODEST_TYPE_DIMMING_RULES_GROUP, NULL));
154
155         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(obj);
156         priv->name = g_strdup(group_name);
157         priv->notifications_enabled = notifications_enabled;
158
159         return obj;
160 }
161
162
163 gchar *
164 modest_dimming_rules_group_get_name (ModestDimmingRulesGroup *self) 
165 {
166         ModestDimmingRulesGroupPrivate *priv;
167         
168         g_return_val_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self), NULL);
169
170         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
171         
172         return g_strdup (priv->name);
173 }
174
175 static void
176 _add_rule (ModestDimmingRulesGroup *self,
177            ModestDimmingRule *rule,
178            ModestWindow *window)
179 {
180         ModestDimmingRulesGroupPrivate *priv;
181         GtkWidget *widget = NULL;
182         const gchar *action_path = NULL;
183
184         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self));
185         g_return_if_fail (MODEST_IS_WINDOW(window));
186         g_return_if_fail (MODEST_IS_DIMMING_RULE (rule));
187         
188         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
189
190         /* Set window to process dimming rules */
191         priv->window = MODEST_WINDOW (window);
192
193         widget = modest_dimming_rule_get_widget (rule);
194 #ifndef MODEST_TOOLKIT_GTK
195         /* Connect insensitive-presss handler to show notifications */
196         g_signal_connect (G_OBJECT (widget), "insensitive-press", 
197                           G_CALLBACK (_insensitive_press_callback), 
198                           rule);
199 #endif
200         /* Register new dimming rule */         
201         modest_dimming_rule_set_group (rule, self);
202         action_path = modest_dimming_rule_get_action_path (rule);
203         if (action_path)
204                 g_hash_table_insert (priv->rules_map, g_strdup(action_path), rule);
205         else
206                 priv->widget_rules = g_slist_prepend (priv->widget_rules, rule);
207 }
208
209 void
210 modest_dimming_rules_group_add_widget_rule (ModestDimmingRulesGroup *self,
211                                             GtkWidget *widget,
212                                             GCallback callback,
213                                             ModestWindow *window)
214 {
215         ModestDimmingRulesGroupPrivate *priv;
216         ModestDimmingRule *dim_rule = NULL;
217
218         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self));
219         g_return_if_fail (GTK_IS_WIDGET (widget));
220         g_return_if_fail (MODEST_IS_WINDOW(window));
221         
222         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
223
224         /* Set window to process dimming rules */
225         priv->window = MODEST_WINDOW (window);
226
227         dim_rule = modest_dimming_rule_new_from_widget (priv->window,
228                                                         (ModestDimmingCallback) callback,
229                                                         widget);
230
231         _add_rule (self, dim_rule, window);
232 }
233
234 void
235 modest_dimming_rules_group_add_rules (ModestDimmingRulesGroup *self,
236                                       const ModestDimmingEntry modest_dimming_entries[],
237                                       guint n_elements,
238                                       ModestWindow *window)
239 {
240         ModestDimmingRulesGroupPrivate *priv;
241         ModestDimmingRule *dim_rule = NULL;
242         ModestDimmingEntry entry;
243         GtkWidget *widget = NULL;
244         gboolean unique = FALSE;
245         guint i;
246
247         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self));
248         g_return_if_fail (modest_dimming_entries != NULL);
249         g_return_if_fail (n_elements > 0);
250         g_return_if_fail (MODEST_IS_WINDOW(window));
251         
252         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
253
254         /* Set window to process dimming rules */
255         priv->window = MODEST_WINDOW (window);
256         g_object_weak_ref (G_OBJECT (window), on_window_destroy, self);
257
258         /* Add dimming rules */
259         for (i=0; i < n_elements; i++) {
260                 entry = modest_dimming_entries[i]; 
261                 
262                 /* Check dimming_rule and action path are NOT NULL */
263                 if (entry.action_path == NULL) continue;
264                 if (entry.callback == NULL) continue;
265                 
266                 /* Check action path is unique */
267                 unique = g_hash_table_lookup (priv->rules_map, entry.action_path) == NULL;
268                 if (!unique) continue;
269
270                 /* Check action path is valid */
271                 widget = modest_window_get_action_widget (MODEST_WINDOW (window), entry.action_path);
272                 if (widget == NULL) continue;
273
274                 /* Create a new dimming rule */
275                 dim_rule = modest_dimming_rule_new (priv->window,
276                                                     (ModestDimmingCallback) entry.callback,
277                                                     entry.action_path);
278
279                 _add_rule (self, dim_rule, window);
280         }
281 }
282
283 gboolean 
284 modest_dimming_rules_group_notifications_enabled (ModestDimmingRulesGroup *self)
285 {
286         ModestDimmingRulesGroupPrivate *priv;
287
288         g_return_val_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self), FALSE);
289         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
290         
291         return priv->notifications_enabled;
292
293
294 void 
295 modest_dimming_rules_group_execute (ModestDimmingRulesGroup *self) 
296 {
297         ModestDimmingRulesGroupPrivate *priv;
298         DimmedState *state = NULL;
299
300         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self));
301         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
302
303         if (!priv->window)
304                 return;
305
306         /* Init dimming rules init data */
307         state = modest_ui_dimming_rules_define_dimming_state (priv->window);    
308         modest_window_set_dimming_state (priv->window, state);
309
310         /* execute group dimming rules */
311         g_hash_table_foreach (priv->rules_map, _execute_dimming_rule, NULL);
312         g_slist_foreach (priv->widget_rules, (GFunc) _execute_widget_dimming_rule, NULL);
313
314         /* Free dimming ruls init data */
315         modest_window_set_dimming_state (priv->window, NULL);
316 }
317
318
319 static void
320 _execute_dimming_rule (gpointer key, gpointer value, gpointer user_data)
321 {
322         g_return_if_fail (MODEST_IS_DIMMING_RULE (value));
323
324         /* Process dimming rule */
325         modest_dimming_rule_process (MODEST_DIMMING_RULE(value));
326 }
327
328 static void
329 _execute_widget_dimming_rule (gpointer data, gpointer user_data)
330 {
331         g_return_if_fail (MODEST_IS_DIMMING_RULE (data));
332
333         /* Process dimming rule */
334         modest_dimming_rule_process (MODEST_DIMMING_RULE(data));
335 }
336
337 #ifndef MODEST_TOOLKIT_GTK
338 static void
339 _insensitive_press_callback (GtkWidget *widget, gpointer user_data)
340 {
341         ModestDimmingRulesGroup *group = NULL;
342         ModestDimmingRule *rule = NULL;
343         gchar *notification = NULL;
344
345         g_return_if_fail (MODEST_IS_DIMMING_RULE (user_data));
346         rule = MODEST_DIMMING_RULE (user_data);
347
348         /* Check if this group has notification system enabled */
349         group = modest_dimming_rule_get_group (rule);
350         if (!modest_dimming_rules_group_notifications_enabled (group))
351                 goto frees;
352
353         /* Get specific notification */
354         notification = modest_dimming_rule_get_notification (rule);
355         if (notification == NULL)
356                 goto frees;
357         
358         /* Show notification banner */
359         modest_platform_information_banner (NULL, NULL, notification);  
360         
361         /* Free */
362  frees:
363         if (group != NULL)
364                 g_object_unref(group);
365         if (notification != NULL)
366                 g_free(notification);
367 }
368 #endif
369
370 static void
371 on_window_destroy (gpointer data,
372                    GObject *object)
373 {
374         ModestDimmingRulesGroup *self = MODEST_DIMMING_RULES_GROUP (data);
375         ModestDimmingRulesGroupPrivate *priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE (self);
376
377         priv->window = NULL;
378 }