6031a59c8b293a943867bbc870ea356c827bce90
[modest] / src / hildon2 / modest-hildon2-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-hildon2-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 #include <hildon/hildon-program.h>
43
44 /* 'private'/'protected' functions */
45 static void modest_hildon2_window_mgr_class_init (ModestHildon2WindowMgrClass *klass);
46 static void modest_hildon2_window_mgr_instance_init (ModestHildon2WindowMgr *obj);
47 static void modest_hildon2_window_mgr_finalize   (GObject *obj);
48
49 static gboolean on_window_destroy        (ModestWindow *window,
50                                           GdkEvent *event,
51                                           ModestHildon2WindowMgr *self);
52
53 static gboolean modest_hildon2_window_mgr_register_window (ModestWindowMgr *self, 
54                                                            ModestWindow *window,
55                                                            ModestWindow *parent);
56 static void modest_hildon2_window_mgr_unregister_window (ModestWindowMgr *self, 
57                                                          ModestWindow *window);
58 static void modest_hildon2_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
59                                                            gboolean on);
60 static gboolean modest_hildon2_window_mgr_get_fullscreen_mode (ModestWindowMgr *self);
61 static void modest_hildon2_window_mgr_show_toolbars (ModestWindowMgr *self,
62                                                      GType window_type,
63                                                      gboolean show_toolbars,
64                                                      gboolean fullscreen);
65 static ModestWindow* modest_hildon2_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show);
66 static GtkWindow *modest_hildon2_window_mgr_get_modal (ModestWindowMgr *self);
67 static void modest_hildon2_window_mgr_set_modal (ModestWindowMgr *self, 
68                                                  GtkWindow *window,
69                                                  GtkWindow *parent);
70 static gboolean modest_hildon2_window_mgr_find_registered_header (ModestWindowMgr *self, 
71                                                                   TnyHeader *header,
72                                                                   ModestWindow **win);
73 static GList *modest_hildon2_window_mgr_get_window_list (ModestWindowMgr *self);
74 static gboolean modest_hildon2_window_mgr_close_all_windows (ModestWindowMgr *self);
75 static gboolean window_can_close (ModestWindow *window);
76 static gboolean window_has_modals (ModestWindow *window);
77
78 typedef struct _ModestHildon2WindowMgrPrivate ModestHildon2WindowMgrPrivate;
79 struct _ModestHildon2WindowMgrPrivate {
80         GList        *window_list;
81         GMutex       *queue_lock;
82         GQueue       *modal_windows;
83         
84         gboolean     fullscreen_mode;
85         
86         GHashTable   *destroy_handlers;
87         GHashTable   *viewer_handlers;
88         GSList       *window_state_uids;
89         
90         guint        closing_time;
91
92         GSList       *modal_handler_uids;
93         ModestWindow *current_top;
94 };
95 #define MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
96                                                                                    MODEST_TYPE_HILDON2_WINDOW_MGR, \
97                                                                                    ModestHildon2WindowMgrPrivate))
98 /* globals */
99 static GObjectClass *parent_class = NULL;
100
101 GType
102 modest_hildon2_window_mgr_get_type (void)
103 {
104         static GType my_type = 0;
105         if (!my_type) {
106                 static const GTypeInfo my_info = {
107                         sizeof(ModestHildon2WindowMgrClass),
108                         NULL,           /* base init */
109                         NULL,           /* base finalize */
110                         (GClassInitFunc) modest_hildon2_window_mgr_class_init,
111                         NULL,           /* class finalize */
112                         NULL,           /* class data */
113                         sizeof(ModestHildon2WindowMgr),
114                         1,              /* n_preallocs */
115                         (GInstanceInitFunc) modest_hildon2_window_mgr_instance_init,
116                         NULL
117                 };
118                 my_type = g_type_register_static (MODEST_TYPE_WINDOW_MGR,
119                                                   "ModestHildon2WindowMgr",
120                                                   &my_info, 0);
121         }
122         return my_type;
123 }
124
125 static void
126 modest_hildon2_window_mgr_class_init (ModestHildon2WindowMgrClass *klass)
127 {
128         GObjectClass *gobject_class;
129         ModestWindowMgrClass *mgr_class;
130
131         gobject_class = (GObjectClass*) klass;
132         mgr_class = (ModestWindowMgrClass *) klass;
133
134         parent_class            = g_type_class_peek_parent (klass);
135         gobject_class->finalize = modest_hildon2_window_mgr_finalize;
136         mgr_class->register_window = modest_hildon2_window_mgr_register_window;
137         mgr_class->unregister_window = modest_hildon2_window_mgr_unregister_window;
138         mgr_class->set_fullscreen_mode = modest_hildon2_window_mgr_set_fullscreen_mode;
139         mgr_class->get_fullscreen_mode = modest_hildon2_window_mgr_get_fullscreen_mode;
140         mgr_class->show_toolbars = modest_hildon2_window_mgr_show_toolbars;
141         mgr_class->get_main_window = modest_hildon2_window_mgr_get_main_window;
142         mgr_class->get_modal = modest_hildon2_window_mgr_get_modal;
143         mgr_class->set_modal = modest_hildon2_window_mgr_set_modal;
144         mgr_class->find_registered_header = modest_hildon2_window_mgr_find_registered_header;
145         mgr_class->get_window_list = modest_hildon2_window_mgr_get_window_list;
146         mgr_class->close_all_windows = modest_hildon2_window_mgr_close_all_windows;
147
148         g_type_class_add_private (gobject_class, sizeof(ModestHildon2WindowMgrPrivate));
149
150 }
151
152 static void
153 modest_hildon2_window_mgr_instance_init (ModestHildon2WindowMgr *obj)
154 {
155         ModestHildon2WindowMgrPrivate *priv;
156
157         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(obj);
158         priv->window_list = NULL;
159         priv->fullscreen_mode = FALSE;
160         priv->current_top = NULL;
161         priv->window_state_uids = NULL;
162
163         priv->modal_windows = g_queue_new ();
164         priv->queue_lock = g_mutex_new ();
165         
166         /* Could not initialize it from gconf, singletons are not
167            ready yet */
168         priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);   
169         priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
170
171         priv->closing_time = 0;
172
173         priv->modal_handler_uids = NULL;
174 }
175
176 static void
177 modest_hildon2_window_mgr_finalize (GObject *obj)
178 {
179         ModestHildon2WindowMgrPrivate *priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(obj);
180
181         modest_signal_mgr_disconnect_all_and_destroy (priv->window_state_uids);
182         priv->window_state_uids = NULL;
183
184         if (priv->window_list) {
185                 GList *iter = priv->window_list;
186                 /* unregister pending windows */
187                 while (iter) {
188                         ModestWindow *window = (ModestWindow *) iter->data;
189                         iter = g_list_next (iter);
190                         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), window);
191                 }
192                 g_list_free (priv->window_list);
193                 priv->window_list = NULL;
194         }
195
196         /* Free the hash table with the handlers */
197         if (priv->destroy_handlers) {
198                 g_hash_table_destroy (priv->destroy_handlers);
199                 priv->destroy_handlers = NULL;
200         }
201
202         if (priv->viewer_handlers) {
203                 g_hash_table_destroy (priv->viewer_handlers);
204                 priv->viewer_handlers = NULL;
205         }
206
207         modest_signal_mgr_disconnect_all_and_destroy (priv->modal_handler_uids);
208         priv->modal_handler_uids = NULL;
209
210         if (priv->modal_windows) {
211                 g_mutex_lock (priv->queue_lock);
212                 g_queue_free (priv->modal_windows);
213                 priv->modal_windows = NULL;
214                 g_mutex_unlock (priv->queue_lock);
215         }
216         g_mutex_free (priv->queue_lock);
217         
218         /* Do not unref priv->main_window because it does not hold a
219            new reference */
220
221         
222         G_OBJECT_CLASS(parent_class)->finalize (obj);
223 }
224
225 ModestWindowMgr*
226 modest_hildon2_window_mgr_new (void)
227 {
228         return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_HILDON2_WINDOW_MGR, NULL));
229 }
230
231 static gboolean
232 modest_hildon2_window_mgr_close_all_windows (ModestWindowMgr *self)
233 {
234         ModestHildon2WindowMgrPrivate *priv = NULL;
235         gboolean ret_value = FALSE;
236         HildonStackableWindow *window;
237         HildonProgram *program;
238         gboolean failed = FALSE;
239         
240         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
241         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
242         
243         program = hildon_program_get_instance ();
244
245         while ((window = hildon_program_peek_window_stack (program)) != NULL) {
246                 g_signal_emit_by_name (G_OBJECT (window), "delete-event", NULL, &ret_value);
247                 if (ret_value == TRUE) {
248                         failed = TRUE;
249                         break;
250                 }
251         }
252
253         return !failed;
254 }
255
256 static gint
257 compare_msguids (ModestWindow *win,
258                  const gchar *uid)
259 {
260         const gchar *msg_uid;
261
262         if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
263                 return 1;
264
265         /* Get message uid from msg window */
266         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
267                 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
268                 if (msg_uid && uid &&!strcmp (msg_uid, uid))
269                         return 0;
270         } else {
271                 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
272         }
273         
274         if (msg_uid && uid &&!strcmp (msg_uid, uid))
275                 return 0;
276         else
277                 return 1;
278 }
279
280 static gboolean
281 modest_hildon2_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
282                                                   ModestWindow **win)
283 {
284         ModestHildon2WindowMgrPrivate *priv = NULL;
285         gchar* uid = NULL;
286         gboolean has_header, has_window = FALSE;
287         GList *item = NULL;
288
289         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
290         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
291         
292         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
293
294         has_header = MODEST_WINDOW_MGR_CLASS (parent_class)->find_registered_header (self, header, win);
295         
296         uid = modest_tny_folder_get_header_unique_id (header);
297         
298         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
299         if (item) {
300                 has_window = TRUE;
301                 if (win) {
302                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
303                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
304                                 g_debug ("not a valid window!");
305                         else {
306                                 g_debug ("found a window");
307                                 *win = MODEST_WINDOW (item->data);
308                         }
309                 }
310         }
311         g_free (uid);
312         
313         return has_header || has_window;
314 }
315
316 static GList *
317 modest_hildon2_window_mgr_get_window_list (ModestWindowMgr *self)
318 {
319         ModestHildon2WindowMgrPrivate *priv;
320
321         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
322         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
323
324         return g_list_copy (priv->window_list);
325 }
326
327 static gboolean
328 modest_hildon2_window_mgr_register_window (ModestWindowMgr *self, 
329                                            ModestWindow *window,
330                                            ModestWindow *parent)
331 {
332         GList *win;
333         ModestHildon2WindowMgrPrivate *priv;
334         gint *handler_id;
335         ModestWindow *main_window;
336         HildonProgram *program;
337         GtkWidget *current_top;
338
339         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
340         g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
341
342         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
343
344         program = hildon_program_get_instance ();
345         win = g_list_find (priv->window_list, window);
346         if (win) {
347                 /* this is for the case we want to register the window and it was already
348                  * registered. We leave silently, telling the operation was succesful.
349                  */
350                 gtk_window_present (GTK_WINDOW (win));
351                 return TRUE;
352         }
353         
354         if (!MODEST_WINDOW_MGR_CLASS (parent_class)->register_window (self, window, parent))
355                 goto fail;
356         
357         /* Add to list. Keep a reference to the window */
358         g_object_ref (window);
359         priv->window_list = g_list_prepend (priv->window_list, window);
360
361         current_top = GTK_WIDGET (hildon_program_peek_window_stack (program));
362
363         if (!parent || ((GtkWidget *) parent != current_top)) {
364                 /* We have to close all windows with the exception of the root window */
365                 hildon_program_go_to_root_window (program);
366
367                 /* We remove 1 because we added it before */
368                 if (modest_window_mgr_num_windows (MODEST_WINDOW_MGR (self)) - 1> 1) {
369                         /* Then we couldn't close one of the preceding windows, so we have to stop the 
370                          * window register process and fail.
371                          */
372                         goto fail;
373                 }
374
375                 current_top = GTK_WIDGET (hildon_program_peek_window_stack (program));
376                 if ((current_top != NULL) && !MODEST_IS_MAIN_WINDOW (current_top)) {
377                         gboolean retval;
378                         g_signal_emit_by_name (G_OBJECT (current_top), "delete-event", NULL, &retval);
379                         if (retval == TRUE) {
380                                 /* Cancelled closing top window, then we fail to register */
381                                 goto fail;
382                         }
383                         current_top = GTK_WIDGET (hildon_program_peek_window_stack (program));
384                 }
385                 if ((current_top != NULL) && !MODEST_IS_MAIN_WINDOW (current_top)) {
386                         /* Closing the stacked windows failed, then we shouldn't show the
387                            new window */
388                         goto fail;
389                 }
390                 if ((current_top != NULL) && 
391                     window_has_modals (MODEST_WINDOW (current_top))) {
392                         /* Window on top but it has opened dialogs */
393                         goto fail;
394                 }
395         }
396
397         /* Listen to object destruction */
398         handler_id = g_malloc0 (sizeof (gint));
399         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
400         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
401
402         main_window = modest_window_mgr_get_main_window (self, FALSE);
403         /* If there is a msg view window, let the main window listen the msg-changed signal */
404         if (MODEST_IS_MSG_VIEW_WINDOW(window) && main_window) {
405                 gulong *handler;
406                 handler = g_malloc0 (sizeof (gulong));
407                 *handler = g_signal_connect (window, "msg-changed", 
408                                              G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed), 
409                                              main_window);
410                 g_hash_table_insert (priv->viewer_handlers, window, handler);
411         }
412
413         /* Show toolbar always */       
414         modest_window_show_toolbar (window, TRUE);
415
416         return TRUE;
417 fail:
418         /* Add to list. Keep a reference to the window */
419         priv->window_list = g_list_remove (priv->window_list, window);
420         g_object_unref (window);
421         current_top = GTK_WIDGET (hildon_program_peek_window_stack (program));
422         if (current_top)
423                 gtk_window_present (GTK_WINDOW (current_top));
424         return FALSE;
425 }
426
427 static void
428 cancel_window_operations (ModestWindow *window)
429 {
430         GSList* pending_ops = NULL;
431
432         /* cancel open and receive operations */
433         pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (), 
434                                                                  G_OBJECT (window));
435         while (pending_ops != NULL) {
436                 ModestMailOperationTypeOperation type;
437                 GSList* tmp_list = NULL;
438
439                 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
440                 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
441                         modest_mail_operation_cancel (pending_ops->data);
442                 }
443                 g_object_unref (G_OBJECT (pending_ops->data));
444                 tmp_list = pending_ops;
445                 pending_ops = g_slist_next (pending_ops);
446                 g_slist_free_1 (tmp_list);
447         }
448 }
449
450 static gboolean
451 window_has_modals (ModestWindow *window)
452 {
453         GList *toplevels;
454         GList *node;
455         gboolean retvalue = FALSE;
456
457         /* First we fetch all toplevels */
458         toplevels = gtk_window_list_toplevels ();
459         for (node = toplevels; node != NULL; node = g_list_next (node)) {
460                 if (GTK_IS_WINDOW (node->data) &&
461                     gtk_window_get_transient_for (GTK_WINDOW (node->data)) == GTK_WINDOW (window)) {
462                         retvalue = TRUE;
463                         break;
464                 }
465         }
466         g_list_free (toplevels);
467         return retvalue;
468 }
469
470 static gboolean
471 window_can_close (ModestWindow *window)
472 {
473         /* An editor can be always closed no matter the dialogs it has 
474          * on top. */
475         if (MODEST_IS_MSG_EDIT_WINDOW (window))
476                 return TRUE;
477
478         return !window_has_modals (window);
479 }
480
481 static gboolean
482 on_window_destroy (ModestWindow *window, 
483                    GdkEvent *event,
484                    ModestHildon2WindowMgr *self)
485 {
486         gboolean no_propagate = FALSE;
487
488         if (!window_can_close (window)) {
489                 return TRUE;
490         }
491         /* Specific stuff first */
492         if (MODEST_IS_MAIN_WINDOW (window)) {
493                 ModestHildon2WindowMgrPrivate *priv;
494                 priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
495
496                 /* Do not unregister it, just hide */
497                 gtk_widget_hide_all (GTK_WIDGET (window));
498
499                 /* Cancel pending operations */
500                 cancel_window_operations (window);
501
502                 /* Fake the window system, make it think that there is no window */
503                 if (modest_window_mgr_num_windows (MODEST_WINDOW_MGR (self)) == 0)
504                         g_signal_emit_by_name (self, "window-list-empty");
505
506                 no_propagate = TRUE;
507         }
508         else {
509                 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
510                         gboolean sent = FALSE;
511                         sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
512                         /* Save currently edited message to Drafts if it was not sent */
513                         if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
514
515                           if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
516                                   return TRUE;
517                         }
518                 }
519                 /* Unregister window */
520                 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (self), window);
521                 no_propagate = FALSE;
522         }
523
524         return no_propagate;
525 }
526
527 static void
528 disconnect_msg_changed (gpointer key, 
529                         gpointer value, 
530                         gpointer user_data)
531 {
532         guint handler_id;
533         handler_id = GPOINTER_TO_UINT(value);
534         
535         if (key && G_IS_OBJECT(key))
536                 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
537 }
538
539 static void 
540 modest_hildon2_window_mgr_unregister_window (ModestWindowMgr *self, 
541                                              ModestWindow *window)
542 {
543         GList *win;
544         ModestHildon2WindowMgrPrivate *priv;
545         gulong *tmp, handler_id;
546
547         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
548         g_return_if_fail (MODEST_IS_WINDOW (window));
549
550         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
551
552         win = g_list_find (priv->window_list, window);
553         if (!win) {
554                 g_warning ("Trying to unregister a window that has not being registered yet");
555                 return;
556         }
557
558         /* If it's the main window unset it */
559         if (MODEST_IS_MAIN_WINDOW (window)) {
560                 modest_window_mgr_set_main_window (self, NULL);
561
562                 /* Disconnect all emissions of msg-changed */
563                 if (priv->viewer_handlers) {
564                         g_hash_table_foreach (priv->viewer_handlers, 
565                                               disconnect_msg_changed, 
566                                               NULL);
567                         g_hash_table_destroy (priv->viewer_handlers);
568                         priv->viewer_handlers = NULL;
569                 }
570         }
571
572         /* Remove the viewer window handler from the hash table. The
573            HashTable could not exist if the main window was closed
574            when there were other windows remaining */
575         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
576                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
577                 /* If the viewer was created without a main window
578                    (for example when opening a message through D-Bus
579                    the viewer handlers was not registered */
580                 if (tmp) {
581                         g_signal_handler_disconnect (window, *tmp);
582                         g_hash_table_remove (priv->viewer_handlers, window);
583                 }
584         }
585
586         /* Remove from list & hash table */
587         priv->window_list = g_list_remove_link (priv->window_list, win);
588         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
589         handler_id = *tmp;
590
591         g_hash_table_remove (priv->destroy_handlers, window);
592
593         /* cancel open and receive operations */
594         cancel_window_operations (window);
595
596         /* Check if it's the topmost window, and remove the window from the stack.
597          * This is needed for the cases there's no other topmost window that will
598          * replace it in topmost handler.
599          */
600          if (window == priv->current_top)
601                  priv->current_top = NULL;
602
603         /* Disconnect the "window-state-event" handler, we won't need it anymore */
604         if (priv->window_state_uids) {
605 #ifndef MODEST_TOOLKIT_GTK
606                 priv->window_state_uids = 
607                         modest_signal_mgr_disconnect (priv->window_state_uids, 
608                                                       G_OBJECT (window), 
609                                                       "notify::is-topmost");
610 #else
611                 priv->window_state_uids = 
612                         modest_signal_mgr_disconnect (priv->window_state_uids, 
613                                                       G_OBJECT (window), 
614                                                       "window-state-event");
615 #endif
616         }
617         
618         /* Disconnect the "delete-event" handler, we won't need it anymore */
619         g_signal_handler_disconnect (window, handler_id);
620
621         /* Destroy the window */
622         g_object_unref (win->data);
623         g_list_free (win);
624         
625         MODEST_WINDOW_MGR_CLASS (parent_class)->unregister_window (self, window);
626
627         /* If there are no more windows registered emit the signal */
628         if (modest_window_mgr_num_windows (self) == 0)
629                 g_signal_emit_by_name (self, "window-list-empty");
630 }
631
632
633
634 static void
635 modest_hildon2_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
636                                                gboolean on)
637 {
638         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
639
640         return;
641 }
642
643 static gboolean
644 modest_hildon2_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
645 {
646         return FALSE;
647 }
648
649 static void 
650 modest_hildon2_window_mgr_show_toolbars (ModestWindowMgr *self,
651                                          GType window_type,
652                                          gboolean show_toolbars,
653                                          gboolean fullscreen)
654 {
655         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
656
657         return;
658 }
659
660 static ModestWindow*  
661 modest_hildon2_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
662 {
663         ModestHildon2WindowMgrPrivate *priv;
664         ModestWindow *result;
665         
666         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
667         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
668         
669         result = MODEST_WINDOW_MGR_CLASS (parent_class)->get_main_window (self, FALSE);
670         /* create the main window, if it hasn't been created yet */
671         if (!result && show) {
672                 /* modest_window_mgr_register_window will set priv->main_window */
673                 result = modest_main_window_new ();
674                 /* We have to remove all other windows */
675                 if (!modest_window_mgr_close_all_windows (self)) {
676                         gtk_widget_destroy (GTK_WIDGET (result));
677                         return NULL;
678                 }
679                 if (!modest_window_mgr_register_window (self, result, NULL)) {
680                         gtk_widget_destroy (GTK_WIDGET (result));
681                         return NULL;
682                 }
683                 MODEST_DEBUG_BLOCK(
684                         g_debug ("%s: created main window: %p\n", __FUNCTION__, result);
685                 );
686         }
687         if (show) {
688                 gtk_widget_show_all (GTK_WIDGET (result));
689                 gtk_window_present (GTK_WINDOW (result));
690         }
691         
692         return result;
693 }
694
695
696 static GtkWindow *
697 modest_hildon2_window_mgr_get_modal (ModestWindowMgr *self)
698 {
699         ModestHildon2WindowMgrPrivate *priv;
700         GList *toplevel_list;
701         GtkWidget *toplevel;
702         
703         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
704         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
705
706         toplevel = NULL;
707         toplevel_list = gtk_window_list_toplevels ();
708         while (toplevel_list) {
709                 if (gtk_window_is_active (toplevel_list->data)) {
710                         toplevel = toplevel_list->data;
711                         break;
712                 }
713                 toplevel_list = g_list_next (toplevel_list);
714         }
715
716         return NULL;
717 }
718
719
720 static void
721 modest_hildon2_window_mgr_set_modal (ModestWindowMgr *self, 
722                                      GtkWindow *window,
723                                      GtkWindow *parent)
724 {
725         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
726         g_return_if_fail (GTK_IS_WINDOW (window));
727
728         gtk_window_set_modal (window, TRUE);
729         gtk_window_set_transient_for (window, parent);
730         gtk_window_set_destroy_with_parent (window, TRUE);
731 }
732