* Forgot to link the close all windows implementation. This broke
[modest] / src / widgets / modest-hildon1-window-mgr.c
1 /* Copyright (c) 2008, 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-hildon1-window-mgr.h"
32 #include "modest-msg-edit-window.h"
33 #include "modest-main-window.h"
34 #include "modest-conf.h"
35 #include "modest-defs.h"
36 #include "modest-signal-mgr.h"
37 #include "modest-runtime.h"
38 #include "modest-platform.h"
39 #include "modest-ui-actions.h"
40 #include "modest-debug.h"
41 #include "modest-tny-folder.h"
42
43 /* 'private'/'protected' functions */
44 static void modest_hildon1_window_mgr_class_init (ModestHildon1WindowMgrClass *klass);
45 static void modest_hildon1_window_mgr_instance_init (ModestHildon1WindowMgr *obj);
46 static void modest_hildon1_window_mgr_finalize   (GObject *obj);
47
48 static gboolean on_window_destroy        (ModestWindow *window,
49                                           GdkEvent *event,
50                                           ModestHildon1WindowMgr *self);
51
52 static gboolean on_modal_window_close    (GtkWidget *widget,
53                                           GdkEvent *event,
54                                           gpointer user_data);
55
56 static void on_modal_dialog_destroy      (GtkObject *object,
57                                           gpointer   user_data);
58
59 static void     on_modal_dialog_close    (GtkDialog *dialog,
60                                           gint arg1,
61                                           gpointer user_data);
62
63 static const gchar* get_show_toolbar_key (GType window_type,
64                                           gboolean fullscreen);
65
66 static gboolean modest_hildon1_window_mgr_register_window (ModestWindowMgr *self, 
67                                                            ModestWindow *window,
68                                                            ModestWindow *parent);
69 static void modest_hildon1_window_mgr_unregister_window (ModestWindowMgr *self, 
70                                                          ModestWindow *window);
71 static void modest_hildon1_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
72                                                            gboolean on);
73 static gboolean modest_hildon1_window_mgr_get_fullscreen_mode (ModestWindowMgr *self);
74 static void modest_hildon1_window_mgr_show_toolbars (ModestWindowMgr *self,
75                                                      GType window_type,
76                                                      gboolean show_toolbars,
77                                                      gboolean fullscreen);
78 static ModestWindow* modest_hildon1_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show);
79 static GtkWindow *modest_hildon1_window_mgr_get_modal (ModestWindowMgr *self);
80 static void modest_hildon1_window_mgr_set_modal (ModestWindowMgr *self, 
81                                                  GtkWindow *window,
82                                                  GtkWindow *parent);
83 static gboolean modest_hildon1_window_mgr_find_registered_header (ModestWindowMgr *self, 
84                                                                   TnyHeader *header,
85                                                                   ModestWindow **win);
86 static GList *modest_hildon1_window_mgr_get_window_list (ModestWindowMgr *self);
87 static void modest_hildon1_window_mgr_close_all_windows (ModestWindowMgr *self);
88
89 typedef struct _ModestHildon1WindowMgrPrivate ModestHildon1WindowMgrPrivate;
90 struct _ModestHildon1WindowMgrPrivate {
91         GList        *window_list;
92         GMutex       *queue_lock;
93         GQueue       *modal_windows;
94         
95         gboolean     fullscreen_mode;
96         
97         GHashTable   *destroy_handlers;
98         GHashTable   *viewer_handlers;
99         GSList       *window_state_uids;
100         
101         guint        closing_time;
102
103         GSList       *modal_handler_uids;
104         ModestWindow *current_top;
105 };
106 #define MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
107                                                                                    MODEST_TYPE_HILDON1_WINDOW_MGR, \
108                                                                                    ModestHildon1WindowMgrPrivate))
109 /* globals */
110 static GObjectClass *parent_class = NULL;
111
112 GType
113 modest_hildon1_window_mgr_get_type (void)
114 {
115         static GType my_type = 0;
116         if (!my_type) {
117                 static const GTypeInfo my_info = {
118                         sizeof(ModestHildon1WindowMgrClass),
119                         NULL,           /* base init */
120                         NULL,           /* base finalize */
121                         (GClassInitFunc) modest_hildon1_window_mgr_class_init,
122                         NULL,           /* class finalize */
123                         NULL,           /* class data */
124                         sizeof(ModestHildon1WindowMgr),
125                         1,              /* n_preallocs */
126                         (GInstanceInitFunc) modest_hildon1_window_mgr_instance_init,
127                         NULL
128                 };
129                 my_type = g_type_register_static (MODEST_TYPE_WINDOW_MGR,
130                                                   "ModestHildon1WindowMgr",
131                                                   &my_info, 0);
132         }
133         return my_type;
134 }
135
136 static void
137 modest_hildon1_window_mgr_class_init (ModestHildon1WindowMgrClass *klass)
138 {
139         GObjectClass *gobject_class;
140         ModestWindowMgrClass *mgr_class;
141
142         gobject_class = (GObjectClass*) klass;
143         mgr_class = (ModestWindowMgrClass *) klass;
144
145         parent_class            = g_type_class_peek_parent (klass);
146         gobject_class->finalize = modest_hildon1_window_mgr_finalize;
147         mgr_class->register_window = modest_hildon1_window_mgr_register_window;
148         mgr_class->unregister_window = modest_hildon1_window_mgr_unregister_window;
149         mgr_class->set_fullscreen_mode = modest_hildon1_window_mgr_set_fullscreen_mode;
150         mgr_class->get_fullscreen_mode = modest_hildon1_window_mgr_get_fullscreen_mode;
151         mgr_class->show_toolbars = modest_hildon1_window_mgr_show_toolbars;
152         mgr_class->get_main_window = modest_hildon1_window_mgr_get_main_window;
153         mgr_class->get_modal = modest_hildon1_window_mgr_get_modal;
154         mgr_class->set_modal = modest_hildon1_window_mgr_set_modal;
155         mgr_class->find_registered_header = modest_hildon1_window_mgr_find_registered_header;
156         mgr_class->get_window_list = modest_hildon1_window_mgr_get_window_list;
157         mgr_class->close_all_windows = modest_hildon1_window_mgr_close_all_windows;
158
159         g_type_class_add_private (gobject_class, sizeof(ModestHildon1WindowMgrPrivate));
160
161 }
162
163 static void
164 modest_hildon1_window_mgr_instance_init (ModestHildon1WindowMgr *obj)
165 {
166         ModestHildon1WindowMgrPrivate *priv;
167
168         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(obj);
169         priv->window_list = NULL;
170         priv->fullscreen_mode = FALSE;
171         priv->current_top = NULL;
172         priv->window_state_uids = NULL;
173
174         priv->modal_windows = g_queue_new ();
175         priv->queue_lock = g_mutex_new ();
176         
177         /* Could not initialize it from gconf, singletons are not
178            ready yet */
179         priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);   
180         priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
181
182         priv->closing_time = 0;
183
184         priv->modal_handler_uids = NULL;
185 }
186
187 static void
188 modest_hildon1_window_mgr_finalize (GObject *obj)
189 {
190         ModestHildon1WindowMgrPrivate *priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(obj);
191
192         modest_signal_mgr_disconnect_all_and_destroy (priv->window_state_uids);
193         priv->window_state_uids = NULL;
194
195         if (priv->window_list) {
196                 GList *iter = priv->window_list;
197                 /* unregister pending windows */
198                 while (iter) {
199                         ModestWindow *window = (ModestWindow *) iter->data;
200                         iter = g_list_next (iter);
201                         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), window);
202                 }
203                 g_list_free (priv->window_list);
204                 priv->window_list = NULL;
205         }
206
207         /* Free the hash table with the handlers */
208         if (priv->destroy_handlers) {
209                 g_hash_table_destroy (priv->destroy_handlers);
210                 priv->destroy_handlers = NULL;
211         }
212
213         if (priv->viewer_handlers) {
214                 g_hash_table_destroy (priv->viewer_handlers);
215                 priv->viewer_handlers = NULL;
216         }
217
218         modest_signal_mgr_disconnect_all_and_destroy (priv->modal_handler_uids);
219         priv->modal_handler_uids = NULL;
220
221         if (priv->modal_windows) {
222                 g_mutex_lock (priv->queue_lock);
223                 g_queue_free (priv->modal_windows);
224                 priv->modal_windows = NULL;
225                 g_mutex_unlock (priv->queue_lock);
226         }
227         g_mutex_free (priv->queue_lock);
228         
229         /* Do not unref priv->main_window because it does not hold a
230            new reference */
231
232         
233         G_OBJECT_CLASS(parent_class)->finalize (obj);
234 }
235
236 ModestWindowMgr*
237 modest_hildon1_window_mgr_new (void)
238 {
239         return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_HILDON1_WINDOW_MGR, NULL));
240 }
241
242 static void
243 modest_hildon1_window_mgr_close_all_windows (ModestWindowMgr *self)
244 {
245         ModestHildon1WindowMgrPrivate *priv = NULL;
246         gboolean ret_value = FALSE;
247         ModestWindow *main_window;
248         
249         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
250         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
251         
252         /* If there is a main window then try to close it, and it will
253            close the others if needed */
254         main_window = modest_window_mgr_get_main_window (self, FALSE);
255         if (main_window) {
256                 g_signal_emit_by_name (main_window, "delete-event", NULL, &ret_value);
257         } else {
258                 GList *wins = NULL, *next = NULL;
259
260                 /* delete-event handler actually removes window_list item, */
261                 wins = priv->window_list;
262                 while (wins) {
263                         next = g_list_next (wins);
264                         g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
265                         wins = next;
266                 }
267         }
268 }
269
270 static gint
271 compare_msguids (ModestWindow *win,
272                  const gchar *uid)
273 {
274         const gchar *msg_uid;
275
276         if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
277                 return 1;
278
279         /* Get message uid from msg window */
280         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
281                 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
282                 if (msg_uid && uid &&!strcmp (msg_uid, uid))
283                         return 0;
284         } else {
285                 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
286         }
287         
288         if (msg_uid && uid &&!strcmp (msg_uid, uid))
289                 return 0;
290         else
291                 return 1;
292 }
293
294 static gboolean
295 modest_hildon1_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
296                                                   ModestWindow **win)
297 {
298         ModestHildon1WindowMgrPrivate *priv = NULL;
299         gchar* uid = NULL;
300         gboolean has_header, has_window = FALSE;
301         GList *item = NULL;
302
303         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
304         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
305         
306         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
307
308         has_header = MODEST_WINDOW_MGR_CLASS (parent_class)->find_registered_header (self, header, win);
309         
310         uid = modest_tny_folder_get_header_unique_id (header);
311         
312         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
313         if (item) {
314                 has_window = TRUE;
315                 if (win) {
316                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
317                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
318                                 g_debug ("not a valid window!");
319                         else {
320                                 g_debug ("found a window");
321                                 *win = MODEST_WINDOW (item->data);
322                         }
323                 }
324         }
325         g_free (uid);
326         
327         return has_header || has_window;
328 }
329
330 static GList *
331 modest_hildon1_window_mgr_get_window_list (ModestWindowMgr *self)
332 {
333         ModestHildon1WindowMgrPrivate *priv;
334
335         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
336         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
337
338         return g_list_copy (priv->window_list);
339 }
340
341 static const gchar *
342 get_show_toolbar_key (GType window_type,
343                       gboolean fullscreen)
344 {
345         const gchar *key = NULL;
346
347         if (window_type == MODEST_TYPE_MAIN_WINDOW)
348                 key = (fullscreen) ? 
349                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
350                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
351         else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
352                 key = (fullscreen) ? 
353                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
354                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
355         else if (window_type ==  MODEST_TYPE_MSG_EDIT_WINDOW)
356                 key = (fullscreen) ? 
357                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
358                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
359         else
360                 g_return_val_if_reached (NULL);
361
362         return key;
363 }
364
365 #ifndef MODEST_TOOLKIT_GTK
366 static void
367 on_window_is_topmost (GObject    *gobject,
368                       GParamSpec *arg1,
369                       gpointer    user_data)
370 {
371         ModestHildon1WindowMgr *self;
372         ModestHildon1WindowMgrPrivate *priv;
373         ModestWindow *win = (ModestWindow *) gobject;
374
375         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data));
376
377         self = MODEST_HILDON1_WINDOW_MGR (user_data);
378         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
379
380         if (hildon_window_get_is_topmost (HILDON_WINDOW (win)))
381                 priv->current_top = win;
382 }
383
384 #else
385 static gboolean
386 on_window_state_event (GtkWidget           *widget,
387                        GdkEventWindowState *event,
388                        gpointer             user_data)
389 {
390         ModestHildon1WindowMgr *self;
391         ModestHildon1WindowMgrPrivate *priv;
392
393         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data), FALSE);
394
395         self = MODEST_HILDON1_WINDOW_MGR (user_data);
396         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
397
398         MODEST_DEBUG_BLOCK (
399                             if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
400                                     g_print ("GDK_WINDOW_STATE_WITHDRAWN\n");
401                             if (event->changed_mask & GDK_WINDOW_STATE_ICONIFIED)
402                                     g_print ("GDK_WINDOW_STATE_ICONIFIED\n");
403                             if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED)
404                                     g_print ("GDK_WINDOW_STATE_MAXIMIZED\n");
405                             if (event->changed_mask & GDK_WINDOW_STATE_STICKY)
406                                     g_print ("GDK_WINDOW_STATE_STICKY\n");
407                             if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
408                                     g_print ("GDK_WINDOW_STATE_FULLSCREEN\n");
409                             if (event->changed_mask & GDK_WINDOW_STATE_ABOVE)
410                                     g_print ("GDK_WINDOW_STATE_ABOVE\n");
411                             if (event->changed_mask & GDK_WINDOW_STATE_BELOW)
412                                     g_print ("GDK_WINDOW_STATE_BELOW\n");
413                             );
414         if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN ||
415             event->changed_mask & GDK_WINDOW_STATE_ABOVE) {
416                 priv->current_top = MODEST_WINDOW (widget);
417         }
418         
419         return FALSE;
420 }
421 #endif
422
423 static gboolean
424 modest_hildon1_window_mgr_register_window (ModestWindowMgr *self, 
425                                            ModestWindow *window,
426                                            ModestWindow *parent)
427 {
428         GList *win;
429         ModestHildon1WindowMgrPrivate *priv;
430         gint *handler_id;
431         const gchar *key;
432         ModestWindow *main_window;
433
434         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
435         g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
436
437         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
438
439         win = g_list_find (priv->window_list, window);
440         if (win) {
441                 /* this is for the case we want to register the window and it was already
442                  * registered. We leave silently.
443                  */
444                 return FALSE;
445         }
446         
447         if (!MODEST_WINDOW_MGR_CLASS (parent_class)->register_window (self, window, parent))
448                 return FALSE;
449         
450         /* Add to list. Keep a reference to the window */
451         g_object_ref (window);
452         priv->window_list = g_list_prepend (priv->window_list, window);
453
454         /* Listen to window state changes. Unfortunately
455            window-state-event does not properly work for the Maemo
456            version, so we need to use is-topmost and the ifdef */
457 #ifndef MODEST_TOOLKIT_GTK
458         priv->window_state_uids = 
459                 modest_signal_mgr_connect (priv->window_state_uids, 
460                                            G_OBJECT (window), 
461                                            "notify::is-topmost",
462                                            G_CALLBACK (on_window_is_topmost), 
463                                            self);
464 #else
465         priv->window_state_uids = 
466                 modest_signal_mgr_connect (priv->window_state_uids, 
467                                            G_OBJECT (window), 
468                                            "window-state-event",
469                                            G_CALLBACK (on_window_state_event), 
470                                            self);
471 #endif
472         /* Listen to object destruction */
473         handler_id = g_malloc0 (sizeof (gint));
474         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
475         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
476
477         main_window = modest_window_mgr_get_main_window (self, FALSE);
478         /* If there is a msg view window, let the main window listen the msg-changed signal */
479         if (MODEST_IS_MSG_VIEW_WINDOW(window) && main_window) {
480                 gulong *handler;
481                 handler = g_malloc0 (sizeof (gulong));
482                 *handler = g_signal_connect (window, "msg-changed", 
483                                              G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed), 
484                                              main_window);
485                 g_hash_table_insert (priv->viewer_handlers, window, handler);
486         }
487
488         /* Put into fullscreen if needed */
489         if (priv->fullscreen_mode)
490                 gtk_window_fullscreen (GTK_WINDOW (window));
491
492         /* Show/hide toolbar & fullscreen */    
493         key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
494         modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
495
496         return TRUE;
497 }
498
499 static void
500 cancel_window_operations (ModestWindow *window)
501 {
502         GSList* pending_ops = NULL;
503
504         /* cancel open and receive operations */
505         pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (), 
506                                                                  G_OBJECT (window));
507         while (pending_ops != NULL) {
508                 ModestMailOperationTypeOperation type;
509                 GSList* tmp_list = NULL;
510
511                 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
512                 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
513                         modest_mail_operation_cancel (pending_ops->data);
514                 }
515                 g_object_unref (G_OBJECT (pending_ops->data));
516                 tmp_list = pending_ops;
517                 pending_ops = g_slist_next (pending_ops);
518                 g_slist_free_1 (tmp_list);
519         }
520 }
521
522
523
524 static gboolean
525 on_window_destroy (ModestWindow *window, 
526                    GdkEvent *event,
527                    ModestHildon1WindowMgr *self)
528 {
529         gint dialog_response = GTK_RESPONSE_NONE;
530         gboolean no_propagate = FALSE;
531
532         /* Specific stuff first */
533         if (MODEST_IS_MAIN_WINDOW (window)) {
534                 ModestHildon1WindowMgrPrivate *priv;
535                 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
536
537                 /* If more than one window already opened */
538                 if (g_list_length (priv->window_list) > 1) {
539
540                         /* Present the window if it's not visible now */
541                         if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window))) {
542                                 gtk_window_present (GTK_WINDOW (window));
543                                 priv->current_top = window;
544                         }
545                         /* Create the confirmation dialog MSG-NOT308 */
546                         dialog_response = modest_platform_run_confirmation_dialog (
547                                         GTK_WINDOW (window), _("emev_nc_close_windows"));
548
549                         /* If the user wants to close all the windows */
550                         if ((dialog_response == GTK_RESPONSE_OK) 
551                             || (dialog_response == GTK_RESPONSE_ACCEPT) 
552                             || (dialog_response == GTK_RESPONSE_YES)) {
553                                         GList *iter = priv->window_list;
554                                         do {
555                                                 if (iter->data != window) {
556                                                         GList *tmp = iter->next;
557                                                         on_window_destroy (MODEST_WINDOW (iter->data),
558                                                                         event,
559                                                                         self);
560                                                         iter = tmp;
561                                                 } else {
562                                                         iter = g_list_next (iter);
563                                                 }
564                                         } while (iter);
565                         } else {
566                                 return TRUE;
567                         }
568                 }
569
570                 /* Do not unregister it, just hide */
571                 gtk_widget_hide_all (GTK_WIDGET (window));
572
573                 /* Cancel pending operations */
574                 cancel_window_operations (window);
575
576                 /* Fake the window system, make it think that there is no window */
577                 if (modest_window_mgr_num_windows (MODEST_WINDOW_MGR (self)) == 0)
578                         g_signal_emit_by_name (self, "window-list-empty");
579
580                 no_propagate = TRUE;
581         }
582         else {
583                 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
584                         gboolean sent = FALSE;
585                         gint response = GTK_RESPONSE_ACCEPT;
586                         sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
587                         /* Save currently edited message to Drafts if it was not sent */
588                         if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
589
590                                 /* Raise the window if it's minimized */
591                                 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
592                                         gtk_window_present (GTK_WINDOW (window));
593                                 
594                                 response =
595                                         modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
596                                                                                  _("mcen_nc_no_email_message_modified_save_changes"));
597                                 /* Save to drafts */
598                                 if (response == GTK_RESPONSE_OK)
599                                         if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
600                                                 return TRUE;
601                         }
602                 }
603                 /* Unregister window */
604                 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (self), window);
605                 no_propagate = TRUE;
606         }
607
608         return no_propagate;
609 }
610
611 static void
612 disconnect_msg_changed (gpointer key, 
613                         gpointer value, 
614                         gpointer user_data)
615 {
616         guint handler_id;
617         handler_id = GPOINTER_TO_UINT(value);
618         
619         if (key && G_IS_OBJECT(key))
620                 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
621 }
622
623 static void 
624 modest_hildon1_window_mgr_unregister_window (ModestWindowMgr *self, 
625                                              ModestWindow *window)
626 {
627         GList *win;
628         ModestHildon1WindowMgrPrivate *priv;
629         gulong *tmp, handler_id;
630
631         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
632         g_return_if_fail (MODEST_IS_WINDOW (window));
633
634         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
635
636         win = g_list_find (priv->window_list, window);
637         if (!win) {
638                 g_warning ("Trying to unregister a window that has not being registered yet");
639                 return;
640         }
641
642         /* If it's the main window unset it */
643         if (MODEST_IS_MAIN_WINDOW (window)) {
644                 modest_window_mgr_set_main_window (self, NULL);
645
646                 /* Disconnect all emissions of msg-changed */
647                 if (priv->viewer_handlers) {
648                         g_hash_table_foreach (priv->viewer_handlers, 
649                                               disconnect_msg_changed, 
650                                               NULL);
651                         g_hash_table_destroy (priv->viewer_handlers);
652                         priv->viewer_handlers = NULL;
653                 }
654         }
655
656         /* Remove the viewer window handler from the hash table. The
657            HashTable could not exist if the main window was closed
658            when there were other windows remaining */
659         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
660                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
661                 /* If the viewer was created without a main window
662                    (for example when opening a message through D-Bus
663                    the viewer handlers was not registered */
664                 if (tmp) {
665                         g_signal_handler_disconnect (window, *tmp);
666                         g_hash_table_remove (priv->viewer_handlers, window);
667                 }
668         }
669
670         /* Remove from list & hash table */
671         priv->window_list = g_list_remove_link (priv->window_list, win);
672         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
673         handler_id = *tmp;
674
675         g_hash_table_remove (priv->destroy_handlers, window);
676
677         /* cancel open and receive operations */
678         cancel_window_operations (window);
679
680         /* Check if it's the topmost window, and remove the window from the stack.
681          * This is needed for the cases there's no other topmost window that will
682          * replace it in topmost handler.
683          */
684          if (window == priv->current_top)
685                  priv->current_top = NULL;
686
687         /* Disconnect the "window-state-event" handler, we won't need it anymore */
688         if (priv->window_state_uids) {
689 #ifndef MODEST_TOOLKIT_GTK
690                 priv->window_state_uids = 
691                         modest_signal_mgr_disconnect (priv->window_state_uids, 
692                                                       G_OBJECT (window), 
693                                                       "notify::is-topmost");
694 #else
695                 priv->window_state_uids = 
696                         modest_signal_mgr_disconnect (priv->window_state_uids, 
697                                                       G_OBJECT (window), 
698                                                       "window-state-event");
699 #endif
700         }
701         
702         /* Disconnect the "delete-event" handler, we won't need it anymore */
703         g_signal_handler_disconnect (window, handler_id);
704
705         /* Destroy the window */
706         g_object_unref (win->data);
707         g_list_free (win);
708         
709         MODEST_WINDOW_MGR_CLASS (parent_class)->unregister_window (self, window);
710
711         /* If there are no more windows registered emit the signal */
712         if (modest_window_mgr_num_windows (self) == 0)
713                 g_signal_emit_by_name (self, "window-list-empty");
714 }
715
716
717
718 static void
719 modest_hildon1_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
720                                                gboolean on)
721 {
722         ModestHildon1WindowMgrPrivate *priv;
723         GList *win = NULL;
724         ModestConf *conf;
725
726         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
727
728         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
729
730         /* If there is no change do nothing */
731         if (priv->fullscreen_mode == on)
732                 return;
733
734         priv->fullscreen_mode = on;
735
736         conf = modest_runtime_get_conf ();
737
738         /* Update windows */
739         win = priv->window_list;
740         while (win) {
741                 gboolean show;
742                 const gchar *key = NULL;
743
744                 /* Getting this from gconf everytime is not that
745                    expensive, we'll do it just a few times */
746                 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
747                 show = modest_conf_get_bool (conf, key, NULL);
748
749                 /* Set fullscreen/unfullscreen */
750                 if (on)
751                         gtk_window_fullscreen (GTK_WINDOW (win->data));
752                 else
753                         gtk_window_unfullscreen (GTK_WINDOW (win->data));
754
755                 /* Show/Hide toolbar */
756                 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
757
758                 win = g_list_next (win);
759         }
760 }
761
762 static gboolean
763 modest_hildon1_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
764 {
765         ModestHildon1WindowMgrPrivate *priv;
766
767         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
768
769         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
770
771         return priv->fullscreen_mode;
772 }
773
774 static void 
775 modest_hildon1_window_mgr_show_toolbars (ModestWindowMgr *self,
776                                          GType window_type,
777                                          gboolean show_toolbars,
778                                          gboolean fullscreen)
779 {
780         ModestHildon1WindowMgrPrivate *priv;
781         ModestConf *conf;
782         const gchar *key = NULL;
783
784         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
785
786         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
787         conf = modest_runtime_get_conf ();
788
789         /* If nothing changes then return */
790         key = get_show_toolbar_key (window_type, fullscreen);
791         conf = modest_runtime_get_conf ();
792         if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
793                 return;
794
795         /* Save in conf */
796         modest_conf_set_bool (conf, key, show_toolbars, NULL);
797
798         /* Apply now if the view mode is the right one */
799         if ((fullscreen && priv->fullscreen_mode) ||
800             (!fullscreen && !priv->fullscreen_mode)) {
801
802                 GList *win = priv->window_list;
803
804                 while (win) {
805                         if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
806                                 modest_window_show_toolbar (MODEST_WINDOW (win->data),
807                                                             show_toolbars);
808                         win = g_list_next (win);
809                 }
810         }
811 }
812
813 static ModestWindow*  
814 modest_hildon1_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
815 {
816         ModestHildon1WindowMgrPrivate *priv;
817         ModestWindow *result;
818         
819         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
820         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
821         
822         result = MODEST_WINDOW_MGR_CLASS (parent_class)->get_main_window (self, FALSE);
823         /* create the main window, if it hasn't been created yet */
824         if (!result && show) {
825                 /* modest_window_mgr_register_window will set priv->main_window */
826                 result = modest_main_window_new ();
827                 modest_window_mgr_register_window (self, result, NULL);
828                 gtk_widget_show_all (GTK_WIDGET (result));
829                 gtk_window_present (GTK_WINDOW (result));
830                 MODEST_DEBUG_BLOCK(
831                         g_debug ("%s: created main window: %p\n", __FUNCTION__, result);
832                 );
833         }
834         
835         return result;
836 }
837
838
839 static GtkWindow *
840 modest_hildon1_window_mgr_get_modal (ModestWindowMgr *self)
841 {
842         ModestHildon1WindowMgrPrivate *priv;
843         
844         g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
845         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
846
847         return g_queue_peek_head (priv->modal_windows);
848 }
849
850
851 static void
852 modest_hildon1_window_mgr_set_modal (ModestWindowMgr *self, 
853                                      GtkWindow *window,
854                                      GtkWindow *parent)
855 {
856         GtkWindow *old_modal;
857         ModestHildon1WindowMgrPrivate *priv;
858
859         g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
860         g_return_if_fail (GTK_IS_WINDOW (window));
861
862         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
863         g_mutex_lock (priv->queue_lock);
864         old_modal = g_queue_peek_head (priv->modal_windows);
865         g_mutex_unlock (priv->queue_lock);
866
867         if (!old_modal) {       
868                 /* make us transient wrt the main window then */
869                 if (priv->current_top && ((GtkWindow *) priv->current_top != window)) {
870                         gtk_window_set_transient_for (window, GTK_WINDOW(priv->current_top));
871                 } else {
872                         ModestWindow *main_win = modest_window_mgr_get_main_window (self, FALSE);
873                         if (GTK_WINDOW(main_win) != window) /* they should not be the same */
874                                 gtk_window_set_transient_for (window, GTK_WINDOW(main_win));
875                 }
876                 gtk_window_set_modal (window, TRUE);
877         } else {
878                 /* un-modalize the old one; the one on top should be the
879                  * modal one */
880                 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));   
881                 gtk_window_set_modal (window, TRUE);
882         }
883
884         /* this will be the new modal window */
885         g_mutex_lock (priv->queue_lock);
886         g_queue_push_head (priv->modal_windows, window);
887         g_mutex_unlock (priv->queue_lock);
888
889         if (GTK_IS_DIALOG (window)) {
890                 /* Note that response is not always enough because it
891                    could be captured and removed easily by dialogs but
892                    works for most of situations */
893                 priv->modal_handler_uids = 
894                         modest_signal_mgr_connect (priv->modal_handler_uids, 
895                                                    G_OBJECT (window), 
896                                                    "response",
897                                                    G_CALLBACK (on_modal_dialog_close), 
898                                                    self);
899                 /* We need this as well because dialogs are often
900                    destroyed with gtk_widget_destroy and this one will
901                    prevent response from happening */
902                 priv->modal_handler_uids = 
903                         modest_signal_mgr_connect (priv->modal_handler_uids, 
904                                                    G_OBJECT (window), 
905                                                    "destroy",
906                                                    G_CALLBACK (on_modal_dialog_destroy),
907                                                    self);
908         } else {
909                 priv->modal_handler_uids = 
910                         modest_signal_mgr_connect (priv->modal_handler_uids, 
911                                                    G_OBJECT (window), 
912                                                    "delete-event",
913                                                    G_CALLBACK (on_modal_window_close), 
914                                                    self);
915         }
916         /* Destroy width parent */
917         gtk_window_set_destroy_with_parent (window, TRUE);
918 }
919
920
921
922 static gboolean
923 idle_top_modal (gpointer data)
924 {
925         ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
926         ModestHildon1WindowMgrPrivate *priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
927         GtkWindow *topmost;
928
929         /* Get the top modal */
930         g_mutex_lock (priv->queue_lock);
931         topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
932         g_mutex_unlock (priv->queue_lock);
933
934         /* Show it */
935         if (topmost) {
936                 gdk_threads_enter ();
937                 gtk_window_present (topmost);
938                 /* It seems that the window looses modality if some
939                    other is shown on top of it after set_transient_for
940                    and set_parent */
941                 gtk_window_set_modal (topmost, TRUE);
942                 gdk_threads_leave ();
943         }
944
945         return FALSE;
946 }
947
948 static void
949 remove_modal_from_queue (GtkWidget *widget,
950                          ModestWindowMgr *self)
951 {
952         ModestHildon1WindowMgrPrivate *priv;
953         GList *item = NULL;
954
955         priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
956
957         /* Remove from queue. We don't use remove, because we want to
958            exit if the widget does not belong to the queue */
959         g_mutex_lock (priv->queue_lock);
960         item = g_queue_find (priv->modal_windows, widget);
961         if (!item) {
962                 g_warning ("Trying to remove a modal window that is not registered");
963                 g_mutex_unlock (priv->queue_lock);
964                 return;
965         }
966         g_queue_unlink (priv->modal_windows, item);
967         g_mutex_unlock (priv->queue_lock);
968
969         /* Disconnect handler */
970         priv->modal_handler_uids =
971                 modest_signal_mgr_disconnect (priv->modal_handler_uids,
972                                               G_OBJECT (widget),
973                                               GTK_IS_DIALOG (widget) ?
974                                               "response" :
975                                               "delete-event");
976         if (GTK_IS_DIALOG (widget))
977                 priv->modal_handler_uids =
978                         modest_signal_mgr_disconnect (priv->modal_handler_uids,
979                                                       G_OBJECT (widget),
980                                                       "destroy");
981
982         /* Schedule the next one for being shown */
983         g_idle_add (idle_top_modal, self);
984 }
985
986 static gboolean
987 on_modal_window_close (GtkWidget *widget,
988                        GdkEvent *event,
989                        gpointer user_data)
990 {
991         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
992
993         /* Remove modal window from queue */
994         remove_modal_from_queue (widget, self);
995
996         /* Continue */
997         return FALSE;
998 }
999
1000 static void
1001 on_modal_dialog_close (GtkDialog *dialog,
1002                        gint arg1,
1003                        gpointer user_data)
1004 {
1005         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1006
1007         /* Remove modal window from queue. Note that if "destroy"
1008            signal was invoked before the response the window could be
1009            already deleted */
1010         remove_modal_from_queue (GTK_WIDGET (dialog), self);
1011 }
1012
1013 static void
1014 on_modal_dialog_destroy (GtkObject *object,
1015                          gpointer   user_data)
1016 {
1017         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1018
1019         /* Remove modal window from queue */
1020         remove_modal_from_queue (GTK_WIDGET (object), self);
1021 }
1022