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