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