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