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