c7bcac230686015511991ebbc0948325c1b04eaf
[modest] / src / widgets / modest-hildon1-window-mgr.c
1 /* Copyright (c) 2008, 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 <string.h>
31 #include "modest-hildon1-window-mgr.h"
32 #include "modest-msg-edit-window.h"
33 #include "modest-main-window.h"
34 #include "modest-conf.h"
35 #include "modest-defs.h"
36 #include "modest-signal-mgr.h"
37 #include "modest-runtime.h"
38 #include "modest-platform.h"
39 #include "modest-ui-actions.h"
40 #include "modest-debug.h"
41 #include "modest-tny-folder.h"
42
43 /* 'private'/'protected' functions */
44 static void modest_hildon1_window_mgr_class_init (ModestHildon1WindowMgrClass *klass);
45 static void modest_hildon1_window_mgr_instance_init (ModestHildon1WindowMgr *obj);
46 static void modest_hildon1_window_mgr_finalize   (GObject *obj);
47
48 static gboolean on_window_destroy        (ModestWindow *window,
49                                           GdkEvent *event,
50                                           ModestHildon1WindowMgr *self);
51
52 static gboolean on_modal_window_close    (GtkWidget *widget,
53                                           GdkEvent *event,
54                                           gpointer user_data);
55
56 static void on_modal_dialog_destroy      (GtkObject *object,
57                                           gpointer   user_data);
58
59 static void     on_modal_dialog_close    (GtkDialog *dialog,
60                                           gint arg1,
61                                           gpointer user_data);
62
63 static const gchar* get_show_toolbar_key (GType window_type,
64                                           gboolean fullscreen);
65
66 static gboolean modest_hildon1_window_mgr_register_window (ModestWindowMgr *self, 
67                                                            ModestWindow *window,
68                                                            ModestWindow *parent);
69 static void modest_hildon1_window_mgr_unregister_window (ModestWindowMgr *self, 
70                                                          ModestWindow *window);
71 static void modest_hildon1_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
72                                                            gboolean on);
73 static gboolean modest_hildon1_window_mgr_get_fullscreen_mode (ModestWindowMgr *self);
74 static void modest_hildon1_window_mgr_show_toolbars (ModestWindowMgr *self,
75                                                      GType window_type,
76                                                      gboolean show_toolbars,
77                                                      gboolean fullscreen);
78 static ModestWindow* modest_hildon1_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show);
79 static GtkWindow *modest_hildon1_window_mgr_get_modal (ModestWindowMgr *self);
80 static void modest_hildon1_window_mgr_set_modal (ModestWindowMgr *self, 
81                                                  GtkWindow *window,
82                                                  GtkWindow *parent);
83 static gboolean modest_hildon1_window_mgr_find_registered_header (ModestWindowMgr *self, 
84                                                                   TnyHeader *header,
85                                                                   ModestWindow **win);
86 static GList *modest_hildon1_window_mgr_get_window_list (ModestWindowMgr *self);
87
88 typedef struct _ModestHildon1WindowMgrPrivate ModestHildon1WindowMgrPrivate;
89 struct _ModestHildon1WindowMgrPrivate {
90         GList        *window_list;
91         GMutex       *queue_lock;
92         GQueue       *modal_windows;
93         
94         gboolean     fullscreen_mode;
95         
96         GHashTable   *destroy_handlers;
97         GHashTable   *viewer_handlers;
98         GSList       *window_state_uids;
99         
100         guint        closing_time;
101
102         GSList       *modal_handler_uids;
103         ModestWindow *current_top;
104 };
105 #define MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
106                                                                                    MODEST_TYPE_HILDON1_WINDOW_MGR, \
107                                                                                    ModestHildon1WindowMgrPrivate))
108 /* globals */
109 static GObjectClass *parent_class = NULL;
110
111 GType
112 modest_hildon1_window_mgr_get_type (void)
113 {
114         static GType my_type = 0;
115         if (!my_type) {
116                 static const GTypeInfo my_info = {
117                         sizeof(ModestHildon1WindowMgrClass),
118                         NULL,           /* base init */
119                         NULL,           /* base finalize */
120                         (GClassInitFunc) modest_hildon1_window_mgr_class_init,
121                         NULL,           /* class finalize */
122                         NULL,           /* class data */
123                         sizeof(ModestHildon1WindowMgr),
124                         1,              /* n_preallocs */
125                         (GInstanceInitFunc) modest_hildon1_window_mgr_instance_init,
126                         NULL
127                 };
128                 my_type = g_type_register_static (MODEST_TYPE_WINDOW_MGR,
129                                                   "ModestHildon1WindowMgr",
130                                                   &my_info, 0);
131         }
132         return my_type;
133 }
134
135 static void
136 modest_hildon1_window_mgr_class_init (ModestHildon1WindowMgrClass *klass)
137 {
138         GObjectClass *gobject_class;
139         ModestWindowMgrClass *mgr_class;
140
141         gobject_class = (GObjectClass*) klass;
142         mgr_class = (ModestWindowMgrClass *) klass;
143
144         parent_class            = g_type_class_peek_parent (klass);
145         gobject_class->finalize = modest_hildon1_window_mgr_finalize;
146         mgr_class->register_window = modest_hildon1_window_mgr_register_window;
147         mgr_class->unregister_window = modest_hildon1_window_mgr_unregister_window;
148         mgr_class->set_fullscreen_mode = modest_hildon1_window_mgr_set_fullscreen_mode;
149         mgr_class->get_fullscreen_mode = modest_hildon1_window_mgr_get_fullscreen_mode;
150         mgr_class->show_toolbars = modest_hildon1_window_mgr_show_toolbars;
151         mgr_class->get_main_window = modest_hildon1_window_mgr_get_main_window;
152         mgr_class->get_modal = modest_hildon1_window_mgr_get_modal;
153         mgr_class->set_modal = modest_hildon1_window_mgr_set_modal;
154         mgr_class->find_registered_header = modest_hildon1_window_mgr_find_registered_header;
155         mgr_class->get_window_list = modest_hildon1_window_mgr_get_window_list;
156
157         g_type_class_add_private (gobject_class, sizeof(ModestHildon1WindowMgrPrivate));
158
159 }
160
161 static void
162 modest_hildon1_window_mgr_instance_init (ModestHildon1WindowMgr *obj)
163 {
164         ModestHildon1WindowMgrPrivate *priv;
165
166         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(obj);
167         priv->window_list = NULL;
168         priv->fullscreen_mode = FALSE;
169         priv->current_top = NULL;
170         priv->window_state_uids = NULL;
171
172         priv->modal_windows = g_queue_new ();
173         priv->queue_lock = g_mutex_new ();
174         
175         /* Could not initialize it from gconf, singletons are not
176            ready yet */
177         priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);   
178         priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
179
180         priv->closing_time = 0;
181
182         priv->modal_handler_uids = NULL;
183 }
184
185 static void
186 modest_hildon1_window_mgr_finalize (GObject *obj)
187 {
188         ModestHildon1WindowMgrPrivate *priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(obj);
189
190         modest_signal_mgr_disconnect_all_and_destroy (priv->window_state_uids);
191         priv->window_state_uids = NULL;
192
193         if (priv->window_list) {
194                 GList *iter = priv->window_list;
195                 /* unregister pending windows */
196                 while (iter) {
197                         ModestWindow *window = (ModestWindow *) iter->data;
198                         iter = g_list_next (iter);
199                         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), window);
200                 }
201                 g_list_free (priv->window_list);
202                 priv->window_list = NULL;
203         }
204
205         /* Free the hash table with the handlers */
206         if (priv->destroy_handlers) {
207                 g_hash_table_destroy (priv->destroy_handlers);
208                 priv->destroy_handlers = NULL;
209         }
210
211         if (priv->viewer_handlers) {
212                 g_hash_table_destroy (priv->viewer_handlers);
213                 priv->viewer_handlers = NULL;
214         }
215
216         modest_signal_mgr_disconnect_all_and_destroy (priv->modal_handler_uids);
217         priv->modal_handler_uids = NULL;
218
219         if (priv->modal_windows) {
220                 g_mutex_lock (priv->queue_lock);
221                 g_queue_free (priv->modal_windows);
222                 priv->modal_windows = NULL;
223                 g_mutex_unlock (priv->queue_lock);
224         }
225         g_mutex_free (priv->queue_lock);
226         
227         /* Do not unref priv->main_window because it does not hold a
228            new reference */
229
230         
231         G_OBJECT_CLASS(parent_class)->finalize (obj);
232 }
233
234 ModestWindowMgr*
235 modest_hildon1_window_mgr_new (void)
236 {
237         return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_HILDON1_WINDOW_MGR, NULL));
238 }
239
240 void
241 modest_hildon1_window_mgr_close_all_windows (ModestWindowMgr *self)
242 {
243         ModestHildon1WindowMgrPrivate *priv = NULL;
244         gboolean ret_value = FALSE;
245         ModestWindow *main_window;
246         
247         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
248         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
249         
250         /* If there is a main window then try to close it, and it will
251            close the others if needed */
252         main_window = modest_window_mgr_get_main_window (self, FALSE);
253         if (main_window) {
254                 g_signal_emit_by_name (main_window, "delete-event", NULL, &ret_value);
255         } else {
256                 GList *wins = NULL, *next = NULL;
257
258                 /* delete-event handler actually removes window_list item, */
259                 wins = priv->window_list;
260                 while (wins) {
261                         next = g_list_next (wins);
262                         g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
263                         wins = next;
264                 }
265         }
266 }
267
268 static gint
269 compare_msguids (ModestWindow *win,
270                  const gchar *uid)
271 {
272         const gchar *msg_uid;
273
274         if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
275                 return 1;
276
277         /* Get message uid from msg window */
278         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
279                 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
280                 if (msg_uid && uid &&!strcmp (msg_uid, uid))
281                         return 0;
282         } else {
283                 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
284         }
285         
286         if (msg_uid && uid &&!strcmp (msg_uid, uid))
287                 return 0;
288         else
289                 return 1;
290 }
291
292 static gboolean
293 modest_hildon1_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
294                                                   ModestWindow **win)
295 {
296         ModestHildon1WindowMgrPrivate *priv = NULL;
297         gchar* uid = NULL;
298         gboolean has_header, has_window = FALSE;
299         GList *item = NULL;
300
301         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
302         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
303         
304         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
305
306         has_header = MODEST_WINDOW_MGR_CLASS (parent_class)->find_registered_header (self, header, win);
307         
308         uid = modest_tny_folder_get_header_unique_id (header);
309         
310         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
311         if (item) {
312                 has_window = TRUE;
313                 if (win) {
314                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
315                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
316                                 g_debug ("not a valid window!");
317                         else {
318                                 g_debug ("found a window");
319                                 *win = MODEST_WINDOW (item->data);
320                         }
321                 }
322         }
323         g_free (uid);
324         
325         return has_header || has_window;
326 }
327
328 static GList *
329 modest_hildon1_window_mgr_get_window_list (ModestWindowMgr *self)
330 {
331         ModestHildon1WindowMgrPrivate *priv;
332
333         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
334         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
335
336         return g_list_copy (priv->window_list);
337 }
338
339 static const gchar *
340 get_show_toolbar_key (GType window_type,
341                       gboolean fullscreen)
342 {
343         const gchar *key = NULL;
344
345         if (window_type == MODEST_TYPE_MAIN_WINDOW)
346                 key = (fullscreen) ? 
347                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
348                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
349         else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
350                 key = (fullscreen) ? 
351                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
352                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
353         else if (window_type ==  MODEST_TYPE_MSG_EDIT_WINDOW)
354                 key = (fullscreen) ? 
355                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
356                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
357         else
358                 g_return_val_if_reached (NULL);
359
360         return key;
361 }
362
363 #ifndef MODEST_TOOLKIT_GTK
364 static void
365 on_window_is_topmost (GObject    *gobject,
366                       GParamSpec *arg1,
367                       gpointer    user_data)
368 {
369         ModestHildon1WindowMgr *self;
370         ModestHildon1WindowMgrPrivate *priv;
371         ModestWindow *win = (ModestWindow *) gobject;
372
373         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data));
374
375         self = MODEST_HILDON1_WINDOW_MGR (user_data);
376         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
377
378         if (hildon_window_get_is_topmost (HILDON_WINDOW (win)))
379                 priv->current_top = win;
380 }
381
382 #else
383 static gboolean
384 on_window_state_event (GtkWidget           *widget,
385                        GdkEventWindowState *event,
386                        gpointer             user_data)
387 {
388         ModestHildon1WindowMgr *self;
389         ModestHildon1WindowMgrPrivate *priv;
390
391         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data), FALSE);
392
393         self = MODEST_HILDON1_WINDOW_MGR (user_data);
394         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
395
396         MODEST_DEBUG_BLOCK (
397                             if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
398                                     g_print ("GDK_WINDOW_STATE_WITHDRAWN\n");
399                             if (event->changed_mask & GDK_WINDOW_STATE_ICONIFIED)
400                                     g_print ("GDK_WINDOW_STATE_ICONIFIED\n");
401                             if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED)
402                                     g_print ("GDK_WINDOW_STATE_MAXIMIZED\n");
403                             if (event->changed_mask & GDK_WINDOW_STATE_STICKY)
404                                     g_print ("GDK_WINDOW_STATE_STICKY\n");
405                             if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
406                                     g_print ("GDK_WINDOW_STATE_FULLSCREEN\n");
407                             if (event->changed_mask & GDK_WINDOW_STATE_ABOVE)
408                                     g_print ("GDK_WINDOW_STATE_ABOVE\n");
409                             if (event->changed_mask & GDK_WINDOW_STATE_BELOW)
410                                     g_print ("GDK_WINDOW_STATE_BELOW\n");
411                             );
412         if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN ||
413             event->changed_mask & GDK_WINDOW_STATE_ABOVE) {
414                 priv->current_top = MODEST_WINDOW (widget);
415         }
416         
417         return FALSE;
418 }
419 #endif
420
421 static gboolean
422 modest_hildon1_window_mgr_register_window (ModestWindowMgr *self, 
423                                            ModestWindow *window,
424                                            ModestWindow *parent)
425 {
426         GList *win;
427         ModestHildon1WindowMgrPrivate *priv;
428         gint *handler_id;
429         const gchar *key;
430         ModestWindow *main_window;
431
432         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
433         g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
434
435         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
436
437         win = g_list_find (priv->window_list, window);
438         if (win) {
439                 /* this is for the case we want to register the window and it was already
440                  * registered. We leave silently.
441                  */
442                 return FALSE;
443         }
444         
445         if (!MODEST_WINDOW_MGR_CLASS (parent_class)->register_window (self, window, parent))
446                 return FALSE;
447         
448         /* Add to list. Keep a reference to the window */
449         g_object_ref (window);
450         priv->window_list = g_list_prepend (priv->window_list, window);
451
452         /* Listen to window state changes. Unfortunately
453            window-state-event does not properly work for the Maemo
454            version, so we need to use is-topmost and the ifdef */
455 #ifndef MODEST_TOOLKIT_GTK
456         priv->window_state_uids = 
457                 modest_signal_mgr_connect (priv->window_state_uids, 
458                                            G_OBJECT (window), 
459                                            "notify::is-topmost",
460                                            G_CALLBACK (on_window_is_topmost), 
461                                            self);
462 #else
463         priv->window_state_uids = 
464                 modest_signal_mgr_connect (priv->window_state_uids, 
465                                            G_OBJECT (window), 
466                                            "window-state-event",
467                                            G_CALLBACK (on_window_state_event), 
468                                            self);
469 #endif
470         /* Listen to object destruction */
471         handler_id = g_malloc0 (sizeof (gint));
472         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
473         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
474
475         main_window = modest_window_mgr_get_main_window (self, FALSE);
476         /* If there is a msg view window, let the main window listen the msg-changed signal */
477         if (MODEST_IS_MSG_VIEW_WINDOW(window) && main_window) {
478                 gulong *handler;
479                 handler = g_malloc0 (sizeof (gulong));
480                 *handler = g_signal_connect (window, "msg-changed", 
481                                              G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed), 
482                                              main_window);
483                 g_hash_table_insert (priv->viewer_handlers, window, handler);
484         }
485
486         /* Put into fullscreen if needed */
487         if (priv->fullscreen_mode)
488                 gtk_window_fullscreen (GTK_WINDOW (window));
489
490         /* Show/hide toolbar & fullscreen */    
491         key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
492         modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
493
494         return TRUE;
495 }
496
497 static void
498 cancel_window_operations (ModestWindow *window)
499 {
500         GSList* pending_ops = NULL;
501
502         /* cancel open and receive operations */
503         pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (), 
504                                                                  G_OBJECT (window));
505         while (pending_ops != NULL) {
506                 ModestMailOperationTypeOperation type;
507                 GSList* tmp_list = NULL;
508
509                 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
510                 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
511                         modest_mail_operation_cancel (pending_ops->data);
512                 }
513                 g_object_unref (G_OBJECT (pending_ops->data));
514                 tmp_list = pending_ops;
515                 pending_ops = g_slist_next (pending_ops);
516                 g_slist_free_1 (tmp_list);
517         }
518 }
519
520
521
522 static gboolean
523 on_window_destroy (ModestWindow *window, 
524                    GdkEvent *event,
525                    ModestHildon1WindowMgr *self)
526 {
527         gint dialog_response = GTK_RESPONSE_NONE;
528         gboolean no_propagate = FALSE;
529
530         /* Specific stuff first */
531         if (MODEST_IS_MAIN_WINDOW (window)) {
532                 ModestHildon1WindowMgrPrivate *priv;
533                 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
534
535                 /* If more than one window already opened */
536                 if (g_list_length (priv->window_list) > 1) {
537
538                         /* Present the window if it's not visible now */
539                         if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window))) {
540                                 gtk_window_present (GTK_WINDOW (window));
541                                 priv->current_top = window;
542                         }
543                         /* Create the confirmation dialog MSG-NOT308 */
544                         dialog_response = modest_platform_run_confirmation_dialog (
545                                         GTK_WINDOW (window), _("emev_nc_close_windows"));
546
547                         /* If the user wants to close all the windows */
548                         if ((dialog_response == GTK_RESPONSE_OK) 
549                             || (dialog_response == GTK_RESPONSE_ACCEPT) 
550                             || (dialog_response == GTK_RESPONSE_YES)) {
551                                         GList *iter = priv->window_list;
552                                         do {
553                                                 if (iter->data != window) {
554                                                         GList *tmp = iter->next;
555                                                         on_window_destroy (MODEST_WINDOW (iter->data),
556                                                                         event,
557                                                                         self);
558                                                         iter = tmp;
559                                                 } else {
560                                                         iter = g_list_next (iter);
561                                                 }
562                                         } while (iter);
563                         } else {
564                                 return TRUE;
565                         }
566                 }
567
568                 /* Do not unregister it, just hide */
569                 gtk_widget_hide_all (GTK_WIDGET (window));
570
571                 /* Cancel pending operations */
572                 cancel_window_operations (window);
573
574                 /* Fake the window system, make it think that there is no window */
575                 if (modest_window_mgr_num_windows (MODEST_WINDOW_MGR (self)) == 0)
576                         g_signal_emit_by_name (self, "window-list-empty");
577
578                 no_propagate = TRUE;
579         }
580         else {
581                 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
582                         gboolean sent = FALSE;
583                         gint response = GTK_RESPONSE_ACCEPT;
584                         sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
585                         /* Save currently edited message to Drafts if it was not sent */
586                         if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
587
588                                 /* Raise the window if it's minimized */
589                                 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
590                                         gtk_window_present (GTK_WINDOW (window));
591                                 
592                                 response =
593                                         modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
594                                                                                  _("mcen_nc_no_email_message_modified_save_changes"));
595                                 /* Save to drafts */
596                                 if (response == GTK_RESPONSE_OK)
597                                         if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
598                                                 return TRUE;
599                         }
600                 }
601                 /* Unregister window */
602                 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (self), window);
603                 no_propagate = TRUE;
604         }
605
606         return no_propagate;
607 }
608
609 static void
610 disconnect_msg_changed (gpointer key, 
611                         gpointer value, 
612                         gpointer user_data)
613 {
614         guint handler_id;
615         handler_id = GPOINTER_TO_UINT(value);
616         
617         if (key && G_IS_OBJECT(key))
618                 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
619 }
620
621 static void 
622 modest_hildon1_window_mgr_unregister_window (ModestWindowMgr *self, 
623                                              ModestWindow *window)
624 {
625         GList *win;
626         ModestHildon1WindowMgrPrivate *priv;
627         gulong *tmp, handler_id;
628
629         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
630         g_return_if_fail (MODEST_IS_WINDOW (window));
631
632         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
633
634         win = g_list_find (priv->window_list, window);
635         if (!win) {
636                 g_warning ("Trying to unregister a window that has not being registered yet");
637                 return;
638         }
639
640         /* If it's the main window unset it */
641         if (MODEST_IS_MAIN_WINDOW (window)) {
642                 modest_window_mgr_set_main_window (self, NULL);
643
644                 /* Disconnect all emissions of msg-changed */
645                 if (priv->viewer_handlers) {
646                         g_hash_table_foreach (priv->viewer_handlers, 
647                                               disconnect_msg_changed, 
648                                               NULL);
649                         g_hash_table_destroy (priv->viewer_handlers);
650                         priv->viewer_handlers = NULL;
651                 }
652         }
653
654         /* Remove the viewer window handler from the hash table. The
655            HashTable could not exist if the main window was closed
656            when there were other windows remaining */
657         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
658                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
659                 /* If the viewer was created without a main window
660                    (for example when opening a message through D-Bus
661                    the viewer handlers was not registered */
662                 if (tmp) {
663                         g_signal_handler_disconnect (window, *tmp);
664                         g_hash_table_remove (priv->viewer_handlers, window);
665                 }
666         }
667
668         /* Remove from list & hash table */
669         priv->window_list = g_list_remove_link (priv->window_list, win);
670         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
671         handler_id = *tmp;
672
673         g_hash_table_remove (priv->destroy_handlers, window);
674
675         /* cancel open and receive operations */
676         cancel_window_operations (window);
677
678         /* Check if it's the topmost window, and remove the window from the stack.
679          * This is needed for the cases there's no other topmost window that will
680          * replace it in topmost handler.
681          */
682          if (window == priv->current_top)
683                  priv->current_top = NULL;
684
685         /* Disconnect the "window-state-event" handler, we won't need it anymore */
686         if (priv->window_state_uids) {
687 #ifndef MODEST_TOOLKIT_GTK
688                 priv->window_state_uids = 
689                         modest_signal_mgr_disconnect (priv->window_state_uids, 
690                                                       G_OBJECT (window), 
691                                                       "notify::is-topmost");
692 #else
693                 priv->window_state_uids = 
694                         modest_signal_mgr_disconnect (priv->window_state_uids, 
695                                                       G_OBJECT (window), 
696                                                       "window-state-event");
697 #endif
698         }
699         
700         /* Disconnect the "delete-event" handler, we won't need it anymore */
701         g_signal_handler_disconnect (window, handler_id);
702
703         /* Destroy the window */
704         g_object_unref (win->data);
705         g_list_free (win);
706         
707         MODEST_WINDOW_MGR_CLASS (parent_class)->unregister_window (self, window);
708
709         /* If there are no more windows registered emit the signal */
710         if (modest_window_mgr_num_windows (self) == 0)
711                 g_signal_emit_by_name (self, "window-list-empty");
712 }
713
714
715
716 static void
717 modest_hildon1_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
718                                                gboolean on)
719 {
720         ModestHildon1WindowMgrPrivate *priv;
721         GList *win = NULL;
722         ModestConf *conf;
723
724         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
725
726         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
727
728         /* If there is no change do nothing */
729         if (priv->fullscreen_mode == on)
730                 return;
731
732         priv->fullscreen_mode = on;
733
734         conf = modest_runtime_get_conf ();
735
736         /* Update windows */
737         win = priv->window_list;
738         while (win) {
739                 gboolean show;
740                 const gchar *key = NULL;
741
742                 /* Getting this from gconf everytime is not that
743                    expensive, we'll do it just a few times */
744                 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
745                 show = modest_conf_get_bool (conf, key, NULL);
746
747                 /* Set fullscreen/unfullscreen */
748                 if (on)
749                         gtk_window_fullscreen (GTK_WINDOW (win->data));
750                 else
751                         gtk_window_unfullscreen (GTK_WINDOW (win->data));
752
753                 /* Show/Hide toolbar */
754                 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
755
756                 win = g_list_next (win);
757         }
758 }
759
760 static gboolean
761 modest_hildon1_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
762 {
763         ModestHildon1WindowMgrPrivate *priv;
764
765         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
766
767         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
768
769         return priv->fullscreen_mode;
770 }
771
772 static void 
773 modest_hildon1_window_mgr_show_toolbars (ModestWindowMgr *self,
774                                          GType window_type,
775                                          gboolean show_toolbars,
776                                          gboolean fullscreen)
777 {
778         ModestHildon1WindowMgrPrivate *priv;
779         ModestConf *conf;
780         const gchar *key = NULL;
781
782         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
783
784         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
785         conf = modest_runtime_get_conf ();
786
787         /* If nothing changes then return */
788         key = get_show_toolbar_key (window_type, fullscreen);
789         conf = modest_runtime_get_conf ();
790         if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
791                 return;
792
793         /* Save in conf */
794         modest_conf_set_bool (conf, key, show_toolbars, NULL);
795
796         /* Apply now if the view mode is the right one */
797         if ((fullscreen && priv->fullscreen_mode) ||
798             (!fullscreen && !priv->fullscreen_mode)) {
799
800                 GList *win = priv->window_list;
801
802                 while (win) {
803                         if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
804                                 modest_window_show_toolbar (MODEST_WINDOW (win->data),
805                                                             show_toolbars);
806                         win = g_list_next (win);
807                 }
808         }
809 }
810
811 static ModestWindow*  
812 modest_hildon1_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
813 {
814         ModestHildon1WindowMgrPrivate *priv;
815         ModestWindow *result;
816         
817         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
818         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
819         
820         result = MODEST_WINDOW_MGR_CLASS (parent_class)->get_main_window (self, FALSE);
821         /* create the main window, if it hasn't been created yet */
822         if (!result && show) {
823                 /* modest_window_mgr_register_window will set priv->main_window */
824                 result = modest_main_window_new ();
825                 modest_window_mgr_register_window (self, result, NULL);
826                 gtk_widget_show_all (GTK_WIDGET (result));
827                 gtk_window_present (GTK_WINDOW (result));
828                 MODEST_DEBUG_BLOCK(
829                         g_debug ("%s: created main window: %p\n", __FUNCTION__, result);
830                 );
831         }
832         
833         return result;
834 }
835
836
837 static GtkWindow *
838 modest_hildon1_window_mgr_get_modal (ModestWindowMgr *self)
839 {
840         ModestHildon1WindowMgrPrivate *priv;
841         
842         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
843         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
844
845         return g_queue_peek_head (priv->modal_windows);
846 }
847
848
849 static void
850 modest_hildon1_window_mgr_set_modal (ModestWindowMgr *self, 
851                                      GtkWindow *window,
852                                      GtkWindow *parent)
853 {
854         GtkWindow *old_modal;
855         ModestHildon1WindowMgrPrivate *priv;
856
857         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
858         g_return_if_fail (GTK_IS_WINDOW (window));
859
860         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
861         g_mutex_lock (priv->queue_lock);
862         old_modal = g_queue_peek_head (priv->modal_windows);
863         g_mutex_unlock (priv->queue_lock);
864
865         if (!old_modal) {       
866                 /* make us transient wrt the main window then */
867                 if (priv->current_top && ((GtkWindow *) priv->current_top != window)) {
868                         gtk_window_set_transient_for (window, GTK_WINDOW(priv->current_top));
869                 } else {
870                         ModestWindow *main_win = modest_window_mgr_get_main_window (self, FALSE);
871                         if (GTK_WINDOW(main_win) != window) /* they should not be the same */
872                                 gtk_window_set_transient_for (window, GTK_WINDOW(main_win));
873                 }
874                 gtk_window_set_modal (window, TRUE);
875         } else {
876                 /* un-modalize the old one; the one on top should be the
877                  * modal one */
878                 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));   
879                 gtk_window_set_modal (window, TRUE);
880         }
881
882         /* this will be the new modal window */
883         g_mutex_lock (priv->queue_lock);
884         g_queue_push_head (priv->modal_windows, window);
885         g_mutex_unlock (priv->queue_lock);
886
887         if (GTK_IS_DIALOG (window)) {
888                 /* Note that response is not always enough because it
889                    could be captured and removed easily by dialogs but
890                    works for most of situations */
891                 priv->modal_handler_uids = 
892                         modest_signal_mgr_connect (priv->modal_handler_uids, 
893                                                    G_OBJECT (window), 
894                                                    "response",
895                                                    G_CALLBACK (on_modal_dialog_close), 
896                                                    self);
897                 /* We need this as well because dialogs are often
898                    destroyed with gtk_widget_destroy and this one will
899                    prevent response from happening */
900                 priv->modal_handler_uids = 
901                         modest_signal_mgr_connect (priv->modal_handler_uids, 
902                                                    G_OBJECT (window), 
903                                                    "destroy",
904                                                    G_CALLBACK (on_modal_dialog_destroy),
905                                                    self);
906         } else {
907                 priv->modal_handler_uids = 
908                         modest_signal_mgr_connect (priv->modal_handler_uids, 
909                                                    G_OBJECT (window), 
910                                                    "delete-event",
911                                                    G_CALLBACK (on_modal_window_close), 
912                                                    self);
913         }
914         /* Destroy width parent */
915         gtk_window_set_destroy_with_parent (window, TRUE);
916 }
917
918
919
920 static gboolean
921 idle_top_modal (gpointer data)
922 {
923         ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
924         ModestHildon1WindowMgrPrivate *priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
925         GtkWindow *topmost;
926
927         /* Get the top modal */
928         g_mutex_lock (priv->queue_lock);
929         topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
930         g_mutex_unlock (priv->queue_lock);
931
932         /* Show it */
933         if (topmost) {
934                 gdk_threads_enter ();
935                 gtk_window_present (topmost);
936                 /* It seems that the window looses modality if some
937                    other is shown on top of it after set_transient_for
938                    and set_parent */
939                 gtk_window_set_modal (topmost, TRUE);
940                 gdk_threads_leave ();
941         }
942
943         return FALSE;
944 }
945
946 static void
947 remove_modal_from_queue (GtkWidget *widget,
948                          ModestWindowMgr *self)
949 {
950         ModestHildon1WindowMgrPrivate *priv;
951         GList *item = NULL;
952
953         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
954
955         /* Remove from queue. We don't use remove, because we want to
956            exit if the widget does not belong to the queue */
957         g_mutex_lock (priv->queue_lock);
958         item = g_queue_find (priv->modal_windows, widget);
959         if (!item) {
960                 g_warning ("Trying to remove a modal window that is not registered");
961                 g_mutex_unlock (priv->queue_lock);
962                 return;
963         }
964         g_queue_unlink (priv->modal_windows, item);
965         g_mutex_unlock (priv->queue_lock);
966
967         /* Disconnect handler */
968         priv->modal_handler_uids =
969                 modest_signal_mgr_disconnect (priv->modal_handler_uids,
970                                               G_OBJECT (widget),
971                                               GTK_IS_DIALOG (widget) ?
972                                               "response" :
973                                               "delete-event");
974         if (GTK_IS_DIALOG (widget))
975                 priv->modal_handler_uids =
976                         modest_signal_mgr_disconnect (priv->modal_handler_uids,
977                                                       G_OBJECT (widget),
978                                                       "destroy");
979
980         /* Schedule the next one for being shown */
981         g_idle_add (idle_top_modal, self);
982 }
983
984 static gboolean
985 on_modal_window_close (GtkWidget *widget,
986                        GdkEvent *event,
987                        gpointer user_data)
988 {
989         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
990
991         /* Remove modal window from queue */
992         remove_modal_from_queue (widget, self);
993
994         /* Continue */
995         return FALSE;
996 }
997
998 static void
999 on_modal_dialog_close (GtkDialog *dialog,
1000                        gint arg1,
1001                        gpointer user_data)
1002 {
1003         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1004
1005         /* Remove modal window from queue. Note that if "destroy"
1006            signal was invoked before the response the window could be
1007            already deleted */
1008         remove_modal_from_queue (GTK_WIDGET (dialog), self);
1009 }
1010
1011 static void
1012 on_modal_dialog_destroy (GtkObject *object,
1013                          gpointer   user_data)
1014 {
1015         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1016
1017         /* Remove modal window from queue */
1018         remove_modal_from_queue (GTK_WIDGET (object), self);
1019 }
1020