5d8d156ca075cdfe658c258f76f3298ffeb532c4
[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         if (MODEST_IS_FOLDER_WINDOW (current_top) && MODEST_IS_FOLDER_WINDOW (window)) {
510                 gtk_window_present (GTK_WINDOW (window));
511                 return FALSE;
512         }
513
514         if (MODEST_IS_MAILBOXES_WINDOW (current_top) && MODEST_IS_MAILBOXES_WINDOW (window)) {
515                 gtk_window_present (GTK_WINDOW (window));
516                 return FALSE;
517         }
518
519         /* Mailboxes window can not replace folder windows */
520         if (MODEST_IS_FOLDER_WINDOW (current_top) && MODEST_IS_MAILBOXES_WINDOW (window)) {
521                 gtk_window_present (GTK_WINDOW (current_top));
522                 return FALSE;
523         }
524
525         /* Trying to open a folders window and a mailboxes window at
526            the same time from the accounts window is not allowed */
527         if (MODEST_IS_MAILBOXES_WINDOW (current_top) &&
528             MODEST_IS_FOLDER_WINDOW (window) &&
529             MODEST_IS_ACCOUNTS_WINDOW (parent)) {
530                 gtk_window_present (GTK_WINDOW (window));
531                 return FALSE;
532         }
533
534         if (MODEST_IS_HEADER_WINDOW (current_top) && MODEST_IS_HEADER_WINDOW (window)) {
535                 g_debug ("Trying to register a second header window is not allowed");
536                 gtk_window_present (GTK_WINDOW (current_top));
537                 return FALSE;
538         }
539
540         if (!MODEST_WINDOW_MGR_CLASS (parent_class)->register_window (self, window, parent))
541                 goto fail;
542
543         /* Add to list. Keep a reference to the window */
544         g_object_ref (window);
545         priv->window_list = g_list_prepend (priv->window_list, window);
546
547         nested_msg = MODEST_IS_MSG_VIEW_WINDOW (window) &&
548                 MODEST_IS_MSG_VIEW_WINDOW (parent);
549
550         /* Close views if they're being shown. Nevertheless we must
551            allow nested messages */
552         if (!nested_msg &&
553             (MODEST_IS_MSG_EDIT_WINDOW (current_top) ||
554              MODEST_IS_MSG_VIEW_WINDOW (current_top))) {
555                 gboolean retval;
556
557                 /* If the current view has modal dialogs then
558                    we fail to register the new view */
559                 if ((current_top != NULL) &&
560                     window_has_modals (MODEST_WINDOW (current_top))) {
561                         /* Window on top but it has opened dialogs */
562                         goto fail;
563                 }
564
565                 /* Close the current view */
566                 g_signal_emit_by_name (G_OBJECT (current_top), "delete-event", NULL, &retval);
567                 if (retval == TRUE) {
568                         /* Cancelled closing top window, then we fail to register */
569                         goto fail;
570                 }
571         }
572
573         /* Listen to object destruction */
574         handler_id = g_malloc0 (sizeof (gint));
575         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
576         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
577
578         /* Show toolbar always */
579         modest_window_show_toolbar (window, TRUE);
580
581         return TRUE;
582 fail:
583         /* Add to list. Keep a reference to the window */
584         priv->window_list = g_list_remove (priv->window_list, window);
585         g_object_unref (window);
586         current_top = (ModestWindow *) hildon_window_stack_peek (stack);
587         if (current_top)
588                 gtk_window_present (GTK_WINDOW (current_top));
589         return FALSE;
590 }
591
592 static void
593 cancel_window_operations (ModestWindow *window)
594 {
595         GSList* pending_ops = NULL;
596
597         /* cancel open and receive operations */
598         pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (), 
599                                                                  G_OBJECT (window));
600         while (pending_ops != NULL) {
601                 ModestMailOperationTypeOperation type;
602                 GSList* tmp_list = NULL;
603
604                 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
605                 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
606                         modest_mail_operation_cancel (pending_ops->data);
607                 }
608                 g_object_unref (G_OBJECT (pending_ops->data));
609                 tmp_list = pending_ops;
610                 pending_ops = g_slist_next (pending_ops);
611                 g_slist_free_1 (tmp_list);
612         }
613 }
614
615 static gboolean
616 window_has_modals (ModestWindow *window)
617 {
618         GList *toplevels;
619         GList *node;
620         gboolean retvalue = FALSE;
621
622         /* First we fetch all toplevels */
623         toplevels = gtk_window_list_toplevels ();
624         for (node = toplevels; node != NULL; node = g_list_next (node)) {
625                 if (GTK_IS_WINDOW (node->data) &&
626                     gtk_window_get_transient_for (GTK_WINDOW (node->data)) == GTK_WINDOW (window) &&
627                     GTK_WIDGET_VISIBLE (node->data)) {
628                         retvalue = TRUE;
629                         break;
630                 }
631         }
632         g_list_free (toplevels);
633         return retvalue;
634 }
635
636 static gboolean
637 window_can_close (ModestWindow *window)
638 {
639         /* An editor can be always closed no matter the dialogs it has 
640          * on top. */
641         if (MODEST_IS_MSG_EDIT_WINDOW (window))
642                 return TRUE;
643
644         return !window_has_modals (window);
645 }
646
647 static gboolean
648 on_window_destroy (ModestWindow *window, 
649                    GdkEvent *event,
650                    ModestHildon2WindowMgr *self)
651 {
652         gboolean no_propagate = FALSE;
653
654         if (!window_can_close (window))
655                 return TRUE;
656
657         if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
658                 gboolean sent = FALSE;
659                 sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
660                 /* Save currently edited message to Drafts if it was not sent */
661                 if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
662                         ModestMsgEditWindow *edit_window;
663                         MsgData *data;
664
665                         edit_window = MODEST_MSG_EDIT_WINDOW (window);
666                         data = modest_msg_edit_window_get_msg_data (edit_window);
667
668                         if (data) {
669                                 gint parts_count;
670                                 guint64 parts_size, available_size, expected_size;
671
672                                 available_size = modest_utils_get_available_space (NULL);
673                                 modest_msg_edit_window_get_parts_size (edit_window, &parts_count, &parts_size);
674                                 expected_size = modest_tny_msg_estimate_size (data->plain_body,
675                                                                               data->html_body,
676                                                                               parts_count,
677                                                                               parts_size);
678                                 modest_msg_edit_window_free_msg_data (edit_window, data);
679                                 data = NULL;
680
681                                 /* If there is not enough space
682                                    available for saving the message
683                                    then show an error and close the
684                                    window without saving */
685                                 if (expected_size >= available_size) {
686                                         modest_platform_run_information_dialog (GTK_WINDOW (edit_window),
687                                                                                 _("mail_in_ui_save_error"),
688                                                                                 FALSE);
689                                 } else {
690                                         if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
691                                                 return TRUE;
692                                 }
693                         } else {
694                                 g_warning ("Edit window without message data. This is probably a bug");
695                         }
696                 }
697         }
698
699         /* Unregister window */
700         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (self), window);
701         no_propagate = FALSE;
702
703         return no_propagate;
704 }
705
706 static void
707 modest_hildon2_window_mgr_unregister_window (ModestWindowMgr *self, 
708                                              ModestWindow *window)
709 {
710         GList *win;
711         ModestHildon2WindowMgrPrivate *priv;
712         gulong *tmp, handler_id;
713         guint num_windows;
714
715         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
716         g_return_if_fail (MODEST_IS_WINDOW (window));
717
718         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
719
720         win = g_list_find (priv->window_list, window);
721         if (!win) {
722                 g_warning ("Trying to unregister a window that has not being registered yet");
723                 return;
724         }
725
726         /* Remove the viewer window handler from the hash table. The
727            HashTable could not exist if the main window was closed
728            when there were other windows remaining */
729         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
730                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
731                 /* If the viewer was created without a main window
732                    (for example when opening a message through D-Bus
733                    the viewer handlers was not registered */
734                 if (tmp) {
735                         g_signal_handler_disconnect (window, *tmp);
736                         g_hash_table_remove (priv->viewer_handlers, window);
737                 }
738         }
739
740         /* Remove from list & hash table */
741         priv->window_list = g_list_remove_link (priv->window_list, win);
742         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
743         handler_id = *tmp;
744
745         g_hash_table_remove (priv->destroy_handlers, window);
746
747         /* cancel open and receive operations */
748         cancel_window_operations (window);
749
750         /* Disconnect the "window-state-event" handler, we won't need it anymore */
751         if (priv->window_state_uids) {
752                 priv->window_state_uids = 
753                         modest_signal_mgr_disconnect (priv->window_state_uids, 
754                                                       G_OBJECT (window), 
755                                                       "notify::is-topmost");
756         }
757
758         /* Disconnect the "delete-event" handler, we won't need it anymore */
759         g_signal_handler_disconnect (window, handler_id);
760
761         /* Destroy the window */
762         g_object_unref (win->data);
763         g_list_free (win);
764
765         MODEST_WINDOW_MGR_CLASS (parent_class)->unregister_window (self, window);
766
767         /* We have to get the number of windows here in order not to
768            emit the signal too many times */
769         num_windows = modest_window_mgr_get_num_windows (self);
770
771         /* If there are no more windows registered emit the signal */
772         if (num_windows == 0)
773                 g_signal_emit_by_name (self, "window-list-empty");
774 }
775
776
777 static void
778 modest_hildon2_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
779                                                gboolean on)
780 {
781         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
782
783         return;
784 }
785
786 static gboolean
787 modest_hildon2_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
788 {
789         return FALSE;
790 }
791
792 static void 
793 modest_hildon2_window_mgr_show_toolbars (ModestWindowMgr *self,
794                                          GType window_type,
795                                          gboolean show_toolbars,
796                                          gboolean fullscreen)
797 {
798         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
799
800         return;
801 }
802
803 static ModestWindow*  
804 modest_hildon2_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
805 {
806         ModestHildon2WindowMgrPrivate *priv;
807         ModestWindow *result;
808
809         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
810         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
811
812         /* TODO: make this return NULL always */
813
814         result = MODEST_WINDOW_MGR_CLASS (parent_class)->get_main_window (self, FALSE);
815         /* create the main window, if it hasn't been created yet */
816         if (!result && show) {
817                 /* modest_window_mgr_register_window will set priv->main_window */
818                 result = modest_main_window_new ();
819                 /* We have to remove all other windows */
820                 if (!modest_window_mgr_close_all_windows (self)) {
821                         gtk_widget_destroy (GTK_WIDGET (result));
822                         return NULL;
823                 }
824                 if (!modest_window_mgr_register_window (self, result, NULL)) {
825                         gtk_widget_destroy (GTK_WIDGET (result));
826                         return NULL;
827                 }
828                 MODEST_DEBUG_BLOCK(
829                         g_debug ("%s: created main window: %p\n", __FUNCTION__, result);
830                 );
831         }
832         if (show) {
833                 gtk_widget_show_all (GTK_WIDGET (result));
834                 gtk_window_present (GTK_WINDOW (result));
835         }
836         
837         return result;
838 }
839
840
841 static GtkWindow *
842 modest_hildon2_window_mgr_get_modal (ModestWindowMgr *self)
843 {
844         ModestHildon2WindowMgrPrivate *priv;
845         GList *toplevel_list;
846         GtkWindow *toplevel;
847
848         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
849         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
850
851         toplevel = NULL;
852         toplevel_list = gtk_window_list_toplevels ();
853         while (toplevel_list) {
854                 if (gtk_window_is_active (toplevel_list->data) &&
855                     gtk_window_get_modal (toplevel_list->data)) {
856                         toplevel = (GtkWindow *) toplevel_list->data;
857                         break;
858                 }
859                 toplevel_list = g_list_next (toplevel_list);
860         }
861
862         return toplevel;
863 }
864
865
866 static void
867 modest_hildon2_window_mgr_set_modal (ModestWindowMgr *self, 
868                                      GtkWindow *window,
869                                      GtkWindow *parent)
870 {
871         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
872         g_return_if_fail (GTK_IS_WINDOW (window));
873
874         gtk_window_set_modal (window, TRUE);
875         gtk_window_set_transient_for (window, parent);
876         gtk_window_set_destroy_with_parent (window, TRUE);
877 }
878
879 static void
880 on_account_removed (TnyAccountStore *acc_store, 
881                     TnyAccount *account,
882                     gpointer user_data)
883 {
884         HildonWindowStack *stack;
885         ModestWindow *current_top;
886         gboolean has_accounts;
887
888         /* Ignore transport account removals */
889         if (TNY_IS_TRANSPORT_ACCOUNT (account))
890                 return;
891
892         stack = hildon_window_stack_get_default ();
893         current_top = (ModestWindow *) hildon_window_stack_peek (stack);
894         has_accounts = modest_account_mgr_has_accounts (modest_runtime_get_account_mgr (), TRUE);
895
896         /* if we're showing the header view of the currently deleted
897            account, or the outbox and we deleted the last account,
898            then close the window */
899         if (current_top && MODEST_IS_HEADER_WINDOW (current_top)) {
900                 ModestHeaderView *header_view;
901                 TnyFolder *folder;
902                 gboolean deleted = FALSE;
903
904                 header_view = modest_header_window_get_header_view (MODEST_HEADER_WINDOW (current_top));
905                 folder = modest_header_view_get_folder (header_view);
906                 if (folder) {
907                         gboolean retval;
908
909                         /* Close if it's my account */
910                         if (!TNY_IS_MERGE_FOLDER (folder)) {
911                                 TnyAccount *my_account;
912
913                                 my_account = tny_folder_get_account (folder);
914                                 if (my_account) {
915                                         if (my_account == account) {
916                                                 g_signal_emit_by_name (G_OBJECT (current_top),
917                                                                        "delete-event",
918                                                                        NULL, &retval);
919                                                 deleted = TRUE;
920                                         }
921                                         g_object_unref (my_account);
922                                 }
923                         }
924
925                         /* Close if viewing outbox and no account left */
926                         if (tny_folder_get_folder_type (folder) == TNY_FOLDER_TYPE_OUTBOX) {
927                                 if (!has_accounts) {
928                                         g_signal_emit_by_name (G_OBJECT (current_top),
929                                                                "delete-event",
930                                                                NULL, &retval);
931                                         deleted = TRUE;
932                                 }
933                         }
934                         g_object_unref (folder);
935
936                         if (deleted) {
937                                 current_top = (ModestWindow *) hildon_window_stack_peek (stack);
938                         }
939                 }
940         }
941 }
942
943 static ModestWindow *
944 modest_hildon2_window_mgr_show_initial_window (ModestWindowMgr *self)
945 {
946         ModestWindow *initial_window = NULL;
947         ModestHildon2WindowMgrPrivate *priv;
948
949         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
950
951         /* Connect to the account store "account-removed" signal". We
952            do this here because in the init the singletons are still
953            not initialized properly */
954         if (!g_signal_handler_is_connected (modest_runtime_get_account_store (),
955                                             priv->accounts_handler)) {
956                 priv->accounts_handler = g_signal_connect (modest_runtime_get_account_store (),
957                                                            "account-removed",
958                                                            G_CALLBACK (on_account_removed),
959                                                            self);
960         }
961
962         /* Return accounts window */
963         initial_window = MODEST_WINDOW (modest_accounts_window_new ());
964         modest_window_mgr_register_window (self, initial_window, NULL);
965
966         return initial_window;
967 }
968
969
970 static ModestWindow *
971 modest_hildon2_window_mgr_get_current_top (ModestWindowMgr *self)
972 {
973         HildonWindowStack *stack;
974         stack = hildon_window_stack_get_default ();
975         return (ModestWindow *) hildon_window_stack_peek (stack);
976 }
977
978 static gint 
979 find_folder_window (gconstpointer a,
980                     gconstpointer b)
981 {
982         return (MODEST_IS_FOLDER_WINDOW (a)) ? 0 : 1;
983 }
984
985 ModestWindow *
986 modest_hildon2_window_mgr_get_folder_window (ModestHildon2WindowMgr *self)
987 {
988         ModestHildon2WindowMgrPrivate *priv;
989         GList *window;
990
991         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
992
993         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
994
995         window = g_list_find_custom (priv->window_list,
996                                      NULL,
997                                      find_folder_window);
998
999         return (window != NULL) ? MODEST_WINDOW (window->data) : NULL;
1000 }
1001
1002 static gboolean
1003 modest_hildon2_window_mgr_screen_is_on (ModestWindowMgr *self)
1004 {
1005         ModestHildon2WindowMgrPrivate *priv = NULL;
1006
1007         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
1008
1009         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
1010         
1011         return (priv->display_state == OSSO_DISPLAY_ON) ? TRUE : FALSE;
1012 }
1013
1014 static void 
1015 osso_display_event_cb (osso_display_state_t state, 
1016                        gpointer data)
1017 {
1018         ModestHildon2WindowMgrPrivate *priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (data);
1019
1020         priv->display_state = state;
1021
1022         /* Stop blinking if the screen becomes on */
1023         if (priv->display_state == OSSO_DISPLAY_ON)
1024                 modest_platform_remove_new_mail_notifications (TRUE);
1025 }
1026