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