884ccc76b3a4995b07d13c9602c5a9d5c625c7d9
[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 };
42
43 #define MODEST_DIMMING_RULE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
44                                                    MODEST_TYPE_DIMMING_RULE, \
45                                                    ModestDimmingRulePrivate))
46
47 /* globals */
48 static GObjectClass *parent_class = NULL;
49
50 GType
51 modest_dimming_rule_get_type (void)
52 {
53         static GType my_type = 0;
54         if (!my_type) {
55                 static const GTypeInfo my_info = {
56                         sizeof(ModestDimmingRuleClass),
57                         NULL,           /* base init */
58                         NULL,           /* base finalize */
59                         (GClassInitFunc) modest_dimming_rule_class_init,
60                         NULL,           /* class finalize */
61                         NULL,           /* class data */
62                         sizeof(ModestDimmingRule),
63                         1,              /* n_preallocs */
64                         (GInstanceInitFunc) modest_dimming_rule_init,
65                         NULL
66                 };
67                 my_type = g_type_register_static (G_TYPE_OBJECT,
68                                                   "ModestDimmingRule",
69                                                   &my_info, 0);
70         }
71         return my_type;
72 }
73
74 static void
75 modest_dimming_rule_class_init (ModestDimmingRuleClass *klass)
76 {
77         GObjectClass *gobject_class;
78         gobject_class = (GObjectClass*) klass;
79
80         parent_class            = g_type_class_peek_parent (klass);
81         gobject_class->finalize = modest_dimming_rule_finalize;
82
83         g_type_class_add_private (gobject_class, sizeof(ModestDimmingRulePrivate));
84 }
85
86 static void
87 modest_dimming_rule_init (ModestDimmingRule *obj)
88 {
89         ModestDimmingRulePrivate *priv;
90
91         priv = MODEST_DIMMING_RULE_GET_PRIVATE(obj);
92         priv->win = NULL;
93         priv->dimming_rule = NULL;
94         priv->action_path = NULL;
95 }
96
97 static void
98 modest_dimming_rule_finalize (GObject *obj)
99 {
100         ModestDimmingRulePrivate *priv;
101
102         priv = MODEST_DIMMING_RULE_GET_PRIVATE(obj);
103
104         if (priv->action_path != NULL)
105                 g_free(priv->action_path);
106
107         G_OBJECT_CLASS(parent_class)->finalize (obj);
108 }
109
110
111 ModestDimmingRule*
112 modest_dimming_rule_new(ModestWindow *win,
113                         ModestDimmingCallback dimming_rule,
114                         const gchar *action_path)
115 {
116         ModestDimmingRule *obj;
117         ModestDimmingRulePrivate *priv;
118                 
119         g_return_val_if_fail (MODEST_IS_WINDOW (win), NULL);
120         g_return_val_if_fail (dimming_rule != NULL, NULL);
121         g_return_val_if_fail (action_path != NULL, NULL);
122                               
123         obj = MODEST_DIMMING_RULE(g_object_new(MODEST_TYPE_DIMMING_RULE, NULL));
124
125         priv = MODEST_DIMMING_RULE_GET_PRIVATE(obj);
126         priv->win = win;
127         priv->dimming_rule = dimming_rule;
128         priv->action_path = g_strdup(action_path);
129
130         return obj;
131 }
132
133
134 void
135 modest_dimming_rule_process (ModestDimmingRule *self)
136 {
137         
138         ModestDimmingRulePrivate *priv = NULL;
139         GtkAction *action = NULL;
140         gboolean dimmed = FALSE;
141
142         priv = MODEST_DIMMING_RULE_GET_PRIVATE(self);
143         g_return_if_fail (priv->win != NULL);
144         g_return_if_fail (priv->dimming_rule != NULL);
145         g_return_if_fail (priv->action_path != NULL);
146
147         /* process dimming rule */
148         dimmed = priv->dimming_rule (priv->win, NULL);
149
150         /* Update dimming status */
151         action = modest_window_get_action (priv->win, priv->action_path);       
152         g_return_if_fail (action != NULL);
153         gtk_action_set_sensitive (action, !dimmed);
154 }