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