Modified webpage: now tinymail repository is in gitorious.
[modest] / src / modest-dimming-rule.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-rule.h"
31 #include "modest-ui-dimming-manager.h"
32
33 static void modest_dimming_rule_class_init (ModestDimmingRuleClass *klass);
34 static void modest_dimming_rule_init       (ModestDimmingRule *obj);
35 static void modest_dimming_rule_finalize   (GObject *obj);
36 static void modest_dimming_rule_dispose    (GObject *obj);
37
38 typedef struct _ModestDimmingRulePrivate ModestDimmingRulePrivate;
39 struct _ModestDimmingRulePrivate {
40         ModestDimmingRulesGroup *group;
41         ModestWindow *win;
42         ModestDimmingCallback dimming_rule;
43         gchar *action_path;
44         GtkWidget *widget;
45         gchar *notification;
46 };
47
48 #define MODEST_DIMMING_RULE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
49                                                    MODEST_TYPE_DIMMING_RULE, \
50                                                    ModestDimmingRulePrivate))
51
52 /* globals */
53 static GObjectClass *parent_class = NULL;
54
55 GType
56 modest_dimming_rule_get_type (void)
57 {
58         static GType my_type = 0;
59         if (!my_type) {
60                 static const GTypeInfo my_info = {
61                         sizeof(ModestDimmingRuleClass),
62                         NULL,           /* base init */
63                         NULL,           /* base finalize */
64                         (GClassInitFunc) modest_dimming_rule_class_init,
65                         NULL,           /* class finalize */
66                         NULL,           /* class data */
67                         sizeof(ModestDimmingRule),
68                         1,              /* n_preallocs */
69                         (GInstanceInitFunc) modest_dimming_rule_init,
70                         NULL
71                 };
72                 my_type = g_type_register_static (G_TYPE_OBJECT,
73                                                   "ModestDimmingRule",
74                                                   &my_info, 0);
75         }
76         return my_type;
77 }
78
79 static void
80 modest_dimming_rule_class_init (ModestDimmingRuleClass *klass)
81 {
82         GObjectClass *gobject_class;
83         gobject_class = (GObjectClass*) klass;
84
85         parent_class            = g_type_class_peek_parent (klass);
86         gobject_class->finalize = modest_dimming_rule_finalize;
87         gobject_class->dispose  = modest_dimming_rule_dispose;
88
89         g_type_class_add_private (gobject_class, sizeof(ModestDimmingRulePrivate));
90 }
91
92 static void
93 modest_dimming_rule_init (ModestDimmingRule *obj)
94 {
95         ModestDimmingRulePrivate *priv;
96
97         priv = MODEST_DIMMING_RULE_GET_PRIVATE(obj);
98         priv->group= NULL;
99         priv->win = NULL;
100         priv->dimming_rule = NULL;
101         priv->action_path = NULL;
102         priv->widget = NULL;
103         priv->notification = NULL;
104 }
105
106 static void
107 modest_dimming_rule_finalize (GObject *obj)
108 {
109         ModestDimmingRulePrivate *priv;
110
111         priv = MODEST_DIMMING_RULE_GET_PRIVATE(obj);
112
113         if (priv->group != NULL)
114                 g_object_unref(priv->group);
115         if (priv->action_path != NULL)
116                 g_free(priv->action_path);
117         if (priv->notification != NULL)
118                 g_free(priv->notification);
119
120         G_OBJECT_CLASS(parent_class)->finalize (obj);
121 }
122
123 static void
124 modest_dimming_rule_dispose (GObject *obj)
125 {
126         ModestDimmingRulePrivate *priv;
127
128         priv = MODEST_DIMMING_RULE_GET_PRIVATE(obj);
129
130         if (priv->group != NULL) {
131                 g_object_unref (priv->group);
132                 priv->group = NULL;
133         }
134
135         G_OBJECT_CLASS(parent_class)->dispose (obj);
136 }
137
138
139 ModestDimmingRule*
140 modest_dimming_rule_new(ModestWindow *win,
141                         ModestDimmingCallback dimming_rule,
142                         const gchar *action_path)
143 {
144         ModestDimmingRule *obj;
145         ModestDimmingRulePrivate *priv;
146                 
147         g_return_val_if_fail (MODEST_IS_WINDOW (win), NULL);
148         g_return_val_if_fail (dimming_rule != NULL, NULL);
149         g_return_val_if_fail (action_path != NULL, NULL);
150                               
151         obj = MODEST_DIMMING_RULE(g_object_new(MODEST_TYPE_DIMMING_RULE, NULL));
152
153         priv = MODEST_DIMMING_RULE_GET_PRIVATE(obj);
154         priv->win = win;
155         priv->dimming_rule = dimming_rule;
156         priv->action_path = g_strdup(action_path);
157
158         return obj;
159 }
160
161 ModestDimmingRule*
162 modest_dimming_rule_new_from_widget (ModestWindow *win,
163                                      ModestDimmingCallback dimming_rule,
164                                      GtkWidget *widget)
165 {
166         ModestDimmingRule *obj;
167         ModestDimmingRulePrivate *priv;
168                 
169         g_return_val_if_fail (MODEST_IS_WINDOW (win), NULL);
170         g_return_val_if_fail (dimming_rule != NULL, NULL);
171         g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
172                               
173         obj = MODEST_DIMMING_RULE(g_object_new(MODEST_TYPE_DIMMING_RULE, NULL));
174
175         priv = MODEST_DIMMING_RULE_GET_PRIVATE(obj);
176         priv->win = win;
177         priv->dimming_rule = dimming_rule;
178         priv->widget = widget;
179
180         return obj;
181 }
182
183
184 void
185 modest_dimming_rule_process (ModestDimmingRule *self)
186 {
187         
188         ModestDimmingRulePrivate *priv = NULL;
189         GtkAction *action = NULL;
190         gboolean dimmed = FALSE;
191
192         priv = MODEST_DIMMING_RULE_GET_PRIVATE(self);
193         g_return_if_fail (priv->win != NULL);
194         g_return_if_fail (priv->dimming_rule != NULL);
195         g_return_if_fail ((priv->action_path != NULL)|| GTK_IS_WIDGET (priv->widget));
196
197         /* process dimming rule */
198         dimmed = priv->dimming_rule (priv->win, self);
199
200         /* Update dimming status */
201         if (priv->action_path != NULL) {
202                 action = modest_window_get_action (priv->win, priv->action_path);       
203                 if (action != NULL)
204                         gtk_action_set_sensitive (action, !dimmed);
205                 else
206                         g_printerr ("modest: action path '%s' has not associatd action\n", priv->action_path);
207         } else if (priv->widget != NULL) {
208                 ModestUIDimmingMode mode;
209
210                 mode = modest_ui_dimming_manager_get_widget_dimming_mode (priv->widget);
211                 switch (mode) {
212                 case MODEST_UI_DIMMING_MODE_HIDE:
213                         if (dimmed) {
214                                 gtk_widget_hide (GTK_WIDGET (priv->widget));
215                         } else {
216                                 gtk_widget_show (GTK_WIDGET (priv->widget));
217                         }
218                         break;
219                 case MODEST_UI_DIMMING_MODE_DIM:
220                 default:
221                         gtk_widget_set_sensitive (priv->widget, !dimmed);
222                         break;
223                 }
224         }
225 }
226
227 void
228 modest_dimming_rule_set_group (ModestDimmingRule *rule,
229                                ModestDimmingRulesGroup *group)
230 {
231         ModestDimmingRulePrivate *priv = NULL;
232
233         g_return_if_fail (MODEST_IS_DIMMING_RULE (rule));
234         g_return_if_fail (MODEST_IS_DIMMING_RULES_GROUP (group));
235         priv = MODEST_DIMMING_RULE_GET_PRIVATE(rule);
236
237         if (priv->group == group) 
238                 return;
239         if (priv->group != NULL) 
240                 g_object_unref (priv->group);
241         priv->group = g_object_ref (group);                     
242 }       
243
244 ModestDimmingRulesGroup *
245 modest_dimming_rule_get_group (ModestDimmingRule *rule)
246 {
247         ModestDimmingRulePrivate *priv = NULL;
248         
249         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (rule), NULL);
250         priv = MODEST_DIMMING_RULE_GET_PRIVATE(rule);
251         g_return_val_if_fail (MODEST_IS_DIMMING_RULES_GROUP (priv->group), NULL);
252         
253         return g_object_ref(priv->group);
254 }
255
256 void
257 modest_dimming_rule_set_notification (ModestDimmingRule *rule,
258                                       const gchar *notification)
259 {
260         ModestDimmingRulePrivate *priv = NULL;
261
262         g_return_if_fail (MODEST_IS_DIMMING_RULE (rule));
263         priv = MODEST_DIMMING_RULE_GET_PRIVATE(rule);
264         
265         /* Free previous notification */
266         if (priv->notification != NULL) {
267                 g_free(priv->notification);
268                 priv->notification = NULL;
269         }
270
271         /* Set new notification message */
272         if (notification != NULL)
273                 priv->notification = g_strdup(notification);
274 }
275
276 gchar *
277 modest_dimming_rule_get_notification (ModestDimmingRule *rule)
278 {
279         ModestDimmingRulePrivate *priv = NULL;
280
281         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (rule), NULL);
282         priv = MODEST_DIMMING_RULE_GET_PRIVATE(rule);
283         
284         return g_strdup(priv->notification);
285 }
286
287 GtkWidget *
288 modest_dimming_rule_get_widget (ModestDimmingRule *rule)
289 {
290         ModestDimmingRulePrivate *priv = NULL;
291
292         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (rule), NULL);
293         priv = MODEST_DIMMING_RULE_GET_PRIVATE(rule);
294
295         if (priv->action_path != NULL)
296                 return modest_window_get_action_widget (MODEST_WINDOW (priv->win), priv->action_path);
297         else
298                 return priv->widget;
299 }
300
301 const gchar *
302 modest_dimming_rule_get_action_path (ModestDimmingRule *rule)
303 {
304         ModestDimmingRulePrivate *priv = NULL;
305
306         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (rule), NULL);
307         priv = MODEST_DIMMING_RULE_GET_PRIVATE(rule);
308
309         return priv->action_path;
310 }