* Implement remain dimming rules (toolbar and context sensitive menus)
[modest] / src / widgets / modest-window.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-window.h"
31 #include "modest-window-priv.h"
32 #include "modest-tny-platform-factory.h"
33
34 /* 'private'/'protected' functions */
35 static void modest_window_class_init (ModestWindowClass *klass);
36 static void modest_window_init       (ModestWindow *obj);
37 static void modest_window_finalize   (GObject *obj);
38
39 static void        modest_window_set_zoom_default       (ModestWindow *window,
40                                                          gdouble zoom);
41 static gdouble     modest_window_get_zoom_default       (ModestWindow *window);
42 static gboolean    modest_window_zoom_plus_default      (ModestWindow *window);
43 static gboolean    modest_window_zoom_minus_default     (ModestWindow *window);
44 static void        modest_window_show_toolbar_default   (ModestWindow *window,
45                                                          gboolean show_toolbar);
46
47 /* list my signals  */
48 enum {
49         LAST_SIGNAL
50 };
51
52 /* globals */
53 static GObjectClass *parent_class = NULL;
54
55 /* uncomment the following if you have defined any signals */
56 /* static guint signals[LAST_SIGNAL] = {0}; */
57
58 GType
59 modest_window_get_type (void)
60 {
61         static GType my_type = 0;
62         static GType parent_type = 0;
63         if (!my_type) {
64                 static const GTypeInfo my_info = {
65                         sizeof(ModestWindowClass),
66                         NULL,           /* base init */
67                         NULL,           /* base finalize */
68                         (GClassInitFunc) modest_window_class_init,
69                         NULL,           /* class finalize */
70                         NULL,           /* class data */
71                         sizeof(ModestWindow),
72                         1,              /* n_preallocs */
73                         (GInstanceInitFunc) modest_window_init,
74                         NULL
75                 };
76 #ifdef MODEST_PLATFORM_MAEMO
77                 parent_type = HILDON_TYPE_WINDOW;
78 #else
79                 parent_type = GTK_TYPE_WINDOW;
80 #endif 
81                 my_type = g_type_register_static (parent_type,
82                                                   "ModestWindow",
83                                                   &my_info, 
84                                                   G_TYPE_FLAG_ABSTRACT);
85         }
86         return my_type;
87 }
88
89 static void
90 modest_window_class_init (ModestWindowClass *klass)
91 {
92         GObjectClass *gobject_class;
93         gobject_class = (GObjectClass*) klass;
94
95         parent_class            = g_type_class_peek_parent (klass);
96         gobject_class->finalize = modest_window_finalize;
97
98         klass->set_zoom_func = modest_window_set_zoom_default;
99         klass->get_zoom_func = modest_window_get_zoom_default;
100         klass->zoom_plus_func = modest_window_zoom_plus_default;
101         klass->zoom_minus_func = modest_window_zoom_minus_default;
102         klass->show_toolbar_func = modest_window_show_toolbar_default;
103
104         g_type_class_add_private (gobject_class, sizeof(ModestWindowPrivate));
105 }
106
107 static void
108 modest_window_init (ModestWindow *obj)
109 {
110         ModestWindowPrivate *priv;
111
112         priv = MODEST_WINDOW_GET_PRIVATE(obj);
113
114         priv->ui_manager     = NULL;
115         priv->ui_dimming_manager     = NULL;
116         priv->toolbar        = NULL;
117         priv->menubar        = NULL;
118
119         priv->active_account = NULL;
120 }
121
122 static void
123 modest_window_finalize (GObject *obj)
124 {
125         ModestWindowPrivate *priv;      
126
127         priv = MODEST_WINDOW_GET_PRIVATE(obj);
128
129         if (priv->ui_manager) {
130                 g_object_unref (G_OBJECT(priv->ui_manager));
131                 priv->ui_manager = NULL;
132         }
133         if (priv->ui_dimming_manager) {
134                 g_object_unref (G_OBJECT(priv->ui_dimming_manager));
135                 priv->ui_dimming_manager = NULL;
136         }
137
138         g_free (priv->active_account);
139         
140         G_OBJECT_CLASS(parent_class)->finalize (obj);
141 }
142
143
144
145 const gchar*
146 modest_window_get_active_account (ModestWindow *self)
147 {
148         g_return_val_if_fail (self, NULL);
149
150         return MODEST_WINDOW_GET_PRIVATE(self)->active_account;
151 }
152
153 void
154 modest_window_set_active_account (ModestWindow *self, const gchar *active_account)
155 {
156         ModestWindowPrivate *priv;      
157
158         priv = MODEST_WINDOW_GET_PRIVATE(self);
159
160         if (active_account == priv->active_account)
161                 return;
162         else {
163                 g_free (priv->active_account);
164                 priv->active_account = NULL;
165                 if (active_account)
166                         priv->active_account = g_strdup (active_account);
167         }
168 }
169
170 void
171 modest_window_check_dimming_rules (ModestWindow *self)
172 {
173         ModestWindowPrivate *priv;      
174
175         g_return_if_fail (MODEST_IS_WINDOW (self));
176         priv = MODEST_WINDOW_GET_PRIVATE(self);
177
178         modest_ui_dimming_manager_process_dimming_rules (priv->ui_dimming_manager);
179 }
180
181 void
182 modest_window_check_dimming_rules_group (ModestWindow *self,
183                                          const gchar *group_name)
184 {
185         ModestWindowPrivate *priv;      
186
187         g_return_if_fail (MODEST_IS_WINDOW (self));
188         priv = MODEST_WINDOW_GET_PRIVATE(self);
189
190         modest_ui_dimming_manager_process_dimming_rules_group (priv->ui_dimming_manager, group_name);
191 }
192
193 GtkAction *
194 modest_window_get_action (ModestWindow *window, 
195                           const gchar *action_path) 
196 {
197         GtkAction *action = NULL;
198         ModestWindowPrivate *priv;      
199
200         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
201         priv = MODEST_WINDOW_GET_PRIVATE(window);
202
203         action = gtk_ui_manager_get_action (priv->ui_manager, action_path);     
204
205         return action;
206 }
207
208 GtkWidget *
209 modest_window_get_action_widget (ModestWindow *window, 
210                                  const gchar *action_path) 
211 {
212         GtkWidget *widget = NULL;
213         ModestWindowPrivate *priv;      
214
215         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
216         priv = MODEST_WINDOW_GET_PRIVATE(window);
217
218         widget = gtk_ui_manager_get_widget (priv->ui_manager, action_path);     
219
220         return widget;
221 }
222
223 void
224 modest_window_set_zoom (ModestWindow *window,
225                         gdouble zoom)
226 {
227         MODEST_WINDOW_GET_CLASS (window)->set_zoom_func (window, zoom);
228         return;
229 }
230
231 gdouble
232 modest_window_get_zoom (ModestWindow *window)
233 {
234         return MODEST_WINDOW_GET_CLASS (window)->get_zoom_func (window);
235 }
236
237 gboolean
238 modest_window_zoom_plus (ModestWindow *window)
239 {
240         return MODEST_WINDOW_GET_CLASS (window)->zoom_plus_func (window);
241 }
242
243 gboolean
244 modest_window_zoom_minus (ModestWindow *window)
245 {
246         return MODEST_WINDOW_GET_CLASS (window)->zoom_minus_func (window);
247 }
248
249 void 
250 modest_window_show_toolbar (ModestWindow *window,
251                             gboolean show_toolbar)
252 {
253         MODEST_WINDOW_GET_CLASS (window)->show_toolbar_func (window,
254                                                              show_toolbar);
255 }
256
257
258 /* Default implementations */
259
260 static void
261 modest_window_set_zoom_default (ModestWindow *window,
262                                 gdouble zoom)
263 {
264         g_warning ("modest: You should implement %s", __FUNCTION__);
265
266 }
267
268 static gdouble
269 modest_window_get_zoom_default (ModestWindow *window)
270 {
271         g_warning ("modest: You should implement %s", __FUNCTION__);
272         return 1.0;
273 }
274
275 static gboolean
276 modest_window_zoom_plus_default (ModestWindow *window)
277 {
278         g_warning ("modest: You should implement %s", __FUNCTION__);
279         return FALSE;
280 }
281
282 static gboolean
283 modest_window_zoom_minus_default (ModestWindow *window)
284 {
285         g_warning ("modest: You should implement %s", __FUNCTION__);
286         return FALSE;
287 }
288
289 static void 
290 modest_window_show_toolbar_default (ModestWindow *window,
291                                     gboolean show_toolbar)
292 {
293         g_warning ("modest: You should implement %s", __FUNCTION__);
294 }
295
296 void
297 modest_window_save_state (ModestWindow *window)
298 {
299         ModestWindowClass *klass = MODEST_WINDOW_GET_CLASS (window);
300         if (klass->save_state_func)
301                 klass->save_state_func (window);
302 }