6259a39f953ebf58349b2d4bc2560dc0e96012cb
[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         gboolean ret_value = FALSE;
476         
477         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
478         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
479         
480         /* If there is a main window then try to close it, and it will
481            close the others if needed */
482         if (priv->main_window) {
483                 g_signal_emit_by_name (priv->main_window, "delete-event", NULL, &ret_value);
484         } else {
485                 GList *wins = NULL, *next = NULL;
486
487                 /* delete-event handler actually removes window_list item, */
488                 wins = priv->window_list;
489                 while (wins) {
490                         next = g_list_next (wins);
491                         g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
492                         wins = next;
493                 }
494         }
495 }
496
497
498 gboolean
499 modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
500                                           ModestWindow **win)
501 {
502         ModestWindowMgrPrivate *priv = NULL;
503         gchar* uid = NULL;
504         gboolean has_header, has_window = FALSE;
505         GList *item = NULL;
506
507         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
508         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
509         
510         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
511
512         uid = modest_tny_folder_get_header_unique_id (header);
513         
514         if (win)
515                 *win = NULL;
516
517         has_header = has_uid (priv->preregistered_uids, uid);
518         
519         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
520         if (item) {
521                 has_window = TRUE;
522                 if (win) {
523                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
524                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
525                                 g_debug ("not a valid window!");
526                         else {
527                                 g_debug ("found a window");
528                                 *win = MODEST_WINDOW (item->data);
529                         }
530                 }
531         }
532         g_free (uid);
533         
534         return has_header || has_window;
535 }
536
537 static const gchar *
538 get_show_toolbar_key (GType window_type,
539                       gboolean fullscreen)
540 {
541         const gchar *key = NULL;
542
543         if (window_type == MODEST_TYPE_MAIN_WINDOW)
544                 key = (fullscreen) ? 
545                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
546                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
547         else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
548                 key = (fullscreen) ? 
549                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
550                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
551         else if (window_type ==  MODEST_TYPE_MSG_EDIT_WINDOW)
552                 key = (fullscreen) ? 
553                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
554                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
555         else
556                 g_return_val_if_reached (NULL);
557
558         return key;
559 }
560
561 void 
562 modest_window_mgr_register_window (ModestWindowMgr *self, 
563                                    ModestWindow *window)
564 {
565         GList *win;
566         ModestWindowMgrPrivate *priv;
567         gint *handler_id;
568         const gchar *key;
569
570         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
571         g_return_if_fail (GTK_IS_WINDOW (window));
572
573         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
574
575         win = g_list_find (priv->window_list, window);
576         if (win) {
577                 /* this is for the case we want to register the window and it was already
578                  * registered. We leave silently.
579                  */
580                 return;
581         }
582         
583         /* Check that it's not a second main window */
584         if (MODEST_IS_MAIN_WINDOW (window)) {
585                 if (priv->main_window) {
586                         g_warning ("%s: trying to register a second main window",
587                                    __FUNCTION__);
588                         return;
589                 } else {
590                         priv->main_window = window;
591                         load_new_view (self);
592                         load_new_editor (self);
593                 }
594         }
595
596         /* remove from the list of pre-registered uids */
597         if (MODEST_IS_MSG_VIEW_WINDOW(window)) {
598                 const gchar *uid = modest_msg_view_window_get_message_uid
599                         (MODEST_MSG_VIEW_WINDOW (window));
600
601                 if (!has_uid (priv->preregistered_uids, uid)) 
602                         g_debug ("weird: no uid for window (%s)", uid);
603                 
604                 MODEST_DEBUG_BLOCK(g_debug ("registering window for %s", uid ? uid : "<none>"););
605                 
606                 priv->preregistered_uids = 
607                         remove_uid (priv->preregistered_uids,
608                                     modest_msg_view_window_get_message_uid
609                                     (MODEST_MSG_VIEW_WINDOW (window)));
610         } else if (MODEST_IS_MSG_EDIT_WINDOW(window)) {
611                 const gchar *uid = modest_msg_edit_window_get_message_uid
612                         (MODEST_MSG_EDIT_WINDOW (window));
613                 
614                 MODEST_DEBUG_BLOCK(g_debug ("registering window for %s", uid););
615
616                 priv->preregistered_uids = 
617                         remove_uid (priv->preregistered_uids,
618                                     modest_msg_edit_window_get_message_uid
619                                     (MODEST_MSG_EDIT_WINDOW (window)));
620         }
621         
622         /* Add to list. Keep a reference to the window */
623         g_object_ref (window);
624         priv->window_list = g_list_prepend (priv->window_list, window);
625
626         /* Listen to object destruction */
627         handler_id = g_malloc0 (sizeof (gint));
628         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
629         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
630
631         /* If there is a msg view window, let the main window listen the msg-changed signal */
632         if (MODEST_IS_MSG_VIEW_WINDOW(window) && priv->main_window) {
633                 gulong *handler;
634                 handler = g_malloc0 (sizeof (gulong));
635                 *handler = g_signal_connect (window, "msg-changed", 
636                                              G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed), 
637                                              priv->main_window);
638                 g_hash_table_insert (priv->viewer_handlers, window, handler);
639         }
640
641         /* Put into fullscreen if needed */
642         if (priv->fullscreen_mode)
643                 gtk_window_fullscreen (GTK_WINDOW (window));
644
645         /* Show/hide toolbar & fullscreen */    
646         key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
647         modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
648 }
649
650 static gboolean
651 on_window_destroy (ModestWindow *window, 
652                    GdkEvent *event,
653                    ModestWindowMgr *self)
654 {
655         gint dialog_response = GTK_RESPONSE_NONE;
656
657         /* Specific stuff first */
658         if (MODEST_IS_MAIN_WINDOW (window)) {
659                 ModestWindowMgrPrivate *priv;
660                 priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
661
662                 /* If more than one window already opened */
663                 if (g_list_length (priv->window_list) > 1) {
664
665                         /* Present the window if it's not visible now */
666                         if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
667                                 gtk_window_present (GTK_WINDOW (window));
668                         /* Create the confirmation dialog MSG-NOT308 */
669                         dialog_response = modest_platform_run_confirmation_dialog (
670                                         GTK_WINDOW (window), _("emev_nc_close_windows"));
671
672                         /* If the user wants to close all the windows */
673                         if ((dialog_response == GTK_RESPONSE_OK) 
674                                         || (dialog_response == GTK_RESPONSE_ACCEPT) 
675                                         || (dialog_response == GTK_RESPONSE_YES))
676                                 {
677                                         GList *iter = priv->window_list;
678                                         do {
679                                                 if (iter->data != window) {
680                                                         GList *tmp = iter->next;
681                                                         on_window_destroy (MODEST_WINDOW (iter->data),
682                                                                         event,
683                                                                         self);
684                                                         iter = tmp;
685                                                 } else {
686                                                         iter = g_list_next (iter);
687                                                 }
688                                         } while (iter);
689                                 }
690                         else
691                                 {
692                                         return TRUE;
693                                 }
694                 }
695         }
696         else {
697                 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
698                         gboolean sent = FALSE;
699                         gint response = GTK_RESPONSE_ACCEPT;
700                         sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
701                         /* Save currently edited message to Drafts if it was not sent */
702                         if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
703
704                                 /* Raise the window if it's minimized */
705                                 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
706                                         gtk_window_present (GTK_WINDOW (window));
707                                 
708                                 response =
709                                         modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
710                                                                                  _("mcen_nc_no_email_message_modified_save_changes"));
711                                 /* Save to drafts */
712                                 if (response != GTK_RESPONSE_CANCEL)
713                                         modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window));                            
714                         }
715                 }
716         }
717
718         /* Unregister window */
719         modest_window_mgr_unregister_window (self, window);
720         
721         return FALSE;
722 }
723
724 static void
725 disconnect_msg_changed (gpointer key, 
726                         gpointer value, 
727                         gpointer user_data)
728 {
729         guint handler_id;
730         handler_id = GPOINTER_TO_UINT(value);
731         
732         if (key && G_IS_OBJECT(key))
733                 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
734 }
735
736 void 
737 modest_window_mgr_unregister_window (ModestWindowMgr *self, 
738                                      ModestWindow *window)
739 {
740         GList *win;
741         ModestWindowMgrPrivate *priv;
742         gulong *tmp, handler_id;
743
744         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
745         g_return_if_fail (MODEST_IS_WINDOW (window));
746
747         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
748
749         win = g_list_find (priv->window_list, window);
750         if (!win) {
751                 g_warning ("Trying to unregister a window that has not being registered yet");
752                 return;
753         }
754
755         /* If it's the main window unset it */
756         if (priv->main_window == window) {
757                 priv->main_window = NULL;
758
759                 /* Disconnect all emissions of msg-changed */
760                 if (priv->viewer_handlers) {
761                         g_hash_table_foreach (priv->viewer_handlers, 
762                                               disconnect_msg_changed, 
763                                               NULL);
764                         g_hash_table_destroy (priv->viewer_handlers);
765                         priv->viewer_handlers = NULL;
766                 }
767         }
768
769         /* Remove the viewer window handler from the hash table. The
770            HashTable could not exist if the main window was closeed
771            when there were other windows remaining */
772         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
773                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
774                 /* If the viewer was created without a main window
775                    (for example when opening a message through D-Bus
776                    the viewer handlers was not registered */
777                 if (tmp) {
778                         g_signal_handler_disconnect (window, *tmp);
779                         g_hash_table_remove (priv->viewer_handlers, window);
780                 }
781         }
782
783         /* Save state */
784         modest_window_save_state (window);
785
786         /* Remove from list & hash table */
787         priv->window_list = g_list_remove_link (priv->window_list, win);
788         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
789         handler_id = *tmp;
790         g_hash_table_remove (priv->destroy_handlers, window);
791
792         /* cancel open and receive operations */
793         if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
794                 ModestMailOperationTypeOperation type;
795                 GSList* pending_ops = NULL;
796                 GSList* tmp_list = NULL;
797                 pending_ops = modest_mail_operation_queue_get_by_source (
798                                 modest_runtime_get_mail_operation_queue (), 
799                                 G_OBJECT (window));
800                 while (pending_ops != NULL) {
801                         type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
802                         if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
803                                 modest_mail_operation_cancel (pending_ops->data);
804                         }
805                         g_object_unref (G_OBJECT (pending_ops->data));
806                         tmp_list = pending_ops;
807                         pending_ops = g_slist_next (pending_ops);
808                         g_slist_free_1 (tmp_list);
809                 }
810         }
811         
812         /* Disconnect the "delete-event" handler, we won't need it anymore */
813         g_signal_handler_disconnect (window, handler_id);
814
815         /* Disconnect all the window signals */
816         modest_window_disconnect_signals (window);
817         
818         /* Destroy the window */
819         gtk_widget_destroy (win->data);
820         
821         /* If there are no more windows registered emit the signal */
822         if (priv->window_list == NULL)
823                 g_signal_emit (self, signals[WINDOW_LIST_EMPTY_SIGNAL], 0);
824 }
825
826
827
828 void
829 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
830                                        gboolean on)
831 {
832         ModestWindowMgrPrivate *priv;
833         GList *win = NULL;
834         ModestConf *conf;
835
836         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
837
838         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
839
840         /* If there is no change do nothing */
841         if (priv->fullscreen_mode == on)
842                 return;
843
844         priv->fullscreen_mode = on;
845
846         conf = modest_runtime_get_conf ();
847
848         /* Update windows */
849         win = priv->window_list;
850         while (win) {
851                 gboolean show;
852                 const gchar *key = NULL;
853
854                 /* Getting this from gconf everytime is not that
855                    expensive, we'll do it just a few times */
856                 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
857                 show = modest_conf_get_bool (conf, key, NULL);
858
859                 /* Set fullscreen/unfullscreen */
860                 if (on)
861                         gtk_window_fullscreen (GTK_WINDOW (win->data));
862                 else
863                         gtk_window_unfullscreen (GTK_WINDOW (win->data));
864
865                 /* Show/Hide toolbar */
866                 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
867
868                 win = g_list_next (win);
869         }
870 }
871
872 gboolean
873 modest_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
874 {
875         ModestWindowMgrPrivate *priv;
876
877         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
878
879         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
880
881         return priv->fullscreen_mode;
882 }
883
884 void 
885 modest_window_mgr_show_toolbars (ModestWindowMgr *self,
886                                  GType window_type,
887                                  gboolean show_toolbars,
888                                  gboolean fullscreen)
889 {
890         ModestWindowMgrPrivate *priv;
891         ModestConf *conf;
892         const gchar *key = NULL;
893
894         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
895
896         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
897         conf = modest_runtime_get_conf ();
898
899         /* If nothing changes then return */
900         key = get_show_toolbar_key (window_type, fullscreen);
901         conf = modest_runtime_get_conf ();
902         if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
903                 return;
904
905         /* Save in conf */
906         modest_conf_set_bool (conf, key, show_toolbars, NULL);
907
908         /* Apply now if the view mode is the right one */
909         if ((fullscreen && priv->fullscreen_mode) ||
910             (!fullscreen && !priv->fullscreen_mode)) {
911
912                 GList *win = priv->window_list;
913
914                 while (win) {
915                         if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
916                                 modest_window_show_toolbar (MODEST_WINDOW (win->data),
917                                                             show_toolbars);
918                         win = g_list_next (win);
919                 }
920         }
921 }
922
923 ModestWindow*  
924 modest_window_mgr_get_main_window (ModestWindowMgr *self, gboolean create)
925 {
926         ModestWindowMgrPrivate *priv;
927         
928         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
929         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
930         
931         /* create the main window, if it hasn't been created yet */
932         if (!priv->main_window && create) {
933                 /* modest_window_mgr_register_window will set priv->main_window */
934                 modest_window_mgr_register_window (self, modest_main_window_new ());
935                 MODEST_DEBUG_BLOCK(
936                         g_debug ("%s: created main window: %p\n", __FUNCTION__, priv->main_window);
937                 );
938         }
939         
940         return priv->main_window;
941 }
942
943
944 gboolean
945 modest_window_mgr_main_window_exists  (ModestWindowMgr *self)
946 {
947         ModestWindowMgrPrivate *priv;
948         
949         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
950         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
951
952         return priv->main_window != NULL;
953 }
954
955
956 GtkWindow *
957 modest_window_mgr_get_modal (ModestWindowMgr *self)
958 {
959         ModestWindowMgrPrivate *priv;
960         
961         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
962         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
963
964         return g_queue_peek_head (priv->modal_windows);
965 }
966
967
968 void
969 modest_window_mgr_set_modal (ModestWindowMgr *self, 
970                              GtkWindow *window)
971 {
972         GtkWindow *old_modal;
973         ModestWindowMgrPrivate *priv;
974
975         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
976         g_return_if_fail (GTK_IS_WINDOW (window));
977
978         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
979         g_mutex_lock (priv->queue_lock);
980         old_modal = g_queue_peek_head (priv->modal_windows);
981         g_mutex_unlock (priv->queue_lock);
982
983         if (!old_modal) {       
984                 gtk_window_set_modal (window, TRUE);
985         } else {
986                 /* un-modalize the old one; the one on top should be the
987                  * modal one */
988                 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));   
989                 gtk_window_set_modal (window, TRUE);
990         }
991
992         /* this will be the new modal window */
993         g_mutex_lock (priv->queue_lock);
994         g_queue_push_head (priv->modal_windows, window);
995         g_mutex_unlock (priv->queue_lock);
996
997         if (GTK_IS_DIALOG (window))
998                 /* Note that response is not always enough because it
999                    could be captured and removed easily by dialogs but
1000                    works for most of situations */
1001                 priv->modal_handler_uids = 
1002                         modest_signal_mgr_connect (priv->modal_handler_uids, 
1003                                                    G_OBJECT (window), 
1004                                                    "response",
1005                                                    G_CALLBACK (on_modal_dialog_close), 
1006                                                    self);
1007         else
1008                 priv->modal_handler_uids = 
1009                         modest_signal_mgr_connect (priv->modal_handler_uids, 
1010                                                    G_OBJECT (window), 
1011                                                    "delete-event",
1012                                                    G_CALLBACK (on_modal_window_close), 
1013                                                    self);
1014 }
1015
1016
1017 static void
1018 on_nonhibernating_window_hide(GtkWidget *widget, gpointer user_data)
1019 {
1020         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1021         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1022         
1023         /* Forget this window,
1024          * so hibernation will be allowed again if no windows are remembered: */
1025         priv->windows_that_prevent_hibernation =
1026                 g_slist_remove (priv->windows_that_prevent_hibernation, GTK_WINDOW(widget));
1027 }
1028
1029 static void
1030 on_nonhibernating_window_show(GtkWidget *widget, gpointer user_data)
1031 {
1032         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1033         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1034         
1035         GtkWindow *window = GTK_WINDOW (widget);
1036         
1037         priv->windows_that_prevent_hibernation = 
1038                         g_slist_append (priv->windows_that_prevent_hibernation, window);
1039         
1040         /* Allow hibernation again when the window has been hidden: */
1041         g_signal_connect (window, "hide", 
1042                 G_CALLBACK (on_nonhibernating_window_hide), self);
1043 }
1044
1045 void
1046 modest_window_mgr_prevent_hibernation_while_window_is_shown (ModestWindowMgr *self,
1047                                                                   GtkWindow *window)
1048 {
1049         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1050         
1051         if (GTK_WIDGET_VISIBLE(window)) {
1052                 on_nonhibernating_window_show (GTK_WIDGET (window), self);
1053         } else {
1054                 /* Wait for it to be shown: */
1055                 g_signal_connect (window, "show", 
1056                         G_CALLBACK (on_nonhibernating_window_show), self);      
1057         }
1058 }
1059
1060 gboolean
1061 modest_window_mgr_get_hibernation_is_prevented (ModestWindowMgr *self)
1062 {
1063         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
1064         
1065         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1066         
1067         /* Prevent hibernation if any open windows are currently 
1068          * preventing hibernation: */
1069         return (g_slist_length (priv->windows_that_prevent_hibernation) > 0);
1070 }
1071
1072
1073 void
1074 modest_window_mgr_save_state_for_all_windows (ModestWindowMgr *self)
1075 {
1076         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1077         
1078         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1079
1080         /* Iterate over all windows */
1081         GList *win = priv->window_list;
1082         while (win) {
1083                 ModestWindow *window = MODEST_WINDOW (win->data);
1084                 if (window) {
1085                         /* This calls the vfunc, 
1086                          * so each window can do its own thing: */
1087                         modest_window_save_state (window);
1088                 }       
1089                 
1090                 win = g_list_next (win);
1091         }
1092 }
1093
1094 static gboolean
1095 idle_top_modal (gpointer data)
1096 {
1097         ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
1098         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1099         GtkWindow *topmost;
1100
1101         /* Get the top modal */
1102         g_mutex_lock (priv->queue_lock);
1103         topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
1104         g_mutex_unlock (priv->queue_lock);
1105
1106         /* Show it */
1107         if (topmost)
1108                 gtk_window_present (topmost);
1109
1110         return FALSE;
1111 }
1112
1113 static void
1114 remove_modal_from_queue (GtkWidget *widget,
1115                          ModestWindowMgr *self)
1116 {
1117         ModestWindowMgrPrivate *priv;
1118         GList *item = NULL;
1119
1120         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1121
1122         /* Remove from queue. We don't use remove, because we want to
1123            exit if the widget does not belong to the queue */
1124         g_mutex_lock (priv->queue_lock);
1125         item = g_queue_find (priv->modal_windows, widget);
1126         if (!item) {
1127                 g_warning ("Trying to remove a modal window that is not registered");
1128                 g_mutex_unlock (priv->queue_lock);
1129                 return;
1130         }
1131         g_queue_unlink (priv->modal_windows, item);
1132         g_mutex_unlock (priv->queue_lock);
1133
1134         /* Disconnect handler */
1135         priv->modal_handler_uids = 
1136                 modest_signal_mgr_disconnect (priv->modal_handler_uids, 
1137                                               G_OBJECT (widget),
1138                                               GTK_IS_DIALOG (widget) ? 
1139                                               "response" : 
1140                                               "destroy-event");
1141
1142         /* Schedule the next one for being shown */
1143         g_idle_add (idle_top_modal, self);
1144 }
1145
1146 static gboolean
1147 on_modal_window_close (GtkWidget *widget,
1148                        GdkEvent *event,
1149                        gpointer user_data)
1150 {
1151         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1152
1153         /* Remove modal window from queue */
1154         remove_modal_from_queue (widget, self);
1155
1156         /* Continue */
1157         return FALSE;
1158 }
1159
1160 static void
1161 on_modal_dialog_close (GtkDialog *dialog,
1162                        gint arg1,
1163                        gpointer user_data)
1164 {
1165         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1166
1167         /* Remove modal window from queue */
1168         remove_modal_from_queue (GTK_WIDGET (dialog), self);
1169 }
1170
1171 gint 
1172 modest_window_mgr_num_windows (ModestWindowMgr *self)
1173 {
1174         ModestWindowMgrPrivate *priv;
1175         gint num_windows = 0;
1176
1177         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), -1);
1178         
1179         priv =  MODEST_WINDOW_MGR_GET_PRIVATE(self);
1180         
1181         if (priv->window_list)
1182                 num_windows = g_list_length (priv->window_list);
1183
1184         return num_windows;
1185 }
1186
1187 GtkWidget *   
1188 modest_window_mgr_get_msg_edit_window (ModestWindowMgr *self)
1189 {
1190         GtkWidget *result;
1191         ModestWindowMgrPrivate *priv;
1192
1193         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), NULL);
1194
1195         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
1196                 
1197         if (priv->cached_editor) {
1198                 result = priv->cached_editor;
1199                 priv->cached_editor = NULL;
1200                 load_new_editor (self);
1201         } else {
1202                 result = g_object_new (MODEST_TYPE_MSG_EDIT_WINDOW, NULL);
1203         }
1204
1205         return result;
1206 }
1207
1208 GtkWidget *   
1209 modest_window_mgr_get_msg_view_window (ModestWindowMgr *self)
1210 {
1211         GtkWidget *result;
1212         ModestWindowMgrPrivate *priv;
1213
1214         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), NULL);
1215         
1216         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
1217
1218         if (priv->cached_view) {
1219                 result = priv->cached_view;
1220                 priv->cached_view = NULL;
1221                 load_new_view (self);
1222         } else {
1223                 result = g_object_new (MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
1224         }
1225
1226         return result;
1227 }
1228