* Changes in the autotools stuff affecting a lot of platform dependent
[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                         ModestWindow *window = (ModestWindow *) iter->data;
259                         iter = g_list_next (iter);
260                         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), window);
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 list;
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         /* we don't need 'self', but for API consistency... */
429         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR (self), NULL);
430         g_return_val_if_fail (win, NULL);
431
432         return g_object_get_data (G_OBJECT(win), MODEST_WINDOW_HELP_ID_PARAM);
433 }
434
435 static gint
436 compare_msguids (ModestWindow *win,
437                  const gchar *uid)
438 {
439         const gchar *msg_uid;
440
441         if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
442                 return 1;
443
444         /* Get message uid from msg window */
445         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
446                 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
447                 if (msg_uid && uid &&!strcmp (msg_uid, uid))
448                         return 0;
449         } else {
450                 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
451         }
452         
453         if (msg_uid && uid &&!strcmp (msg_uid, uid))
454                 return 0;
455         else
456                 return 1;
457 }
458
459 void
460 modest_window_mgr_close_all_windows (ModestWindowMgr *self)
461 {
462         ModestWindowMgrPrivate *priv = NULL;
463         gboolean ret_value = FALSE;
464         
465         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
466         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
467         
468         /* If there is a main window then try to close it, and it will
469            close the others if needed */
470         if (priv->main_window) {
471                 g_signal_emit_by_name (priv->main_window, "delete-event", NULL, &ret_value);
472         } else {
473                 GList *wins = NULL, *next = NULL;
474
475                 /* delete-event handler actually removes window_list item, */
476                 wins = priv->window_list;
477                 while (wins) {
478                         next = g_list_next (wins);
479                         g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
480                         wins = next;
481                 }
482         }
483 }
484
485
486 gboolean
487 modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
488                                           ModestWindow **win)
489 {
490         ModestWindowMgrPrivate *priv = NULL;
491         gchar* uid = NULL;
492         gboolean has_header, has_window = FALSE;
493         GList *item = NULL;
494
495         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
496         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
497         
498         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
499
500         uid = modest_tny_folder_get_header_unique_id (header);
501         
502         if (win)
503                 *win = NULL;
504
505         has_header = has_uid (priv->preregistered_uids, uid);
506         
507         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
508         if (item) {
509                 has_window = TRUE;
510                 if (win) {
511                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
512                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
513                                 g_debug ("not a valid window!");
514                         else {
515                                 g_debug ("found a window");
516                                 *win = MODEST_WINDOW (item->data);
517                         }
518                 }
519         }
520         g_free (uid);
521         
522         return has_header || has_window;
523 }
524
525 static const gchar *
526 get_show_toolbar_key (GType window_type,
527                       gboolean fullscreen)
528 {
529         const gchar *key = NULL;
530
531         if (window_type == MODEST_TYPE_MAIN_WINDOW)
532                 key = (fullscreen) ? 
533                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
534                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
535         else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
536                 key = (fullscreen) ? 
537                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
538                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
539         else if (window_type ==  MODEST_TYPE_MSG_EDIT_WINDOW)
540                 key = (fullscreen) ? 
541                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
542                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
543         else
544                 g_return_val_if_reached (NULL);
545
546         return key;
547 }
548
549 #ifndef MODEST_TOOLKIT_GTK
550 static void
551 on_window_is_topmost (GObject    *gobject,
552                       GParamSpec *arg1,
553                       gpointer    user_data)
554 {
555         ModestWindowMgr *self;
556         ModestWindowMgrPrivate *priv;
557         ModestWindow *win = (ModestWindow *) gobject;
558
559         g_return_if_fail (MODEST_IS_WINDOW_MGR (user_data));
560
561         self = MODEST_WINDOW_MGR (user_data);
562         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
563
564         if (hildon_window_get_is_topmost (HILDON_WINDOW (win)))
565                 priv->current_top = win;
566 }
567
568 #else
569 static gboolean
570 on_window_state_event (GtkWidget           *widget,
571                        GdkEventWindowState *event,
572                        gpointer             user_data)
573 {
574         ModestWindowMgr *self;
575         ModestWindowMgrPrivate *priv;
576
577         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (user_data), FALSE);
578
579         self = MODEST_WINDOW_MGR (user_data);
580         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
581
582         MODEST_DEBUG_BLOCK (
583                             if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
584                                     g_print ("GDK_WINDOW_STATE_WITHDRAWN\n");
585                             if (event->changed_mask & GDK_WINDOW_STATE_ICONIFIED)
586                                     g_print ("GDK_WINDOW_STATE_ICONIFIED\n");
587                             if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED)
588                                     g_print ("GDK_WINDOW_STATE_MAXIMIZED\n");
589                             if (event->changed_mask & GDK_WINDOW_STATE_STICKY)
590                                     g_print ("GDK_WINDOW_STATE_STICKY\n");
591                             if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
592                                     g_print ("GDK_WINDOW_STATE_FULLSCREEN\n");
593                             if (event->changed_mask & GDK_WINDOW_STATE_ABOVE)
594                                     g_print ("GDK_WINDOW_STATE_ABOVE\n");
595                             if (event->changed_mask & GDK_WINDOW_STATE_BELOW)
596                                     g_print ("GDK_WINDOW_STATE_BELOW\n");
597                             );
598         if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN ||
599             event->changed_mask & GDK_WINDOW_STATE_ABOVE) {
600                 priv->current_top = MODEST_WINDOW (widget);
601         }
602         
603         return FALSE;
604 }
605 #endif
606
607 void 
608 modest_window_mgr_register_window (ModestWindowMgr *self, 
609                                    ModestWindow *window)
610 {
611         GList *win;
612         ModestWindowMgrPrivate *priv;
613         gint *handler_id;
614         const gchar *key;
615
616         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
617         g_return_if_fail (GTK_IS_WINDOW (window));
618
619         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
620
621         win = g_list_find (priv->window_list, window);
622         if (win) {
623                 /* this is for the case we want to register the window and it was already
624                  * registered. We leave silently.
625                  */
626                 return;
627         }
628         
629         /* Check that it's not a second main window */
630         if (MODEST_IS_MAIN_WINDOW (window)) {
631                 if (priv->main_window) {
632                         g_warning ("%s: trying to register a second main window",
633                                    __FUNCTION__);
634                         return;
635                 } else {
636                         priv->main_window = window;
637                         load_new_view (self);
638                         load_new_editor (self);
639                 }
640         }
641
642         /* remove from the list of pre-registered uids */
643         if (MODEST_IS_MSG_VIEW_WINDOW(window)) {
644                 const gchar *uid = modest_msg_view_window_get_message_uid
645                         (MODEST_MSG_VIEW_WINDOW (window));
646
647                 if (!has_uid (priv->preregistered_uids, uid)) 
648                         g_debug ("weird: no uid for window (%s)", uid);
649                 
650                 MODEST_DEBUG_BLOCK(g_debug ("registering window for %s", uid ? uid : "<none>"););
651                 
652                 priv->preregistered_uids = 
653                         remove_uid (priv->preregistered_uids,
654                                     modest_msg_view_window_get_message_uid
655                                     (MODEST_MSG_VIEW_WINDOW (window)));
656         } else if (MODEST_IS_MSG_EDIT_WINDOW(window)) {
657                 const gchar *uid = modest_msg_edit_window_get_message_uid
658                         (MODEST_MSG_EDIT_WINDOW (window));
659                 
660                 MODEST_DEBUG_BLOCK(g_debug ("registering window for %s", uid););
661
662                 priv->preregistered_uids = 
663                         remove_uid (priv->preregistered_uids,
664                                     modest_msg_edit_window_get_message_uid
665                                     (MODEST_MSG_EDIT_WINDOW (window)));
666         }
667         
668         /* Add to list. Keep a reference to the window */
669         g_object_ref (window);
670         priv->window_list = g_list_prepend (priv->window_list, window);
671
672         /* Listen to window state changes. Unfortunately
673            window-state-event does not properly work for the Maemo
674            version, so we need to use is-topmost and the ifdef */
675 #ifndef MODEST_TOOLKIT_GTK
676         priv->window_state_uids = 
677                 modest_signal_mgr_connect (priv->window_state_uids, 
678                                            G_OBJECT (window), 
679                                            "notify::is-topmost",
680                                            G_CALLBACK (on_window_is_topmost), 
681                                            self);
682 #else
683         priv->window_state_uids = 
684                 modest_signal_mgr_connect (priv->window_state_uids, 
685                                            G_OBJECT (window), 
686                                            "window-state-event",
687                                            G_CALLBACK (on_window_state_event), 
688                                            self);
689 #endif
690         /* Listen to object destruction */
691         handler_id = g_malloc0 (sizeof (gint));
692         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
693         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
694
695         /* If there is a msg view window, let the main window listen the msg-changed signal */
696         if (MODEST_IS_MSG_VIEW_WINDOW(window) && priv->main_window) {
697                 gulong *handler;
698                 handler = g_malloc0 (sizeof (gulong));
699                 *handler = g_signal_connect (window, "msg-changed", 
700                                              G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed), 
701                                              priv->main_window);
702                 g_hash_table_insert (priv->viewer_handlers, window, handler);
703         }
704
705         /* Put into fullscreen if needed */
706         if (priv->fullscreen_mode)
707                 gtk_window_fullscreen (GTK_WINDOW (window));
708
709         /* Show/hide toolbar & fullscreen */    
710         key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
711         modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
712 }
713
714 static void
715 cancel_window_operations (ModestWindow *window)
716 {
717         GSList* pending_ops = NULL;
718
719         /* cancel open and receive operations */
720         pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (), 
721                                                                  G_OBJECT (window));
722         while (pending_ops != NULL) {
723                 ModestMailOperationTypeOperation type;
724                 GSList* tmp_list = NULL;
725
726                 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
727                 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
728                         modest_mail_operation_cancel (pending_ops->data);
729                 }
730                 g_object_unref (G_OBJECT (pending_ops->data));
731                 tmp_list = pending_ops;
732                 pending_ops = g_slist_next (pending_ops);
733                 g_slist_free_1 (tmp_list);
734         }
735 }
736
737
738
739 static gboolean
740 on_window_destroy (ModestWindow *window, 
741                    GdkEvent *event,
742                    ModestWindowMgr *self)
743 {
744         gint dialog_response = GTK_RESPONSE_NONE;
745         gboolean no_propagate = FALSE;
746
747         /* Specific stuff first */
748         if (MODEST_IS_MAIN_WINDOW (window)) {
749                 ModestWindowMgrPrivate *priv;
750                 priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
751
752                 /* If more than one window already opened */
753                 if (g_list_length (priv->window_list) > 1) {
754
755                         /* Present the window if it's not visible now */
756                         if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window))) {
757                                 gtk_window_present (GTK_WINDOW (window));
758                                 priv->current_top = window;
759                         }
760                         /* Create the confirmation dialog MSG-NOT308 */
761                         dialog_response = modest_platform_run_confirmation_dialog (
762                                         GTK_WINDOW (window), _("emev_nc_close_windows"));
763
764                         /* If the user wants to close all the windows */
765                         if ((dialog_response == GTK_RESPONSE_OK) 
766                             || (dialog_response == GTK_RESPONSE_ACCEPT) 
767                             || (dialog_response == GTK_RESPONSE_YES)) {
768                                         GList *iter = priv->window_list;
769                                         do {
770                                                 if (iter->data != window) {
771                                                         GList *tmp = iter->next;
772                                                         on_window_destroy (MODEST_WINDOW (iter->data),
773                                                                         event,
774                                                                         self);
775                                                         iter = tmp;
776                                                 } else {
777                                                         iter = g_list_next (iter);
778                                                 }
779                                         } while (iter);
780                         } else {
781                                 return TRUE;
782                         }
783                 }
784
785                 /* Do not unregister it, just hide */
786                 gtk_widget_hide_all (GTK_WIDGET (window));
787
788                 /* Cancel pending operations */
789                 cancel_window_operations (window);
790
791                 /* Fake the window system, make it think that there is no window */
792                 if (modest_window_mgr_num_windows (self) == 0)
793                         g_signal_emit (self, signals[WINDOW_LIST_EMPTY_SIGNAL], 0);
794
795                 no_propagate = TRUE;
796         }
797         else {
798                 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
799                         gboolean sent = FALSE;
800                         gint response = GTK_RESPONSE_ACCEPT;
801                         sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
802                         /* Save currently edited message to Drafts if it was not sent */
803                         if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
804
805                                 /* Raise the window if it's minimized */
806                                 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
807                                         gtk_window_present (GTK_WINDOW (window));
808                                 
809                                 response =
810                                         modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
811                                                                                  _("mcen_nc_no_email_message_modified_save_changes"));
812                                 /* Save to drafts */
813                                 if (response == GTK_RESPONSE_OK)
814                                         if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
815                                                 return TRUE;
816                         }
817                 }
818                 /* Unregister window */
819                 modest_window_mgr_unregister_window (self, window);
820                 no_propagate = TRUE;
821         }
822
823         return no_propagate;
824 }
825
826 static void
827 disconnect_msg_changed (gpointer key, 
828                         gpointer value, 
829                         gpointer user_data)
830 {
831         guint handler_id;
832         handler_id = GPOINTER_TO_UINT(value);
833         
834         if (key && G_IS_OBJECT(key))
835                 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
836 }
837
838 void 
839 modest_window_mgr_unregister_window (ModestWindowMgr *self, 
840                                      ModestWindow *window)
841 {
842         GList *win;
843         ModestWindowMgrPrivate *priv;
844         gulong *tmp, handler_id;
845
846         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
847         g_return_if_fail (MODEST_IS_WINDOW (window));
848
849         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
850
851         win = g_list_find (priv->window_list, window);
852         if (!win) {
853                 g_warning ("Trying to unregister a window that has not being registered yet");
854                 return;
855         }
856
857         /* If it's the main window unset it */
858         if (priv->main_window == window) {
859                 priv->main_window = NULL;
860
861                 /* Disconnect all emissions of msg-changed */
862                 if (priv->viewer_handlers) {
863                         g_hash_table_foreach (priv->viewer_handlers, 
864                                               disconnect_msg_changed, 
865                                               NULL);
866                         g_hash_table_destroy (priv->viewer_handlers);
867                         priv->viewer_handlers = NULL;
868                 }
869         }
870
871         /* Remove the viewer window handler from the hash table. The
872            HashTable could not exist if the main window was closed
873            when there were other windows remaining */
874         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
875                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
876                 /* If the viewer was created without a main window
877                    (for example when opening a message through D-Bus
878                    the viewer handlers was not registered */
879                 if (tmp) {
880                         g_signal_handler_disconnect (window, *tmp);
881                         g_hash_table_remove (priv->viewer_handlers, window);
882                 }
883         }
884
885         /* Save state */
886         modest_window_save_state (window);
887
888         /* Remove from list & hash table */
889         priv->window_list = g_list_remove_link (priv->window_list, win);
890         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
891         handler_id = *tmp;
892
893         g_hash_table_remove (priv->destroy_handlers, window);
894
895         /* cancel open and receive operations */
896         cancel_window_operations (window);
897
898         /* Check if it's the topmost window, and remove the window from the stack.
899          * This is needed for the cases there's no other topmost window that will
900          * replace it in topmost handler.
901          */
902          if (window == priv->current_top)
903                  priv->current_top = NULL;
904
905         /* Disconnect the "window-state-event" handler, we won't need it anymore */
906         if (priv->window_state_uids) {
907 #ifndef MODEST_TOOLKIT_GTK
908                 priv->window_state_uids = 
909                         modest_signal_mgr_disconnect (priv->window_state_uids, 
910                                                       G_OBJECT (window), 
911                                                       "notify::is-topmost");
912 #else
913                 priv->window_state_uids = 
914                         modest_signal_mgr_disconnect (priv->window_state_uids, 
915                                                       G_OBJECT (window), 
916                                                       "window-state-event");
917 #endif
918         }
919         
920         /* Disconnect the "delete-event" handler, we won't need it anymore */
921         g_signal_handler_disconnect (window, handler_id);
922
923         /* Disconnect all the window signals */
924         modest_window_disconnect_signals (window);
925         
926         /* Destroy the window */
927         g_object_unref (win->data);
928         gtk_widget_destroy (win->data);
929         g_list_free (win);
930         
931         /* If there are no more windows registered emit the signal */
932         if (modest_window_mgr_num_windows (self) == 0)
933                 g_signal_emit (self, signals[WINDOW_LIST_EMPTY_SIGNAL], 0);
934 }
935
936
937
938 void
939 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
940                                        gboolean on)
941 {
942         ModestWindowMgrPrivate *priv;
943         GList *win = NULL;
944         ModestConf *conf;
945
946         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
947
948         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
949
950         /* If there is no change do nothing */
951         if (priv->fullscreen_mode == on)
952                 return;
953
954         priv->fullscreen_mode = on;
955
956         conf = modest_runtime_get_conf ();
957
958         /* Update windows */
959         win = priv->window_list;
960         while (win) {
961                 gboolean show;
962                 const gchar *key = NULL;
963
964                 /* Getting this from gconf everytime is not that
965                    expensive, we'll do it just a few times */
966                 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
967                 show = modest_conf_get_bool (conf, key, NULL);
968
969                 /* Set fullscreen/unfullscreen */
970                 if (on)
971                         gtk_window_fullscreen (GTK_WINDOW (win->data));
972                 else
973                         gtk_window_unfullscreen (GTK_WINDOW (win->data));
974
975                 /* Show/Hide toolbar */
976                 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
977
978                 win = g_list_next (win);
979         }
980 }
981
982 gboolean
983 modest_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
984 {
985         ModestWindowMgrPrivate *priv;
986
987         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
988
989         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
990
991         return priv->fullscreen_mode;
992 }
993
994 void 
995 modest_window_mgr_show_toolbars (ModestWindowMgr *self,
996                                  GType window_type,
997                                  gboolean show_toolbars,
998                                  gboolean fullscreen)
999 {
1000         ModestWindowMgrPrivate *priv;
1001         ModestConf *conf;
1002         const gchar *key = NULL;
1003
1004         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1005
1006         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1007         conf = modest_runtime_get_conf ();
1008
1009         /* If nothing changes then return */
1010         key = get_show_toolbar_key (window_type, fullscreen);
1011         conf = modest_runtime_get_conf ();
1012         if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
1013                 return;
1014
1015         /* Save in conf */
1016         modest_conf_set_bool (conf, key, show_toolbars, NULL);
1017
1018         /* Apply now if the view mode is the right one */
1019         if ((fullscreen && priv->fullscreen_mode) ||
1020             (!fullscreen && !priv->fullscreen_mode)) {
1021
1022                 GList *win = priv->window_list;
1023
1024                 while (win) {
1025                         if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
1026                                 modest_window_show_toolbar (MODEST_WINDOW (win->data),
1027                                                             show_toolbars);
1028                         win = g_list_next (win);
1029                 }
1030         }
1031 }
1032
1033 ModestWindow*  
1034 modest_window_mgr_get_main_window (ModestWindowMgr *self, gboolean create)
1035 {
1036         ModestWindowMgrPrivate *priv;
1037         
1038         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
1039         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1040         
1041         /* create the main window, if it hasn't been created yet */
1042         if (!priv->main_window && create) {
1043                 /* modest_window_mgr_register_window will set priv->main_window */
1044                 modest_window_mgr_register_window (self, modest_main_window_new ());
1045                 MODEST_DEBUG_BLOCK(
1046                         g_debug ("%s: created main window: %p\n", __FUNCTION__, priv->main_window);
1047                 );
1048         }
1049         
1050         return priv->main_window;
1051 }
1052
1053
1054 gboolean
1055 modest_window_mgr_main_window_exists  (ModestWindowMgr *self)
1056 {
1057         ModestWindowMgrPrivate *priv;
1058         
1059         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
1060         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1061
1062         return priv->main_window != NULL;
1063 }
1064
1065
1066 GtkWindow *
1067 modest_window_mgr_get_modal (ModestWindowMgr *self)
1068 {
1069         ModestWindowMgrPrivate *priv;
1070         
1071         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
1072         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1073
1074         return g_queue_peek_head (priv->modal_windows);
1075 }
1076
1077
1078 void
1079 modest_window_mgr_set_modal (ModestWindowMgr *self, 
1080                              GtkWindow *window)
1081 {
1082         GtkWindow *old_modal;
1083         ModestWindowMgrPrivate *priv;
1084
1085         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1086         g_return_if_fail (GTK_IS_WINDOW (window));
1087
1088         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1089         g_mutex_lock (priv->queue_lock);
1090         old_modal = g_queue_peek_head (priv->modal_windows);
1091         g_mutex_unlock (priv->queue_lock);
1092
1093         if (!old_modal) {       
1094                 /* make us transient wrt the main window then */
1095                 if (priv->current_top && ((GtkWindow *) priv->current_top != window)) {
1096                         gtk_window_set_transient_for (window, GTK_WINDOW(priv->current_top));
1097                 } else {
1098                         ModestWindow *main_win = modest_window_mgr_get_main_window (self, FALSE);
1099                         if (GTK_WINDOW(main_win) != window) /* they should not be the same */
1100                                 gtk_window_set_transient_for (window, GTK_WINDOW(main_win));
1101                 }
1102                 gtk_window_set_modal (window, TRUE);
1103         } else {
1104                 /* un-modalize the old one; the one on top should be the
1105                  * modal one */
1106                 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));   
1107                 gtk_window_set_modal (window, TRUE);
1108         }
1109
1110         /* this will be the new modal window */
1111         g_mutex_lock (priv->queue_lock);
1112         g_queue_push_head (priv->modal_windows, window);
1113         g_mutex_unlock (priv->queue_lock);
1114
1115         if (GTK_IS_DIALOG (window)) {
1116                 /* Note that response is not always enough because it
1117                    could be captured and removed easily by dialogs but
1118                    works for most of situations */
1119                 priv->modal_handler_uids = 
1120                         modest_signal_mgr_connect (priv->modal_handler_uids, 
1121                                                    G_OBJECT (window), 
1122                                                    "response",
1123                                                    G_CALLBACK (on_modal_dialog_close), 
1124                                                    self);
1125                 /* We need this as well because dialogs are often
1126                    destroyed with gtk_widget_destroy and this one will
1127                    prevent response from happening */
1128                 priv->modal_handler_uids = 
1129                         modest_signal_mgr_connect (priv->modal_handler_uids, 
1130                                                    G_OBJECT (window), 
1131                                                    "destroy",
1132                                                    G_CALLBACK (on_modal_dialog_destroy),
1133                                                    self);
1134         } else {
1135                 priv->modal_handler_uids = 
1136                         modest_signal_mgr_connect (priv->modal_handler_uids, 
1137                                                    G_OBJECT (window), 
1138                                                    "delete-event",
1139                                                    G_CALLBACK (on_modal_window_close), 
1140                                                    self);
1141         }
1142         /* Destroy width parent */
1143         gtk_window_set_destroy_with_parent (window, TRUE);
1144 }
1145
1146
1147 static void
1148 on_nonhibernating_window_hide(GtkWidget *widget, gpointer user_data)
1149 {
1150         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1151         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1152         
1153         /* Forget this window,
1154          * so hibernation will be allowed again if no windows are remembered: */
1155         priv->windows_that_prevent_hibernation =
1156                 g_slist_remove (priv->windows_that_prevent_hibernation, GTK_WINDOW(widget));
1157 }
1158
1159 static void
1160 on_nonhibernating_window_show(GtkWidget *widget, gpointer user_data)
1161 {
1162         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1163         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1164         
1165         GtkWindow *window = GTK_WINDOW (widget);
1166         
1167         priv->windows_that_prevent_hibernation = 
1168                         g_slist_append (priv->windows_that_prevent_hibernation, window);
1169         
1170         /* Allow hibernation again when the window has been hidden: */
1171         g_signal_connect (window, "hide", 
1172                 G_CALLBACK (on_nonhibernating_window_hide), self);
1173 }
1174
1175 void
1176 modest_window_mgr_prevent_hibernation_while_window_is_shown (ModestWindowMgr *self,
1177                                                                   GtkWindow *window)
1178 {
1179         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1180         
1181         if (GTK_WIDGET_VISIBLE(window)) {
1182                 on_nonhibernating_window_show (GTK_WIDGET (window), self);
1183         } else {
1184                 /* Wait for it to be shown: */
1185                 g_signal_connect (window, "show", 
1186                         G_CALLBACK (on_nonhibernating_window_show), self);      
1187         }
1188 }
1189
1190 gboolean
1191 modest_window_mgr_get_hibernation_is_prevented (ModestWindowMgr *self)
1192 {
1193         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
1194         
1195         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1196         
1197         /* Prevent hibernation if any open windows are currently 
1198          * preventing hibernation: */
1199         return (g_slist_length (priv->windows_that_prevent_hibernation) > 0);
1200 }
1201
1202
1203 void
1204 modest_window_mgr_save_state_for_all_windows (ModestWindowMgr *self)
1205 {
1206         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1207         
1208         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1209
1210         /* Iterate over all windows */
1211         GList *win = priv->window_list;
1212         while (win) {
1213                 ModestWindow *window = MODEST_WINDOW (win->data);
1214                 if (window) {
1215                         /* This calls the vfunc, 
1216                          * so each window can do its own thing: */
1217                         modest_window_save_state (window);
1218                 }       
1219                 
1220                 win = g_list_next (win);
1221         }
1222 }
1223
1224 static gboolean
1225 idle_top_modal (gpointer data)
1226 {
1227         ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
1228         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1229         GtkWindow *topmost;
1230
1231         /* Get the top modal */
1232         g_mutex_lock (priv->queue_lock);
1233         topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
1234         g_mutex_unlock (priv->queue_lock);
1235
1236         /* Show it */
1237         if (topmost) {
1238                 gdk_threads_enter ();
1239                 gtk_window_present (topmost);
1240                 /* It seems that the window looses modality if some
1241                    other is shown on top of it after set_transient_for
1242                    and set_parent */
1243                 gtk_window_set_modal (topmost, TRUE);
1244                 gdk_threads_leave ();
1245         }
1246
1247         return FALSE;
1248 }
1249
1250 static void
1251 remove_modal_from_queue (GtkWidget *widget,
1252                          ModestWindowMgr *self)
1253 {
1254         ModestWindowMgrPrivate *priv;
1255         GList *item = NULL;
1256
1257         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1258
1259         /* Remove from queue. We don't use remove, because we want to
1260            exit if the widget does not belong to the queue */
1261         g_mutex_lock (priv->queue_lock);
1262         item = g_queue_find (priv->modal_windows, widget);
1263         if (!item) {
1264                 g_warning ("Trying to remove a modal window that is not registered");
1265                 g_mutex_unlock (priv->queue_lock);
1266                 return;
1267         }
1268         g_queue_unlink (priv->modal_windows, item);
1269         g_mutex_unlock (priv->queue_lock);
1270
1271         /* Disconnect handler */
1272         priv->modal_handler_uids =
1273                 modest_signal_mgr_disconnect (priv->modal_handler_uids,
1274                                               G_OBJECT (widget),
1275                                               GTK_IS_DIALOG (widget) ?
1276                                               "response" :
1277                                               "delete-event");
1278         if (GTK_IS_DIALOG (widget))
1279                 priv->modal_handler_uids =
1280                         modest_signal_mgr_disconnect (priv->modal_handler_uids,
1281                                                       G_OBJECT (widget),
1282                                                       "destroy");
1283
1284         /* Schedule the next one for being shown */
1285         g_idle_add (idle_top_modal, self);
1286 }
1287
1288 static gboolean
1289 on_modal_window_close (GtkWidget *widget,
1290                        GdkEvent *event,
1291                        gpointer user_data)
1292 {
1293         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1294
1295         /* Remove modal window from queue */
1296         remove_modal_from_queue (widget, self);
1297
1298         /* Continue */
1299         return FALSE;
1300 }
1301
1302 static void
1303 on_modal_dialog_close (GtkDialog *dialog,
1304                        gint arg1,
1305                        gpointer user_data)
1306 {
1307         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1308
1309         /* Remove modal window from queue. Note that if "destroy"
1310            signal was invoked before the response the window could be
1311            already deleted */
1312         remove_modal_from_queue (GTK_WIDGET (dialog), self);
1313 }
1314
1315 static void
1316 on_modal_dialog_destroy (GtkObject *object,
1317                          gpointer   user_data)
1318 {
1319         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1320
1321         /* Remove modal window from queue */
1322         remove_modal_from_queue (GTK_WIDGET (object), self);
1323 }
1324
1325 gint 
1326 modest_window_mgr_num_windows (ModestWindowMgr *self)
1327 {
1328         ModestWindowMgrPrivate *priv;
1329         gint num_windows = 0;
1330
1331         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), -1);
1332         
1333         priv =  MODEST_WINDOW_MGR_GET_PRIVATE(self);
1334         
1335         if (priv->window_list)
1336                 num_windows = g_list_length (priv->window_list);
1337
1338         /* Do not take into account the main window if it was hidden */
1339         if (priv->main_window && !GTK_WIDGET_VISIBLE (priv->main_window))
1340                 num_windows--;
1341
1342         return num_windows + priv->banner_counter;
1343 }
1344
1345 GtkWidget *   
1346 modest_window_mgr_get_msg_edit_window (ModestWindowMgr *self)
1347 {
1348         GtkWidget *result;
1349         ModestWindowMgrPrivate *priv;
1350
1351         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), NULL);
1352
1353         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
1354                 
1355         if (priv->cached_editor) {
1356                 result = priv->cached_editor;
1357                 priv->cached_editor = NULL;
1358                 load_new_editor (self);
1359         } else {
1360                 result = g_object_new (MODEST_TYPE_MSG_EDIT_WINDOW, NULL);
1361         }
1362
1363         return result;
1364 }
1365
1366 GtkWidget *   
1367 modest_window_mgr_get_msg_view_window (ModestWindowMgr *self)
1368 {
1369         GtkWidget *result;
1370         ModestWindowMgrPrivate *priv;
1371
1372         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), NULL);
1373         
1374         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
1375
1376         if (priv->cached_view) {
1377                 result = priv->cached_view;
1378                 priv->cached_view = NULL;
1379                 load_new_view (self);
1380         } else {
1381                 result = g_object_new (MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
1382         }
1383
1384         return result;
1385 }
1386
1387 void           
1388 modest_window_mgr_register_banner (ModestWindowMgr *self)
1389 {
1390         ModestWindowMgrPrivate *priv;
1391
1392         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1393         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1394
1395         priv->banner_counter++;
1396         
1397 }
1398
1399 void           
1400 modest_window_mgr_unregister_banner (ModestWindowMgr *self)
1401 {
1402         ModestWindowMgrPrivate *priv;
1403
1404         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1405         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1406
1407         priv->banner_counter--;
1408         if (modest_window_mgr_num_windows (self) == 0)
1409                 g_signal_emit (self, signals[WINDOW_LIST_EMPTY_SIGNAL], 0);
1410 }