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