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