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