patch for NB#73896
[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->viewer_handlers) {
686                 /* If the viewer was created without a main window
687                    (for example when opening a message through D-Bus
688                    the viewer handlers was not registered */
689                 if (tmp) {
690                         g_signal_handler_disconnect (window, *tmp);
691                         g_hash_table_remove (priv->viewer_handlers, window);
692                 }
693         }
694
695         /* Save state */
696         modest_window_save_state (window);
697
698         /* Remove from list & hash table */
699         priv->window_list = g_list_remove_link (priv->window_list, win);
700         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
701         handler_id = *tmp;
702         g_hash_table_remove (priv->destroy_handlers, window);
703
704         /* Disconnect the "delete-event" handler, we won't need it anymore */
705         g_signal_handler_disconnect (window, handler_id);
706
707         /* Disconnect all the window signals */
708         modest_window_disconnect_signals (window);
709         
710         /* Destroy the window */
711         gtk_widget_destroy (win->data);
712         
713         /* If there are no more windows registered then exit program */
714         if (priv->window_list == NULL)
715                 g_timeout_add (CLOSING_RETRY_INTERVAL,
716                                (GSourceFunc)on_quit_maybe, self);
717 }
718
719
720
721 void
722 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
723                                        gboolean on)
724 {
725         ModestWindowMgrPrivate *priv;
726         GList *win = NULL;
727         ModestConf *conf;
728
729         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
730
731         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
732
733         /* If there is no change do nothing */
734         if (priv->fullscreen_mode == on)
735                 return;
736
737         priv->fullscreen_mode = on;
738
739         conf = modest_runtime_get_conf ();
740
741         /* Update windows */
742         win = priv->window_list;
743         while (win) {
744                 gboolean show;
745                 const gchar *key = NULL;
746
747                 /* Getting this from gconf everytime is not that
748                    expensive, we'll do it just a few times */
749                 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
750                 show = modest_conf_get_bool (conf, key, NULL);
751
752                 /* Set fullscreen/unfullscreen */
753                 if (on)
754                         gtk_window_fullscreen (GTK_WINDOW (win->data));
755                 else
756                         gtk_window_unfullscreen (GTK_WINDOW (win->data));
757
758                 /* Show/Hide toolbar */
759                 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
760
761                 win = g_list_next (win);
762         }
763 }
764
765 gboolean
766 modest_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
767 {
768         ModestWindowMgrPrivate *priv;
769
770         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
771
772         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
773
774         return priv->fullscreen_mode;
775 }
776
777 void 
778 modest_window_mgr_show_toolbars (ModestWindowMgr *self,
779                                  GType window_type,
780                                  gboolean show_toolbars,
781                                  gboolean fullscreen)
782 {
783         ModestWindowMgrPrivate *priv;
784         ModestConf *conf;
785         const gchar *key = NULL;
786
787         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
788
789         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
790         conf = modest_runtime_get_conf ();
791
792         /* If nothing changes then return */
793         key = get_show_toolbar_key (window_type, fullscreen);
794         conf = modest_runtime_get_conf ();
795         if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
796                 return;
797
798         /* Save in conf */
799         modest_conf_set_bool (conf, key, show_toolbars, NULL);
800
801         /* Apply now if the view mode is the right one */
802         if ((fullscreen && priv->fullscreen_mode) ||
803             (!fullscreen && !priv->fullscreen_mode)) {
804
805                 GList *win = priv->window_list;
806
807                 while (win) {
808                         if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
809                                 modest_window_show_toolbar (MODEST_WINDOW (win->data),
810                                                             show_toolbars);
811                         win = g_list_next (win);
812                 }
813         }
814 }
815
816 ModestWindow*  
817 modest_window_mgr_get_main_window (ModestWindowMgr *self)
818 {
819         ModestWindowMgrPrivate *priv;
820         
821         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
822         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
823         
824         /* create the main window, if it hasn't been created yet */
825         if (!priv->main_window) {
826                 /* modest_window_mgr_register_window will set priv->main_window */
827                 modest_window_mgr_register_window (self, modest_main_window_new ());
828                 g_debug ("%s: created main window: %p\n", __FUNCTION__, priv->main_window);
829         }
830         
831         return priv->main_window;
832 }
833
834
835 GtkWindow *
836 modest_window_mgr_get_modal (ModestWindowMgr *self)
837 {
838         ModestWindowMgrPrivate *priv;
839         
840         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
841         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
842
843         return g_queue_peek_head (priv->modal_windows);
844 }
845
846
847 void
848 modest_window_mgr_set_modal (ModestWindowMgr *self, 
849                              GtkWindow *window)
850 {
851         GtkWindow *old_modal;
852         ModestWindowMgrPrivate *priv;
853
854         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
855         g_return_if_fail (GTK_IS_WINDOW (window));
856
857         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
858         g_mutex_lock (priv->queue_lock);
859         old_modal = g_queue_peek_head (priv->modal_windows);
860         g_mutex_unlock (priv->queue_lock);
861
862         if (!old_modal) {       
863                 gtk_window_set_modal (window, TRUE);
864         } else {
865                 /* un-modalize the old one; the one on top should be the
866                  * modal one */
867                 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));   
868                 gtk_window_set_modal (window, TRUE);
869         }
870
871         /* this will be the new modal window */
872         g_mutex_lock (priv->queue_lock);
873         g_queue_push_head (priv->modal_windows, window);
874         g_mutex_unlock (priv->queue_lock);
875
876         if (GTK_IS_DIALOG (window))
877                 /* Note that response is not always enough because it
878                    could be captured and removed easily by dialogs but
879                    works for most of situations */
880                 priv->modal_handler_uids = 
881                         modest_signal_mgr_connect (priv->modal_handler_uids, 
882                                                    G_OBJECT (window), 
883                                                    "response",
884                                                    G_CALLBACK (on_modal_dialog_close), 
885                                                    self);
886         else
887                 priv->modal_handler_uids = 
888                         modest_signal_mgr_connect (priv->modal_handler_uids, 
889                                                    G_OBJECT (window), 
890                                                    "delete-event",
891                                                    G_CALLBACK (on_modal_window_close), 
892                                                    self);
893 }
894
895
896 static void
897 on_nonhibernating_window_hide(GtkWidget *widget, gpointer user_data)
898 {
899         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
900         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
901         
902         /* Forget this window,
903          * so hibernation will be allowed again if no windows are remembered: */
904         priv->windows_that_prevent_hibernation =
905                 g_slist_remove (priv->windows_that_prevent_hibernation, GTK_WINDOW(widget));
906 }
907
908 static void
909 on_nonhibernating_window_show(GtkWidget *widget, gpointer user_data)
910 {
911         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
912         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
913         
914         GtkWindow *window = GTK_WINDOW (widget);
915         
916         priv->windows_that_prevent_hibernation = 
917                         g_slist_append (priv->windows_that_prevent_hibernation, window);
918         
919         /* Allow hibernation again when the window has been hidden: */
920         g_signal_connect (window, "hide", 
921                 G_CALLBACK (on_nonhibernating_window_hide), self);
922 }
923
924 void
925 modest_window_mgr_prevent_hibernation_while_window_is_shown (ModestWindowMgr *self,
926                                                                   GtkWindow *window)
927 {
928         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
929         
930         if (GTK_WIDGET_VISIBLE(window)) {
931                 on_nonhibernating_window_show (GTK_WIDGET (window), self);
932         } else {
933                 /* Wait for it to be shown: */
934                 g_signal_connect (window, "show", 
935                         G_CALLBACK (on_nonhibernating_window_show), self);      
936         }
937 }
938
939 gboolean
940 modest_window_mgr_get_hibernation_is_prevented (ModestWindowMgr *self)
941 {
942         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
943         
944         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
945         
946         /* Prevent hibernation if any open windows are currently 
947          * preventing hibernation: */
948         return (g_slist_length (priv->windows_that_prevent_hibernation) > 0);
949 }
950
951
952 void
953 modest_window_mgr_save_state_for_all_windows (ModestWindowMgr *self)
954 {
955         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
956         
957         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
958
959         /* Iterate over all windows */
960         GList *win = priv->window_list;
961         while (win) {
962                 ModestWindow *window = MODEST_WINDOW (win->data);
963                 if (window) {
964                         /* This calls the vfunc, 
965                          * so each window can do its own thing: */
966                         modest_window_save_state (window);
967                 }       
968                 
969                 win = g_list_next (win);
970         }
971 }
972
973 static gboolean
974 idle_top_modal (gpointer data)
975 {
976         ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
977         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
978         GtkWindow *topmost;
979
980         /* Get the top modal */
981         g_mutex_lock (priv->queue_lock);
982         topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
983         g_mutex_unlock (priv->queue_lock);
984
985         /* Show it */
986         if (topmost)
987                 gtk_window_present (topmost);
988
989         return FALSE;
990 }
991
992 static void
993 remove_modal_from_queue (GtkWidget *widget,
994                          ModestWindowMgr *self)
995 {
996         ModestWindowMgrPrivate *priv;
997         GList *item = NULL;
998
999         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1000
1001         /* Remove from queue. We don't use remove, because we want to
1002            exit if the widget does not belong to the queue */
1003         g_mutex_lock (priv->queue_lock);
1004         item = g_queue_find (priv->modal_windows, widget);
1005         if (!item) {
1006                 g_warning ("Trying to remove a modal window that is not registered");
1007                 g_mutex_unlock (priv->queue_lock);
1008                 return;
1009         }
1010         g_queue_unlink (priv->modal_windows, item);
1011         g_mutex_unlock (priv->queue_lock);
1012
1013         /* Disconnect handler */
1014         priv->modal_handler_uids = 
1015                 modest_signal_mgr_disconnect (priv->modal_handler_uids, 
1016                                               G_OBJECT (widget),
1017                                               GTK_IS_DIALOG (widget) ? 
1018                                               "response" : 
1019                                               "destroy-event");
1020
1021         /* Schedule the next one for being shown */
1022         g_idle_add (idle_top_modal, self);
1023 }
1024
1025 static gboolean
1026 on_modal_window_close (GtkWidget *widget,
1027                        GdkEvent *event,
1028                        gpointer user_data)
1029 {
1030         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1031
1032         /* Remove modal window from queue */
1033         remove_modal_from_queue (widget, self);
1034
1035         /* Continue */
1036         return FALSE;
1037 }
1038
1039 static void
1040 on_modal_dialog_close (GtkDialog *dialog,
1041                        gint arg1,
1042                        gpointer user_data)
1043 {
1044         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1045
1046         /* Remove modal window from queue */
1047         remove_modal_from_queue (GTK_WIDGET (dialog), self);
1048 }
1049