2007-07-12 Murray Cumming <murrayc@murrayc.com>
[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
37 /* 'private'/'protected' functions */
38 static void modest_window_class_init (ModestWindowClass *klass);
39 static void modest_window_init       (ModestWindow *obj);
40 static void modest_window_finalize   (GObject *obj);
41
42 static void        modest_window_set_zoom_default       (ModestWindow *window,
43                                                          gdouble zoom);
44 static gdouble     modest_window_get_zoom_default       (ModestWindow *window);
45 static gboolean    modest_window_zoom_plus_default      (ModestWindow *window);
46 static gboolean    modest_window_zoom_minus_default     (ModestWindow *window);
47 static void        modest_window_show_toolbar_default   (ModestWindow *window,
48                                                          gboolean show_toolbar);
49
50 static gboolean    on_key_pressed (GtkWidget *self, GdkEventKey *event, gpointer user_data);
51
52
53 /* list my signals  */
54 enum {
55         LAST_SIGNAL
56 };
57
58 /* globals */
59 static GObjectClass *parent_class = NULL;
60
61 /* uncomment the following if you have defined any signals */
62 /* static guint signals[LAST_SIGNAL] = {0}; */
63
64 GType
65 modest_window_get_type (void)
66 {
67         static GType my_type = 0;
68         static GType parent_type = 0;
69         if (!my_type) {
70                 static const GTypeInfo my_info = {
71                         sizeof(ModestWindowClass),
72                         NULL,           /* base init */
73                         NULL,           /* base finalize */
74                         (GClassInitFunc) modest_window_class_init,
75                         NULL,           /* class finalize */
76                         NULL,           /* class data */
77                         sizeof(ModestWindow),
78                         1,              /* n_preallocs */
79                         (GInstanceInitFunc) modest_window_init,
80                         NULL
81                 };
82 #ifdef MODEST_PLATFORM_MAEMO
83                 parent_type = HILDON_TYPE_WINDOW;
84 #else
85                 parent_type = GTK_TYPE_WINDOW;
86 #endif 
87                 my_type = g_type_register_static (parent_type,
88                                                   "ModestWindow",
89                                                   &my_info, 
90                                                   G_TYPE_FLAG_ABSTRACT);
91         }
92         return my_type;
93 }
94
95 static void
96 modest_window_class_init (ModestWindowClass *klass)
97 {
98         GObjectClass *gobject_class;
99         gobject_class = (GObjectClass*) klass;
100
101         parent_class            = g_type_class_peek_parent (klass);
102         gobject_class->finalize = modest_window_finalize;
103
104         klass->set_zoom_func = modest_window_set_zoom_default;
105         klass->get_zoom_func = modest_window_get_zoom_default;
106         klass->zoom_plus_func = modest_window_zoom_plus_default;
107         klass->zoom_minus_func = modest_window_zoom_minus_default;
108         klass->show_toolbar_func = modest_window_show_toolbar_default;
109
110         g_type_class_add_private (gobject_class, sizeof(ModestWindowPrivate));
111 }
112
113 static void
114 modest_window_init (ModestWindow *obj)
115 {
116         ModestWindowPrivate *priv;
117
118         priv = MODEST_WINDOW_GET_PRIVATE(obj);
119
120         priv->ui_manager     = NULL;
121         priv->ui_dimming_manager     = NULL;
122         priv->toolbar        = NULL;
123         priv->menubar        = NULL;
124
125         priv->active_account = NULL;
126
127         /* Connect signals */
128         g_signal_connect (G_OBJECT (obj), 
129                           "key-press-event", 
130                           G_CALLBACK (on_key_pressed), NULL);
131 }
132
133 static void
134 modest_window_finalize (GObject *obj)
135 {
136         ModestWindowPrivate *priv;      
137
138         priv = MODEST_WINDOW_GET_PRIVATE(obj);
139
140         if (priv->ui_manager) {
141                 g_object_unref (G_OBJECT(priv->ui_manager));
142                 priv->ui_manager = NULL;
143         }
144         if (priv->ui_dimming_manager) {
145                 g_object_unref (G_OBJECT(priv->ui_dimming_manager));
146                 priv->ui_dimming_manager = NULL;
147         }
148
149         g_free (priv->active_account);
150         
151         G_OBJECT_CLASS(parent_class)->finalize (obj);
152 }
153
154
155
156 const gchar*
157 modest_window_get_active_account (ModestWindow *self)
158 {
159         g_return_val_if_fail (self, NULL);
160
161         return MODEST_WINDOW_GET_PRIVATE(self)->active_account;
162 }
163
164 void
165 modest_window_set_active_account (ModestWindow *self, const gchar *active_account)
166 {
167         printf ("DEBUG: %s: active_account=%s\n", __FUNCTION__, active_account);
168
169         ModestWindowPrivate *priv;      
170
171         priv = MODEST_WINDOW_GET_PRIVATE(self);
172
173         if (active_account == priv->active_account)
174                 return;
175         else {
176                 g_free (priv->active_account);
177                 priv->active_account = NULL;
178                 if (active_account)
179                         priv->active_account = g_strdup (active_account);
180         }
181 }
182
183 void
184 modest_window_check_dimming_rules (ModestWindow *self)
185 {
186         ModestWindowPrivate *priv;      
187
188         g_return_if_fail (MODEST_IS_WINDOW (self));
189         priv = MODEST_WINDOW_GET_PRIVATE(self);
190
191         modest_ui_dimming_manager_process_dimming_rules (priv->ui_dimming_manager);
192 }
193
194 void
195 modest_window_check_dimming_rules_group (ModestWindow *self,
196                                          const gchar *group_name)
197 {
198         ModestWindowPrivate *priv;      
199
200         g_return_if_fail (MODEST_IS_WINDOW (self));
201         priv = MODEST_WINDOW_GET_PRIVATE(self);
202
203         modest_ui_dimming_manager_process_dimming_rules_group (priv->ui_dimming_manager, group_name);
204 }
205
206 GtkAction *
207 modest_window_get_action (ModestWindow *window, 
208                           const gchar *action_path) 
209 {
210         GtkAction *action = NULL;
211         ModestWindowPrivate *priv;      
212
213         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
214         priv = MODEST_WINDOW_GET_PRIVATE(window);
215
216         action = gtk_ui_manager_get_action (priv->ui_manager, action_path);     
217
218         return action;
219 }
220
221 GtkWidget *
222 modest_window_get_action_widget (ModestWindow *window, 
223                                  const gchar *action_path) 
224 {
225         GtkWidget *widget = NULL;
226         ModestWindowPrivate *priv;      
227
228         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
229         priv = MODEST_WINDOW_GET_PRIVATE(window);
230
231         widget = gtk_ui_manager_get_widget (priv->ui_manager, action_path);     
232
233         return widget;
234 }
235
236 void
237 modest_window_set_zoom (ModestWindow *window,
238                         gdouble zoom)
239 {
240         MODEST_WINDOW_GET_CLASS (window)->set_zoom_func (window, zoom);
241         return;
242 }
243
244 gdouble
245 modest_window_get_zoom (ModestWindow *window)
246 {
247         return MODEST_WINDOW_GET_CLASS (window)->get_zoom_func (window);
248 }
249
250 gboolean
251 modest_window_zoom_plus (ModestWindow *window)
252 {
253         return MODEST_WINDOW_GET_CLASS (window)->zoom_plus_func (window);
254 }
255
256 gboolean
257 modest_window_zoom_minus (ModestWindow *window)
258 {
259         return MODEST_WINDOW_GET_CLASS (window)->zoom_minus_func (window);
260 }
261
262 void 
263 modest_window_show_toolbar (ModestWindow *window,
264                             gboolean show_toolbar)
265 {
266         MODEST_WINDOW_GET_CLASS (window)->show_toolbar_func (window,
267                                                              show_toolbar);
268 }
269
270
271 /* Default implementations */
272
273 static void
274 modest_window_set_zoom_default (ModestWindow *window,
275                                 gdouble zoom)
276 {
277         g_warning ("modest: You should implement %s", __FUNCTION__);
278
279 }
280
281 static gdouble
282 modest_window_get_zoom_default (ModestWindow *window)
283 {
284         g_warning ("modest: You should implement %s", __FUNCTION__);
285         return 1.0;
286 }
287
288 static gboolean
289 modest_window_zoom_plus_default (ModestWindow *window)
290 {
291         g_warning ("modest: You should implement %s", __FUNCTION__);
292         return FALSE;
293 }
294
295 static gboolean
296 modest_window_zoom_minus_default (ModestWindow *window)
297 {
298         g_warning ("modest: You should implement %s", __FUNCTION__);
299         return FALSE;
300 }
301
302 static void 
303 modest_window_show_toolbar_default (ModestWindow *window,
304                                     gboolean show_toolbar)
305 {
306         g_warning ("modest: You should implement %s", __FUNCTION__);
307 }
308
309
310
311 void
312 modest_window_save_state (ModestWindow *window)
313 {
314         ModestWindowClass *klass = MODEST_WINDOW_GET_CLASS (window);
315         if (klass->save_state_func)
316                 klass->save_state_func (window);
317 }
318
319 static gboolean
320 on_key_pressed (GtkWidget *self,
321                 GdkEventKey *event,
322                 gpointer user_data)
323 {
324         ModestWindowMgr *mgr = NULL;
325
326         mgr = modest_runtime_get_window_mgr ();
327
328         switch (event->keyval) {
329         case GDK_F6: 
330                 modest_ui_actions_on_change_fullscreen (NULL, MODEST_WINDOW(self));
331                 return TRUE;
332         case GDK_F7: 
333                 modest_ui_actions_on_zoom_plus (NULL, MODEST_WINDOW(self));
334                 return TRUE;
335         case GDK_F8: 
336                 modest_ui_actions_on_zoom_minus (NULL, MODEST_WINDOW(self));
337                 return TRUE;
338         case GDK_Escape: 
339                 if (modest_window_mgr_get_fullscreen_mode (mgr))
340                         modest_ui_actions_on_change_fullscreen (NULL, MODEST_WINDOW(self));
341                 else if (MODEST_IS_MSG_VIEW_WINDOW (self)||MODEST_IS_MSG_EDIT_WINDOW (self))
342                         modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (self));
343                 break;
344         }
345         
346         return FALSE;
347 }