* Fixes NB#64415, now the show_toolbar options are per window type specific
[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 gboolean
321 modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
322                                           ModestWindow **win)
323 {
324         ModestWindowMgrPrivate *priv;
325         gchar* uid;
326         gboolean has_header, has_window = FALSE;
327         GList *item = NULL;
328
329         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
330         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
331         
332         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
333
334         uid = modest_tny_folder_get_header_unique_id (header);
335         
336         if (win)
337                 *win = NULL;
338         
339         has_header = has_uid (priv->preregistered_uids, uid);
340                 
341         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
342         if (item) {
343                 has_window = TRUE;
344                 if (win) {
345                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
346                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
347                                 g_debug ("not a valid window!");
348                         else {
349                                 g_debug ("found a window");
350                                 *win = MODEST_WINDOW (item->data);
351                         }
352                 }
353         }
354
355         g_free (uid);
356         return has_header || has_window;
357 }
358
359 static const gchar *
360 get_show_toolbar_key (GType window_type,
361                       gboolean fullscreen)
362 {
363         const gchar *key = NULL;
364
365         if (window_type == MODEST_TYPE_MAIN_WINDOW)
366                 key = (fullscreen) ? 
367                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
368                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
369         else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
370                 key = (fullscreen) ? 
371                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
372                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
373         else if (window_type ==  MODEST_TYPE_MSG_EDIT_WINDOW)
374                 key = (fullscreen) ? 
375                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
376                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
377         else
378                 g_return_val_if_reached (NULL);
379
380         return key;
381 }
382
383 void 
384 modest_window_mgr_register_window (ModestWindowMgr *self, 
385                                    ModestWindow *window)
386 {
387         GList *win;
388         ModestWindowMgrPrivate *priv;
389         gint *handler_id;
390         const gchar *key;
391
392         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
393         g_return_if_fail (GTK_IS_WINDOW (window));
394
395         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
396
397         win = g_list_find (priv->window_list, window);
398         if (win) {
399                 g_warning ("%s: trying to re-register a window",
400                            __FUNCTION__);
401                 return;
402         }
403         
404         /* Check that it's not a second main window */
405         if (MODEST_IS_MAIN_WINDOW (window)) {
406                 if (priv->main_window) {
407                         g_warning ("%s: trying to register a second main window",
408                                    __FUNCTION__);
409                         return;
410                 } else {
411                         priv->main_window = window;
412                 }
413         }
414
415         /* remove from the list of pre-registered uids */
416         if (MODEST_IS_MSG_VIEW_WINDOW(window)) {
417                 const gchar *uid = modest_msg_view_window_get_message_uid
418                         (MODEST_MSG_VIEW_WINDOW (window));
419                 
420                 if (!has_uid (priv->preregistered_uids, uid)) 
421                         g_debug ("weird: no uid for window (%s)", uid);
422                 
423                 g_debug ("registering window for %s", uid ? uid : "<none>");
424
425                 priv->preregistered_uids = 
426                         remove_uid (priv->preregistered_uids,
427                                     modest_msg_view_window_get_message_uid
428                                     (MODEST_MSG_VIEW_WINDOW (window)));
429
430         } else if (MODEST_IS_MSG_EDIT_WINDOW(window)) {
431                 const gchar *uid = modest_msg_edit_window_get_message_uid
432                         (MODEST_MSG_EDIT_WINDOW (window));
433                 
434                 g_debug ("registering window for %s", uid);
435
436                 priv->preregistered_uids = 
437                         remove_uid (priv->preregistered_uids,
438                                     modest_msg_edit_window_get_message_uid
439                                     (MODEST_MSG_EDIT_WINDOW (window)));
440         }
441         
442         /* Add to list. Keep a reference to the window */
443         g_object_ref (window);
444         priv->window_list = g_list_prepend (priv->window_list, window);
445
446         /* Listen to object destruction */
447         handler_id = g_malloc0 (sizeof (gint));
448         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
449         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
450
451         /* If there is a msg view window, let the main window listen the msg-changed signal */
452         if (MODEST_IS_MSG_VIEW_WINDOW(window) && priv->main_window) {
453                 gulong *handler;
454                 handler = g_malloc0 (sizeof (gulong));
455                 *handler = g_signal_connect (window, "msg-changed", 
456                                              G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed), 
457                                              priv->main_window);
458                 g_hash_table_insert (priv->viewer_handlers, window, handler);
459         }
460
461         /* Put into fullscreen if needed */
462         if (priv->fullscreen_mode)
463                 gtk_window_fullscreen (GTK_WINDOW (window));
464
465         /* Show/hide toolbar & fullscreen */    
466         key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
467         modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
468 }
469
470 static gboolean
471 on_window_destroy (ModestWindow *window, 
472                    GdkEvent *event,
473                    ModestWindowMgr *self)
474 {
475         /* Specific stuff first */
476         if (MODEST_IS_MAIN_WINDOW (window)) {
477                 ModestWindowMgrPrivate *priv;
478                 priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
479
480                 /* If more than one window already opened */
481                 if (g_list_length (priv->window_list) > 1) {
482
483                         /* If the user wants to close all the windows */
484                         if (modest_main_window_close_all (MODEST_MAIN_WINDOW (window))) {
485                                 GList *iter = priv->window_list;
486                                 do {
487                                         if (iter->data != window) {
488                                                 GList *tmp = iter->next;
489                                                 on_window_destroy (MODEST_WINDOW (iter->data),
490                                                                    event,
491                                                                    self);
492                                                 iter = tmp;
493                                         } else {
494                                                 iter = g_list_next (iter);
495                                         }
496                                 } while (iter);
497                         }
498                 }
499         }
500         else {
501                 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
502                         gboolean sent = FALSE;
503                         gint response = GTK_RESPONSE_ACCEPT;
504                         sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
505                         /* Save currently edited message to Drafts if it was not sent */
506                         if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
507                                 
508                                 response =
509                                         modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
510                                                                                  _("mcen_nc_no_email_message_modified_save_changes"));
511                                 /* Save to drafts */
512                                 if (response != GTK_RESPONSE_CANCEL)                            
513                                         modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window));
514                                 
515                         } 
516                 }
517         }
518
519         /* Save configuration state (TODO: why edit window does not require this function ?) */
520         if (!MODEST_IS_MSG_EDIT_WINDOW (window)) 
521                 modest_window_save_state (MODEST_WINDOW(window));
522
523
524         /* Unregister window */
525         modest_window_mgr_unregister_window (self, window);
526         
527         return FALSE;
528 }
529
530 static void
531 disconnect_msg_changed (gpointer key, 
532                         gpointer value, 
533                         gpointer user_data)
534 {
535         gulong *handler_id;
536
537         handler_id = (gulong *) value;
538         g_signal_handler_disconnect (G_OBJECT (key), *handler_id);
539 }
540
541
542
543 /* interval before retrying to close the application */
544 #define CLOSING_RETRY_INTERVAL 3000 
545 /* interval before cancel whatever is left in the queue, and closing anyway */
546 #define MAX_WAIT_FOR_CLOSING 30 * 1000 
547
548 static gboolean
549 on_quit_maybe (ModestWindowMgr *self)
550 {
551         ModestWindowMgrPrivate *priv;
552         guint queue_num;
553         
554         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
555
556         /* it seems, in the meantime some windows were
557          * created. in that case, stop  'on_quit_maybe' */
558         if (priv->window_list) {
559                 priv->closing_time = 0;
560                 return FALSE;
561         }
562
563         if (priv->closing_time >= MAX_WAIT_FOR_CLOSING) {
564                 /* we waited long enough: cancel all remaining operations */
565                 g_debug ("%s: we waited long enough (%ds), cancelling queue and quiting",
566                          __FUNCTION__, priv->closing_time/1000);
567                 /* FIXME: below gives me a lot of:
568                  * GLIB CRITICAL ** default - modest_mail_operation_cancel:
569                  *                     assertion `priv->account' failed
570                  * which means there is no account for the given operation
571                  * so, we're not trying to be nice, we're just quiting.
572                  */
573                 //modest_mail_operation_queue_cancel_all
574                 //      (modest_runtime_get_mail_operation_queue());
575         } else {
576         
577                 /* if there is anything left in our operation queue,
578                  * wait another round
579                  */
580                 queue_num = modest_mail_operation_queue_num_elements
581                         (modest_runtime_get_mail_operation_queue()); 
582                 if  (queue_num > 0) {
583                         g_debug ("%s: waiting, there are still %d operation(s) queued",
584                                  __FUNCTION__, queue_num);
585                         priv->closing_time += CLOSING_RETRY_INTERVAL;
586                         return TRUE;
587                 }
588         }
589         
590         /* so: no windows left, nothing in the queue: quit */
591         priv->closing_time = 0;
592         gtk_main_quit ();
593         return FALSE;
594 }
595
596
597 void 
598 modest_window_mgr_unregister_window (ModestWindowMgr *self, 
599                                      ModestWindow *window)
600 {
601         GList *win;
602         ModestWindowMgrPrivate *priv;
603         gulong *tmp, handler_id;
604
605         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
606         g_return_if_fail (MODEST_IS_WINDOW (window));
607
608         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
609
610         win = g_list_find (priv->window_list, window);
611         if (!win) {
612                 g_warning ("Trying to unregister a window that has not being registered yet");
613                 return;
614         }
615
616         /* If it's the main window unset it */
617         if (priv->main_window == window) {
618                 priv->main_window = NULL;
619
620                 /* Disconnect all emissions of msg-changed */
621                 if (priv->viewer_handlers) {
622                         g_hash_table_foreach (priv->viewer_handlers, 
623                                               disconnect_msg_changed, 
624                                               NULL);
625                         g_hash_table_destroy (priv->viewer_handlers);
626                         priv->viewer_handlers = NULL;
627                 }
628         }
629
630         /* Remove the viewer window handler from the hash table. The
631            HashTable could not exist if the main window was closeed
632            when there were other windows remaining */
633         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
634                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
635                 g_signal_handler_disconnect (window, *tmp);
636                 g_hash_table_remove (priv->viewer_handlers, window);
637         }
638
639         /* Save state */
640         modest_window_save_state (window);
641
642         /* Remove from list & hash table */
643         priv->window_list = g_list_remove_link (priv->window_list, win);
644         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
645         handler_id = *tmp;
646         g_hash_table_remove (priv->destroy_handlers, window);
647
648         /* Disconnect the "delete-event" handler, we won't need it anymore */
649         g_signal_handler_disconnect (window, handler_id);
650
651         /* Disconnect all the window signals */
652         modest_window_disconnect_signals (window);
653         
654         /* Destroy the window */
655         gtk_widget_destroy (win->data);
656         
657         /* If there are no more windows registered then exit program */
658         if (priv->window_list == NULL)
659                 g_timeout_add (CLOSING_RETRY_INTERVAL,
660                                (GSourceFunc)on_quit_maybe, self);
661 }
662
663
664
665 void
666 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
667                                        gboolean on)
668 {
669         ModestWindowMgrPrivate *priv;
670         GList *win = NULL;
671         ModestConf *conf;
672
673         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
674
675         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
676
677         /* If there is no change do nothing */
678         if (priv->fullscreen_mode == on)
679                 return;
680
681         priv->fullscreen_mode = on;
682
683         conf = modest_runtime_get_conf ();
684
685         /* Update windows */
686         win = priv->window_list;
687         while (win) {
688                 gboolean show;
689                 const gchar *key = NULL;
690
691                 /* Getting this from gconf everytime is not that
692                    expensive, we'll do it just a few times */
693                 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
694                 show = modest_conf_get_bool (conf, key, NULL);
695
696                 /* Set fullscreen/unfullscreen */
697                 if (on)
698                         gtk_window_fullscreen (GTK_WINDOW (win->data));
699                 else
700                         gtk_window_unfullscreen (GTK_WINDOW (win->data));
701
702                 /* Show/Hide toolbar */
703                 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
704
705                 win = g_list_next (win);
706         }
707 }
708
709 gboolean
710 modest_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
711 {
712         ModestWindowMgrPrivate *priv;
713
714         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
715
716         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
717
718         return priv->fullscreen_mode;
719 }
720
721 void 
722 modest_window_mgr_show_toolbars (ModestWindowMgr *self,
723                                  GType window_type,
724                                  gboolean show_toolbars,
725                                  gboolean fullscreen)
726 {
727         ModestWindowMgrPrivate *priv;
728         ModestConf *conf;
729         const gchar *key = NULL;
730
731         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
732
733         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
734         conf = modest_runtime_get_conf ();
735
736         /* If nothing changes then return */
737         key = get_show_toolbar_key (window_type, fullscreen);
738         conf = modest_runtime_get_conf ();
739         if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
740                 return;
741
742         /* Save in conf */
743         modest_conf_set_bool (conf, key, show_toolbars, NULL);
744
745         /* Apply now if the view mode is the right one */
746         if ((fullscreen && priv->fullscreen_mode) ||
747             (!fullscreen && !priv->fullscreen_mode)) {
748
749                 GList *win = priv->window_list;
750
751                 while (win) {
752                         if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
753                                 modest_window_show_toolbar (MODEST_WINDOW (win->data),
754                                                             show_toolbars);
755                         win = g_list_next (win);
756                 }
757         }
758 }
759
760 ModestWindow*  
761 modest_window_mgr_get_main_window (ModestWindowMgr *self)
762 {
763         ModestWindowMgrPrivate *priv;
764         
765         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
766         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
767         
768         /* create the main window, if it hasn't been created yet */
769         if (!priv->main_window) {
770                 /* modest_window_mgr_register_window will set priv->main_window */
771                 modest_window_mgr_register_window (self, modest_main_window_new ());
772                 g_debug ("%s: created main window: %p\n", __FUNCTION__, priv->main_window);
773         }
774         
775         return priv->main_window;
776 }
777
778
779 GtkDialog*
780 modest_window_mgr_get_easysetup_dialog (ModestWindowMgr *self)
781 {
782         ModestWindowMgrPrivate *priv;
783         
784         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
785         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
786
787         return priv->easysetup_dialog;
788 }
789
790
791 GtkDialog*
792 modest_window_mgr_set_easysetup_dialog (ModestWindowMgr *self, GtkDialog *dialog)
793 {
794         ModestWindowMgrPrivate *priv;
795         
796         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
797         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
798
799         return priv->easysetup_dialog = dialog;
800 }
801
802
803 static void
804 on_nonhibernating_window_hide(GtkWidget *widget, gpointer user_data)
805 {
806         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
807         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
808         
809         /* Forget this window,
810          * so hibernation will be allowed again if no windows are remembered: */
811         priv->windows_that_prevent_hibernation =
812                 g_slist_remove (priv->windows_that_prevent_hibernation, GTK_WINDOW(widget));
813 }
814
815 static void
816 on_nonhibernating_window_show(GtkWidget *widget, gpointer user_data)
817 {
818         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
819         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
820         
821         GtkWindow *window = GTK_WINDOW (widget);
822         
823         priv->windows_that_prevent_hibernation = 
824                         g_slist_append (priv->windows_that_prevent_hibernation, window);
825         
826         /* Allow hibernation again when the window has been hidden: */
827         g_signal_connect (window, "hide", 
828                 G_CALLBACK (on_nonhibernating_window_hide), self);
829 }
830
831 void
832 modest_window_mgr_prevent_hibernation_while_window_is_shown (ModestWindowMgr *self,
833                                                                   GtkWindow *window)
834 {
835         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
836         
837         if (GTK_WIDGET_VISIBLE(window)) {
838                 on_nonhibernating_window_show (GTK_WIDGET (window), self);
839         } else {
840                 /* Wait for it to be shown: */
841                 g_signal_connect (window, "show", 
842                         G_CALLBACK (on_nonhibernating_window_show), self);      
843         }
844 }
845
846 gboolean
847 modest_window_mgr_get_hibernation_is_prevented (ModestWindowMgr *self)
848 {
849         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
850         
851         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
852         
853         /* Prevent hibernation if any open windows are currently 
854          * preventing hibernation: */
855         return (g_slist_length (priv->windows_that_prevent_hibernation) > 0);
856 }
857
858
859 void
860 modest_window_mgr_save_state_for_all_windows (ModestWindowMgr *self)
861 {
862         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
863         
864         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
865
866         /* Iterate over all windows */
867         GList *win = priv->window_list;
868         while (win) {
869                 ModestWindow *window = MODEST_WINDOW (win->data);
870                 if (window) {
871                         /* This calls the vfunc, 
872                          * so each window can do its own thing: */
873                         modest_window_save_state (window);
874                 }       
875                 
876                 win = g_list_next (win);
877         }
878 }