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