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