* check for a valid foldername
[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
35 static void modest_dimming_rules_group_class_init (ModestDimmingRulesGroupClass *klass);
36 static void modest_dimming_rules_group_init       (ModestDimmingRulesGroup *obj);
37 static void modest_dimming_rules_group_finalize   (GObject *obj);
38
39 static void _insensitive_press_callback (GtkWidget *widget, gpointer user_data);
40
41
42 typedef struct _ModestDimmingRulesGroupPrivate ModestDimmingRulesGroupPrivate;
43 struct _ModestDimmingRulesGroupPrivate {        
44         gchar *name;
45         gboolean notifications_enabled;
46         GHashTable *rules_map;
47 };
48
49
50 #define MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
51                                                    MODEST_TYPE_DIMMING_RULES_GROUP, \
52                                                    ModestDimmingRulesGroupPrivate))
53
54 /* globals */
55 static GObjectClass *parent_class = NULL;
56
57 static void _execute_dimming_rule (gpointer key, gpointer value, gpointer user_data);
58 static void _insensitive_press_callback (GtkWidget *widget, gpointer user_data);
59
60
61
62 GType
63 modest_dimming_rules_group_get_type (void)
64 {
65         static GType my_type = 0;
66         if (!my_type) {
67                 static const GTypeInfo my_info = {
68                         sizeof(ModestDimmingRulesGroupClass),
69                         NULL,           /* base init */
70                         NULL,           /* base finalize */
71                         (GClassInitFunc) modest_dimming_rules_group_class_init,
72                         NULL,           /* class finalize */
73                         NULL,           /* class data */
74                         sizeof(ModestDimmingRulesGroup),
75                         1,              /* n_preallocs */
76                         (GInstanceInitFunc) modest_dimming_rules_group_init,
77                         NULL
78                 };
79                 my_type = g_type_register_static (G_TYPE_OBJECT,
80                                                   "ModestDimmingRulesGroup",
81                                                   &my_info, 0);
82         }
83         return my_type;
84 }
85
86 static void
87 modest_dimming_rules_group_class_init (ModestDimmingRulesGroupClass *klass)
88 {
89         GObjectClass *gobject_class;
90         gobject_class = (GObjectClass*) klass;
91
92         parent_class            = g_type_class_peek_parent (klass);
93         gobject_class->finalize = modest_dimming_rules_group_finalize;
94
95         g_type_class_add_private (gobject_class, sizeof(ModestDimmingRulesGroupPrivate));
96 }
97
98 static void
99 modest_dimming_rules_group_init (ModestDimmingRulesGroup *obj)
100 {
101         ModestDimmingRulesGroupPrivate *priv;
102
103         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(obj);
104
105         priv->name = NULL;
106         priv->rules_map = g_hash_table_new_full ((GHashFunc) g_str_hash,
107                                                  (GEqualFunc) g_str_equal,
108                                                  (GDestroyNotify) g_free,
109                                                  (GDestroyNotify) g_object_unref);
110 }
111
112 static void
113 modest_dimming_rules_group_finalize (GObject *obj)
114 {
115         ModestDimmingRulesGroupPrivate *priv;
116
117         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(obj);
118
119         if (priv->name != NULL)
120                 g_free(priv->name);
121
122         if (priv->rules_map != NULL)
123                 g_hash_table_destroy (priv->rules_map);
124
125         G_OBJECT_CLASS(parent_class)->finalize (obj);
126 }
127
128
129 ModestDimmingRulesGroup*
130 modest_dimming_rules_group_new(const gchar *group_name,
131                                gboolean notifications_enabled)
132 {
133         ModestDimmingRulesGroup *obj;
134         ModestDimmingRulesGroupPrivate *priv;
135                 
136         g_return_val_if_fail (group_name != NULL, NULL);
137
138         obj = MODEST_DIMMING_RULES_GROUP(g_object_new(MODEST_TYPE_DIMMING_RULES_GROUP, NULL));
139
140         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(obj);
141         priv->name = g_strdup(group_name);
142         priv->notifications_enabled = notifications_enabled;
143
144         return obj;
145 }
146
147
148 gchar *
149 modest_dimming_rules_group_get_name (ModestDimmingRulesGroup *self) 
150 {
151         ModestDimmingRulesGroupPrivate *priv;
152         
153         g_return_val_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self), NULL);
154
155         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
156         
157         return g_strdup (priv->name);
158 }
159
160 void
161 modest_dimming_rules_group_add_rules (ModestDimmingRulesGroup *self,
162                                       const ModestDimmingEntry modest_dimming_entries[],
163                                       guint n_elements,
164                                       gpointer user_data)
165 {
166         ModestDimmingRulesGroupPrivate *priv;
167         ModestDimmingRule *dim_rule = NULL;
168         ModestDimmingEntry entry;
169         GtkWidget *widget = NULL;
170         gboolean unique = FALSE;
171         guint i;
172
173         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self));
174         g_return_if_fail (modest_dimming_entries != NULL);
175         g_return_if_fail (n_elements > 0);
176         g_return_if_fail (MODEST_IS_WINDOW(user_data));
177         
178         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
179
180         for (i=0; i < n_elements; i++) {
181                 entry = modest_dimming_entries[i]; 
182                 
183                 /* Check dimming_rule and action path are NOT NULL */
184                 if (entry.action_path == NULL) continue;
185                 if (entry.callback == NULL) continue;
186                 
187                 /* Check action path is unique */
188                 unique = g_hash_table_lookup (priv->rules_map, entry.action_path) == NULL;
189                 if (!unique) continue;
190
191                 /* Check action path is valid */
192                 widget = modest_window_get_action_widget (MODEST_WINDOW (user_data), entry.action_path);
193                 if (widget == NULL) continue;
194
195                 /* Create a new dimming rule */
196                 dim_rule = modest_dimming_rule_new (MODEST_WINDOW(user_data),
197                                                     (ModestDimmingCallback) entry.callback,
198                                                     entry.action_path);
199                 
200                 /* Connect insensitive-presss handler to show notifications */
201                 g_signal_connect (G_OBJECT (widget), "insensitive-press", 
202                                   G_CALLBACK (_insensitive_press_callback), 
203                                   dim_rule);
204
205                 /* Register new dimming rule */         
206                 modest_dimming_rule_set_group (dim_rule, self);
207                 g_hash_table_insert (priv->rules_map, g_strdup(entry.action_path), dim_rule);
208         }
209 }
210
211 gboolean 
212 modest_dimming_rules_group_notifications_enabled (ModestDimmingRulesGroup *self)
213 {
214         ModestDimmingRulesGroupPrivate *priv;
215
216         g_return_val_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self), FALSE);
217         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
218         
219         return priv->notifications_enabled;
220
221
222 void 
223 modest_dimming_rules_group_execute (ModestDimmingRulesGroup *self) 
224 {
225         ModestDimmingRulesGroupPrivate *priv;
226
227         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP(self));
228         priv = MODEST_DIMMING_RULES_GROUP_GET_PRIVATE(self);
229
230         g_hash_table_foreach (priv->rules_map, _execute_dimming_rule, NULL);
231 }
232
233
234 static void
235 _execute_dimming_rule (gpointer key, gpointer value, gpointer user_data)
236 {
237         g_return_if_fail (MODEST_IS_DIMMING_RULE (value));
238
239         /* Process dimming rule */
240         modest_dimming_rule_process (MODEST_DIMMING_RULE(value));
241 }
242
243 static void
244 _insensitive_press_callback (GtkWidget *widget, gpointer user_data)
245 {
246         ModestDimmingRulesGroup *group = NULL;
247         ModestDimmingRule *rule = NULL;
248         gchar *notification = NULL;
249
250         g_return_if_fail (MODEST_IS_DIMMING_RULE (user_data));
251         rule = MODEST_DIMMING_RULE (user_data);
252
253         /* Check if this group has notification system enabled */
254         group = modest_dimming_rule_get_group (rule);
255         if (!modest_dimming_rules_group_notifications_enabled (group))
256                 goto frees;
257
258         /* Get specific notification */
259         notification = modest_dimming_rule_get_notification (rule);
260         if (notification == NULL)
261                 goto frees;
262         
263         /* Show notification banner */
264         modest_platform_information_banner (NULL, NULL, notification);  
265         
266         /* Free */
267  frees:
268         if (group != NULL)
269                 g_object_unref(group);
270         if (notification != NULL)
271                 g_free(notification);
272 }