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