3cfc695c999a05761817be2c92c53f975dad4024
[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 <hildon/hildon.h>
32 #include "modest-hildon2-window.h"
33 #include "modest-hildon2-window-mgr.h"
34 #include "modest-msg-edit-window.h"
35 #include "modest-mailboxes-window.h"
36 #include "modest-header-window.h"
37 #include "modest-main-window.h"
38 #include "modest-conf.h"
39 #include "modest-defs.h"
40 #include "modest-signal-mgr.h"
41 #include "modest-runtime.h"
42 #include "modest-platform.h"
43 #include "modest-ui-actions.h"
44 #include "modest-debug.h"
45 #include "modest-tny-folder.h"
46 #include "modest-folder-window.h"
47 #include "modest-accounts-window.h"
48 #include "modest-maemo-utils.h"
49 #include "modest-utils.h"
50 #include "modest-tny-msg.h"
51 #include <tny-merge-folder.h>
52
53 /* 'private'/'protected' functions */
54 static void modest_hildon2_window_mgr_class_init (ModestHildon2WindowMgrClass *klass);
55 static void modest_hildon2_window_mgr_instance_init (ModestHildon2WindowMgr *obj);
56 static void modest_hildon2_window_mgr_finalize   (GObject *obj);
57
58 static gboolean on_window_destroy        (ModestWindow *window,
59                                           GdkEvent *event,
60                                           ModestHildon2WindowMgr *self);
61
62 static gboolean modest_hildon2_window_mgr_register_window (ModestWindowMgr *self, 
63                                                            ModestWindow *window,
64                                                            ModestWindow *parent);
65 static void modest_hildon2_window_mgr_unregister_window (ModestWindowMgr *self, 
66                                                          ModestWindow *window);
67 static void modest_hildon2_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
68                                                            gboolean on);
69 static gboolean modest_hildon2_window_mgr_get_fullscreen_mode (ModestWindowMgr *self);
70 static void modest_hildon2_window_mgr_show_toolbars (ModestWindowMgr *self,
71                                                      GType window_type,
72                                                      gboolean show_toolbars,
73                                                      gboolean fullscreen);
74 static ModestWindow* modest_hildon2_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show);
75 static GtkWindow *modest_hildon2_window_mgr_get_modal (ModestWindowMgr *self);
76 static void modest_hildon2_window_mgr_set_modal (ModestWindowMgr *self, 
77                                                  GtkWindow *window,
78                                                  GtkWindow *parent);
79 static gboolean modest_hildon2_window_mgr_find_registered_header (ModestWindowMgr *self, 
80                                                                   TnyHeader *header,
81                                                                   ModestWindow **win);
82 static gboolean modest_hildon2_window_mgr_find_registered_message_uid (ModestWindowMgr *self, 
83                                                                        const gchar *msg_uid,
84                                                                        ModestWindow **win);
85 static GList *modest_hildon2_window_mgr_get_window_list (ModestWindowMgr *self);
86 static gboolean modest_hildon2_window_mgr_close_all_windows (ModestWindowMgr *self);
87 static gboolean window_can_close (ModestWindow *window);
88 static gboolean window_has_modals (ModestWindow *window);
89 static ModestWindow *modest_hildon2_window_mgr_show_initial_window (ModestWindowMgr *self);
90 static ModestWindow *modest_hildon2_window_mgr_get_current_top (ModestWindowMgr *self);
91 static gboolean modest_hildon2_window_mgr_screen_is_on (ModestWindowMgr *self);
92 static void osso_display_event_cb (osso_display_state_t state, 
93                                    gpointer data);
94 static void on_account_removed (TnyAccountStore *acc_store, 
95                                 TnyAccount *account,
96                                 gpointer user_data);
97
98 typedef struct _ModestHildon2WindowMgrPrivate ModestHildon2WindowMgrPrivate;
99 struct _ModestHildon2WindowMgrPrivate {
100         GList        *window_list;
101         GMutex       *queue_lock;
102         GQueue       *modal_windows;
103
104         gboolean     fullscreen_mode;
105
106         GHashTable   *destroy_handlers;
107         GHashTable   *viewer_handlers;
108         GSList       *window_state_uids;
109
110         guint        closing_time;
111
112         GSList       *modal_handler_uids;
113         ModestWindow *current_top;
114
115         gulong        accounts_handler;
116
117         /* Display state */
118         osso_display_state_t display_state;
119 };
120 #define MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
121                                                                                    MODEST_TYPE_HILDON2_WINDOW_MGR, \
122                                                                                    ModestHildon2WindowMgrPrivate))
123 /* globals */
124 static GObjectClass *parent_class = NULL;
125
126 GType
127 modest_hildon2_window_mgr_get_type (void)
128 {
129         static GType my_type = 0;
130         if (!my_type) {
131                 static const GTypeInfo my_info = {
132                         sizeof(ModestHildon2WindowMgrClass),
133                         NULL,           /* base init */
134                         NULL,           /* base finalize */
135                         (GClassInitFunc) modest_hildon2_window_mgr_class_init,
136                         NULL,           /* class finalize */
137                         NULL,           /* class data */
138                         sizeof(ModestHildon2WindowMgr),
139                         1,              /* n_preallocs */
140                         (GInstanceInitFunc) modest_hildon2_window_mgr_instance_init,
141                         NULL
142                 };
143                 my_type = g_type_register_static (MODEST_TYPE_WINDOW_MGR,
144                                                   "ModestHildon2WindowMgr",
145                                                   &my_info, 0);
146         }
147         return my_type;
148 }
149
150 static void
151 modest_hildon2_window_mgr_class_init (ModestHildon2WindowMgrClass *klass)
152 {
153         GObjectClass *gobject_class;
154         ModestWindowMgrClass *mgr_class;
155
156         gobject_class = (GObjectClass*) klass;
157         mgr_class = (ModestWindowMgrClass *) klass;
158
159         parent_class            = g_type_class_peek_parent (klass);
160         gobject_class->finalize = modest_hildon2_window_mgr_finalize;
161         mgr_class->register_window = modest_hildon2_window_mgr_register_window;
162         mgr_class->unregister_window = modest_hildon2_window_mgr_unregister_window;
163         mgr_class->set_fullscreen_mode = modest_hildon2_window_mgr_set_fullscreen_mode;
164         mgr_class->get_fullscreen_mode = modest_hildon2_window_mgr_get_fullscreen_mode;
165         mgr_class->show_toolbars = modest_hildon2_window_mgr_show_toolbars;
166         mgr_class->get_main_window = modest_hildon2_window_mgr_get_main_window;
167         mgr_class->get_modal = modest_hildon2_window_mgr_get_modal;
168         mgr_class->set_modal = modest_hildon2_window_mgr_set_modal;
169         mgr_class->find_registered_header = modest_hildon2_window_mgr_find_registered_header;
170         mgr_class->find_registered_message_uid = modest_hildon2_window_mgr_find_registered_message_uid;
171         mgr_class->get_window_list = modest_hildon2_window_mgr_get_window_list;
172         mgr_class->close_all_windows = modest_hildon2_window_mgr_close_all_windows;
173         mgr_class->show_initial_window = modest_hildon2_window_mgr_show_initial_window;
174         mgr_class->get_current_top = modest_hildon2_window_mgr_get_current_top;
175         mgr_class->screen_is_on = modest_hildon2_window_mgr_screen_is_on;
176
177         g_type_class_add_private (gobject_class, sizeof(ModestHildon2WindowMgrPrivate));
178
179 }
180
181 static void
182 modest_hildon2_window_mgr_instance_init (ModestHildon2WindowMgr *obj)
183 {
184         ModestHildon2WindowMgrPrivate *priv;
185
186         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(obj);
187         priv->window_list = NULL;
188         priv->fullscreen_mode = FALSE;
189         priv->window_state_uids = NULL;
190
191         priv->modal_windows = g_queue_new ();
192         priv->queue_lock = g_mutex_new ();
193
194         /* Could not initialize it from gconf, singletons are not
195            ready yet */
196         priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
197         priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
198
199         priv->closing_time = 0;
200
201         priv->modal_handler_uids = NULL;
202         priv->display_state = OSSO_DISPLAY_ON;
203
204         /* Listen for changes in the screen, we don't want to show a
205            led pattern when the display is on for example */
206         osso_hw_set_display_event_cb (modest_maemo_utils_get_osso_context (),
207                                       osso_display_event_cb,
208                                       obj); 
209
210 }
211
212 static void
213 modest_hildon2_window_mgr_finalize (GObject *obj)
214 {
215         ModestHildon2WindowMgrPrivate *priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(obj);
216         ModestTnyAccountStore *acc_store;
217
218         modest_signal_mgr_disconnect_all_and_destroy (priv->window_state_uids);
219         priv->window_state_uids = NULL;
220
221         osso_hw_set_display_event_cb (modest_maemo_utils_get_osso_context (),
222                                       NULL, NULL);
223
224         acc_store = modest_runtime_get_account_store ();
225         if (acc_store && g_signal_handler_is_connected (acc_store, priv->accounts_handler))
226                 g_signal_handler_disconnect (acc_store, priv->accounts_handler);
227
228         if (priv->window_list) {
229                 GList *iter = priv->window_list;
230                 /* unregister pending windows */
231                 while (iter) {
232                         ModestWindow *window = (ModestWindow *) iter->data;
233                         iter = g_list_next (iter);
234                         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), window);
235                 }
236                 g_list_free (priv->window_list);
237                 priv->window_list = NULL;
238         }
239
240         /* Free the hash table with the handlers */
241         if (priv->destroy_handlers) {
242                 g_hash_table_destroy (priv->destroy_handlers);
243                 priv->destroy_handlers = NULL;
244         }
245
246         if (priv->viewer_handlers) {
247                 g_hash_table_destroy (priv->viewer_handlers);
248                 priv->viewer_handlers = NULL;
249         }
250
251         modest_signal_mgr_disconnect_all_and_destroy (priv->modal_handler_uids);
252         priv->modal_handler_uids = NULL;
253
254         if (priv->modal_windows) {
255                 g_mutex_lock (priv->queue_lock);
256                 g_queue_free (priv->modal_windows);
257                 priv->modal_windows = NULL;
258                 g_mutex_unlock (priv->queue_lock);
259         }
260         g_mutex_free (priv->queue_lock);
261
262         /* Do not unref priv->main_window because it does not hold a
263            new reference */
264
265         G_OBJECT_CLASS(parent_class)->finalize (obj);
266 }
267
268 ModestWindowMgr*
269 modest_hildon2_window_mgr_new (void)
270 {
271         return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_HILDON2_WINDOW_MGR, NULL));
272 }
273
274 static gboolean
275 modest_hildon2_window_mgr_close_all_windows (ModestWindowMgr *self)
276 {
277         ModestHildon2WindowMgrPrivate *priv = NULL;
278         gboolean ret_value = FALSE;
279         GtkWidget *window;
280         HildonWindowStack *stack;
281         gboolean failed = FALSE;
282
283         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
284         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
285
286         stack = hildon_window_stack_get_default ();
287
288         while ((window = hildon_window_stack_peek (stack)) != NULL) {
289                 g_signal_emit_by_name (G_OBJECT (window), "delete-event", NULL, &ret_value);
290                 if (ret_value == TRUE) {
291                         failed = TRUE;
292                         break;
293                 }
294         }
295
296         return !failed;
297 }
298
299 static gint
300 compare_msguids (ModestWindow *win,
301                  const gchar *uid)
302 {
303         const gchar *msg_uid;
304
305         if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
306                 return 1;
307
308         /* Get message uid from msg window */
309         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
310                 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
311                 if (msg_uid && uid &&!strcmp (msg_uid, uid))
312                         return 0;
313         } else {
314                 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
315         }
316
317         if (msg_uid && uid &&!strcmp (msg_uid, uid))
318                 return 0;
319         else
320                 return 1;
321 }
322
323 static gint
324 compare_headers (ModestWindow *win,
325                  TnyHeader *header)
326 {
327         TnyHeader *my_header;
328         gint result = 1;
329
330         if (!MODEST_IS_MSG_VIEW_WINDOW (win))
331                 return 1;
332
333         /* Get message uid from msg window */
334         my_header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
335         if (my_header) {
336                 if (my_header == header)
337                         result = 0;
338                 g_object_unref (my_header);
339         }
340         return result;
341 }
342
343
344 static gboolean
345 modest_hildon2_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
346                                                   ModestWindow **win)
347 {
348         ModestHildon2WindowMgrPrivate *priv = NULL;
349         gchar* uid = NULL;
350         gboolean has_header, has_window = FALSE;
351         GList *item = NULL;
352
353         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
354         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
355         
356         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
357
358         has_header = MODEST_WINDOW_MGR_CLASS (parent_class)->find_registered_header (self, header, win);
359         
360         uid = modest_tny_folder_get_header_unique_id (header);
361         
362         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
363         if (item) {
364                 has_window = TRUE;
365                 if (win) {
366                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
367                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
368                                 g_debug ("not a valid window!");
369                         else {
370                                 g_debug ("found a window");
371                                 *win = MODEST_WINDOW (item->data);
372                         }
373                 }
374         }
375         g_free (uid);
376         
377         return has_header || has_window;
378 }
379
380 static gboolean
381 modest_hildon2_window_mgr_find_registered_message_uid (ModestWindowMgr *self, const gchar *msg_uid,
382                                                        ModestWindow **win)
383 {
384         ModestHildon2WindowMgrPrivate *priv = NULL;
385         gboolean has_header, has_window = FALSE;
386         GList *item = NULL;
387
388         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
389         g_return_val_if_fail (msg_uid && msg_uid[0] != '\0', FALSE);
390         
391         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
392
393         has_header = MODEST_WINDOW_MGR_CLASS (parent_class)->find_registered_message_uid (self, msg_uid, win);
394         
395         item = g_list_find_custom (priv->window_list, msg_uid, (GCompareFunc) compare_msguids);
396         if (item) {
397                 has_window = TRUE;
398                 if (win) {
399                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
400                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
401                                 g_debug ("not a valid window!");
402                         else {
403                                 g_debug ("found a window");
404                                 *win = MODEST_WINDOW (item->data);
405                         }
406                 }
407         }
408         
409         return has_header || has_window;
410 }
411
412 static GList *
413 modest_hildon2_window_mgr_get_window_list (ModestWindowMgr *self)
414 {
415         ModestHildon2WindowMgrPrivate *priv;
416
417         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
418         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
419
420         return g_list_copy (priv->window_list);
421 }
422
423 static gboolean
424 modest_hildon2_window_mgr_register_window (ModestWindowMgr *self, 
425                                            ModestWindow *window,
426                                            ModestWindow *parent)
427 {
428         GList *win;
429         ModestHildon2WindowMgrPrivate *priv;
430         gint *handler_id;
431         HildonWindowStack *stack;
432         gboolean nested_msg = FALSE;
433         ModestWindow *current_top;
434         GtkWidget *modal;
435
436         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
437         g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
438
439         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
440
441         /* Check that there is no active modal dialog */
442         modal = (GtkWidget *) modest_window_mgr_get_modal (self);
443         while (modal && GTK_IS_DIALOG (modal)) {
444                 GtkWidget *parent;
445
446                 /* Get the parent */
447                 parent = (GtkWidget *) gtk_window_get_transient_for (GTK_WINDOW (modal));
448
449                 /* Try to close it */
450                 gtk_dialog_response (GTK_DIALOG (modal), GTK_RESPONSE_DELETE_EVENT);
451
452                 /* Maybe the dialog was not closed, because a close
453                    confirmation dialog for example. Then ignore the
454                    register process */
455                 if (GTK_IS_WINDOW (modal)) {
456                         gtk_window_present (GTK_WINDOW (modal));
457                         return FALSE;
458                 }
459
460                 /* Get next modal */
461                 modal = parent;
462         }
463
464         stack = hildon_window_stack_get_default ();
465         current_top = (ModestWindow *) hildon_window_stack_peek (stack);
466
467         win = g_list_find (priv->window_list, window);
468         if (win) {
469                 /* this is for the case we want to register the window
470                    and it was already registered */
471                 gtk_window_present (GTK_WINDOW (window));
472                 return FALSE;
473         }
474
475         /* Do not allow standalone editors or standalone viewers */
476         if (!current_top &&
477             (MODEST_IS_MSG_VIEW_WINDOW (window) ||
478              MODEST_IS_MSG_EDIT_WINDOW (window)))
479                 modest_window_mgr_show_initial_window (self);
480
481         if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
482                 gchar *uid;
483                 TnyHeader *header;
484                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (window));
485
486                 if (header) {
487                         uid = modest_tny_folder_get_header_unique_id (header);
488
489                         /* Embedded messages do not have uid */
490                         if (uid) {
491                                 if (g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids)) {
492                                         g_debug ("%s found another view window showing the same header", __FUNCTION__);
493                                         g_free (uid);
494                                         g_object_unref (header);
495                                         return FALSE;
496                                 }
497                                 g_free (uid);
498                         } else {
499                                 if (g_list_find_custom (priv->window_list, header, (GCompareFunc) compare_headers)) {
500                                         g_debug ("%s found another view window showing the same header", __FUNCTION__);
501                                         g_object_unref (header);
502                                         return FALSE;
503                                 }
504                         }
505                         g_object_unref (header);
506                 }
507         }
508
509         /* Do not go backwards */
510         if ((MODEST_IS_MSG_VIEW_WINDOW (current_top) || MODEST_IS_MSG_EDIT_WINDOW (current_top)) &&
511             (MODEST_IS_FOLDER_WINDOW (window) || MODEST_IS_ACCOUNTS_WINDOW (window) || 
512              MODEST_IS_MAILBOXES_WINDOW (window))) {
513                 gtk_window_present (GTK_WINDOW (window));
514                 return FALSE;
515         }
516
517         if (MODEST_IS_FOLDER_WINDOW (current_top) && MODEST_IS_FOLDER_WINDOW (window)) {
518                 gtk_window_present (GTK_WINDOW (window));
519                 return FALSE;
520         }
521
522         if (MODEST_IS_MAILBOXES_WINDOW (current_top) && MODEST_IS_MAILBOXES_WINDOW (window)) {
523                 gtk_window_present (GTK_WINDOW (window));
524                 return FALSE;
525         }
526
527         /* Mailboxes window can not replace folder windows */
528         if (MODEST_IS_FOLDER_WINDOW (current_top) && MODEST_IS_MAILBOXES_WINDOW (window)) {
529                 gtk_window_present (GTK_WINDOW (current_top));
530                 return FALSE;
531         }
532
533         /* Trying to open a folders window and a mailboxes window at
534            the same time from the accounts window is not allowed */
535         if (MODEST_IS_MAILBOXES_WINDOW (current_top) &&
536             MODEST_IS_FOLDER_WINDOW (window) &&
537             MODEST_IS_ACCOUNTS_WINDOW (parent)) {
538                 gtk_window_present (GTK_WINDOW (window));
539                 return FALSE;
540         }
541
542         if (MODEST_IS_HEADER_WINDOW (current_top) && MODEST_IS_HEADER_WINDOW (window)) {
543                 g_debug ("Trying to register a second header window is not allowed");
544                 gtk_window_present (GTK_WINDOW (current_top));
545                 return FALSE;
546         }
547
548         if (!MODEST_WINDOW_MGR_CLASS (parent_class)->register_window (self, window, parent))
549                 goto fail;
550
551         /* Add to list. Keep a reference to the window */
552         g_object_ref (window);
553         priv->window_list = g_list_prepend (priv->window_list, window);
554
555         nested_msg = MODEST_IS_MSG_VIEW_WINDOW (window) &&
556                 MODEST_IS_MSG_VIEW_WINDOW (parent);
557
558         /* Close views if they're being shown. Nevertheless we must
559            allow nested messages */
560         if (!nested_msg &&
561             (MODEST_IS_MSG_EDIT_WINDOW (current_top) ||
562              MODEST_IS_MSG_VIEW_WINDOW (current_top))) {
563                 gboolean retval;
564
565                 /* If the current view has modal dialogs then
566                    we fail to register the new view */
567                 if ((current_top != NULL) &&
568                     window_has_modals (MODEST_WINDOW (current_top))) {
569                         /* Window on top but it has opened dialogs */
570                         goto fail;
571                 }
572
573                 /* Close the current view */
574                 g_signal_emit_by_name (G_OBJECT (current_top), "delete-event", NULL, &retval);
575                 if (retval == TRUE) {
576                         /* Cancelled closing top window, then we fail to register */
577                         goto fail;
578                 }
579         }
580
581         /* Listen to object destruction */
582         handler_id = g_malloc0 (sizeof (gint));
583         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
584         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
585
586         /* Show toolbar always */
587         modest_window_show_toolbar (window, TRUE);
588
589         return TRUE;
590 fail:
591         /* Add to list. Keep a reference to the window */
592         priv->window_list = g_list_remove (priv->window_list, window);
593         g_object_unref (window);
594         current_top = (ModestWindow *) hildon_window_stack_peek (stack);
595         if (current_top)
596                 gtk_window_present (GTK_WINDOW (current_top));
597         return FALSE;
598 }
599
600 static void
601 cancel_window_operations (ModestWindow *window)
602 {
603         GSList* pending_ops = NULL;
604
605         /* cancel open and receive operations */
606         pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (), 
607                                                                  G_OBJECT (window));
608         while (pending_ops != NULL) {
609                 ModestMailOperationTypeOperation type;
610                 GSList* tmp_list = NULL;
611
612                 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
613                 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE ||
614                     type == MODEST_MAIL_OPERATION_TYPE_OPEN ||
615                     type == MODEST_MAIL_OPERATION_TYPE_SEND_AND_RECEIVE) {
616                         modest_mail_operation_cancel (pending_ops->data);
617                 }
618                 g_object_unref (G_OBJECT (pending_ops->data));
619                 tmp_list = pending_ops;
620                 pending_ops = g_slist_next (pending_ops);
621                 g_slist_free_1 (tmp_list);
622         }
623 }
624
625 static gboolean
626 window_has_modals (ModestWindow *window)
627 {
628         GList *toplevels;
629         GList *node;
630         gboolean retvalue = FALSE;
631
632         /* First we fetch all toplevels */
633         toplevels = gtk_window_list_toplevels ();
634         for (node = toplevels; node != NULL; node = g_list_next (node)) {
635                 if (GTK_IS_WINDOW (node->data) &&
636                     gtk_window_get_transient_for (GTK_WINDOW (node->data)) == GTK_WINDOW (window) &&
637                     GTK_WIDGET_VISIBLE (node->data)) {
638                         retvalue = TRUE;
639                         break;
640                 }
641         }
642         g_list_free (toplevels);
643         return retvalue;
644 }
645
646 static gboolean
647 window_can_close (ModestWindow *window)
648 {
649         /* An editor can be always closed no matter the dialogs it has 
650          * on top. */
651         if (MODEST_IS_MSG_EDIT_WINDOW (window))
652                 return TRUE;
653
654         return !window_has_modals (window);
655 }
656
657 static gboolean
658 on_window_destroy (ModestWindow *window, 
659                    GdkEvent *event,
660                    ModestHildon2WindowMgr *self)
661 {
662         gboolean no_propagate = FALSE;
663
664         if (!window_can_close (window))
665                 return TRUE;
666
667         if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
668                 gboolean sent = FALSE;
669                 sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
670                 /* Save currently edited message to Drafts if it was not sent */
671                 if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
672                         ModestMsgEditWindow *edit_window;
673                         MsgData *data;
674
675                         edit_window = MODEST_MSG_EDIT_WINDOW (window);
676                         data = modest_msg_edit_window_get_msg_data (edit_window);
677
678                         if (data) {
679                                 gint parts_count;
680                                 guint64 parts_size, available_size, expected_size;
681
682                                 available_size = modest_utils_get_available_space (NULL);
683                                 modest_msg_edit_window_get_parts_size (edit_window, &parts_count, &parts_size);
684                                 expected_size = modest_tny_msg_estimate_size (data->plain_body,
685                                                                               data->html_body,
686                                                                               parts_count,
687                                                                               parts_size);
688                                 modest_msg_edit_window_free_msg_data (edit_window, data);
689                                 data = NULL;
690
691                                 /* If there is not enough space
692                                    available for saving the message
693                                    then show an error and close the
694                                    window without saving */
695                                 if (expected_size >= available_size) {
696                                         modest_platform_run_information_dialog (GTK_WINDOW (edit_window),
697                                                                                 _("mail_in_ui_save_error"),
698                                                                                 FALSE);
699                                 } else {
700                                         if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
701                                                 return TRUE;
702                                 }
703                         } else {
704                                 g_warning ("Edit window without message data. This is probably a bug");
705                         }
706                 }
707         }
708
709         /* Unregister window */
710         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (self), window);
711         no_propagate = FALSE;
712
713         return no_propagate;
714 }
715
716 static void
717 modest_hildon2_window_mgr_unregister_window (ModestWindowMgr *self, 
718                                              ModestWindow *window)
719 {
720         GList *win;
721         ModestHildon2WindowMgrPrivate *priv;
722         gulong *tmp, handler_id;
723         guint num_windows;
724
725         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
726         g_return_if_fail (MODEST_IS_WINDOW (window));
727
728         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
729
730         win = g_list_find (priv->window_list, window);
731         if (!win) {
732                 g_warning ("Trying to unregister a window that has not being registered yet");
733                 return;
734         }
735
736         /* Remove the viewer window handler from the hash table. The
737            HashTable could not exist if the main window was closed
738            when there were other windows remaining */
739         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
740                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
741                 /* If the viewer was created without a main window
742                    (for example when opening a message through D-Bus
743                    the viewer handlers was not registered */
744                 if (tmp) {
745                         g_signal_handler_disconnect (window, *tmp);
746                         g_hash_table_remove (priv->viewer_handlers, window);
747                 }
748         }
749
750         /* Remove from list & hash table */
751         priv->window_list = g_list_remove_link (priv->window_list, win);
752         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
753         handler_id = *tmp;
754
755         g_hash_table_remove (priv->destroy_handlers, window);
756
757         /* cancel open and receive operations */
758         cancel_window_operations (window);
759
760         /* Disconnect the "window-state-event" handler, we won't need it anymore */
761         if (priv->window_state_uids) {
762                 priv->window_state_uids = 
763                         modest_signal_mgr_disconnect (priv->window_state_uids, 
764                                                       G_OBJECT (window), 
765                                                       "notify::is-topmost");
766         }
767
768         /* Disconnect the "delete-event" handler, we won't need it anymore */
769         g_signal_handler_disconnect (window, handler_id);
770
771         /* Destroy the window */
772         g_object_unref (win->data);
773         g_list_free (win);
774
775         MODEST_WINDOW_MGR_CLASS (parent_class)->unregister_window (self, window);
776
777         /* We have to get the number of windows here in order not to
778            emit the signal too many times */
779         num_windows = modest_window_mgr_get_num_windows (self);
780
781         /* If there are no more windows registered emit the signal */
782         if (num_windows == 0)
783                 g_signal_emit_by_name (self, "window-list-empty");
784 }
785
786
787 static void
788 modest_hildon2_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
789                                                gboolean on)
790 {
791         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
792
793         return;
794 }
795
796 static gboolean
797 modest_hildon2_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
798 {
799         return FALSE;
800 }
801
802 static void 
803 modest_hildon2_window_mgr_show_toolbars (ModestWindowMgr *self,
804                                          GType window_type,
805                                          gboolean show_toolbars,
806                                          gboolean fullscreen)
807 {
808         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
809
810         return;
811 }
812
813 static ModestWindow*  
814 modest_hildon2_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
815 {
816         ModestHildon2WindowMgrPrivate *priv;
817         ModestWindow *result;
818
819         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
820         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
821
822         /* TODO: make this return NULL always */
823
824         result = MODEST_WINDOW_MGR_CLASS (parent_class)->get_main_window (self, FALSE);
825         /* create the main window, if it hasn't been created yet */
826         if (!result && show) {
827                 /* modest_window_mgr_register_window will set priv->main_window */
828                 result = modest_main_window_new ();
829                 /* We have to remove all other windows */
830                 if (!modest_window_mgr_close_all_windows (self)) {
831                         gtk_widget_destroy (GTK_WIDGET (result));
832                         return NULL;
833                 }
834                 if (!modest_window_mgr_register_window (self, result, NULL)) {
835                         gtk_widget_destroy (GTK_WIDGET (result));
836                         return NULL;
837                 }
838                 MODEST_DEBUG_BLOCK(
839                         g_debug ("%s: created main window: %p\n", __FUNCTION__, result);
840                 );
841         }
842         if (show) {
843                 gtk_widget_show_all (GTK_WIDGET (result));
844                 gtk_window_present (GTK_WINDOW (result));
845         }
846
847         return result;
848 }
849
850 static gint
851 look_for_transient (gconstpointer a,
852                     gconstpointer b)
853 {
854         GtkWindow *win, *child;
855
856         if (a == b)
857                 return 1;
858
859         child = (GtkWindow *) b;
860         win = (GtkWindow *) a;
861
862         if (gtk_window_get_transient_for (win) == child)
863                 return 0;
864         else
865                 return 1;
866 }
867
868 static GtkWindow *
869 modest_hildon2_window_mgr_get_modal (ModestWindowMgr *self)
870 {
871         ModestHildon2WindowMgrPrivate *priv;
872         GList *toplevel_list;
873         GtkWidget *current_top, *toplevel;
874         HildonWindowStack *stack;
875
876         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
877         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
878
879         /* Get current top */
880         stack = hildon_window_stack_get_default ();
881         current_top = hildon_window_stack_peek (stack);
882         toplevel = current_top;
883         toplevel_list = gtk_window_list_toplevels ();
884
885         while (toplevel) {
886                 GList *parent_link;
887
888                 parent_link = g_list_find_custom (toplevel_list, toplevel, look_for_transient);
889                 if (parent_link)
890                         toplevel = (GtkWidget *) parent_link->data;
891                 else
892                         break;
893         }
894
895         return (GtkWindow *) toplevel;
896 }
897
898
899 static void
900 modest_hildon2_window_mgr_set_modal (ModestWindowMgr *self, 
901                                      GtkWindow *window,
902                                      GtkWindow *parent)
903 {
904         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
905         g_return_if_fail (GTK_IS_WINDOW (window));
906
907         gtk_window_set_modal (window, TRUE);
908         gtk_window_set_transient_for (window, parent);
909         gtk_window_set_destroy_with_parent (window, TRUE);
910 }
911
912 static void
913 close_all_but_first (HildonWindowStack *stack)
914 {
915         gint num_windows, i;
916         gboolean retval;
917
918         num_windows = hildon_window_stack_size (stack);
919
920         for (i = 0; i < (num_windows - 1); i++) {
921                 GtkWidget *current_top;
922
923                 /* Close window */
924                 current_top = hildon_window_stack_peek (stack);
925                 g_signal_emit_by_name (G_OBJECT (current_top), "delete-event", NULL, &retval);
926         }
927 }
928
929 static void
930 on_account_removed (TnyAccountStore *acc_store, 
931                     TnyAccount *account,
932                     gpointer user_data)
933 {
934         HildonWindowStack *stack;
935         ModestWindow *current_top;
936         gboolean has_accounts;
937
938         /* Ignore transport account removals */
939         if (TNY_IS_TRANSPORT_ACCOUNT (account))
940                 return;
941
942         stack = hildon_window_stack_get_default ();
943         current_top = (ModestWindow *) hildon_window_stack_peek (stack);
944         has_accounts = modest_account_mgr_has_accounts (modest_runtime_get_account_mgr (), TRUE);
945
946         /* if we're showing the header view of the currently deleted
947            account, or the outbox and we deleted the last account,
948            then close the window */
949         if (current_top && MODEST_IS_HEADER_WINDOW (current_top)) {
950                 ModestHeaderView *header_view;
951                 TnyFolder *folder;
952                 gboolean deleted = FALSE;
953
954                 header_view = modest_header_window_get_header_view (MODEST_HEADER_WINDOW (current_top));
955                 folder = modest_header_view_get_folder (header_view);
956                 if (folder) {
957                         /* Close if it's my account */
958                         if (!TNY_IS_MERGE_FOLDER (folder)) {
959                                 TnyAccount *my_account;
960
961                                 my_account = tny_folder_get_account (folder);
962                                 if (my_account) {
963                                         if (my_account == account) {
964                                                 close_all_but_first (stack);
965                                                 deleted = TRUE;
966                                         }
967                                         g_object_unref (my_account);
968                                 }
969                         }
970
971                         /* Close if viewing outbox and no account left */
972                         if (tny_folder_get_folder_type (folder) == TNY_FOLDER_TYPE_OUTBOX) {
973                                 if (!has_accounts) {
974                                         close_all_but_first (stack);
975                                         deleted = TRUE;
976                                 }
977                         }
978                         g_object_unref (folder);
979
980                         if (deleted)
981                                 current_top = (ModestWindow *) hildon_window_stack_peek (stack);
982                 }
983         }
984 }
985
986 static ModestWindow *
987 modest_hildon2_window_mgr_show_initial_window (ModestWindowMgr *self)
988 {
989         ModestWindow *initial_window = NULL;
990         ModestHildon2WindowMgrPrivate *priv;
991
992         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
993
994         /* Connect to the account store "account-removed" signal". We
995            do this here because in the init the singletons are still
996            not initialized properly */
997         if (!g_signal_handler_is_connected (modest_runtime_get_account_store (),
998                                             priv->accounts_handler)) {
999                 priv->accounts_handler = g_signal_connect (modest_runtime_get_account_store (),
1000                                                            "account-removed",
1001                                                            G_CALLBACK (on_account_removed),
1002                                                            self);
1003         }
1004
1005         /* Return accounts window */
1006         initial_window = MODEST_WINDOW (modest_accounts_window_new ());
1007         modest_window_mgr_register_window (self, initial_window, NULL);
1008
1009         return initial_window;
1010 }
1011
1012
1013 static ModestWindow *
1014 modest_hildon2_window_mgr_get_current_top (ModestWindowMgr *self)
1015 {
1016         HildonWindowStack *stack;
1017         stack = hildon_window_stack_get_default ();
1018         return (ModestWindow *) hildon_window_stack_peek (stack);
1019 }
1020
1021 static gint 
1022 find_folder_window (gconstpointer a,
1023                     gconstpointer b)
1024 {
1025         return (MODEST_IS_FOLDER_WINDOW (a)) ? 0 : 1;
1026 }
1027
1028 ModestWindow *
1029 modest_hildon2_window_mgr_get_folder_window (ModestHildon2WindowMgr *self)
1030 {
1031         ModestHildon2WindowMgrPrivate *priv;
1032         GList *window;
1033
1034         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
1035
1036         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
1037
1038         window = g_list_find_custom (priv->window_list,
1039                                      NULL,
1040                                      find_folder_window);
1041
1042         return (window != NULL) ? MODEST_WINDOW (window->data) : NULL;
1043 }
1044
1045 static gboolean
1046 modest_hildon2_window_mgr_screen_is_on (ModestWindowMgr *self)
1047 {
1048         ModestHildon2WindowMgrPrivate *priv = NULL;
1049
1050         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
1051
1052         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
1053         
1054         return (priv->display_state == OSSO_DISPLAY_ON) ? TRUE : FALSE;
1055 }
1056
1057 static void 
1058 osso_display_event_cb (osso_display_state_t state, 
1059                        gpointer data)
1060 {
1061         ModestHildon2WindowMgrPrivate *priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (data);
1062
1063         priv->display_state = state;
1064
1065         /* Stop blinking if the screen becomes on */
1066         if (priv->display_state == OSSO_DISPLAY_ON)
1067                 modest_platform_remove_new_mail_notifications (TRUE);
1068 }
1069