* Implement optimizations on DimmingRules management.
[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-ui-actions.h"
33 #include "modest-tny-platform-factory.h"
34 #include "modest-runtime.h"
35 #include "modest-window-mgr.h"
36
37 /* 'private'/'protected' functions */
38 static void modest_window_class_init (ModestWindowClass *klass);
39 static void modest_window_init       (ModestWindow *obj);
40 static void modest_window_finalize   (GObject *obj);
41
42 static gdouble  modest_window_get_zoom_default           (ModestWindow *window);
43
44 static gboolean modest_window_zoom_plus_default          (ModestWindow *window);
45
46 static gboolean modest_window_zoom_minus_default         (ModestWindow *window);
47
48 static void     modest_window_disconnect_signals_default (ModestWindow *self);
49
50 static void     modest_window_show_toolbar_default       (ModestWindow *window,
51                                                           gboolean show_toolbar);
52
53 static void     modest_window_set_zoom_default           (ModestWindow *window,
54                                                           gdouble zoom);
55
56 static gboolean on_key_pressed (GtkWidget *self, GdkEventKey *event, gpointer user_data);
57
58
59 /* list my signals  */
60 enum {
61         LAST_SIGNAL
62 };
63
64 /* globals */
65 static GObjectClass *parent_class = NULL;
66
67 /* uncomment the following if you have defined any signals */
68 /* static guint signals[LAST_SIGNAL] = {0}; */
69
70 GType
71 modest_window_get_type (void)
72 {
73         static GType my_type = 0;
74         static GType parent_type = 0;
75         if (!my_type) {
76                 static const GTypeInfo my_info = {
77                         sizeof(ModestWindowClass),
78                         NULL,           /* base init */
79                         NULL,           /* base finalize */
80                         (GClassInitFunc) modest_window_class_init,
81                         NULL,           /* class finalize */
82                         NULL,           /* class data */
83                         sizeof(ModestWindow),
84                         1,              /* n_preallocs */
85                         (GInstanceInitFunc) modest_window_init,
86                         NULL
87                 };
88 #ifdef MODEST_PLATFORM_MAEMO
89                 parent_type = HILDON_TYPE_WINDOW;
90 #else
91                 parent_type = GTK_TYPE_WINDOW;
92 #endif 
93                 my_type = g_type_register_static (parent_type,
94                                                   "ModestWindow",
95                                                   &my_info, 
96                                                   G_TYPE_FLAG_ABSTRACT);
97         }
98         return my_type;
99 }
100
101 static void
102 modest_window_class_init (ModestWindowClass *klass)
103 {
104         GObjectClass *gobject_class;
105         gobject_class = (GObjectClass*) klass;
106
107         parent_class            = g_type_class_peek_parent (klass);
108         gobject_class->finalize = modest_window_finalize;
109
110         klass->set_zoom_func = modest_window_set_zoom_default;
111         klass->get_zoom_func = modest_window_get_zoom_default;
112         klass->zoom_plus_func = modest_window_zoom_plus_default;
113         klass->zoom_minus_func = modest_window_zoom_minus_default;
114         klass->show_toolbar_func = modest_window_show_toolbar_default;
115         klass->disconnect_signals_func = modest_window_disconnect_signals_default;
116
117         g_type_class_add_private (gobject_class, sizeof(ModestWindowPrivate));
118 }
119
120 static void
121 modest_window_init (ModestWindow *obj)
122 {
123         ModestWindowPrivate *priv;
124
125         priv = MODEST_WINDOW_GET_PRIVATE(obj);
126
127         priv->ui_manager     = NULL;
128         priv->ui_dimming_manager     = NULL;
129         priv->toolbar        = NULL;
130         priv->menubar        = NULL;
131
132         priv->active_account = NULL;
133
134         /* Connect signals */
135         g_signal_connect (G_OBJECT (obj), 
136                           "key-press-event", 
137                           G_CALLBACK (on_key_pressed), NULL);
138 }
139
140 static void
141 modest_window_finalize (GObject *obj)
142 {
143         ModestWindowPrivate *priv;      
144
145         priv = MODEST_WINDOW_GET_PRIVATE(obj);
146
147         if (priv->ui_manager) {
148                 g_object_unref (G_OBJECT(priv->ui_manager));
149                 priv->ui_manager = NULL;
150         }
151         if (priv->ui_dimming_manager) {
152                 g_object_unref (G_OBJECT(priv->ui_dimming_manager));
153                 priv->ui_dimming_manager = NULL;
154         }
155
156         g_free (priv->active_account);
157         
158         G_OBJECT_CLASS(parent_class)->finalize (obj);
159 }
160
161
162
163 const gchar*
164 modest_window_get_active_account (ModestWindow *self)
165 {
166         g_return_val_if_fail (self, NULL);
167
168         return MODEST_WINDOW_GET_PRIVATE(self)->active_account;
169 }
170
171 void
172 modest_window_set_active_account (ModestWindow *self, const gchar *active_account)
173 {
174         ModestWindowPrivate *priv;      
175
176         priv = MODEST_WINDOW_GET_PRIVATE(self);
177
178         if (active_account == priv->active_account)
179                 return;
180         else {
181                 g_free (priv->active_account);
182                 priv->active_account = NULL;
183                 if (active_account)
184                         priv->active_account = g_strdup (active_account);
185         }
186 }
187
188 void
189 modest_window_check_dimming_rules (ModestWindow *self)
190 {
191         ModestWindowPrivate *priv;      
192
193         g_return_if_fail (MODEST_IS_WINDOW (self));
194         priv = MODEST_WINDOW_GET_PRIVATE(self);
195
196         modest_ui_dimming_manager_process_dimming_rules (priv->ui_dimming_manager);
197 }
198
199 void
200 modest_window_check_dimming_rules_group (ModestWindow *self,
201                                          const gchar *group_name)
202 {
203         ModestWindowPrivate *priv;      
204
205         g_return_if_fail (MODEST_IS_WINDOW (self));
206         priv = MODEST_WINDOW_GET_PRIVATE(self);
207
208         modest_ui_dimming_manager_process_dimming_rules_group (priv->ui_dimming_manager, group_name);
209 }
210
211 void
212 modest_window_set_dimming_state (ModestWindow *window,
213                                  DimmedState *state)
214 {
215         ModestWindowPrivate *priv;      
216
217         g_return_if_fail (MODEST_IS_WINDOW (window));
218         priv = MODEST_WINDOW_GET_PRIVATE(window);
219
220         /* Free previous */
221         if (priv->dimming_state != NULL)
222                 g_slice_free (DimmedState, priv->dimming_state);
223
224         /* Set new state */
225         priv->dimming_state = state;
226 }
227
228 const DimmedState *
229 modest_window_get_dimming_state (ModestWindow *window)
230 {
231         ModestWindowPrivate *priv;      
232
233         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
234         priv = MODEST_WINDOW_GET_PRIVATE(window);
235
236         return priv->dimming_state;
237 }
238
239 GtkAction *
240 modest_window_get_action (ModestWindow *window, 
241                           const gchar *action_path) 
242 {
243         GtkAction *action = NULL;
244         ModestWindowPrivate *priv;      
245
246         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
247         priv = MODEST_WINDOW_GET_PRIVATE(window);
248
249         action = gtk_ui_manager_get_action (priv->ui_manager, action_path);     
250
251         return action;
252 }
253
254 GtkWidget *
255 modest_window_get_action_widget (ModestWindow *window, 
256                                  const gchar *action_path) 
257 {
258         GtkWidget *widget = NULL;
259         ModestWindowPrivate *priv;      
260
261         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
262         priv = MODEST_WINDOW_GET_PRIVATE(window);
263
264         widget = gtk_ui_manager_get_widget (priv->ui_manager, action_path);     
265
266         return widget;
267 }
268
269 void
270 modest_window_set_zoom (ModestWindow *window,
271                         gdouble zoom)
272 {
273         MODEST_WINDOW_GET_CLASS (window)->set_zoom_func (window, zoom);
274         return;
275 }
276
277 gdouble
278 modest_window_get_zoom (ModestWindow *window)
279 {
280         return MODEST_WINDOW_GET_CLASS (window)->get_zoom_func (window);
281 }
282
283 gboolean
284 modest_window_zoom_plus (ModestWindow *window)
285 {
286         return MODEST_WINDOW_GET_CLASS (window)->zoom_plus_func (window);
287 }
288
289 gboolean
290 modest_window_zoom_minus (ModestWindow *window)
291 {
292         return MODEST_WINDOW_GET_CLASS (window)->zoom_minus_func (window);
293 }
294
295 void 
296 modest_window_show_toolbar (ModestWindow *window,
297                             gboolean show_toolbar)
298 {
299         MODEST_WINDOW_GET_CLASS (window)->show_toolbar_func (window,
300                                                              show_toolbar);
301 }
302
303 void 
304 modest_window_disconnect_signals (ModestWindow *window)
305 {
306         MODEST_WINDOW_GET_CLASS (window)->disconnect_signals_func (window);
307 }
308
309
310 /* Default implementations */
311
312 static void
313 modest_window_set_zoom_default (ModestWindow *window,
314                                 gdouble zoom)
315 {
316         g_warning ("modest: You should implement %s", __FUNCTION__);
317
318 }
319
320 static gdouble
321 modest_window_get_zoom_default (ModestWindow *window)
322 {
323         g_warning ("modest: You should implement %s", __FUNCTION__);
324         return 1.0;
325 }
326
327 static gboolean
328 modest_window_zoom_plus_default (ModestWindow *window)
329 {
330         g_warning ("modest: You should implement %s", __FUNCTION__);
331         return FALSE;
332 }
333
334 static gboolean
335 modest_window_zoom_minus_default (ModestWindow *window)
336 {
337         g_warning ("modest: You should implement %s", __FUNCTION__);
338         return FALSE;
339 }
340
341 static void 
342 modest_window_show_toolbar_default (ModestWindow *window,
343                                     gboolean show_toolbar)
344 {
345         g_warning ("modest: You should implement %s", __FUNCTION__);
346 }
347
348 static void 
349 modest_window_disconnect_signals_default (ModestWindow *self)
350 {
351         g_warning ("modest: You should implement %s", __FUNCTION__);
352 }
353
354 void
355 modest_window_save_state (ModestWindow *window)
356 {
357         ModestWindowClass *klass = MODEST_WINDOW_GET_CLASS (window);
358         if (klass->save_state_func)
359                 klass->save_state_func (window);
360 }
361
362 static gboolean
363 on_key_pressed (GtkWidget *self,
364                 GdkEventKey *event,
365                 gpointer user_data)
366 {
367         ModestWindowMgr *mgr = NULL;
368
369         mgr = modest_runtime_get_window_mgr ();
370
371         switch (event->keyval) {
372         case GDK_F6: 
373                 modest_ui_actions_on_change_fullscreen (NULL, MODEST_WINDOW(self));
374                 return TRUE;
375         case GDK_F7: 
376                 modest_ui_actions_on_zoom_plus (NULL, MODEST_WINDOW(self));
377                 return TRUE;
378         case GDK_F8: 
379                 modest_ui_actions_on_zoom_minus (NULL, MODEST_WINDOW(self));
380                 return TRUE;
381         case GDK_Escape: 
382                 if (modest_window_mgr_get_fullscreen_mode (mgr))
383                         modest_ui_actions_on_change_fullscreen (NULL, MODEST_WINDOW(self));
384                 else if (MODEST_IS_MSG_VIEW_WINDOW (self)||MODEST_IS_MSG_EDIT_WINDOW (self))
385                         modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (self));
386                 break;
387         }
388         
389         return FALSE;
390 }