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