d97f8db07af525aba2de6651d0119ccdce6d94df
[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
40
41 /* 'private'/'protected' functions */
42 static void modest_window_mgr_class_init (ModestWindowMgrClass *klass);
43 static void modest_window_mgr_init       (ModestWindowMgr *obj);
44 static void modest_window_mgr_finalize   (GObject *obj);
45
46 static gboolean on_window_destroy        (ModestWindow *window,
47                                           GdkEvent *event,
48                                           ModestWindowMgr *self);
49
50 static const gchar* get_show_toolbar_key (GType window_type,
51                                           gboolean fullscreen);
52
53 /* list my signals  */
54 enum {
55         /* MY_SIGNAL_1, */
56         /* MY_SIGNAL_2, */
57         LAST_SIGNAL
58 };
59
60 typedef struct _ModestWindowMgrPrivate ModestWindowMgrPrivate;
61 struct _ModestWindowMgrPrivate {
62         GList        *window_list;
63
64         ModestWindow *main_window;
65         GtkDialog    *easysetup_dialog;
66         
67         gboolean     fullscreen_mode;
68         
69         GSList       *windows_that_prevent_hibernation;
70         GSList       *preregistered_uids;
71         GHashTable   *destroy_handlers;
72         GHashTable   *viewer_handlers;
73         
74         guint        closing_time;
75 };
76 #define MODEST_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
77                                                MODEST_TYPE_WINDOW_MGR, \
78                                                ModestWindowMgrPrivate))
79 /* globals */
80 static GObjectClass *parent_class = NULL;
81
82 /* uncomment the following if you have defined any signals */
83 /* static guint signals[LAST_SIGNAL] = {0}; */
84
85 GType
86 modest_window_mgr_get_type (void)
87 {
88         static GType my_type = 0;
89         if (!my_type) {
90                 static const GTypeInfo my_info = {
91                         sizeof(ModestWindowMgrClass),
92                         NULL,           /* base init */
93                         NULL,           /* base finalize */
94                         (GClassInitFunc) modest_window_mgr_class_init,
95                         NULL,           /* class finalize */
96                         NULL,           /* class data */
97                         sizeof(ModestWindowMgr),
98                         1,              /* n_preallocs */
99                         (GInstanceInitFunc) modest_window_mgr_init,
100                         NULL
101                 };
102                 my_type = g_type_register_static (G_TYPE_OBJECT,
103                                                   "ModestWindowMgr",
104                                                   &my_info, 0);
105         }
106         return my_type;
107 }
108
109 static void
110 modest_window_mgr_class_init (ModestWindowMgrClass *klass)
111 {
112         GObjectClass *gobject_class;
113         gobject_class = (GObjectClass*) klass;
114
115         parent_class            = g_type_class_peek_parent (klass);
116         gobject_class->finalize = modest_window_mgr_finalize;
117
118         g_type_class_add_private (gobject_class, sizeof(ModestWindowMgrPrivate));
119 }
120
121 static void
122 modest_window_mgr_init (ModestWindowMgr *obj)
123 {
124         ModestWindowMgrPrivate *priv;
125
126         priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
127         priv->window_list = NULL;
128         priv->main_window = NULL;
129         priv->fullscreen_mode = FALSE;
130         priv->easysetup_dialog = NULL;
131         
132         priv->preregistered_uids = NULL;
133
134         /* Could not initialize it from gconf, singletons are not
135            ready yet */
136         priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);   
137         priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
138
139         priv->closing_time = 0;
140 }
141
142 static void
143 modest_window_mgr_finalize (GObject *obj)
144 {
145         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
146
147         if (priv->window_list) {
148                 GList *iter = priv->window_list;
149                 /* unregister pending windows */
150                 while (iter) {
151                         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), 
152                                                              MODEST_WINDOW (iter->data));
153                         iter = g_list_next (iter);
154                 }
155                 g_list_free (priv->window_list);
156                 priv->window_list = NULL;
157         }
158
159         g_slist_foreach (priv->preregistered_uids, (GFunc)g_free, NULL);
160         g_slist_free (priv->preregistered_uids);
161
162         
163         /* Free the hash table with the handlers */
164         if (priv->destroy_handlers) {
165                 g_hash_table_destroy (priv->destroy_handlers);
166                 priv->destroy_handlers = NULL;
167         }
168
169         if (priv->viewer_handlers) {
170                 g_hash_table_destroy (priv->viewer_handlers);
171                 priv->viewer_handlers = NULL;
172         }
173
174         if (priv->easysetup_dialog) {
175                 g_warning ("%s: forgot to destroy an easysetup dialog somewhere",
176                            __FUNCTION__);
177                 gtk_widget_destroy (GTK_WIDGET(priv->easysetup_dialog));
178                 priv->easysetup_dialog = NULL;
179         }
180         
181         /* Do not unref priv->main_window because it does not hold a
182            new reference */
183
184         
185         G_OBJECT_CLASS(parent_class)->finalize (obj);
186 }
187
188 ModestWindowMgr*
189 modest_window_mgr_new (void)
190 {
191         return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_WINDOW_MGR, NULL));
192 }
193
194
195
196
197 /* do we have uid? */
198 static gboolean
199 has_uid (GSList *list, const gchar *uid)
200 {
201         GSList *cursor = list;
202
203         if (!uid)
204                 return FALSE;
205         
206         while (cursor) {
207                 if (cursor->data && strcmp (cursor->data, uid) == 0)
208                         return TRUE;
209                 cursor = g_slist_next (cursor);
210         }
211         return FALSE;
212 }
213
214
215 /* remove all from the list have have uid = uid */
216 static GSList*
217 remove_uid (GSList *list, const gchar *uid)
218 {
219         GSList *cursor = list, *start = list;
220         
221         if (!uid)
222                 return FALSE;
223         
224         while (cursor) {
225                 GSList *next = g_slist_next (cursor);
226                 if (cursor->data && strcmp (cursor->data, uid) == 0) {
227                         g_free (cursor->data);
228                         start = g_slist_delete_link (start, cursor);
229                 }
230                 cursor = next;
231         }
232         return start;
233 }
234
235
236 static GSList *
237 append_uid (GSList *list, const gchar *uid)
238 {
239         return g_slist_append (list, g_strdup(uid));
240 }
241
242
243
244 void 
245 modest_window_mgr_register_header (ModestWindowMgr *self,  TnyHeader *header)
246 {
247         ModestWindowMgrPrivate *priv;
248         gchar* uid;
249         
250         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
251         g_return_if_fail (TNY_IS_HEADER(header));
252                 
253         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
254         uid = modest_tny_folder_get_header_unique_id (header);
255
256
257         if (!has_uid (priv->preregistered_uids, uid)) {
258                 g_debug ("registering new uid %s", uid);
259                 priv->preregistered_uids = append_uid (priv->preregistered_uids, uid);
260         } else
261                 g_debug ("already had uid %s", uid);
262
263         g_free (uid);
264 }
265
266 void 
267 modest_window_mgr_unregister_header (ModestWindowMgr *self,  TnyHeader *header)
268 {
269         ModestWindowMgrPrivate *priv;
270         gchar* uid;
271         
272         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
273         g_return_if_fail (TNY_IS_HEADER(header));
274                 
275         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
276         uid = modest_tny_folder_get_header_unique_id (header);
277         
278         if (!has_uid (priv->preregistered_uids, uid)) {
279                 g_debug ("trying to unregister non-existing uid %s", uid);
280                 priv->preregistered_uids = append_uid (priv->preregistered_uids, uid);
281         } else
282                 g_debug ("unregistering uid %s", uid);
283
284         if (has_uid (priv->preregistered_uids, uid)) {
285                 priv->preregistered_uids = remove_uid (priv->preregistered_uids, uid);
286                 if (has_uid (priv->preregistered_uids, uid))
287                         g_debug ("BUG: uid %s NOT removed", uid);
288                 else
289                         g_debug ("uid %s removed", uid);
290         }
291
292         g_free (uid);
293 }
294
295 static gint
296 compare_msguids (ModestWindow *win,
297                  const gchar *uid)
298 {
299         const gchar *msg_uid;
300
301         if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
302                 return 1;
303
304         /* Get message uid from msg window */
305         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
306                 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
307                 if (msg_uid && uid &&!strcmp (msg_uid, uid))
308                         return 0;
309         } else {
310                 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
311         }
312         
313         if (msg_uid && uid &&!strcmp (msg_uid, uid))
314                 return 0;
315         else
316                 return 1;
317 }
318
319
320 void
321 modest_window_mgr_close_all_windows (ModestWindowMgr *self)
322 {
323         ModestWindowMgrPrivate *priv = NULL;
324         GList *wins = NULL;
325         gboolean ret_value = FALSE;
326
327         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
328         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
329         
330         /* delete-event handler already removes window_list item, */
331         /* so no next its required on this loop  */
332         wins = priv->window_list;
333         while (wins) {          
334                 g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
335
336                 wins = priv->window_list;
337         }
338 }
339
340
341 gboolean
342 modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
343                                           ModestWindow **win)
344 {
345         ModestWindowMgrPrivate *priv = NULL;
346         gchar* uid = NULL;
347         gboolean has_header, has_window = FALSE;
348         GList *item = NULL;
349
350         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
351         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
352         
353         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
354
355         uid = modest_tny_folder_get_header_unique_id (header);
356         
357         if (win)
358                 *win = NULL;
359         
360         has_header = has_uid (priv->preregistered_uids, uid);
361                 
362         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
363         if (item) {
364                 has_window = TRUE;
365                 if (win) {
366                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
367                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
368                                 g_debug ("not a valid window!");
369                         else {
370                                 g_debug ("found a window");
371                                 *win = MODEST_WINDOW (item->data);
372                         }
373                 }
374         }
375
376         g_free (uid);
377         return has_header || has_window;
378 }
379
380 static const gchar *
381 get_show_toolbar_key (GType window_type,
382                       gboolean fullscreen)
383 {
384         const gchar *key = NULL;
385
386         if (window_type == MODEST_TYPE_MAIN_WINDOW)
387                 key = (fullscreen) ? 
388                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
389                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
390         else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
391                 key = (fullscreen) ? 
392                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
393                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
394         else if (window_type ==  MODEST_TYPE_MSG_EDIT_WINDOW)
395                 key = (fullscreen) ? 
396                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
397                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
398         else
399                 g_return_val_if_reached (NULL);
400
401         return key;
402 }
403
404 void 
405 modest_window_mgr_register_window (ModestWindowMgr *self, 
406                                    ModestWindow *window)
407 {
408         GList *win;
409         ModestWindowMgrPrivate *priv;
410         gint *handler_id;
411         const gchar *key;
412
413         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
414         g_return_if_fail (GTK_IS_WINDOW (window));
415
416         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
417
418         win = g_list_find (priv->window_list, window);
419         if (win) {
420                 g_warning ("%s: trying to re-register a window",
421                            __FUNCTION__);
422                 return;
423         }
424         
425         /* Check that it's not a second main window */
426         if (MODEST_IS_MAIN_WINDOW (window)) {
427                 if (priv->main_window) {
428                         g_warning ("%s: trying to register a second main window",
429                                    __FUNCTION__);
430                         return;
431                 } else {
432                         priv->main_window = window;
433                 }
434         }
435
436         /* remove from the list of pre-registered uids */
437         if (MODEST_IS_MSG_VIEW_WINDOW(window)) {
438                 const gchar *uid = modest_msg_view_window_get_message_uid
439                         (MODEST_MSG_VIEW_WINDOW (window));
440                 
441                 if (!has_uid (priv->preregistered_uids, uid)) 
442                         g_debug ("weird: no uid for window (%s)", uid);
443                 
444                 g_debug ("registering window for %s", uid ? uid : "<none>");
445
446                 priv->preregistered_uids = 
447                         remove_uid (priv->preregistered_uids,
448                                     modest_msg_view_window_get_message_uid
449                                     (MODEST_MSG_VIEW_WINDOW (window)));
450
451         } else if (MODEST_IS_MSG_EDIT_WINDOW(window)) {
452                 const gchar *uid = modest_msg_edit_window_get_message_uid
453                         (MODEST_MSG_EDIT_WINDOW (window));
454                 
455                 g_debug ("registering window for %s", uid);
456
457                 priv->preregistered_uids = 
458                         remove_uid (priv->preregistered_uids,
459                                     modest_msg_edit_window_get_message_uid
460                                     (MODEST_MSG_EDIT_WINDOW (window)));
461         }
462         
463         /* Add to list. Keep a reference to the window */
464         g_object_ref (window);
465         priv->window_list = g_list_prepend (priv->window_list, window);
466
467         /* Listen to object destruction */
468         handler_id = g_malloc0 (sizeof (gint));
469         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
470         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
471
472         /* If there is a msg view window, let the main window listen the msg-changed signal */
473         if (MODEST_IS_MSG_VIEW_WINDOW(window) && priv->main_window) {
474                 gulong *handler;
475                 handler = g_malloc0 (sizeof (gulong));
476                 *handler = g_signal_connect (window, "msg-changed", 
477                                              G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed), 
478                                              priv->main_window);
479                 g_hash_table_insert (priv->viewer_handlers, window, handler);
480         }
481
482         /* Put into fullscreen if needed */
483         if (priv->fullscreen_mode)
484                 gtk_window_fullscreen (GTK_WINDOW (window));
485
486         /* Show/hide toolbar & fullscreen */    
487         key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
488         modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
489 }
490
491 static gboolean
492 on_window_destroy (ModestWindow *window, 
493                    GdkEvent *event,
494                    ModestWindowMgr *self)
495 {
496         /* Specific stuff first */
497         if (MODEST_IS_MAIN_WINDOW (window)) {
498                 ModestWindowMgrPrivate *priv;
499                 priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
500
501                 /* If more than one window already opened */
502                 if (g_list_length (priv->window_list) > 1) {
503
504                         /* If the user wants to close all the windows */
505                         if (modest_main_window_close_all (MODEST_MAIN_WINDOW (window))) {
506                                 GList *iter = priv->window_list;
507                                 do {
508                                         if (iter->data != window) {
509                                                 GList *tmp = iter->next;
510                                                 on_window_destroy (MODEST_WINDOW (iter->data),
511                                                                    event,
512                                                                    self);
513                                                 iter = tmp;
514                                         } else {
515                                                 iter = g_list_next (iter);
516                                         }
517                                 } while (iter);
518                         }
519                 }
520         }
521         else {
522                 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
523                         gboolean sent = FALSE;
524                         gint response = GTK_RESPONSE_ACCEPT;
525                         sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
526                         /* Save currently edited message to Drafts if it was not sent */
527                         if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
528                                 
529                                 response =
530                                         modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
531                                                                                  _("mcen_nc_no_email_message_modified_save_changes"));
532                                 /* Save to drafts */
533                                 if (response != GTK_RESPONSE_CANCEL)                            
534                                         modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window));
535                                 
536                         } 
537                 }
538         }
539
540         /* Save configuration state (TODO: why edit window does not require this function ?) */
541         if (!MODEST_IS_MSG_EDIT_WINDOW (window)) 
542                 modest_window_save_state (MODEST_WINDOW(window));
543
544
545         /* Unregister window */
546         modest_window_mgr_unregister_window (self, window);
547         
548         return FALSE;
549 }
550
551 static void
552 disconnect_msg_changed (gpointer key, 
553                         gpointer value, 
554                         gpointer user_data)
555 {
556         gulong *handler_id;
557
558         handler_id = (gulong *) value;
559         g_signal_handler_disconnect (G_OBJECT (key), *handler_id);
560 }
561
562
563
564 /* interval before retrying to close the application */
565 #define CLOSING_RETRY_INTERVAL 3000 
566 /* interval before cancel whatever is left in the queue, and closing anyway */
567 #define MAX_WAIT_FOR_CLOSING 30 * 1000 
568
569 static gboolean
570 on_quit_maybe (ModestWindowMgr *self)
571 {
572         ModestWindowMgrPrivate *priv;
573         guint queue_num;
574         
575         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
576
577         /* it seems, in the meantime some windows were
578          * created. in that case, stop  'on_quit_maybe' */
579         if (priv->window_list) {
580                 priv->closing_time = 0;
581                 return FALSE;
582         }
583
584         if (priv->closing_time >= MAX_WAIT_FOR_CLOSING) {
585                 /* we waited long enough: cancel all remaining operations */
586                 g_debug ("%s: we waited long enough (%ds), cancelling queue and quiting",
587                          __FUNCTION__, priv->closing_time/1000);
588                 /* FIXME: below gives me a lot of:
589                  * GLIB CRITICAL ** default - modest_mail_operation_cancel:
590                  *                     assertion `priv->account' failed
591                  * which means there is no account for the given operation
592                  * so, we're not trying to be nice, we're just quiting.
593                  */
594                 //modest_mail_operation_queue_cancel_all
595                 //      (modest_runtime_get_mail_operation_queue());
596         } else {
597         
598                 /* if there is anything left in our operation queue,
599                  * wait another round
600                  */
601                 queue_num = modest_mail_operation_queue_num_elements
602                         (modest_runtime_get_mail_operation_queue()); 
603                 if  (queue_num > 0) {
604                         g_debug ("%s: waiting, there are still %d operation(s) queued",
605                                  __FUNCTION__, queue_num);
606                         priv->closing_time += CLOSING_RETRY_INTERVAL;
607                         return TRUE;
608                 }
609         }
610         
611         /* so: no windows left, nothing in the queue: quit */
612         priv->closing_time = 0;
613         gtk_main_quit ();
614         return FALSE;
615 }
616
617
618 void 
619 modest_window_mgr_unregister_window (ModestWindowMgr *self, 
620                                      ModestWindow *window)
621 {
622         GList *win;
623         ModestWindowMgrPrivate *priv;
624         gulong *tmp, handler_id;
625
626         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
627         g_return_if_fail (MODEST_IS_WINDOW (window));
628
629         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
630
631         win = g_list_find (priv->window_list, window);
632         if (!win) {
633                 g_warning ("Trying to unregister a window that has not being registered yet");
634                 return;
635         }
636
637         /* If it's the main window unset it */
638         if (priv->main_window == window) {
639                 priv->main_window = NULL;
640
641                 /* Disconnect all emissions of msg-changed */
642                 if (priv->viewer_handlers) {
643                         g_hash_table_foreach (priv->viewer_handlers, 
644                                               disconnect_msg_changed, 
645                                               NULL);
646                         g_hash_table_destroy (priv->viewer_handlers);
647                         priv->viewer_handlers = NULL;
648                 }
649         }
650
651         /* Remove the viewer window handler from the hash table. The
652            HashTable could not exist if the main window was closeed
653            when there were other windows remaining */
654         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
655                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
656                 g_signal_handler_disconnect (window, *tmp);
657                 g_hash_table_remove (priv->viewer_handlers, window);
658         }
659
660         /* Save state */
661         modest_window_save_state (window);
662
663         /* Remove from list & hash table */
664         priv->window_list = g_list_remove_link (priv->window_list, win);
665         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
666         handler_id = *tmp;
667         g_hash_table_remove (priv->destroy_handlers, window);
668
669         /* Disconnect the "delete-event" handler, we won't need it anymore */
670         g_signal_handler_disconnect (window, handler_id);
671
672         /* Disconnect all the window signals */
673         modest_window_disconnect_signals (window);
674         
675         /* Destroy the window */
676         gtk_widget_destroy (win->data);
677         
678         /* If there are no more windows registered then exit program */
679         if (priv->window_list == NULL)
680                 g_timeout_add (CLOSING_RETRY_INTERVAL,
681                                (GSourceFunc)on_quit_maybe, self);
682 }
683
684
685
686 void
687 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
688                                        gboolean on)
689 {
690         ModestWindowMgrPrivate *priv;
691         GList *win = NULL;
692         ModestConf *conf;
693
694         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
695
696         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
697
698         /* If there is no change do nothing */
699         if (priv->fullscreen_mode == on)
700                 return;
701
702         priv->fullscreen_mode = on;
703
704         conf = modest_runtime_get_conf ();
705
706         /* Update windows */
707         win = priv->window_list;
708         while (win) {
709                 gboolean show;
710                 const gchar *key = NULL;
711
712                 /* Getting this from gconf everytime is not that
713                    expensive, we'll do it just a few times */
714                 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
715                 show = modest_conf_get_bool (conf, key, NULL);
716
717                 /* Set fullscreen/unfullscreen */
718                 if (on)
719                         gtk_window_fullscreen (GTK_WINDOW (win->data));
720                 else
721                         gtk_window_unfullscreen (GTK_WINDOW (win->data));
722
723                 /* Show/Hide toolbar */
724                 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
725
726                 win = g_list_next (win);
727         }
728 }
729
730 gboolean
731 modest_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
732 {
733         ModestWindowMgrPrivate *priv;
734
735         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
736
737         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
738
739         return priv->fullscreen_mode;
740 }
741
742 void 
743 modest_window_mgr_show_toolbars (ModestWindowMgr *self,
744                                  GType window_type,
745                                  gboolean show_toolbars,
746                                  gboolean fullscreen)
747 {
748         ModestWindowMgrPrivate *priv;
749         ModestConf *conf;
750         const gchar *key = NULL;
751
752         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
753
754         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
755         conf = modest_runtime_get_conf ();
756
757         /* If nothing changes then return */
758         key = get_show_toolbar_key (window_type, fullscreen);
759         conf = modest_runtime_get_conf ();
760         if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
761                 return;
762
763         /* Save in conf */
764         modest_conf_set_bool (conf, key, show_toolbars, NULL);
765
766         /* Apply now if the view mode is the right one */
767         if ((fullscreen && priv->fullscreen_mode) ||
768             (!fullscreen && !priv->fullscreen_mode)) {
769
770                 GList *win = priv->window_list;
771
772                 while (win) {
773                         if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
774                                 modest_window_show_toolbar (MODEST_WINDOW (win->data),
775                                                             show_toolbars);
776                         win = g_list_next (win);
777                 }
778         }
779 }
780
781 ModestWindow*  
782 modest_window_mgr_get_main_window (ModestWindowMgr *self)
783 {
784         ModestWindowMgrPrivate *priv;
785         
786         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
787         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
788         
789         /* create the main window, if it hasn't been created yet */
790         if (!priv->main_window) {
791                 /* modest_window_mgr_register_window will set priv->main_window */
792                 modest_window_mgr_register_window (self, modest_main_window_new ());
793                 g_debug ("%s: created main window: %p\n", __FUNCTION__, priv->main_window);
794         }
795         
796         return priv->main_window;
797 }
798
799
800 GtkDialog*
801 modest_window_mgr_get_easysetup_dialog (ModestWindowMgr *self)
802 {
803         ModestWindowMgrPrivate *priv;
804         
805         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
806         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
807
808         return priv->easysetup_dialog;
809 }
810
811
812 GtkDialog*
813 modest_window_mgr_set_easysetup_dialog (ModestWindowMgr *self, GtkDialog *dialog)
814 {
815         ModestWindowMgrPrivate *priv;
816         
817         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
818         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
819
820         return priv->easysetup_dialog = dialog;
821 }
822
823
824 static void
825 on_nonhibernating_window_hide(GtkWidget *widget, gpointer user_data)
826 {
827         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
828         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
829         
830         /* Forget this window,
831          * so hibernation will be allowed again if no windows are remembered: */
832         priv->windows_that_prevent_hibernation =
833                 g_slist_remove (priv->windows_that_prevent_hibernation, GTK_WINDOW(widget));
834 }
835
836 static void
837 on_nonhibernating_window_show(GtkWidget *widget, gpointer user_data)
838 {
839         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
840         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
841         
842         GtkWindow *window = GTK_WINDOW (widget);
843         
844         priv->windows_that_prevent_hibernation = 
845                         g_slist_append (priv->windows_that_prevent_hibernation, window);
846         
847         /* Allow hibernation again when the window has been hidden: */
848         g_signal_connect (window, "hide", 
849                 G_CALLBACK (on_nonhibernating_window_hide), self);
850 }
851
852 void
853 modest_window_mgr_prevent_hibernation_while_window_is_shown (ModestWindowMgr *self,
854                                                                   GtkWindow *window)
855 {
856         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
857         
858         if (GTK_WIDGET_VISIBLE(window)) {
859                 on_nonhibernating_window_show (GTK_WIDGET (window), self);
860         } else {
861                 /* Wait for it to be shown: */
862                 g_signal_connect (window, "show", 
863                         G_CALLBACK (on_nonhibernating_window_show), self);      
864         }
865 }
866
867 gboolean
868 modest_window_mgr_get_hibernation_is_prevented (ModestWindowMgr *self)
869 {
870         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
871         
872         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
873         
874         /* Prevent hibernation if any open windows are currently 
875          * preventing hibernation: */
876         return (g_slist_length (priv->windows_that_prevent_hibernation) > 0);
877 }
878
879
880 void
881 modest_window_mgr_save_state_for_all_windows (ModestWindowMgr *self)
882 {
883         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
884         
885         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
886
887         /* Iterate over all windows */
888         GList *win = priv->window_list;
889         while (win) {
890                 ModestWindow *window = MODEST_WINDOW (win->data);
891                 if (window) {
892                         /* This calls the vfunc, 
893                          * so each window can do its own thing: */
894                         modest_window_save_state (window);
895                 }       
896                 
897                 win = g_list_next (win);
898         }
899 }