* src/maemo/modest-msg-edit-window.c:
[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         ModestWindowPrivate *priv;      
168
169         priv = MODEST_WINDOW_GET_PRIVATE(self);
170
171         if (active_account == priv->active_account)
172                 return;
173         else {
174                 g_free (priv->active_account);
175                 priv->active_account = NULL;
176                 if (active_account)
177                         priv->active_account = g_strdup (active_account);
178         }
179 }
180
181 void
182 modest_window_check_dimming_rules (ModestWindow *self)
183 {
184         ModestWindowPrivate *priv;      
185
186         g_return_if_fail (MODEST_IS_WINDOW (self));
187         priv = MODEST_WINDOW_GET_PRIVATE(self);
188
189         modest_ui_dimming_manager_process_dimming_rules (priv->ui_dimming_manager);
190 }
191
192 void
193 modest_window_check_dimming_rules_group (ModestWindow *self,
194                                          const gchar *group_name)
195 {
196         ModestWindowPrivate *priv;      
197
198         g_return_if_fail (MODEST_IS_WINDOW (self));
199         priv = MODEST_WINDOW_GET_PRIVATE(self);
200
201         modest_ui_dimming_manager_process_dimming_rules_group (priv->ui_dimming_manager, group_name);
202 }
203
204 GtkAction *
205 modest_window_get_action (ModestWindow *window, 
206                           const gchar *action_path) 
207 {
208         GtkAction *action = NULL;
209         ModestWindowPrivate *priv;      
210
211         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
212         priv = MODEST_WINDOW_GET_PRIVATE(window);
213
214         action = gtk_ui_manager_get_action (priv->ui_manager, action_path);     
215
216         return action;
217 }
218
219 GtkWidget *
220 modest_window_get_action_widget (ModestWindow *window, 
221                                  const gchar *action_path) 
222 {
223         GtkWidget *widget = NULL;
224         ModestWindowPrivate *priv;      
225
226         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
227         priv = MODEST_WINDOW_GET_PRIVATE(window);
228
229         widget = gtk_ui_manager_get_widget (priv->ui_manager, action_path);     
230
231         return widget;
232 }
233
234 void
235 modest_window_set_zoom (ModestWindow *window,
236                         gdouble zoom)
237 {
238         MODEST_WINDOW_GET_CLASS (window)->set_zoom_func (window, zoom);
239         return;
240 }
241
242 gdouble
243 modest_window_get_zoom (ModestWindow *window)
244 {
245         return MODEST_WINDOW_GET_CLASS (window)->get_zoom_func (window);
246 }
247
248 gboolean
249 modest_window_zoom_plus (ModestWindow *window)
250 {
251         return MODEST_WINDOW_GET_CLASS (window)->zoom_plus_func (window);
252 }
253
254 gboolean
255 modest_window_zoom_minus (ModestWindow *window)
256 {
257         return MODEST_WINDOW_GET_CLASS (window)->zoom_minus_func (window);
258 }
259
260 void 
261 modest_window_show_toolbar (ModestWindow *window,
262                             gboolean show_toolbar)
263 {
264         MODEST_WINDOW_GET_CLASS (window)->show_toolbar_func (window,
265                                                              show_toolbar);
266 }
267
268
269 /* Default implementations */
270
271 static void
272 modest_window_set_zoom_default (ModestWindow *window,
273                                 gdouble zoom)
274 {
275         g_warning ("modest: You should implement %s", __FUNCTION__);
276
277 }
278
279 static gdouble
280 modest_window_get_zoom_default (ModestWindow *window)
281 {
282         g_warning ("modest: You should implement %s", __FUNCTION__);
283         return 1.0;
284 }
285
286 static gboolean
287 modest_window_zoom_plus_default (ModestWindow *window)
288 {
289         g_warning ("modest: You should implement %s", __FUNCTION__);
290         return FALSE;
291 }
292
293 static gboolean
294 modest_window_zoom_minus_default (ModestWindow *window)
295 {
296         g_warning ("modest: You should implement %s", __FUNCTION__);
297         return FALSE;
298 }
299
300 static void 
301 modest_window_show_toolbar_default (ModestWindow *window,
302                                     gboolean show_toolbar)
303 {
304         g_warning ("modest: You should implement %s", __FUNCTION__);
305 }
306
307
308
309 void
310 modest_window_save_state (ModestWindow *window)
311 {
312         ModestWindowClass *klass = MODEST_WINDOW_GET_CLASS (window);
313         if (klass->save_state_func)
314                 klass->save_state_func (window);
315 }
316
317 static gboolean
318 on_key_pressed (GtkWidget *self,
319                 GdkEventKey *event,
320                 gpointer user_data)
321 {
322         ModestWindowMgr *mgr = NULL;
323
324         mgr = modest_runtime_get_window_mgr ();
325
326         switch (event->keyval) {
327         case GDK_F6: 
328                 modest_ui_actions_on_change_fullscreen (NULL, MODEST_WINDOW(self));
329                 return TRUE;
330         case GDK_F7: 
331                 modest_ui_actions_on_zoom_plus (NULL, MODEST_WINDOW(self));
332                 return TRUE;
333         case GDK_F8: 
334                 modest_ui_actions_on_zoom_minus (NULL, MODEST_WINDOW(self));
335                 return TRUE;
336         case GDK_Escape: 
337                 if (modest_window_mgr_get_fullscreen_mode (mgr))
338                         modest_ui_actions_on_change_fullscreen (NULL, MODEST_WINDOW(self));
339                 else if (MODEST_IS_MSG_VIEW_WINDOW (self)||MODEST_IS_MSG_EDIT_WINDOW (self))
340                         modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (self));
341                 break;
342         }
343         
344         return FALSE;
345 }