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