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