Fix modest_tny_msg_header_get_all_recipients_list (in case from is empty)
[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->window)
132                 g_object_weak_unref (G_OBJECT (priv->window), on_window_destroy, obj);
133
134         if (priv->name != NULL)
135                 g_free(priv->name);
136
137         if (priv->rules_map != NULL)
138                 g_hash_table_destroy (priv->rules_map);
139
140         if (priv->widget_rules != NULL)
141                 g_slist_foreach (priv->widget_rules, (GFunc) g_object_unref, NULL);
142
143         G_OBJECT_CLASS(parent_class)->finalize (obj);
144 }
145
146
147 ModestDimmingRulesGroup*
148 modest_dimming_rules_group_new(const gchar *group_name,
149                                gboolean notifications_enabled)
150 {
151         ModestDimmingRulesGroup *obj;
152         ModestDimmingRulesGroupPrivate *priv;
153                 
154         g_return_val_if_fail (group_name != NULL, NULL);
155
156         obj = MODEST_DIMMING_RULES_GROUP(g_object_new(MODEST_TYPE_DIMMING_RULES_GROUP, NULL));
157
158         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(obj);
159         priv->name = g_strdup(group_name);
160         priv->notifications_enabled = notifications_enabled;
161
162         return obj;
163 }
164
165
166 gchar *
167 modest_dimming_rules_group_get_name (ModestDimmingRulesGroup *self) 
168 {
169         ModestDimmingRulesGroupPrivate *priv;
170         
171         g_return_val_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self), NULL);
172
173         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
174         
175         return g_strdup (priv->name);
176 }
177
178 static void
179 _add_rule (ModestDimmingRulesGroup *self,
180            ModestDimmingRule *rule,
181            ModestWindow *window)
182 {
183         ModestDimmingRulesGroupPrivate *priv;
184         GtkWidget *widget = NULL;
185         const gchar *action_path = NULL;
186
187         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self));
188         g_return_if_fail (MODEST_IS_WINDOW(window));
189         g_return_if_fail (MODEST_IS_DIMMING_RULE (rule));
190         
191         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
192
193         /* Set window to process dimming rules */
194         priv->window = MODEST_WINDOW (window);
195
196         widget = modest_dimming_rule_get_widget (rule);
197 #ifndef MODEST_TOOLKIT_GTK
198         /* Connect insensitive-presss handler to show notifications */
199         g_signal_connect (G_OBJECT (widget), "insensitive-press", 
200                           G_CALLBACK (_insensitive_press_callback), 
201                           rule);
202 #endif
203         /* Register new dimming rule */         
204         modest_dimming_rule_set_group (rule, self);
205         action_path = modest_dimming_rule_get_action_path (rule);
206         if (action_path)
207                 g_hash_table_insert (priv->rules_map, g_strdup(action_path), rule);
208         else
209                 priv->widget_rules = g_slist_prepend (priv->widget_rules, rule);
210 }
211
212 void
213 modest_dimming_rules_group_add_widget_rule (ModestDimmingRulesGroup *self,
214                                             GtkWidget *widget,
215                                             GCallback callback,
216                                             ModestWindow *window)
217 {
218         ModestDimmingRulesGroupPrivate *priv;
219         ModestDimmingRule *dim_rule = NULL;
220
221         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self));
222         g_return_if_fail (GTK_IS_WIDGET (widget));
223         g_return_if_fail (MODEST_IS_WINDOW(window));
224         
225         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
226
227         /* Set window to process dimming rules */
228         priv->window = MODEST_WINDOW (window);
229
230         dim_rule = modest_dimming_rule_new_from_widget (priv->window,
231                                                         (ModestDimmingCallback) callback,
232                                                         widget);
233
234         _add_rule (self, dim_rule, window);
235 }
236
237 void
238 modest_dimming_rules_group_add_rules (ModestDimmingRulesGroup *self,
239                                       const ModestDimmingEntry modest_dimming_entries[],
240                                       guint n_elements,
241                                       ModestWindow *window)
242 {
243         ModestDimmingRulesGroupPrivate *priv;
244         ModestDimmingRule *dim_rule = NULL;
245         ModestDimmingEntry entry;
246         GtkWidget *widget = NULL;
247         gboolean unique = FALSE;
248         guint i;
249
250         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self));
251         g_return_if_fail (modest_dimming_entries != NULL);
252         g_return_if_fail (n_elements > 0);
253         g_return_if_fail (MODEST_IS_WINDOW(window));
254         
255         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
256
257         /* Set window to process dimming rules */
258         priv->window = MODEST_WINDOW (window);
259         g_object_weak_ref (G_OBJECT (window), on_window_destroy, self);
260
261         /* Add dimming rules */
262         for (i=0; i < n_elements; i++) {
263                 entry = modest_dimming_entries[i]; 
264                 
265                 /* Check dimming_rule and action path are NOT NULL */
266                 if (entry.action_path == NULL) continue;
267                 if (entry.callback == NULL) continue;
268                 
269                 /* Check action path is unique */
270                 unique = g_hash_table_lookup (priv->rules_map, entry.action_path) == NULL;
271                 if (!unique) continue;
272
273                 /* Check action path is valid */
274                 widget = modest_window_get_action_widget (MODEST_WINDOW (window), entry.action_path);
275                 if (widget == NULL) continue;
276
277                 /* Create a new dimming rule */
278                 dim_rule = modest_dimming_rule_new (priv->window,
279                                                     (ModestDimmingCallback) entry.callback,
280                                                     entry.action_path);
281
282                 _add_rule (self, dim_rule, window);
283         }
284 }
285
286 gboolean 
287 modest_dimming_rules_group_notifications_enabled (ModestDimmingRulesGroup *self)
288 {
289         ModestDimmingRulesGroupPrivate *priv;
290
291         g_return_val_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self), FALSE);
292         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
293         
294         return priv->notifications_enabled;
295
296
297 void 
298 modest_dimming_rules_group_execute (ModestDimmingRulesGroup *self) 
299 {
300         ModestDimmingRulesGroupPrivate *priv;
301         DimmedState *state = NULL;
302
303         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self));
304         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
305
306         if (!priv->window)
307                 return;
308
309         /* Init dimming rules init data */
310         state = modest_ui_dimming_rules_define_dimming_state (priv->window);    
311         modest_window_set_dimming_state (priv->window, state);
312
313         /* execute group dimming rules */
314         g_hash_table_foreach (priv->rules_map, _execute_dimming_rule, NULL);
315         g_slist_foreach (priv->widget_rules, (GFunc) _execute_widget_dimming_rule, NULL);
316
317         /* Free dimming ruls init data */
318         modest_window_set_dimming_state (priv->window, NULL);
319 }
320
321
322 static void
323 _execute_dimming_rule (gpointer key, gpointer value, gpointer user_data)
324 {
325         g_return_if_fail (MODEST_IS_DIMMING_RULE (value));
326
327         /* Process dimming rule */
328         modest_dimming_rule_process (MODEST_DIMMING_RULE(value));
329 }
330
331 static void
332 _execute_widget_dimming_rule (gpointer data, gpointer user_data)
333 {
334         g_return_if_fail (MODEST_IS_DIMMING_RULE (data));
335
336         /* Process dimming rule */
337         modest_dimming_rule_process (MODEST_DIMMING_RULE(data));
338 }
339
340 #ifndef MODEST_TOOLKIT_GTK
341 static void
342 _insensitive_press_callback (GtkWidget *widget, gpointer user_data)
343 {
344         ModestDimmingRulesGroup *group = NULL;
345         ModestDimmingRule *rule = NULL;
346         gchar *notification = NULL;
347
348         g_return_if_fail (MODEST_IS_DIMMING_RULE (user_data));
349         rule = MODEST_DIMMING_RULE (user_data);
350
351         /* Check if this group has notification system enabled */
352         group = modest_dimming_rule_get_group (rule);
353         if (!modest_dimming_rules_group_notifications_enabled (group))
354                 goto frees;
355
356         /* Get specific notification */
357         notification = modest_dimming_rule_get_notification (rule);
358         if (notification == NULL)
359                 goto frees;
360
361         /* Notifications for dimmed items are not shown in Hildon2 */
362 #ifndef MODEST_TOOLKIT_HILDON2
363         /* Show notification banner */
364         modest_platform_information_banner (NULL, NULL, notification);
365 #endif
366
367         /* Free */
368  frees:
369         if (group != NULL)
370                 g_object_unref(group);
371         if (notification != NULL)
372                 g_free(notification);
373 }
374 #endif
375
376 static void
377 on_window_destroy (gpointer data,
378                    GObject *object)
379 {
380         ModestDimmingRulesGroup *self = MODEST_DIMMING_RULES_GROUP (data);
381         ModestDimmingRulesGroupPrivate *priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE (self);
382
383         priv->window = NULL;
384 }