* Define a new mail-operation to remove several messages.
[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->dimming_state = NULL;
133         priv->ui_dimming_enabled = TRUE;
134         priv->active_account = NULL;
135
136         /* Connect signals */
137         g_signal_connect (G_OBJECT (obj), 
138                           "key-press-event", 
139                           G_CALLBACK (on_key_pressed), NULL);
140 }
141
142 static void
143 modest_window_finalize (GObject *obj)
144 {
145         ModestWindowPrivate *priv;      
146
147         priv = MODEST_WINDOW_GET_PRIVATE(obj);
148
149         if (priv->ui_manager) {
150                 g_object_unref (G_OBJECT(priv->ui_manager));
151                 priv->ui_manager = NULL;
152         }
153         if (priv->ui_dimming_manager) {
154                 g_object_unref (G_OBJECT(priv->ui_dimming_manager));
155                 priv->ui_dimming_manager = NULL;
156         }
157
158         g_free (priv->active_account);
159         
160         G_OBJECT_CLASS(parent_class)->finalize (obj);
161 }
162
163
164
165 const gchar*
166 modest_window_get_active_account (ModestWindow *self)
167 {
168         g_return_val_if_fail (self, NULL);
169
170         return MODEST_WINDOW_GET_PRIVATE(self)->active_account;
171 }
172
173 void
174 modest_window_set_active_account (ModestWindow *self, const gchar *active_account)
175 {
176         ModestWindowPrivate *priv;      
177
178         priv = MODEST_WINDOW_GET_PRIVATE(self);
179
180         if (active_account == priv->active_account)
181                 return;
182         else {
183                 g_free (priv->active_account);
184                 priv->active_account = NULL;
185                 if (active_account)
186                         priv->active_account = g_strdup (active_account);
187         }
188 }
189
190 void
191 modest_window_check_dimming_rules (ModestWindow *self)
192 {
193         ModestWindowPrivate *priv;      
194
195         g_return_if_fail (MODEST_IS_WINDOW (self));
196         priv = MODEST_WINDOW_GET_PRIVATE(self);
197
198         if (priv->ui_dimming_enabled)
199                 modest_ui_dimming_manager_process_dimming_rules (priv->ui_dimming_manager);
200 }
201
202 void
203 modest_window_check_dimming_rules_group (ModestWindow *self,
204                                          const gchar *group_name)
205 {
206         ModestWindowPrivate *priv;      
207
208         g_return_if_fail (MODEST_IS_WINDOW (self));
209         priv = MODEST_WINDOW_GET_PRIVATE(self);
210
211         if (priv->ui_dimming_enabled)
212                 modest_ui_dimming_manager_process_dimming_rules_group (priv->ui_dimming_manager, group_name);
213 }
214
215 void
216 modest_window_set_dimming_state (ModestWindow *window,
217                                  DimmedState *state)
218 {
219         ModestWindowPrivate *priv;      
220
221         g_return_if_fail (MODEST_IS_WINDOW (window));
222         priv = MODEST_WINDOW_GET_PRIVATE(window);
223
224         /* Free previous */
225         if (priv->dimming_state != NULL)
226                 g_slice_free (DimmedState, priv->dimming_state);
227
228         /* Set new state */
229         priv->dimming_state = state;
230 }
231
232 const DimmedState *
233 modest_window_get_dimming_state (ModestWindow *window)
234 {
235         ModestWindowPrivate *priv;      
236
237         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
238         priv = MODEST_WINDOW_GET_PRIVATE(window);
239
240         return priv->dimming_state;
241 }
242
243 void
244 modest_window_disable_dimming (ModestWindow *self)
245 {
246         ModestWindowPrivate *priv;      
247
248         g_return_if_fail (MODEST_IS_WINDOW (self));
249         priv = MODEST_WINDOW_GET_PRIVATE(self);
250
251         priv->ui_dimming_enabled = FALSE;
252 }
253
254 void
255 modest_window_enable_dimming (ModestWindow *self)
256 {
257         ModestWindowPrivate *priv;      
258
259         g_return_if_fail (MODEST_IS_WINDOW (self));
260         priv = MODEST_WINDOW_GET_PRIVATE(self);
261
262         priv->ui_dimming_enabled = TRUE;
263 }
264
265 GtkAction *
266 modest_window_get_action (ModestWindow *window, 
267                           const gchar *action_path) 
268 {
269         GtkAction *action = NULL;
270         ModestWindowPrivate *priv;      
271
272         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
273         priv = MODEST_WINDOW_GET_PRIVATE(window);
274
275         action = gtk_ui_manager_get_action (priv->ui_manager, action_path);     
276
277         return action;
278 }
279
280 GtkWidget *
281 modest_window_get_action_widget (ModestWindow *window, 
282                                  const gchar *action_path) 
283 {
284         GtkWidget *widget = NULL;
285         ModestWindowPrivate *priv;      
286
287         g_return_val_if_fail (MODEST_IS_WINDOW (window), NULL);
288         priv = MODEST_WINDOW_GET_PRIVATE(window);
289
290         widget = gtk_ui_manager_get_widget (priv->ui_manager, action_path);     
291
292         return widget;
293 }
294
295 void
296 modest_window_set_zoom (ModestWindow *window,
297                         gdouble zoom)
298 {
299         MODEST_WINDOW_GET_CLASS (window)->set_zoom_func (window, zoom);
300         return;
301 }
302
303 gdouble
304 modest_window_get_zoom (ModestWindow *window)
305 {
306         return MODEST_WINDOW_GET_CLASS (window)->get_zoom_func (window);
307 }
308
309 gboolean
310 modest_window_zoom_plus (ModestWindow *window)
311 {
312         return MODEST_WINDOW_GET_CLASS (window)->zoom_plus_func (window);
313 }
314
315 gboolean
316 modest_window_zoom_minus (ModestWindow *window)
317 {
318         return MODEST_WINDOW_GET_CLASS (window)->zoom_minus_func (window);
319 }
320
321 void 
322 modest_window_show_toolbar (ModestWindow *window,
323                             gboolean show_toolbar)
324 {
325         MODEST_WINDOW_GET_CLASS (window)->show_toolbar_func (window,
326                                                              show_toolbar);
327 }
328
329 void 
330 modest_window_disconnect_signals (ModestWindow *window)
331 {
332         MODEST_WINDOW_GET_CLASS (window)->disconnect_signals_func (window);
333 }
334
335
336 /* Default implementations */
337
338 static void
339 modest_window_set_zoom_default (ModestWindow *window,
340                                 gdouble zoom)
341 {
342         g_warning ("modest: You should implement %s", __FUNCTION__);
343
344 }
345
346 static gdouble
347 modest_window_get_zoom_default (ModestWindow *window)
348 {
349         g_warning ("modest: You should implement %s", __FUNCTION__);
350         return 1.0;
351 }
352
353 static gboolean
354 modest_window_zoom_plus_default (ModestWindow *window)
355 {
356         g_warning ("modest: You should implement %s", __FUNCTION__);
357         return FALSE;
358 }
359
360 static gboolean
361 modest_window_zoom_minus_default (ModestWindow *window)
362 {
363         g_warning ("modest: You should implement %s", __FUNCTION__);
364         return FALSE;
365 }
366
367 static void 
368 modest_window_show_toolbar_default (ModestWindow *window,
369                                     gboolean show_toolbar)
370 {
371         g_warning ("modest: You should implement %s", __FUNCTION__);
372 }
373
374 static void 
375 modest_window_disconnect_signals_default (ModestWindow *self)
376 {
377         g_warning ("modest: You should implement %s", __FUNCTION__);
378 }
379
380 void
381 modest_window_save_state (ModestWindow *window)
382 {
383         ModestWindowClass *klass = MODEST_WINDOW_GET_CLASS (window);
384         if (klass->save_state_func)
385                 klass->save_state_func (window);
386 }
387
388 static gboolean
389 on_key_pressed (GtkWidget *self,
390                 GdkEventKey *event,
391                 gpointer user_data)
392 {
393         ModestWindowMgr *mgr = NULL;
394
395         mgr = modest_runtime_get_window_mgr ();
396
397         switch (event->keyval) {
398         case GDK_F6: 
399                 modest_ui_actions_on_change_fullscreen (NULL, MODEST_WINDOW(self));
400                 return TRUE;
401         case GDK_F7: 
402                 modest_ui_actions_on_zoom_plus (NULL, MODEST_WINDOW(self));
403                 return TRUE;
404         case GDK_F8: 
405                 modest_ui_actions_on_zoom_minus (NULL, MODEST_WINDOW(self));
406                 return TRUE;
407         case GDK_Escape: 
408                 if (modest_window_mgr_get_fullscreen_mode (mgr))
409                         modest_ui_actions_on_change_fullscreen (NULL, MODEST_WINDOW(self));
410                 else if (MODEST_IS_MSG_VIEW_WINDOW (self)||MODEST_IS_MSG_EDIT_WINDOW (self))
411                         modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (self));
412                 break;
413         }
414         
415         return FALSE;
416 }