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