* Fixes NB#85455, close the dialogs with their parent, this avoids some possible...
[modest] / src / widgets / modest-window-mgr.c
1 /* Copyright (c) 2006,2007 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-window-mgr.h"
32 #include "modest-runtime.h"
33 #include "modest-tny-folder.h"
34 #include "modest-ui-actions.h"
35 #include "modest-platform.h"
36 #include "widgets/modest-main-window.h"
37 #include "widgets/modest-msg-edit-window.h"
38 #include "widgets/modest-msg-view-window.h"
39 #include "modest-debug.h"
40
41
42 /* 'private'/'protected' functions */
43 static void modest_window_mgr_class_init (ModestWindowMgrClass *klass);
44 static void modest_window_mgr_init       (ModestWindowMgr *obj);
45 static void modest_window_mgr_finalize   (GObject *obj);
46
47 static gboolean on_window_destroy        (ModestWindow *window,
48                                           GdkEvent *event,
49                                           ModestWindowMgr *self);
50
51 static gboolean on_modal_window_close    (GtkWidget *widget,
52                                           GdkEvent *event,
53                                           gpointer user_data);
54
55 static void on_modal_dialog_destroy      (GtkObject *object,
56                                           gpointer   user_data);
57
58 static void     on_modal_dialog_close    (GtkDialog *dialog,
59                                           gint arg1,
60                                           gpointer user_data);
61
62 static const gchar* get_show_toolbar_key (GType window_type,
63                                           gboolean fullscreen);
64
65 /* list my signals  */
66 enum {
67         WINDOW_LIST_EMPTY_SIGNAL,
68         NUM_SIGNALS
69 };
70
71 typedef struct _ModestWindowMgrPrivate ModestWindowMgrPrivate;
72 struct _ModestWindowMgrPrivate {
73         GList        *window_list;
74         guint         banner_counter;
75
76         ModestWindow *main_window;
77
78         GMutex       *queue_lock;
79         GQueue       *modal_windows;
80         
81         gboolean     fullscreen_mode;
82         
83         GSList       *windows_that_prevent_hibernation;
84         GSList       *preregistered_uids;
85         GHashTable   *destroy_handlers;
86         GHashTable   *viewer_handlers;
87         GSList       *window_state_uids;
88         
89         guint        closing_time;
90
91         GSList       *modal_handler_uids;
92         GtkWidget    *cached_view;
93         GtkWidget    *cached_editor;
94         guint        idle_load_view_id;
95         guint        idle_load_editor_id;
96
97         ModestWindow *current_top;
98 };
99 #define MODEST_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
100                                                MODEST_TYPE_WINDOW_MGR, \
101                                                ModestWindowMgrPrivate))
102 /* globals */
103 static GObjectClass *parent_class = NULL;
104
105 /* uncomment the following if you have defined any signals */
106 static guint signals[NUM_SIGNALS] = {0};
107
108 GType
109 modest_window_mgr_get_type (void)
110 {
111         static GType my_type = 0;
112         if (!my_type) {
113                 static const GTypeInfo my_info = {
114                         sizeof(ModestWindowMgrClass),
115                         NULL,           /* base init */
116                         NULL,           /* base finalize */
117                         (GClassInitFunc) modest_window_mgr_class_init,
118                         NULL,           /* class finalize */
119                         NULL,           /* class data */
120                         sizeof(ModestWindowMgr),
121                         1,              /* n_preallocs */
122                         (GInstanceInitFunc) modest_window_mgr_init,
123                         NULL
124                 };
125                 my_type = g_type_register_static (G_TYPE_OBJECT,
126                                                   "ModestWindowMgr",
127                                                   &my_info, 0);
128         }
129         return my_type;
130 }
131
132 static void
133 modest_window_mgr_class_init (ModestWindowMgrClass *klass)
134 {
135         GObjectClass *gobject_class;
136         gobject_class = (GObjectClass*) klass;
137
138         parent_class            = g_type_class_peek_parent (klass);
139         gobject_class->finalize = modest_window_mgr_finalize;
140
141         g_type_class_add_private (gobject_class, sizeof(ModestWindowMgrPrivate));
142
143
144         /**
145          * ModestWindowMgr::window-list-empty
146          * @self: the #ModestWindowMgr that emits the signal
147          * @user_data: user data set when the signal handler was connected
148          *
149          * Issued whenever the window list becomes empty
150          */
151         signals[WINDOW_LIST_EMPTY_SIGNAL] =
152                 g_signal_new ("window-list-empty",
153                               G_TYPE_FROM_CLASS (gobject_class),
154                               G_SIGNAL_RUN_FIRST,
155                               G_STRUCT_OFFSET (ModestWindowMgrClass, window_list_empty),
156                               NULL, NULL,
157                               g_cclosure_marshal_VOID__VOID,
158                               G_TYPE_NONE, 0);
159 }
160
161 static gboolean
162 idle_load_view (ModestWindowMgr *mgr)
163 {
164         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (mgr);
165         
166         priv->cached_view = g_object_new (MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
167         priv->idle_load_view_id = 0;
168         return FALSE;
169 }
170
171 static gboolean
172 idle_load_editor (ModestWindowMgr *mgr)
173 {
174         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (mgr);
175         
176         priv->cached_editor = g_object_new (MODEST_TYPE_MSG_EDIT_WINDOW, NULL);
177         priv->idle_load_editor_id = 0;
178         return FALSE;
179 }
180
181 static void
182 load_new_view (ModestWindowMgr *self)
183 {
184         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
185         if ((priv->cached_view == NULL) && (priv->idle_load_view_id == 0))
186                 priv->idle_load_view_id = g_idle_add ((GSourceFunc) idle_load_view, self);
187 }
188
189 static void
190 load_new_editor (ModestWindowMgr *self)
191 {
192         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
193         if ((priv->cached_editor == NULL) && (priv->idle_load_editor_id == 0))
194                 priv->idle_load_editor_id = g_idle_add ((GSourceFunc) idle_load_editor, self);
195 }
196
197 static void
198 modest_window_mgr_init (ModestWindowMgr *obj)
199 {
200         ModestWindowMgrPrivate *priv;
201
202         priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
203         priv->window_list = NULL;
204         priv->banner_counter = 0;
205         priv->main_window = NULL;
206         priv->fullscreen_mode = FALSE;
207         priv->current_top = NULL;
208         priv->window_state_uids = NULL;
209
210         priv->modal_windows = g_queue_new ();
211         priv->queue_lock = g_mutex_new ();
212         
213         priv->preregistered_uids = NULL;
214
215         /* Could not initialize it from gconf, singletons are not
216            ready yet */
217         priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);   
218         priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
219
220         priv->closing_time = 0;
221
222         priv->modal_handler_uids = NULL;
223         priv->cached_view = NULL;
224         priv->cached_editor = NULL;
225 }
226
227 static void
228 modest_window_mgr_finalize (GObject *obj)
229 {
230         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
231
232         if (priv->idle_load_view_id > 0) {
233                 g_source_remove (priv->idle_load_view_id);
234                 priv->idle_load_view_id = 0;
235         }
236         
237         if (priv->idle_load_editor_id > 0) {
238                 g_source_remove (priv->idle_load_editor_id);
239                 priv->idle_load_editor_id = 0;
240         }
241         
242         if (priv->cached_view) {
243                 gtk_widget_destroy (priv->cached_view);
244                 priv->cached_view = NULL;
245         }
246         if (priv->cached_editor) {
247                 gtk_widget_destroy (priv->cached_editor);
248                 priv->cached_editor = NULL;
249         }
250
251         modest_signal_mgr_disconnect_all_and_destroy (priv->window_state_uids);
252         priv->window_state_uids = NULL;
253
254         if (priv->window_list) {
255                 GList *iter = priv->window_list;
256                 /* unregister pending windows */
257                 while (iter) {
258                         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), 
259                                                              MODEST_WINDOW (iter->data));
260                         iter = g_list_next (iter);
261                 }
262                 g_list_free (priv->window_list);
263                 priv->window_list = NULL;
264         }
265
266         g_slist_foreach (priv->preregistered_uids, (GFunc)g_free, NULL);
267         g_slist_free (priv->preregistered_uids);
268
269         
270         /* Free the hash table with the handlers */
271         if (priv->destroy_handlers) {
272                 g_hash_table_destroy (priv->destroy_handlers);
273                 priv->destroy_handlers = NULL;
274         }
275
276         if (priv->viewer_handlers) {
277                 g_hash_table_destroy (priv->viewer_handlers);
278                 priv->viewer_handlers = NULL;
279         }
280
281         modest_signal_mgr_disconnect_all_and_destroy (priv->modal_handler_uids);
282         priv->modal_handler_uids = NULL;
283
284         if (priv->modal_windows) {
285                 g_mutex_lock (priv->queue_lock);
286                 g_queue_free (priv->modal_windows);
287                 priv->modal_windows = NULL;
288                 g_mutex_unlock (priv->queue_lock);
289         }
290         g_mutex_free (priv->queue_lock);
291         
292         /* Do not unref priv->main_window because it does not hold a
293            new reference */
294
295         
296         G_OBJECT_CLASS(parent_class)->finalize (obj);
297 }
298
299 ModestWindowMgr*
300 modest_window_mgr_new (void)
301 {
302         return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_WINDOW_MGR, NULL));
303 }
304
305
306
307
308 /* do we have uid? */
309 static gboolean
310 has_uid (GSList *list, const gchar *uid)
311 {
312         GSList *cursor = list;
313
314         if (!uid)
315                 return FALSE;
316         
317         while (cursor) {
318                 if (cursor->data && strcmp (cursor->data, uid) == 0)
319                         return TRUE;
320                 cursor = g_slist_next (cursor);
321         }
322         return FALSE;
323 }
324
325
326 /* remove all from the list have have uid = uid */
327 static GSList*
328 remove_uid (GSList *list, const gchar *uid)
329 {
330         GSList *cursor = list, *start = list;
331         
332         if (!uid)
333                 return FALSE;
334         
335         while (cursor) {
336                 GSList *next = g_slist_next (cursor);
337                 if (cursor->data && strcmp (cursor->data, uid) == 0) {
338                         g_free (cursor->data);
339                         start = g_slist_delete_link (start, cursor);
340                 }
341                 cursor = next;
342         }
343         return start;
344 }
345
346
347 static GSList *
348 append_uid (GSList *list, const gchar *uid)
349 {
350         return g_slist_append (list, g_strdup(uid));
351 }
352
353
354
355 void 
356 modest_window_mgr_register_header (ModestWindowMgr *self,  TnyHeader *header, const gchar *alt_uid)
357 {
358         ModestWindowMgrPrivate *priv;
359         gchar* uid;
360         
361         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
362         g_return_if_fail (TNY_IS_HEADER(header));
363                 
364         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
365         uid = modest_tny_folder_get_header_unique_id (header);
366
367         if (uid == NULL)
368                 uid = g_strdup (alt_uid);
369         
370         if (!has_uid (priv->preregistered_uids, uid)) {
371                 MODEST_DEBUG_BLOCK(g_debug ("registering new uid %s", uid););
372                 priv->preregistered_uids = append_uid (priv->preregistered_uids, uid);
373         } else
374                 MODEST_DEBUG_BLOCK(g_debug ("already had uid %s", uid););
375         
376         g_free (uid);
377 }
378
379 void 
380 modest_window_mgr_unregister_header (ModestWindowMgr *self,  TnyHeader *header)
381 {
382         ModestWindowMgrPrivate *priv;
383         gchar* uid;
384         
385         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
386         g_return_if_fail (TNY_IS_HEADER(header));
387                 
388         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
389         uid = modest_tny_folder_get_header_unique_id (header);
390
391         if (!has_uid (priv->preregistered_uids, uid)) {
392                 MODEST_DEBUG_BLOCK(g_debug ("trying to unregister non-existing uid %s", uid););
393                 priv->preregistered_uids = append_uid (priv->preregistered_uids, uid);
394         } else
395                 MODEST_DEBUG_BLOCK(g_debug ("unregistering uid %s", uid););
396         
397         if (has_uid (priv->preregistered_uids, uid)) {
398                 priv->preregistered_uids = remove_uid (priv->preregistered_uids, uid);
399                 if (has_uid (priv->preregistered_uids, uid))
400                         g_debug ("BUG: uid %s NOT removed", uid);
401                 else
402                         MODEST_DEBUG_BLOCK(g_debug ("uid %s removed", uid););
403         }
404                 
405         g_free (uid);
406 }
407
408
409 #define MODEST_WINDOW_HELP_ID_PARAM "help-id"
410
411 void
412 modest_window_mgr_register_help_id (ModestWindowMgr *self, GtkWindow *win, const gchar* help_id)
413 {
414         /* we don't need 'self', but for API consistency... */
415         g_return_if_fail (self && MODEST_IS_WINDOW_MGR(self));
416
417         g_return_if_fail (win && GTK_IS_WINDOW(win));
418         g_return_if_fail (help_id);
419         
420         g_object_set_data_full (G_OBJECT(win), MODEST_WINDOW_HELP_ID_PARAM,
421                                 g_strdup(help_id), g_free);
422 }
423
424
425 const gchar*
426 modest_window_mgr_get_help_id (ModestWindowMgr *self, GtkWindow *win)
427 {
428         const gchar* help_id = NULL;
429
430         /* we don't need 'self', but for API consistency... */
431         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), NULL);
432         
433         g_return_val_if_fail (win, NULL);
434         g_return_val_if_fail (GTK_IS_WINDOW(win), NULL);
435         
436         if (MODEST_IS_MAIN_WINDOW (win)) {
437                 GtkWidget *folder_view;
438                 TnyFolderStore *folder_store;
439                 
440                 /* Get selected folder */
441                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
442                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
443                 folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
444
445                 /* Switch help_id */
446                 if (folder_store && TNY_IS_FOLDER (folder_store)) {
447                         help_id = modest_tny_folder_get_help_id (TNY_FOLDER (folder_store));
448                         if (!help_id)
449                                 g_warning ("%s: BUG: did not get a valid help_id", __FUNCTION__);
450                 }
451                 if (folder_store)
452                         g_object_unref (folder_store);
453         }
454
455         if (!help_id)
456                 help_id = g_object_get_data (G_OBJECT(win), MODEST_WINDOW_HELP_ID_PARAM);
457                 
458         return help_id;
459 }
460
461 static gint
462 compare_msguids (ModestWindow *win,
463                  const gchar *uid)
464 {
465         const gchar *msg_uid;
466
467         if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
468                 return 1;
469
470         /* Get message uid from msg window */
471         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
472                 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
473                 if (msg_uid && uid &&!strcmp (msg_uid, uid))
474                         return 0;
475         } else {
476                 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
477         }
478         
479         if (msg_uid && uid &&!strcmp (msg_uid, uid))
480                 return 0;
481         else
482                 return 1;
483 }
484
485 void
486 modest_window_mgr_close_all_windows (ModestWindowMgr *self)
487 {
488         ModestWindowMgrPrivate *priv = NULL;
489         gboolean ret_value = FALSE;
490         
491         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
492         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
493         
494         /* If there is a main window then try to close it, and it will
495            close the others if needed */
496         if (priv->main_window) {
497                 g_signal_emit_by_name (priv->main_window, "delete-event", NULL, &ret_value);
498         } else {
499                 GList *wins = NULL, *next = NULL;
500
501                 /* delete-event handler actually removes window_list item, */
502                 wins = priv->window_list;
503                 while (wins) {
504                         next = g_list_next (wins);
505                         g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
506                         wins = next;
507                 }
508         }
509 }
510
511
512 gboolean
513 modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
514                                           ModestWindow **win)
515 {
516         ModestWindowMgrPrivate *priv = NULL;
517         gchar* uid = NULL;
518         gboolean has_header, has_window = FALSE;
519         GList *item = NULL;
520
521         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
522         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
523         
524         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
525
526         uid = modest_tny_folder_get_header_unique_id (header);
527         
528         if (win)
529                 *win = NULL;
530
531         has_header = has_uid (priv->preregistered_uids, uid);
532         
533         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
534         if (item) {
535                 has_window = TRUE;
536                 if (win) {
537                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
538                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
539                                 g_debug ("not a valid window!");
540                         else {
541                                 g_debug ("found a window");
542                                 *win = MODEST_WINDOW (item->data);
543                         }
544                 }
545         }
546         g_free (uid);
547         
548         return has_header || has_window;
549 }
550
551 static const gchar *
552 get_show_toolbar_key (GType window_type,
553                       gboolean fullscreen)
554 {
555         const gchar *key = NULL;
556
557         if (window_type == MODEST_TYPE_MAIN_WINDOW)
558                 key = (fullscreen) ? 
559                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
560                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
561         else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
562                 key = (fullscreen) ? 
563                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
564                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
565         else if (window_type ==  MODEST_TYPE_MSG_EDIT_WINDOW)
566                 key = (fullscreen) ? 
567                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
568                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
569         else
570                 g_return_val_if_reached (NULL);
571
572         return key;
573 }
574
575 #ifdef MODEST_PLATFORM_MAEMO
576 static void
577 on_window_is_topmost (GObject    *gobject,
578                       GParamSpec *arg1,
579                       gpointer    user_data)
580 {
581         ModestWindowMgr *self;
582         ModestWindowMgrPrivate *priv;
583         ModestWindow *win = (ModestWindow *) gobject;
584
585         g_return_if_fail (MODEST_IS_WINDOW_MGR (user_data));
586
587         self = MODEST_WINDOW_MGR (user_data);
588         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
589
590         if (hildon_window_get_is_topmost (HILDON_WINDOW (win)))
591                 priv->current_top = win;
592 }
593
594 #else
595 static gboolean
596 on_window_state_event (GtkWidget           *widget,
597                        GdkEventWindowState *event,
598                        gpointer             user_data)
599 {
600         ModestWindowMgr *self;
601         ModestWindowMgrPrivate *priv;
602
603         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (user_data), FALSE);
604
605         self = MODEST_WINDOW_MGR (user_data);
606         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
607
608         MODEST_DEBUG_BLOCK (
609                             if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
610                                     g_print ("GDK_WINDOW_STATE_WITHDRAWN\n");
611                             if (event->changed_mask & GDK_WINDOW_STATE_ICONIFIED)
612                                     g_print ("GDK_WINDOW_STATE_ICONIFIED\n");
613                             if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED)
614                                     g_print ("GDK_WINDOW_STATE_MAXIMIZED\n");
615                             if (event->changed_mask & GDK_WINDOW_STATE_STICKY)
616                                     g_print ("GDK_WINDOW_STATE_STICKY\n");
617                             if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
618                                     g_print ("GDK_WINDOW_STATE_FULLSCREEN\n");
619                             if (event->changed_mask & GDK_WINDOW_STATE_ABOVE)
620                                     g_print ("GDK_WINDOW_STATE_ABOVE\n");
621                             if (event->changed_mask & GDK_WINDOW_STATE_BELOW)
622                                     g_print ("GDK_WINDOW_STATE_BELOW\n");
623                             );
624         if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN ||
625             event->changed_mask & GDK_WINDOW_STATE_ABOVE) {
626                 priv->current_top = MODEST_WINDOW (widget);
627         }
628         
629         return FALSE;
630 }
631 #endif
632
633 void 
634 modest_window_mgr_register_window (ModestWindowMgr *self, 
635                                    ModestWindow *window)
636 {
637         GList *win;
638         ModestWindowMgrPrivate *priv;
639         gint *handler_id;
640         const gchar *key;
641
642         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
643         g_return_if_fail (GTK_IS_WINDOW (window));
644
645         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
646
647         win = g_list_find (priv->window_list, window);
648         if (win) {
649                 /* this is for the case we want to register the window and it was already
650                  * registered. We leave silently.
651                  */
652                 return;
653         }
654         
655         /* Check that it's not a second main window */
656         if (MODEST_IS_MAIN_WINDOW (window)) {
657                 if (priv->main_window) {
658                         g_warning ("%s: trying to register a second main window",
659                                    __FUNCTION__);
660                         return;
661                 } else {
662                         priv->main_window = window;
663                         load_new_view (self);
664                         load_new_editor (self);
665                 }
666         }
667
668         /* remove from the list of pre-registered uids */
669         if (MODEST_IS_MSG_VIEW_WINDOW(window)) {
670                 const gchar *uid = modest_msg_view_window_get_message_uid
671                         (MODEST_MSG_VIEW_WINDOW (window));
672
673                 if (!has_uid (priv->preregistered_uids, uid)) 
674                         g_debug ("weird: no uid for window (%s)", uid);
675                 
676                 MODEST_DEBUG_BLOCK(g_debug ("registering window for %s", uid ? uid : "<none>"););
677                 
678                 priv->preregistered_uids = 
679                         remove_uid (priv->preregistered_uids,
680                                     modest_msg_view_window_get_message_uid
681                                     (MODEST_MSG_VIEW_WINDOW (window)));
682         } else if (MODEST_IS_MSG_EDIT_WINDOW(window)) {
683                 const gchar *uid = modest_msg_edit_window_get_message_uid
684                         (MODEST_MSG_EDIT_WINDOW (window));
685                 
686                 MODEST_DEBUG_BLOCK(g_debug ("registering window for %s", uid););
687
688                 priv->preregistered_uids = 
689                         remove_uid (priv->preregistered_uids,
690                                     modest_msg_edit_window_get_message_uid
691                                     (MODEST_MSG_EDIT_WINDOW (window)));
692         }
693         
694         /* Add to list. Keep a reference to the window */
695         g_object_ref (window);
696         priv->window_list = g_list_prepend (priv->window_list, window);
697
698         /* Listen to window state changes. Unfortunately
699            window-state-event does not properly work for the Maemo
700            version, so we need to use is-topmost and the ifdef */
701 #ifdef MODEST_PLATFORM_MAEMO
702         priv->window_state_uids = 
703                 modest_signal_mgr_connect (priv->window_state_uids, 
704                                            G_OBJECT (window), 
705                                            "notify::is-topmost",
706                                            G_CALLBACK (on_window_is_topmost), 
707                                            self);
708 #else
709         priv->window_state_uids = 
710                 modest_signal_mgr_connect (priv->window_state_uids, 
711                                            G_OBJECT (window), 
712                                            "window-state-event",
713                                            G_CALLBACK (on_window_state_event), 
714                                            self);
715 #endif
716         /* Listen to object destruction */
717         handler_id = g_malloc0 (sizeof (gint));
718         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
719         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
720
721         /* If there is a msg view window, let the main window listen the msg-changed signal */
722         if (MODEST_IS_MSG_VIEW_WINDOW(window) && priv->main_window) {
723                 gulong *handler;
724                 handler = g_malloc0 (sizeof (gulong));
725                 *handler = g_signal_connect (window, "msg-changed", 
726                                              G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed), 
727                                              priv->main_window);
728                 g_hash_table_insert (priv->viewer_handlers, window, handler);
729         }
730
731         /* Put into fullscreen if needed */
732         if (priv->fullscreen_mode)
733                 gtk_window_fullscreen (GTK_WINDOW (window));
734
735         /* Show/hide toolbar & fullscreen */    
736         key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
737         modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
738 }
739
740 static void
741 cancel_window_operations (ModestWindow *window)
742 {
743         GSList* pending_ops = NULL;
744
745         /* cancel open and receive operations */
746         pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (), 
747                                                                  G_OBJECT (window));
748         while (pending_ops != NULL) {
749                 ModestMailOperationTypeOperation type;
750                 GSList* tmp_list = NULL;
751
752                 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
753                 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
754                         modest_mail_operation_cancel (pending_ops->data);
755                 }
756                 g_object_unref (G_OBJECT (pending_ops->data));
757                 tmp_list = pending_ops;
758                 pending_ops = g_slist_next (pending_ops);
759                 g_slist_free_1 (tmp_list);
760         }
761 }
762
763
764
765 static gboolean
766 on_window_destroy (ModestWindow *window, 
767                    GdkEvent *event,
768                    ModestWindowMgr *self)
769 {
770         gint dialog_response = GTK_RESPONSE_NONE;
771         gboolean no_propagate = FALSE;
772
773         /* Specific stuff first */
774         if (MODEST_IS_MAIN_WINDOW (window)) {
775                 ModestWindowMgrPrivate *priv;
776                 priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
777
778                 /* If more than one window already opened */
779                 if (g_list_length (priv->window_list) > 1) {
780
781                         /* Present the window if it's not visible now */
782                         if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
783                                 gtk_window_present (GTK_WINDOW (window));
784                         /* Create the confirmation dialog MSG-NOT308 */
785                         dialog_response = modest_platform_run_confirmation_dialog (
786                                         GTK_WINDOW (window), _("emev_nc_close_windows"));
787
788                         /* If the user wants to close all the windows */
789                         if ((dialog_response == GTK_RESPONSE_OK) 
790                             || (dialog_response == GTK_RESPONSE_ACCEPT) 
791                             || (dialog_response == GTK_RESPONSE_YES)) {
792                                         GList *iter = priv->window_list;
793                                         do {
794                                                 if (iter->data != window) {
795                                                         GList *tmp = iter->next;
796                                                         on_window_destroy (MODEST_WINDOW (iter->data),
797                                                                         event,
798                                                                         self);
799                                                         iter = tmp;
800                                                 } else {
801                                                         iter = g_list_next (iter);
802                                                 }
803                                         } while (iter);
804                         } else {
805                                 return TRUE;
806                         }
807                 }
808
809                 /* Do not unregister it, just hide */
810                 gtk_widget_hide_all (GTK_WIDGET (window));
811
812                 /* Cancel pending operations */
813                 cancel_window_operations (window);
814
815                 /* Fake the window system, make it think that there is no window */
816                 if (modest_window_mgr_num_windows (self) == 0)
817                         g_signal_emit (self, signals[WINDOW_LIST_EMPTY_SIGNAL], 0);
818
819                 no_propagate = TRUE;
820         }
821         else {
822                 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
823                         gboolean sent = FALSE;
824                         gint response = GTK_RESPONSE_ACCEPT;
825                         sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
826                         /* Save currently edited message to Drafts if it was not sent */
827                         if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
828
829                                 /* Raise the window if it's minimized */
830                                 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
831                                         gtk_window_present (GTK_WINDOW (window));
832                                 
833                                 response =
834                                         modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
835                                                                                  _("mcen_nc_no_email_message_modified_save_changes"));
836                                 /* Save to drafts */
837                                 if (response == GTK_RESPONSE_OK)
838                                         if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
839                                                 return TRUE;
840                         }
841                 }
842                 /* Unregister window */
843                 modest_window_mgr_unregister_window (self, window);
844                 no_propagate = FALSE;
845         }
846
847         return no_propagate;
848 }
849
850 static void
851 disconnect_msg_changed (gpointer key, 
852                         gpointer value, 
853                         gpointer user_data)
854 {
855         guint handler_id;
856         handler_id = GPOINTER_TO_UINT(value);
857         
858         if (key && G_IS_OBJECT(key))
859                 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
860 }
861
862 void 
863 modest_window_mgr_unregister_window (ModestWindowMgr *self, 
864                                      ModestWindow *window)
865 {
866         GList *win;
867         ModestWindowMgrPrivate *priv;
868         gulong *tmp, handler_id;
869
870         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
871         g_return_if_fail (MODEST_IS_WINDOW (window));
872
873         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
874
875         win = g_list_find (priv->window_list, window);
876         if (!win) {
877                 g_warning ("Trying to unregister a window that has not being registered yet");
878                 return;
879         }
880
881         /* If it's the main window unset it */
882         if (priv->main_window == window) {
883                 priv->main_window = NULL;
884
885                 /* Disconnect all emissions of msg-changed */
886                 if (priv->viewer_handlers) {
887                         g_hash_table_foreach (priv->viewer_handlers, 
888                                               disconnect_msg_changed, 
889                                               NULL);
890                         g_hash_table_destroy (priv->viewer_handlers);
891                         priv->viewer_handlers = NULL;
892                 }
893         }
894
895         /* Remove the viewer window handler from the hash table. The
896            HashTable could not exist if the main window was closed
897            when there were other windows remaining */
898         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
899                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
900                 /* If the viewer was created without a main window
901                    (for example when opening a message through D-Bus
902                    the viewer handlers was not registered */
903                 if (tmp) {
904                         g_signal_handler_disconnect (window, *tmp);
905                         g_hash_table_remove (priv->viewer_handlers, window);
906                 }
907         }
908
909         /* Save state */
910         modest_window_save_state (window);
911
912         /* Remove from list & hash table */
913         priv->window_list = g_list_remove_link (priv->window_list, win);
914         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
915         handler_id = *tmp;
916         g_hash_table_remove (priv->destroy_handlers, window);
917
918         /* cancel open and receive operations */
919         cancel_window_operations (window);
920
921         /* Check if it's the topmost window, and remove the window from the stack.
922          * This is needed for the cases there's no other topmost window that will
923          * replace it in topmost handler.
924          */
925          if (window == priv->current_top)
926                  priv->current_top = NULL;
927
928         /* Disconnect the "window-state-event" handler, we won't need it anymore */
929         if (priv->window_state_uids) {
930 #ifdef MODEST_PLATFORM_MAEMO
931                 priv->window_state_uids = 
932                         modest_signal_mgr_disconnect (priv->window_state_uids, 
933                                                       G_OBJECT (window), 
934                                                       "notify::is-topmost");
935 #else
936                 priv->window_state_uids = 
937                         modest_signal_mgr_disconnect (priv->window_state_uids, 
938                                                       G_OBJECT (window), 
939                                                       "window-state-event");
940 #endif
941         }
942         
943         /* Disconnect the "delete-event" handler, we won't need it anymore */
944         g_signal_handler_disconnect (window, handler_id);
945
946         /* Disconnect all the window signals */
947         modest_window_disconnect_signals (window);
948         
949         /* Destroy the window */
950         gtk_widget_destroy (win->data);
951         
952         /* If there are no more windows registered emit the signal */
953         if (modest_window_mgr_num_windows (self) == 0)
954                 g_signal_emit (self, signals[WINDOW_LIST_EMPTY_SIGNAL], 0);
955 }
956
957
958
959 void
960 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
961                                        gboolean on)
962 {
963         ModestWindowMgrPrivate *priv;
964         GList *win = NULL;
965         ModestConf *conf;
966
967         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
968
969         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
970
971         /* If there is no change do nothing */
972         if (priv->fullscreen_mode == on)
973                 return;
974
975         priv->fullscreen_mode = on;
976
977         conf = modest_runtime_get_conf ();
978
979         /* Update windows */
980         win = priv->window_list;
981         while (win) {
982                 gboolean show;
983                 const gchar *key = NULL;
984
985                 /* Getting this from gconf everytime is not that
986                    expensive, we'll do it just a few times */
987                 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
988                 show = modest_conf_get_bool (conf, key, NULL);
989
990                 /* Set fullscreen/unfullscreen */
991                 if (on)
992                         gtk_window_fullscreen (GTK_WINDOW (win->data));
993                 else
994                         gtk_window_unfullscreen (GTK_WINDOW (win->data));
995
996                 /* Show/Hide toolbar */
997                 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
998
999                 win = g_list_next (win);
1000         }
1001 }
1002
1003 gboolean
1004 modest_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
1005 {
1006         ModestWindowMgrPrivate *priv;
1007
1008         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
1009
1010         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1011
1012         return priv->fullscreen_mode;
1013 }
1014
1015 void 
1016 modest_window_mgr_show_toolbars (ModestWindowMgr *self,
1017                                  GType window_type,
1018                                  gboolean show_toolbars,
1019                                  gboolean fullscreen)
1020 {
1021         ModestWindowMgrPrivate *priv;
1022         ModestConf *conf;
1023         const gchar *key = NULL;
1024
1025         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1026
1027         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1028         conf = modest_runtime_get_conf ();
1029
1030         /* If nothing changes then return */
1031         key = get_show_toolbar_key (window_type, fullscreen);
1032         conf = modest_runtime_get_conf ();
1033         if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
1034                 return;
1035
1036         /* Save in conf */
1037         modest_conf_set_bool (conf, key, show_toolbars, NULL);
1038
1039         /* Apply now if the view mode is the right one */
1040         if ((fullscreen && priv->fullscreen_mode) ||
1041             (!fullscreen && !priv->fullscreen_mode)) {
1042
1043                 GList *win = priv->window_list;
1044
1045                 while (win) {
1046                         if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
1047                                 modest_window_show_toolbar (MODEST_WINDOW (win->data),
1048                                                             show_toolbars);
1049                         win = g_list_next (win);
1050                 }
1051         }
1052 }
1053
1054 ModestWindow*  
1055 modest_window_mgr_get_main_window (ModestWindowMgr *self, gboolean create)
1056 {
1057         ModestWindowMgrPrivate *priv;
1058         
1059         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
1060         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1061         
1062         /* create the main window, if it hasn't been created yet */
1063         if (!priv->main_window && create) {
1064                 /* modest_window_mgr_register_window will set priv->main_window */
1065                 modest_window_mgr_register_window (self, modest_main_window_new ());
1066                 MODEST_DEBUG_BLOCK(
1067                         g_debug ("%s: created main window: %p\n", __FUNCTION__, priv->main_window);
1068                 );
1069         }
1070         
1071         return priv->main_window;
1072 }
1073
1074
1075 gboolean
1076 modest_window_mgr_main_window_exists  (ModestWindowMgr *self)
1077 {
1078         ModestWindowMgrPrivate *priv;
1079         
1080         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
1081         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1082
1083         return priv->main_window != NULL;
1084 }
1085
1086
1087 GtkWindow *
1088 modest_window_mgr_get_modal (ModestWindowMgr *self)
1089 {
1090         ModestWindowMgrPrivate *priv;
1091         
1092         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
1093         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1094
1095         return g_queue_peek_head (priv->modal_windows);
1096 }
1097
1098
1099 void
1100 modest_window_mgr_set_modal (ModestWindowMgr *self, 
1101                              GtkWindow *window)
1102 {
1103         GtkWindow *old_modal;
1104         ModestWindowMgrPrivate *priv;
1105
1106         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1107         g_return_if_fail (GTK_IS_WINDOW (window));
1108
1109         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1110         g_mutex_lock (priv->queue_lock);
1111         old_modal = g_queue_peek_head (priv->modal_windows);
1112         g_mutex_unlock (priv->queue_lock);
1113
1114         if (!old_modal) {       
1115                 /* make us transient wrt the main window then */
1116                 if (priv->current_top && ((GtkWindow *) priv->current_top != window)) {
1117                         gtk_window_set_transient_for (window, GTK_WINDOW(priv->current_top));
1118                 } else {
1119                         ModestWindow *main_win = modest_window_mgr_get_main_window (self, FALSE);
1120                         if (GTK_WINDOW(main_win) != window) /* they should not be the same */
1121                                 gtk_window_set_transient_for (window, GTK_WINDOW(main_win));
1122                 }
1123                 gtk_window_set_modal (window, TRUE);
1124         } else {
1125                 /* un-modalize the old one; the one on top should be the
1126                  * modal one */
1127                 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));   
1128                 gtk_window_set_modal (window, TRUE);
1129         }
1130
1131         /* this will be the new modal window */
1132         g_mutex_lock (priv->queue_lock);
1133         g_queue_push_head (priv->modal_windows, window);
1134         g_mutex_unlock (priv->queue_lock);
1135
1136         if (GTK_IS_DIALOG (window)) {
1137                 /* Note that response is not always enough because it
1138                    could be captured and removed easily by dialogs but
1139                    works for most of situations */
1140                 priv->modal_handler_uids = 
1141                         modest_signal_mgr_connect (priv->modal_handler_uids, 
1142                                                    G_OBJECT (window), 
1143                                                    "response",
1144                                                    G_CALLBACK (on_modal_dialog_close), 
1145                                                    self);
1146                 /* We need this as well because dialogs are often
1147                    destroyed with gtk_widget_destroy and this one will
1148                    prevent response from happening */
1149                 priv->modal_handler_uids = 
1150                         modest_signal_mgr_connect (priv->modal_handler_uids, 
1151                                                    G_OBJECT (window), 
1152                                                    "destroy",
1153                                                    G_CALLBACK (on_modal_dialog_destroy),
1154                                                    self);
1155         } else {
1156                 priv->modal_handler_uids = 
1157                         modest_signal_mgr_connect (priv->modal_handler_uids, 
1158                                                    G_OBJECT (window), 
1159                                                    "delete-event",
1160                                                    G_CALLBACK (on_modal_window_close), 
1161                                                    self);
1162         }
1163         /* Destroy width parent */
1164         gtk_window_set_destroy_with_parent (window, TRUE);
1165 }
1166
1167
1168 static void
1169 on_nonhibernating_window_hide(GtkWidget *widget, gpointer user_data)
1170 {
1171         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1172         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1173         
1174         /* Forget this window,
1175          * so hibernation will be allowed again if no windows are remembered: */
1176         priv->windows_that_prevent_hibernation =
1177                 g_slist_remove (priv->windows_that_prevent_hibernation, GTK_WINDOW(widget));
1178 }
1179
1180 static void
1181 on_nonhibernating_window_show(GtkWidget *widget, gpointer user_data)
1182 {
1183         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1184         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1185         
1186         GtkWindow *window = GTK_WINDOW (widget);
1187         
1188         priv->windows_that_prevent_hibernation = 
1189                         g_slist_append (priv->windows_that_prevent_hibernation, window);
1190         
1191         /* Allow hibernation again when the window has been hidden: */
1192         g_signal_connect (window, "hide", 
1193                 G_CALLBACK (on_nonhibernating_window_hide), self);
1194 }
1195
1196 void
1197 modest_window_mgr_prevent_hibernation_while_window_is_shown (ModestWindowMgr *self,
1198                                                                   GtkWindow *window)
1199 {
1200         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1201         
1202         if (GTK_WIDGET_VISIBLE(window)) {
1203                 on_nonhibernating_window_show (GTK_WIDGET (window), self);
1204         } else {
1205                 /* Wait for it to be shown: */
1206                 g_signal_connect (window, "show", 
1207                         G_CALLBACK (on_nonhibernating_window_show), self);      
1208         }
1209 }
1210
1211 gboolean
1212 modest_window_mgr_get_hibernation_is_prevented (ModestWindowMgr *self)
1213 {
1214         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
1215         
1216         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1217         
1218         /* Prevent hibernation if any open windows are currently 
1219          * preventing hibernation: */
1220         return (g_slist_length (priv->windows_that_prevent_hibernation) > 0);
1221 }
1222
1223
1224 void
1225 modest_window_mgr_save_state_for_all_windows (ModestWindowMgr *self)
1226 {
1227         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1228         
1229         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1230
1231         /* Iterate over all windows */
1232         GList *win = priv->window_list;
1233         while (win) {
1234                 ModestWindow *window = MODEST_WINDOW (win->data);
1235                 if (window) {
1236                         /* This calls the vfunc, 
1237                          * so each window can do its own thing: */
1238                         modest_window_save_state (window);
1239                 }       
1240                 
1241                 win = g_list_next (win);
1242         }
1243 }
1244
1245 static gboolean
1246 idle_top_modal (gpointer data)
1247 {
1248         ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
1249         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1250         GtkWindow *topmost;
1251
1252         /* Get the top modal */
1253         g_mutex_lock (priv->queue_lock);
1254         topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
1255         g_mutex_unlock (priv->queue_lock);
1256
1257         /* Show it */
1258         if (topmost) {
1259                 gdk_threads_enter ();
1260                 gtk_window_present (topmost);
1261                 gdk_threads_leave ();
1262         }
1263
1264         return FALSE;
1265 }
1266
1267 static void
1268 remove_modal_from_queue (GtkWidget *widget,
1269                          ModestWindowMgr *self)
1270 {
1271         ModestWindowMgrPrivate *priv;
1272         GList *item = NULL;
1273
1274         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1275
1276         /* Remove from queue. We don't use remove, because we want to
1277            exit if the widget does not belong to the queue */
1278         g_mutex_lock (priv->queue_lock);
1279         item = g_queue_find (priv->modal_windows, widget);
1280         if (!item) {
1281                 g_warning ("Trying to remove a modal window that is not registered");
1282                 g_mutex_unlock (priv->queue_lock);
1283                 return;
1284         }
1285         g_queue_unlink (priv->modal_windows, item);
1286         g_mutex_unlock (priv->queue_lock);
1287
1288         /* Disconnect handler */
1289         priv->modal_handler_uids =
1290                 modest_signal_mgr_disconnect (priv->modal_handler_uids,
1291                                               G_OBJECT (widget),
1292                                               GTK_IS_DIALOG (widget) ?
1293                                               "response" :
1294                                               "delete-event");
1295         if (GTK_IS_DIALOG (widget))
1296                 priv->modal_handler_uids =
1297                         modest_signal_mgr_disconnect (priv->modal_handler_uids,
1298                                                       G_OBJECT (widget),
1299                                                       "destroy");
1300
1301         /* Schedule the next one for being shown */
1302         g_idle_add (idle_top_modal, self);
1303 }
1304
1305 static gboolean
1306 on_modal_window_close (GtkWidget *widget,
1307                        GdkEvent *event,
1308                        gpointer user_data)
1309 {
1310         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1311
1312         /* Remove modal window from queue */
1313         remove_modal_from_queue (widget, self);
1314
1315         /* Continue */
1316         return FALSE;
1317 }
1318
1319 static void
1320 on_modal_dialog_close (GtkDialog *dialog,
1321                        gint arg1,
1322                        gpointer user_data)
1323 {
1324         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1325
1326         /* Remove modal window from queue. Note that if "destroy"
1327            signal was invoked before the response the window could be
1328            already deleted */
1329         remove_modal_from_queue (GTK_WIDGET (dialog), self);
1330 }
1331
1332 static void
1333 on_modal_dialog_destroy (GtkObject *object,
1334                          gpointer   user_data)
1335 {
1336         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1337
1338         /* Remove modal window from queue */
1339         remove_modal_from_queue (GTK_WIDGET (object), self);
1340 }
1341
1342 gint 
1343 modest_window_mgr_num_windows (ModestWindowMgr *self)
1344 {
1345         ModestWindowMgrPrivate *priv;
1346         gint num_windows = 0;
1347
1348         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), -1);
1349         
1350         priv =  MODEST_WINDOW_MGR_GET_PRIVATE(self);
1351         
1352         if (priv->window_list)
1353                 num_windows = g_list_length (priv->window_list);
1354
1355         /* Do not take into account the main window if it was hidden */
1356         if (priv->main_window && !GTK_WIDGET_VISIBLE (priv->main_window))
1357                 num_windows--;
1358
1359         return num_windows + priv->banner_counter;
1360 }
1361
1362 GtkWidget *   
1363 modest_window_mgr_get_msg_edit_window (ModestWindowMgr *self)
1364 {
1365         GtkWidget *result;
1366         ModestWindowMgrPrivate *priv;
1367
1368         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), NULL);
1369
1370         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
1371                 
1372         if (priv->cached_editor) {
1373                 result = priv->cached_editor;
1374                 priv->cached_editor = NULL;
1375                 load_new_editor (self);
1376         } else {
1377                 result = g_object_new (MODEST_TYPE_MSG_EDIT_WINDOW, NULL);
1378         }
1379
1380         return result;
1381 }
1382
1383 GtkWidget *   
1384 modest_window_mgr_get_msg_view_window (ModestWindowMgr *self)
1385 {
1386         GtkWidget *result;
1387         ModestWindowMgrPrivate *priv;
1388
1389         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), NULL);
1390         
1391         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
1392
1393         if (priv->cached_view) {
1394                 result = priv->cached_view;
1395                 priv->cached_view = NULL;
1396                 load_new_view (self);
1397         } else {
1398                 result = g_object_new (MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
1399         }
1400
1401         return result;
1402 }
1403
1404 void           
1405 modest_window_mgr_register_banner (ModestWindowMgr *self)
1406 {
1407         ModestWindowMgrPrivate *priv;
1408
1409         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1410         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1411
1412         priv->banner_counter++;
1413         
1414 }
1415
1416 void           
1417 modest_window_mgr_unregister_banner (ModestWindowMgr *self)
1418 {
1419         ModestWindowMgrPrivate *priv;
1420
1421         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1422         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1423
1424         priv->banner_counter--;
1425         if (modest_window_mgr_num_windows (self) == 0)
1426                 g_signal_emit (self, signals[WINDOW_LIST_EMPTY_SIGNAL], 0);
1427 }