3b94951ecaad1e91c47a6bfbaf0aa266b2f3a122
[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
317 #define MODEST_WINDOW_HELP_ID_PARAM "help-id"
318
319 void
320 modest_window_mgr_register_help_id (ModestWindowMgr *self, GtkWindow *win, const gchar* help_id)
321 {
322         /* we don't need 'self', but for API consistency... */
323         g_return_if_fail (self && MODEST_IS_WINDOW_MGR(self));
324
325         g_return_if_fail (win && GTK_IS_WINDOW(win));
326         g_return_if_fail (help_id);
327         
328         g_object_set_data_full (G_OBJECT(win), MODEST_WINDOW_HELP_ID_PARAM,
329                                 g_strdup(help_id), g_free);
330 }
331
332
333 const gchar*
334 modest_window_mgr_get_help_id (ModestWindowMgr *self, GtkWindow *win)
335 {
336         const gchar* help_id = NULL;
337
338         /* we don't need 'self', but for API consistency... */
339         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), NULL);
340         
341         g_return_val_if_fail (win, NULL);
342         g_return_val_if_fail (GTK_IS_WINDOW(win), NULL);
343         
344         if (MODEST_IS_MAIN_WINDOW (win)) {
345                 GtkWidget *folder_view;
346                 TnyFolderStore *folder_store;
347                 
348                 /* Get selected folder */
349                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
350                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
351                 folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
352
353                 /* Switch help_id */
354                 if (folder_store && TNY_IS_FOLDER (folder_store)) {
355                         help_id = modest_tny_folder_get_help_id (TNY_FOLDER (folder_store));
356                         if (!help_id)
357                                 g_warning ("%s: BUG: did not get a valid help_id", __FUNCTION__);
358                 }
359                 if (folder_store)
360                         g_object_unref (folder_store);
361         }
362
363         if (!help_id)
364                 help_id = g_object_get_data (G_OBJECT(win), MODEST_WINDOW_HELP_ID_PARAM);
365                 
366         return help_id;
367 }
368
369
370
371
372
373
374
375
376
377
378 static gint
379 compare_msguids (ModestWindow *win,
380                  const gchar *uid)
381 {
382         const gchar *msg_uid;
383
384         if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
385                 return 1;
386
387         /* Get message uid from msg window */
388         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
389                 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
390                 if (msg_uid && uid &&!strcmp (msg_uid, uid))
391                         return 0;
392         } else {
393                 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
394         }
395         
396         if (msg_uid && uid &&!strcmp (msg_uid, uid))
397                 return 0;
398         else
399                 return 1;
400 }
401
402 void
403 modest_window_mgr_close_all_windows (ModestWindowMgr *self)
404 {
405         ModestWindowMgrPrivate *priv = NULL;
406         GList *wins = NULL;
407         gboolean ret_value = FALSE;
408         
409         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
410         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
411         
412         /* delete-event handler already removes window_list item, */
413         /* so no next its required on this loop  */
414         wins = priv->window_list;
415         while (wins) {          
416                 g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
417
418                 wins = priv->window_list;
419         }
420 }
421
422
423 gboolean
424 modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
425                                           ModestWindow **win)
426 {
427         ModestWindowMgrPrivate *priv = NULL;
428         gchar* uid = NULL;
429         gboolean has_header, has_window = FALSE;
430         GList *item = NULL;
431
432         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
433         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
434         
435         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
436
437         uid = modest_tny_folder_get_header_unique_id (header);
438         
439         if (win)
440                 *win = NULL;
441
442         has_header = has_uid (priv->preregistered_uids, uid);
443         
444         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
445         if (item) {
446                 has_window = TRUE;
447                 if (win) {
448                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
449                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
450                                 g_debug ("not a valid window!");
451                         else {
452                                 g_debug ("found a window");
453                                 *win = MODEST_WINDOW (item->data);
454                         }
455                 }
456         }
457         g_free (uid);
458         
459         return has_header || has_window;
460 }
461
462 static const gchar *
463 get_show_toolbar_key (GType window_type,
464                       gboolean fullscreen)
465 {
466         const gchar *key = NULL;
467
468         if (window_type == MODEST_TYPE_MAIN_WINDOW)
469                 key = (fullscreen) ? 
470                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
471                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
472         else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
473                 key = (fullscreen) ? 
474                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
475                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
476         else if (window_type ==  MODEST_TYPE_MSG_EDIT_WINDOW)
477                 key = (fullscreen) ? 
478                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
479                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
480         else
481                 g_return_val_if_reached (NULL);
482
483         return key;
484 }
485
486 void 
487 modest_window_mgr_register_window (ModestWindowMgr *self, 
488                                    ModestWindow *window)
489 {
490         GList *win;
491         ModestWindowMgrPrivate *priv;
492         gint *handler_id;
493         const gchar *key;
494
495         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
496         g_return_if_fail (GTK_IS_WINDOW (window));
497
498         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
499
500         win = g_list_find (priv->window_list, window);
501         if (win) {
502                 g_warning ("%s: trying to re-register a window",
503                            __FUNCTION__);
504                 return;
505         }
506         
507         /* Check that it's not a second main window */
508         if (MODEST_IS_MAIN_WINDOW (window)) {
509                 if (priv->main_window) {
510                         g_warning ("%s: trying to register a second main window",
511                                    __FUNCTION__);
512                         return;
513                 } else {
514                         priv->main_window = window;
515                 }
516         }
517
518         /* remove from the list of pre-registered uids */
519         if (MODEST_IS_MSG_VIEW_WINDOW(window)) {
520                 const gchar *uid = modest_msg_view_window_get_message_uid
521                         (MODEST_MSG_VIEW_WINDOW (window));
522
523                 if (!has_uid (priv->preregistered_uids, uid)) 
524                         g_debug ("weird: no uid for window (%s)", uid);
525                 
526                 g_debug ("registering window for %s", uid ? uid : "<none>");
527                 
528                 priv->preregistered_uids = 
529                         remove_uid (priv->preregistered_uids,
530                                     modest_msg_view_window_get_message_uid
531                                     (MODEST_MSG_VIEW_WINDOW (window)));
532         } else if (MODEST_IS_MSG_EDIT_WINDOW(window)) {
533                 const gchar *uid = modest_msg_edit_window_get_message_uid
534                         (MODEST_MSG_EDIT_WINDOW (window));
535                 
536                 g_debug ("registering window for %s", uid);
537
538                 priv->preregistered_uids = 
539                         remove_uid (priv->preregistered_uids,
540                                     modest_msg_edit_window_get_message_uid
541                                     (MODEST_MSG_EDIT_WINDOW (window)));
542         }
543         
544         /* Add to list. Keep a reference to the window */
545         g_object_ref (window);
546         priv->window_list = g_list_prepend (priv->window_list, window);
547
548         /* Listen to object destruction */
549         handler_id = g_malloc0 (sizeof (gint));
550         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
551         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
552
553         /* If there is a msg view window, let the main window listen the msg-changed signal */
554         if (MODEST_IS_MSG_VIEW_WINDOW(window) && priv->main_window) {
555                 gulong *handler;
556                 handler = g_malloc0 (sizeof (gulong));
557                 *handler = g_signal_connect (window, "msg-changed", 
558                                              G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed), 
559                                              priv->main_window);
560                 g_hash_table_insert (priv->viewer_handlers, window, handler);
561         }
562
563         /* Put into fullscreen if needed */
564         if (priv->fullscreen_mode)
565                 gtk_window_fullscreen (GTK_WINDOW (window));
566
567         /* Show/hide toolbar & fullscreen */    
568         key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
569         modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
570 }
571
572 static gboolean
573 on_window_destroy (ModestWindow *window, 
574                    GdkEvent *event,
575                    ModestWindowMgr *self)
576 {
577         gint dialog_response = GTK_RESPONSE_NONE;
578
579         /* Specific stuff first */
580         if (MODEST_IS_MAIN_WINDOW (window)) {
581                 ModestWindowMgrPrivate *priv;
582                 priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
583
584                 /* If more than one window already opened */
585                 if (g_list_length (priv->window_list) > 1) {
586
587                         /* Create the confirmation dialog MSG-NOT308 */
588                         dialog_response = modest_platform_run_confirmation_dialog (
589                                         GTK_WINDOW (window), _("emev_nc_close_windows"));
590
591                         /* If the user wants to close all the windows */
592                         if ((dialog_response == GTK_RESPONSE_OK) 
593                                         || (dialog_response == GTK_RESPONSE_ACCEPT) 
594                                         || (dialog_response == GTK_RESPONSE_YES))
595                                 {
596                                         GList *iter = priv->window_list;
597                                         do {
598                                                 if (iter->data != window) {
599                                                         GList *tmp = iter->next;
600                                                         on_window_destroy (MODEST_WINDOW (iter->data),
601                                                                         event,
602                                                                         self);
603                                                         iter = tmp;
604                                                 } else {
605                                                         iter = g_list_next (iter);
606                                                 }
607                                         } while (iter);
608                                 }
609                         else
610                                 {
611                                         return TRUE;
612                                 }
613                 }
614         }
615         else {
616                 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
617                         gboolean sent = FALSE;
618                         gint response = GTK_RESPONSE_ACCEPT;
619                         sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
620                         /* Save currently edited message to Drafts if it was not sent */
621                         if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
622
623                                 /* Raise the window if it's minimized */
624                                 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
625                                         gtk_window_present (GTK_WINDOW (window));
626                                 
627                                 response =
628                                         modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
629                                                                                  _("mcen_nc_no_email_message_modified_save_changes"));
630                                 /* Save to drafts */
631                                 if (response != GTK_RESPONSE_CANCEL)                            
632                                         modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window));                            
633                         }
634                 }
635         }
636
637         /* Unregister window */
638         modest_window_mgr_unregister_window (self, window);
639         
640         return FALSE;
641 }
642
643 static void
644 disconnect_msg_changed (gpointer key, 
645                         gpointer value, 
646                         gpointer user_data)
647 {
648         guint handler_id;
649         handler_id = GPOINTER_TO_UINT(value);
650         
651         if (key && G_IS_OBJECT(key))
652                 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
653 }
654
655
656
657 /* interval before retrying to close the application */
658 #define CLOSING_RETRY_INTERVAL 3000 
659 /* interval before cancel whatever is left in the queue, and closing anyway */
660 #define MAX_WAIT_FOR_CLOSING 30 * 1000 
661
662 static gboolean
663 on_quit_maybe (ModestWindowMgr *self)
664 {
665         ModestWindowMgrPrivate *priv;
666         guint queue_num;
667         
668         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
669
670         /* it seems, in the meantime some windows were
671          * created. in that case, stop  'on_quit_maybe' */
672         if (priv->window_list) {
673                 priv->closing_time = 0;
674                 return FALSE;
675         }
676
677         if (priv->closing_time >= MAX_WAIT_FOR_CLOSING) {
678                 /* we waited long enough: cancel all remaining operations */
679                 g_debug ("%s: we waited long enough (%ds), cancelling queue and quiting",
680                          __FUNCTION__, priv->closing_time/1000);
681                 /* FIXME: below gives me a lot of:
682                  * GLIB CRITICAL ** default - modest_mail_operation_cancel:
683                  *                     assertion `priv->account' failed
684                  * which means there is no account for the given operation
685                  * so, we're not trying to be nice, we're just quiting.
686                  */
687                 //modest_mail_operation_queue_cancel_all
688                 //      (modest_runtime_get_mail_operation_queue());
689         } else {
690         
691                 /* if there is anything left in our operation queue,
692                  * wait another round
693                  */
694                 queue_num = modest_mail_operation_queue_num_elements
695                         (modest_runtime_get_mail_operation_queue()); 
696                 if  (queue_num > 0) {
697                         g_debug ("%s: waiting, there are still %d operation(s) queued",
698                                  __FUNCTION__, queue_num);
699                         priv->closing_time += CLOSING_RETRY_INTERVAL;
700                         return TRUE;
701                 }
702         }
703         
704         /* so: no windows left, nothing in the queue: quit */
705         priv->closing_time = 0;
706         gtk_main_quit ();
707         return FALSE;
708 }
709
710
711 void 
712 modest_window_mgr_unregister_window (ModestWindowMgr *self, 
713                                      ModestWindow *window)
714 {
715         GList *win;
716         ModestWindowMgrPrivate *priv;
717         gulong *tmp, handler_id;
718
719         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
720         g_return_if_fail (MODEST_IS_WINDOW (window));
721
722         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
723
724         win = g_list_find (priv->window_list, window);
725         if (!win) {
726                 g_warning ("Trying to unregister a window that has not being registered yet");
727                 return;
728         }
729
730         /* If it's the main window unset it */
731         if (priv->main_window == window) {
732                 priv->main_window = NULL;
733
734                 /* Disconnect all emissions of msg-changed */
735                 if (priv->viewer_handlers) {
736                         g_hash_table_foreach (priv->viewer_handlers, 
737                                               disconnect_msg_changed, 
738                                               NULL);
739                         g_hash_table_destroy (priv->viewer_handlers);
740                         priv->viewer_handlers = NULL;
741                 }
742         }
743
744         /* Remove the viewer window handler from the hash table. The
745            HashTable could not exist if the main window was closeed
746            when there were other windows remaining */
747         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
748                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
749                 /* If the viewer was created without a main window
750                    (for example when opening a message through D-Bus
751                    the viewer handlers was not registered */
752                 if (tmp) {
753                         g_signal_handler_disconnect (window, *tmp);
754                         g_hash_table_remove (priv->viewer_handlers, window);
755                 }
756         }
757
758         /* Save state */
759         modest_window_save_state (window);
760
761         /* Remove from list & hash table */
762         priv->window_list = g_list_remove_link (priv->window_list, win);
763         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
764         handler_id = *tmp;
765         g_hash_table_remove (priv->destroy_handlers, window);
766
767         /* Disconnect the "delete-event" handler, we won't need it anymore */
768         g_signal_handler_disconnect (window, handler_id);
769
770         /* Disconnect all the window signals */
771         modest_window_disconnect_signals (window);
772         
773         /* Destroy the window */
774         gtk_widget_destroy (win->data);
775         
776         /* If there are no more windows registered then exit program */
777         if (priv->window_list == NULL)
778                 g_timeout_add (CLOSING_RETRY_INTERVAL,
779                                (GSourceFunc)on_quit_maybe, self);
780 }
781
782
783
784 void
785 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
786                                        gboolean on)
787 {
788         ModestWindowMgrPrivate *priv;
789         GList *win = NULL;
790         ModestConf *conf;
791
792         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
793
794         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
795
796         /* If there is no change do nothing */
797         if (priv->fullscreen_mode == on)
798                 return;
799
800         priv->fullscreen_mode = on;
801
802         conf = modest_runtime_get_conf ();
803
804         /* Update windows */
805         win = priv->window_list;
806         while (win) {
807                 gboolean show;
808                 const gchar *key = NULL;
809
810                 /* Getting this from gconf everytime is not that
811                    expensive, we'll do it just a few times */
812                 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
813                 show = modest_conf_get_bool (conf, key, NULL);
814
815                 /* Set fullscreen/unfullscreen */
816                 if (on)
817                         gtk_window_fullscreen (GTK_WINDOW (win->data));
818                 else
819                         gtk_window_unfullscreen (GTK_WINDOW (win->data));
820
821                 /* Show/Hide toolbar */
822                 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
823
824                 win = g_list_next (win);
825         }
826 }
827
828 gboolean
829 modest_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
830 {
831         ModestWindowMgrPrivate *priv;
832
833         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
834
835         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
836
837         return priv->fullscreen_mode;
838 }
839
840 void 
841 modest_window_mgr_show_toolbars (ModestWindowMgr *self,
842                                  GType window_type,
843                                  gboolean show_toolbars,
844                                  gboolean fullscreen)
845 {
846         ModestWindowMgrPrivate *priv;
847         ModestConf *conf;
848         const gchar *key = NULL;
849
850         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
851
852         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
853         conf = modest_runtime_get_conf ();
854
855         /* If nothing changes then return */
856         key = get_show_toolbar_key (window_type, fullscreen);
857         conf = modest_runtime_get_conf ();
858         if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
859                 return;
860
861         /* Save in conf */
862         modest_conf_set_bool (conf, key, show_toolbars, NULL);
863
864         /* Apply now if the view mode is the right one */
865         if ((fullscreen && priv->fullscreen_mode) ||
866             (!fullscreen && !priv->fullscreen_mode)) {
867
868                 GList *win = priv->window_list;
869
870                 while (win) {
871                         if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
872                                 modest_window_show_toolbar (MODEST_WINDOW (win->data),
873                                                             show_toolbars);
874                         win = g_list_next (win);
875                 }
876         }
877 }
878
879 ModestWindow*  
880 modest_window_mgr_get_main_window (ModestWindowMgr *self, gboolean create)
881 {
882         ModestWindowMgrPrivate *priv;
883         
884         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
885         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
886         
887         /* create the main window, if it hasn't been created yet */
888         if (!priv->main_window && create) {
889                 /* modest_window_mgr_register_window will set priv->main_window */
890                 modest_window_mgr_register_window (self, modest_main_window_new ());
891                 g_debug ("%s: created main window: %p\n", __FUNCTION__, priv->main_window);
892         }
893         
894         return priv->main_window;
895 }
896
897
898 gboolean
899 modest_window_mgr_main_window_exists  (ModestWindowMgr *self)
900 {
901         ModestWindowMgrPrivate *priv;
902         
903         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
904         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
905
906         return priv->main_window != NULL;
907 }
908
909
910 GtkWindow *
911 modest_window_mgr_get_modal (ModestWindowMgr *self)
912 {
913         ModestWindowMgrPrivate *priv;
914         
915         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
916         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
917
918         return g_queue_peek_head (priv->modal_windows);
919 }
920
921
922 void
923 modest_window_mgr_set_modal (ModestWindowMgr *self, 
924                              GtkWindow *window)
925 {
926         GtkWindow *old_modal;
927         ModestWindowMgrPrivate *priv;
928
929         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
930         g_return_if_fail (GTK_IS_WINDOW (window));
931
932         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
933         g_mutex_lock (priv->queue_lock);
934         old_modal = g_queue_peek_head (priv->modal_windows);
935         g_mutex_unlock (priv->queue_lock);
936
937         if (!old_modal) {       
938                 gtk_window_set_modal (window, TRUE);
939         } else {
940                 /* un-modalize the old one; the one on top should be the
941                  * modal one */
942                 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));   
943                 gtk_window_set_modal (window, TRUE);
944         }
945
946         /* this will be the new modal window */
947         g_mutex_lock (priv->queue_lock);
948         g_queue_push_head (priv->modal_windows, window);
949         g_mutex_unlock (priv->queue_lock);
950
951         if (GTK_IS_DIALOG (window))
952                 /* Note that response is not always enough because it
953                    could be captured and removed easily by dialogs but
954                    works for most of situations */
955                 priv->modal_handler_uids = 
956                         modest_signal_mgr_connect (priv->modal_handler_uids, 
957                                                    G_OBJECT (window), 
958                                                    "response",
959                                                    G_CALLBACK (on_modal_dialog_close), 
960                                                    self);
961         else
962                 priv->modal_handler_uids = 
963                         modest_signal_mgr_connect (priv->modal_handler_uids, 
964                                                    G_OBJECT (window), 
965                                                    "delete-event",
966                                                    G_CALLBACK (on_modal_window_close), 
967                                                    self);
968 }
969
970
971 static void
972 on_nonhibernating_window_hide(GtkWidget *widget, gpointer user_data)
973 {
974         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
975         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
976         
977         /* Forget this window,
978          * so hibernation will be allowed again if no windows are remembered: */
979         priv->windows_that_prevent_hibernation =
980                 g_slist_remove (priv->windows_that_prevent_hibernation, GTK_WINDOW(widget));
981 }
982
983 static void
984 on_nonhibernating_window_show(GtkWidget *widget, gpointer user_data)
985 {
986         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
987         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
988         
989         GtkWindow *window = GTK_WINDOW (widget);
990         
991         priv->windows_that_prevent_hibernation = 
992                         g_slist_append (priv->windows_that_prevent_hibernation, window);
993         
994         /* Allow hibernation again when the window has been hidden: */
995         g_signal_connect (window, "hide", 
996                 G_CALLBACK (on_nonhibernating_window_hide), self);
997 }
998
999 void
1000 modest_window_mgr_prevent_hibernation_while_window_is_shown (ModestWindowMgr *self,
1001                                                                   GtkWindow *window)
1002 {
1003         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1004         
1005         if (GTK_WIDGET_VISIBLE(window)) {
1006                 on_nonhibernating_window_show (GTK_WIDGET (window), self);
1007         } else {
1008                 /* Wait for it to be shown: */
1009                 g_signal_connect (window, "show", 
1010                         G_CALLBACK (on_nonhibernating_window_show), self);      
1011         }
1012 }
1013
1014 gboolean
1015 modest_window_mgr_get_hibernation_is_prevented (ModestWindowMgr *self)
1016 {
1017         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
1018         
1019         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1020         
1021         /* Prevent hibernation if any open windows are currently 
1022          * preventing hibernation: */
1023         return (g_slist_length (priv->windows_that_prevent_hibernation) > 0);
1024 }
1025
1026
1027 void
1028 modest_window_mgr_save_state_for_all_windows (ModestWindowMgr *self)
1029 {
1030         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1031         
1032         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1033
1034         /* Iterate over all windows */
1035         GList *win = priv->window_list;
1036         while (win) {
1037                 ModestWindow *window = MODEST_WINDOW (win->data);
1038                 if (window) {
1039                         /* This calls the vfunc, 
1040                          * so each window can do its own thing: */
1041                         modest_window_save_state (window);
1042                 }       
1043                 
1044                 win = g_list_next (win);
1045         }
1046 }
1047
1048 static gboolean
1049 idle_top_modal (gpointer data)
1050 {
1051         ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
1052         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1053         GtkWindow *topmost;
1054
1055         /* Get the top modal */
1056         g_mutex_lock (priv->queue_lock);
1057         topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
1058         g_mutex_unlock (priv->queue_lock);
1059
1060         /* Show it */
1061         if (topmost)
1062                 gtk_window_present (topmost);
1063
1064         return FALSE;
1065 }
1066
1067 static void
1068 remove_modal_from_queue (GtkWidget *widget,
1069                          ModestWindowMgr *self)
1070 {
1071         ModestWindowMgrPrivate *priv;
1072         GList *item = NULL;
1073
1074         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1075
1076         /* Remove from queue. We don't use remove, because we want to
1077            exit if the widget does not belong to the queue */
1078         g_mutex_lock (priv->queue_lock);
1079         item = g_queue_find (priv->modal_windows, widget);
1080         if (!item) {
1081                 g_warning ("Trying to remove a modal window that is not registered");
1082                 g_mutex_unlock (priv->queue_lock);
1083                 return;
1084         }
1085         g_queue_unlink (priv->modal_windows, item);
1086         g_mutex_unlock (priv->queue_lock);
1087
1088         /* Disconnect handler */
1089         priv->modal_handler_uids = 
1090                 modest_signal_mgr_disconnect (priv->modal_handler_uids, 
1091                                               G_OBJECT (widget),
1092                                               GTK_IS_DIALOG (widget) ? 
1093                                               "response" : 
1094                                               "destroy-event");
1095
1096         /* Schedule the next one for being shown */
1097         g_idle_add (idle_top_modal, self);
1098 }
1099
1100 static gboolean
1101 on_modal_window_close (GtkWidget *widget,
1102                        GdkEvent *event,
1103                        gpointer user_data)
1104 {
1105         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1106
1107         /* Remove modal window from queue */
1108         remove_modal_from_queue (widget, self);
1109
1110         /* Continue */
1111         return FALSE;
1112 }
1113
1114 static void
1115 on_modal_dialog_close (GtkDialog *dialog,
1116                        gint arg1,
1117                        gpointer user_data)
1118 {
1119         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1120
1121         /* Remove modal window from queue */
1122         remove_modal_from_queue (GTK_WIDGET (dialog), self);
1123 }
1124