2007-06-12 Murray Cumming <murrayc@murrayc.com>
[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         ModestWindow *win;
39         ModestDimmingCallback dimming_rule;
40         gchar *action_path;
41         gchar *notification;
42 };
43
44 #define MODEST_DIMMING_RULE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
45                                                    MODEST_TYPE_DIMMING_RULE, \
46                                                    ModestDimmingRulePrivate))
47
48 /* globals */
49 static GObjectClass *parent_class = NULL;
50
51 GType
52 modest_dimming_rule_get_type (void)
53 {
54         static GType my_type = 0;
55         if (!my_type) {
56                 static const GTypeInfo my_info = {
57                         sizeof(ModestDimmingRuleClass),
58                         NULL,           /* base init */
59                         NULL,           /* base finalize */
60                         (GClassInitFunc) modest_dimming_rule_class_init,
61                         NULL,           /* class finalize */
62                         NULL,           /* class data */
63                         sizeof(ModestDimmingRule),
64                         1,              /* n_preallocs */
65                         (GInstanceInitFunc) modest_dimming_rule_init,
66                         NULL
67                 };
68                 my_type = g_type_register_static (G_TYPE_OBJECT,
69                                                   "ModestDimmingRule",
70                                                   &my_info, 0);
71         }
72         return my_type;
73 }
74
75 static void
76 modest_dimming_rule_class_init (ModestDimmingRuleClass *klass)
77 {
78         GObjectClass *gobject_class;
79         gobject_class = (GObjectClass*) klass;
80
81         parent_class            = g_type_class_peek_parent (klass);
82         gobject_class->finalize = modest_dimming_rule_finalize;
83
84         g_type_class_add_private (gobject_class, sizeof(ModestDimmingRulePrivate));
85 }
86
87 static void
88 modest_dimming_rule_init (ModestDimmingRule *obj)
89 {
90         ModestDimmingRulePrivate *priv;
91
92         priv = MODEST_DIMMING_RULE_GET_PRIVATE(obj);
93         priv->win = NULL;
94         priv->dimming_rule = NULL;
95         priv->action_path = NULL;
96         priv->notification = NULL;
97 }
98
99 static void
100 modest_dimming_rule_finalize (GObject *obj)
101 {
102         ModestDimmingRulePrivate *priv;
103
104         priv = MODEST_DIMMING_RULE_GET_PRIVATE(obj);
105
106         if (priv->action_path != NULL)
107                 g_free(priv->action_path);
108         if (priv->notification != NULL)
109                 g_free(priv->notification);
110
111         G_OBJECT_CLASS(parent_class)->finalize (obj);
112 }
113
114
115 ModestDimmingRule*
116 modest_dimming_rule_new(ModestWindow *win,
117                         ModestDimmingCallback dimming_rule,
118                         const gchar *action_path)
119 {
120         ModestDimmingRule *obj;
121         ModestDimmingRulePrivate *priv;
122                 
123         g_return_val_if_fail (MODEST_IS_WINDOW (win), NULL);
124         g_return_val_if_fail (dimming_rule != NULL, NULL);
125         g_return_val_if_fail (action_path != NULL, NULL);
126                               
127         obj = MODEST_DIMMING_RULE(g_object_new(MODEST_TYPE_DIMMING_RULE, NULL));
128
129         priv = MODEST_DIMMING_RULE_GET_PRIVATE(obj);
130         priv->win = win;
131         priv->dimming_rule = dimming_rule;
132         priv->action_path = g_strdup(action_path);
133
134         return obj;
135 }
136
137
138 void
139 modest_dimming_rule_process (ModestDimmingRule *self)
140 {
141         
142         ModestDimmingRulePrivate *priv = NULL;
143         GtkAction *action = NULL;
144         gboolean dimmed = FALSE;
145
146         priv = MODEST_DIMMING_RULE_GET_PRIVATE(self);
147         g_return_if_fail (priv->win != NULL);
148         g_return_if_fail (priv->dimming_rule != NULL);
149         g_return_if_fail (priv->action_path != NULL);
150
151         /* process dimming rule */
152         dimmed = priv->dimming_rule (priv->win, self);
153
154         /* Update dimming status */
155         action = modest_window_get_action (priv->win, priv->action_path);       
156         g_return_if_fail (action != NULL);
157         gtk_action_set_sensitive (action, !dimmed);
158 }
159
160 void
161 modest_dimming_rule_set_notification (ModestDimmingRule *rule,
162                                       const gchar *notification)
163 {
164         ModestDimmingRulePrivate *priv = NULL;
165
166         g_return_if_fail (MODEST_IS_DIMMING_RULE (rule));
167         priv = MODEST_DIMMING_RULE_GET_PRIVATE(rule);
168         
169         /* Free previous notification */
170         if (priv->notification != NULL)
171                 g_free(priv->notification);
172
173         /* Set new notification message */
174         priv->notification = g_strdup(notification);
175 }
176
177 gchar *
178 modest_dimming_rule_get_notification (ModestDimmingRule *rule)
179 {
180         ModestDimmingRulePrivate *priv = NULL;
181
182         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (rule), NULL);
183         priv = MODEST_DIMMING_RULE_GET_PRIVATE(rule);
184         
185         return g_strdup(priv->notification);
186 }