* Fixes NB#95749, properly manage new windows
[modest] / src / hildon2 / modest-hildon2-window-mgr.c
1 /* Copyright (c) 2008, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <string.h>
31 #include <hildon/hildon.h>
32 #include "modest-hildon2-window-mgr.h"
33 #include "modest-msg-edit-window.h"
34 #include "modest-main-window.h"
35 #include "modest-conf.h"
36 #include "modest-defs.h"
37 #include "modest-signal-mgr.h"
38 #include "modest-runtime.h"
39 #include "modest-platform.h"
40 #include "modest-ui-actions.h"
41 #include "modest-debug.h"
42 #include "modest-tny-folder.h"
43 #include "modest-folder-window.h"
44 #include "modest-accounts-window.h"
45
46 /* 'private'/'protected' functions */
47 static void modest_hildon2_window_mgr_class_init (ModestHildon2WindowMgrClass *klass);
48 static void modest_hildon2_window_mgr_instance_init (ModestHildon2WindowMgr *obj);
49 static void modest_hildon2_window_mgr_finalize   (GObject *obj);
50
51 static gboolean on_window_destroy        (ModestWindow *window,
52                                           GdkEvent *event,
53                                           ModestHildon2WindowMgr *self);
54
55 static gboolean modest_hildon2_window_mgr_register_window (ModestWindowMgr *self, 
56                                                            ModestWindow *window,
57                                                            ModestWindow *parent);
58 static void modest_hildon2_window_mgr_unregister_window (ModestWindowMgr *self, 
59                                                          ModestWindow *window);
60 static void modest_hildon2_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
61                                                            gboolean on);
62 static gboolean modest_hildon2_window_mgr_get_fullscreen_mode (ModestWindowMgr *self);
63 static void modest_hildon2_window_mgr_show_toolbars (ModestWindowMgr *self,
64                                                      GType window_type,
65                                                      gboolean show_toolbars,
66                                                      gboolean fullscreen);
67 static ModestWindow* modest_hildon2_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show);
68 static GtkWindow *modest_hildon2_window_mgr_get_modal (ModestWindowMgr *self);
69 static void modest_hildon2_window_mgr_set_modal (ModestWindowMgr *self, 
70                                                  GtkWindow *window,
71                                                  GtkWindow *parent);
72 static gboolean modest_hildon2_window_mgr_find_registered_header (ModestWindowMgr *self, 
73                                                                   TnyHeader *header,
74                                                                   ModestWindow **win);
75 static GList *modest_hildon2_window_mgr_get_window_list (ModestWindowMgr *self);
76 static gboolean modest_hildon2_window_mgr_close_all_windows (ModestWindowMgr *self);
77 static gboolean window_can_close (ModestWindow *window);
78 static gboolean window_has_modals (ModestWindow *window);
79 static ModestWindow *modest_hildon2_window_mgr_show_initial_window (ModestWindowMgr *self);
80
81 typedef struct _ModestHildon2WindowMgrPrivate ModestHildon2WindowMgrPrivate;
82 struct _ModestHildon2WindowMgrPrivate {
83         GList        *window_list;
84         GMutex       *queue_lock;
85         GQueue       *modal_windows;
86
87         gboolean     fullscreen_mode;
88
89         GHashTable   *destroy_handlers;
90         GHashTable   *viewer_handlers;
91         GSList       *window_state_uids;
92
93         guint        closing_time;
94
95         GSList       *modal_handler_uids;
96         ModestWindow *current_top;
97 };
98 #define MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
99                                                                                    MODEST_TYPE_HILDON2_WINDOW_MGR, \
100                                                                                    ModestHildon2WindowMgrPrivate))
101 /* globals */
102 static GObjectClass *parent_class = NULL;
103
104 GType
105 modest_hildon2_window_mgr_get_type (void)
106 {
107         static GType my_type = 0;
108         if (!my_type) {
109                 static const GTypeInfo my_info = {
110                         sizeof(ModestHildon2WindowMgrClass),
111                         NULL,           /* base init */
112                         NULL,           /* base finalize */
113                         (GClassInitFunc) modest_hildon2_window_mgr_class_init,
114                         NULL,           /* class finalize */
115                         NULL,           /* class data */
116                         sizeof(ModestHildon2WindowMgr),
117                         1,              /* n_preallocs */
118                         (GInstanceInitFunc) modest_hildon2_window_mgr_instance_init,
119                         NULL
120                 };
121                 my_type = g_type_register_static (MODEST_TYPE_WINDOW_MGR,
122                                                   "ModestHildon2WindowMgr",
123                                                   &my_info, 0);
124         }
125         return my_type;
126 }
127
128 static void
129 modest_hildon2_window_mgr_class_init (ModestHildon2WindowMgrClass *klass)
130 {
131         GObjectClass *gobject_class;
132         ModestWindowMgrClass *mgr_class;
133
134         gobject_class = (GObjectClass*) klass;
135         mgr_class = (ModestWindowMgrClass *) klass;
136
137         parent_class            = g_type_class_peek_parent (klass);
138         gobject_class->finalize = modest_hildon2_window_mgr_finalize;
139         mgr_class->register_window = modest_hildon2_window_mgr_register_window;
140         mgr_class->unregister_window = modest_hildon2_window_mgr_unregister_window;
141         mgr_class->set_fullscreen_mode = modest_hildon2_window_mgr_set_fullscreen_mode;
142         mgr_class->get_fullscreen_mode = modest_hildon2_window_mgr_get_fullscreen_mode;
143         mgr_class->show_toolbars = modest_hildon2_window_mgr_show_toolbars;
144         mgr_class->get_main_window = modest_hildon2_window_mgr_get_main_window;
145         mgr_class->get_modal = modest_hildon2_window_mgr_get_modal;
146         mgr_class->set_modal = modest_hildon2_window_mgr_set_modal;
147         mgr_class->find_registered_header = modest_hildon2_window_mgr_find_registered_header;
148         mgr_class->get_window_list = modest_hildon2_window_mgr_get_window_list;
149         mgr_class->close_all_windows = modest_hildon2_window_mgr_close_all_windows;
150         mgr_class->show_initial_window = modest_hildon2_window_mgr_show_initial_window;
151
152         g_type_class_add_private (gobject_class, sizeof(ModestHildon2WindowMgrPrivate));
153
154 }
155
156 static void
157 modest_hildon2_window_mgr_instance_init (ModestHildon2WindowMgr *obj)
158 {
159         ModestHildon2WindowMgrPrivate *priv;
160
161         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(obj);
162         priv->window_list = NULL;
163         priv->fullscreen_mode = FALSE;
164         priv->current_top = NULL;
165         priv->window_state_uids = NULL;
166
167         priv->modal_windows = g_queue_new ();
168         priv->queue_lock = g_mutex_new ();
169
170         /* Could not initialize it from gconf, singletons are not
171            ready yet */
172         priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
173         priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
174
175         priv->closing_time = 0;
176
177         priv->modal_handler_uids = NULL;
178 }
179
180 static void
181 modest_hildon2_window_mgr_finalize (GObject *obj)
182 {
183         ModestHildon2WindowMgrPrivate *priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(obj);
184
185         modest_signal_mgr_disconnect_all_and_destroy (priv->window_state_uids);
186         priv->window_state_uids = NULL;
187
188         if (priv->window_list) {
189                 GList *iter = priv->window_list;
190                 /* unregister pending windows */
191                 while (iter) {
192                         ModestWindow *window = (ModestWindow *) iter->data;
193                         iter = g_list_next (iter);
194                         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), window);
195                 }
196                 g_list_free (priv->window_list);
197                 priv->window_list = NULL;
198         }
199
200         /* Free the hash table with the handlers */
201         if (priv->destroy_handlers) {
202                 g_hash_table_destroy (priv->destroy_handlers);
203                 priv->destroy_handlers = NULL;
204         }
205
206         if (priv->viewer_handlers) {
207                 g_hash_table_destroy (priv->viewer_handlers);
208                 priv->viewer_handlers = NULL;
209         }
210
211         modest_signal_mgr_disconnect_all_and_destroy (priv->modal_handler_uids);
212         priv->modal_handler_uids = NULL;
213
214         if (priv->modal_windows) {
215                 g_mutex_lock (priv->queue_lock);
216                 g_queue_free (priv->modal_windows);
217                 priv->modal_windows = NULL;
218                 g_mutex_unlock (priv->queue_lock);
219         }
220         g_mutex_free (priv->queue_lock);
221
222         /* Do not unref priv->main_window because it does not hold a
223            new reference */
224
225         G_OBJECT_CLASS(parent_class)->finalize (obj);
226 }
227
228 ModestWindowMgr*
229 modest_hildon2_window_mgr_new (void)
230 {
231         return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_HILDON2_WINDOW_MGR, NULL));
232 }
233
234 static gboolean
235 modest_hildon2_window_mgr_close_all_windows (ModestWindowMgr *self)
236 {
237         ModestHildon2WindowMgrPrivate *priv = NULL;
238         gboolean ret_value = FALSE;
239         GtkWidget *window;
240         HildonWindowStack *stack;
241         gboolean failed = FALSE;
242
243         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
244         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
245
246         stack = hildon_window_stack_get_default ();
247
248         while ((window = hildon_window_stack_peek (stack)) != NULL) {
249                 g_signal_emit_by_name (G_OBJECT (window), "delete-event", NULL, &ret_value);
250                 if (ret_value == TRUE) {
251                         failed = TRUE;
252                         break;
253                 }
254         }
255
256         return !failed;
257 }
258
259 static gint
260 compare_msguids (ModestWindow *win,
261                  const gchar *uid)
262 {
263         const gchar *msg_uid;
264
265         if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
266                 return 1;
267
268         /* Get message uid from msg window */
269         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
270                 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
271                 if (msg_uid && uid &&!strcmp (msg_uid, uid))
272                         return 0;
273         } else {
274                 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
275         }
276         
277         if (msg_uid && uid &&!strcmp (msg_uid, uid))
278                 return 0;
279         else
280                 return 1;
281 }
282
283 static gboolean
284 modest_hildon2_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
285                                                   ModestWindow **win)
286 {
287         ModestHildon2WindowMgrPrivate *priv = NULL;
288         gchar* uid = NULL;
289         gboolean has_header, has_window = FALSE;
290         GList *item = NULL;
291
292         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
293         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
294         
295         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
296
297         has_header = MODEST_WINDOW_MGR_CLASS (parent_class)->find_registered_header (self, header, win);
298         
299         uid = modest_tny_folder_get_header_unique_id (header);
300         
301         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
302         if (item) {
303                 has_window = TRUE;
304                 if (win) {
305                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
306                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
307                                 g_debug ("not a valid window!");
308                         else {
309                                 g_debug ("found a window");
310                                 *win = MODEST_WINDOW (item->data);
311                         }
312                 }
313         }
314         g_free (uid);
315         
316         return has_header || has_window;
317 }
318
319 static GList *
320 modest_hildon2_window_mgr_get_window_list (ModestWindowMgr *self)
321 {
322         ModestHildon2WindowMgrPrivate *priv;
323
324         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
325         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
326
327         return g_list_copy (priv->window_list);
328 }
329
330 static gboolean
331 modest_hildon2_window_mgr_register_window (ModestWindowMgr *self, 
332                                            ModestWindow *window,
333                                            ModestWindow *parent)
334 {
335         GList *win;
336         ModestHildon2WindowMgrPrivate *priv;
337         gint *handler_id;
338         HildonProgram *program;
339         GtkWidget *current_top;
340         HildonWindowStack *stack;
341         gboolean nested_msg;
342
343         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
344         g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
345
346         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
347
348         program = hildon_program_get_instance ();
349         win = g_list_find (priv->window_list, window);
350         if (win) {
351                 /* this is for the case we want to register the window and it was already
352                  * registered. We leave silently, telling the operation was succesful.
353                  */
354                 gtk_window_present (GTK_WINDOW (win));
355                 return TRUE;
356         }
357
358         stack = hildon_window_stack_get_default ();
359
360         if (!MODEST_WINDOW_MGR_CLASS (parent_class)->register_window (self, window, parent))
361                 goto fail;
362
363         /* Add to list. Keep a reference to the window */
364         g_object_ref (window);
365         priv->window_list = g_list_prepend (priv->window_list, window);
366
367         current_top = hildon_window_stack_peek (stack);
368
369         nested_msg = MODEST_IS_MSG_VIEW_WINDOW (window) && 
370                 MODEST_IS_MSG_VIEW_WINDOW (parent);
371         /* Close views if they're being shown */
372         if (!nested_msg &&
373             (MODEST_IS_MSG_EDIT_WINDOW (current_top) ||
374              MODEST_IS_MSG_VIEW_WINDOW (current_top))) {
375                 gboolean retval;
376
377                 /* If the current view has modal dialogs then
378                    we fail to register the new view */
379                 if ((current_top != NULL) &&
380                     window_has_modals (MODEST_WINDOW (current_top))) {
381                         /* Window on top but it has opened dialogs */
382                         goto fail;
383                 }
384
385                 /* Close the current view */
386                 g_signal_emit_by_name (G_OBJECT (current_top), "delete-event", NULL, &retval);
387                 if (retval == TRUE) {
388                         /* Cancelled closing top window, then we fail to register */
389                         goto fail;
390                 }
391         }
392
393         /* Listen to object destruction */
394         handler_id = g_malloc0 (sizeof (gint));
395         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
396         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
397
398         /* Show toolbar always */
399         modest_window_show_toolbar (window, TRUE);
400
401         return TRUE;
402 fail:
403         /* Add to list. Keep a reference to the window */
404         priv->window_list = g_list_remove (priv->window_list, window);
405         g_object_unref (window);
406         current_top = hildon_window_stack_peek (stack);
407         if (current_top)
408                 gtk_window_present (GTK_WINDOW (current_top));
409         return FALSE;
410 }
411
412 static void
413 cancel_window_operations (ModestWindow *window)
414 {
415         GSList* pending_ops = NULL;
416
417         /* cancel open and receive operations */
418         pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (), 
419                                                                  G_OBJECT (window));
420         while (pending_ops != NULL) {
421                 ModestMailOperationTypeOperation type;
422                 GSList* tmp_list = NULL;
423
424                 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
425                 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
426                         modest_mail_operation_cancel (pending_ops->data);
427                 }
428                 g_object_unref (G_OBJECT (pending_ops->data));
429                 tmp_list = pending_ops;
430                 pending_ops = g_slist_next (pending_ops);
431                 g_slist_free_1 (tmp_list);
432         }
433 }
434
435 static gboolean
436 window_has_modals (ModestWindow *window)
437 {
438         GList *toplevels;
439         GList *node;
440         gboolean retvalue = FALSE;
441
442         /* First we fetch all toplevels */
443         toplevels = gtk_window_list_toplevels ();
444         for (node = toplevels; node != NULL; node = g_list_next (node)) {
445                 if (GTK_IS_WINDOW (node->data) &&
446                     gtk_window_get_transient_for (GTK_WINDOW (node->data)) == GTK_WINDOW (window) &&
447                     GTK_WIDGET_VISIBLE (node->data)) {
448                         retvalue = TRUE;
449                         break;
450                 }
451         }
452         g_list_free (toplevels);
453         return retvalue;
454 }
455
456 static gboolean
457 window_can_close (ModestWindow *window)
458 {
459         /* An editor can be always closed no matter the dialogs it has 
460          * on top. */
461         if (MODEST_IS_MSG_EDIT_WINDOW (window))
462                 return TRUE;
463
464         return !window_has_modals (window);
465 }
466
467 static gboolean
468 on_window_destroy (ModestWindow *window, 
469                    GdkEvent *event,
470                    ModestHildon2WindowMgr *self)
471 {
472         gboolean no_propagate = FALSE;
473
474         if (!window_can_close (window))
475                 return TRUE;
476
477         if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
478                 gboolean sent = FALSE;
479                 sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
480                 /* Save currently edited message to Drafts if it was not sent */
481                 if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
482
483                         if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
484                                 return TRUE;
485                 }
486         }
487
488         /* Unregister window */
489         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (self), window);
490         no_propagate = FALSE;
491
492         return no_propagate;
493 }
494
495 static void
496 modest_hildon2_window_mgr_unregister_window (ModestWindowMgr *self, 
497                                              ModestWindow *window)
498 {
499         GList *win;
500         ModestHildon2WindowMgrPrivate *priv;
501         gulong *tmp, handler_id;
502         gboolean check_close_all = FALSE;
503         guint num_windows;
504
505         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
506         g_return_if_fail (MODEST_IS_WINDOW (window));
507
508         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
509
510         win = g_list_find (priv->window_list, window);
511         if (!win) {
512                 g_warning ("Trying to unregister a window that has not being registered yet");
513                 return;
514         }
515
516         /* Remember this for the end of the method */
517         if (MODEST_IS_FOLDER_WINDOW (window))
518                 check_close_all = TRUE;
519
520         /* Remove the viewer window handler from the hash table. The
521            HashTable could not exist if the main window was closed
522            when there were other windows remaining */
523         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
524                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
525                 /* If the viewer was created without a main window
526                    (for example when opening a message through D-Bus
527                    the viewer handlers was not registered */
528                 if (tmp) {
529                         g_signal_handler_disconnect (window, *tmp);
530                         g_hash_table_remove (priv->viewer_handlers, window);
531                 }
532         }
533
534         /* Remove from list & hash table */
535         priv->window_list = g_list_remove_link (priv->window_list, win);
536         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
537         handler_id = *tmp;
538
539         g_hash_table_remove (priv->destroy_handlers, window);
540
541         /* cancel open and receive operations */
542         cancel_window_operations (window);
543
544         /* Check if it's the topmost window, and remove the window from the stack.
545          * This is needed for the cases there's no other topmost window that will
546          * replace it in topmost handler.
547          */
548          if (window == priv->current_top)
549                  priv->current_top = NULL;
550
551         /* Disconnect the "window-state-event" handler, we won't need it anymore */
552         if (priv->window_state_uids) {
553                 priv->window_state_uids = 
554                         modest_signal_mgr_disconnect (priv->window_state_uids, 
555                                                       G_OBJECT (window), 
556                                                       "notify::is-topmost");
557         }
558
559         /* Disconnect the "delete-event" handler, we won't need it anymore */
560         g_signal_handler_disconnect (window, handler_id);
561
562         /* Destroy the window */
563         g_object_unref (win->data);
564         g_list_free (win);
565
566         MODEST_WINDOW_MGR_CLASS (parent_class)->unregister_window (self, window);
567
568         /* We have to get the number of windows here in order not to
569            emit the signal too many times */
570         num_windows = modest_window_mgr_get_num_windows (self);
571
572         /* Check if we have to destroy the accounts window as
573            well. This happens if we only have one or none remote
574            accounts */
575         if (check_close_all) {
576                 ModestTnyAccountStore *acc_store = modest_runtime_get_account_store ();
577                 if (modest_tny_account_store_get_num_remote_accounts (acc_store) < 2)
578                         modest_window_mgr_close_all_windows (self);
579         }
580
581         /* If there are no more windows registered emit the signal */
582         if (num_windows == 0)
583                 g_signal_emit_by_name (self, "window-list-empty");
584 }
585
586
587 static void
588 modest_hildon2_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
589                                                gboolean on)
590 {
591         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
592
593         return;
594 }
595
596 static gboolean
597 modest_hildon2_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
598 {
599         return FALSE;
600 }
601
602 static void 
603 modest_hildon2_window_mgr_show_toolbars (ModestWindowMgr *self,
604                                          GType window_type,
605                                          gboolean show_toolbars,
606                                          gboolean fullscreen)
607 {
608         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
609
610         return;
611 }
612
613 static ModestWindow*  
614 modest_hildon2_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
615 {
616         ModestHildon2WindowMgrPrivate *priv;
617         ModestWindow *result;
618
619         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
620         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
621
622         /* TODO: make this return NULL always */
623
624         result = MODEST_WINDOW_MGR_CLASS (parent_class)->get_main_window (self, FALSE);
625         /* create the main window, if it hasn't been created yet */
626         if (!result && show) {
627                 /* modest_window_mgr_register_window will set priv->main_window */
628                 result = modest_main_window_new ();
629                 /* We have to remove all other windows */
630                 if (!modest_window_mgr_close_all_windows (self)) {
631                         gtk_widget_destroy (GTK_WIDGET (result));
632                         return NULL;
633                 }
634                 if (!modest_window_mgr_register_window (self, result, NULL)) {
635                         gtk_widget_destroy (GTK_WIDGET (result));
636                         return NULL;
637                 }
638                 MODEST_DEBUG_BLOCK(
639                         g_debug ("%s: created main window: %p\n", __FUNCTION__, result);
640                 );
641         }
642         if (show) {
643                 gtk_widget_show_all (GTK_WIDGET (result));
644                 gtk_window_present (GTK_WINDOW (result));
645         }
646         
647         return result;
648 }
649
650
651 static GtkWindow *
652 modest_hildon2_window_mgr_get_modal (ModestWindowMgr *self)
653 {
654         ModestHildon2WindowMgrPrivate *priv;
655         GList *toplevel_list;
656         GtkWidget *toplevel;
657         
658         g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
659         priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
660
661         toplevel = NULL;
662         toplevel_list = gtk_window_list_toplevels ();
663         while (toplevel_list) {
664                 if (gtk_window_is_active (toplevel_list->data)) {
665                         toplevel = toplevel_list->data;
666                         break;
667                 }
668                 toplevel_list = g_list_next (toplevel_list);
669         }
670
671         return NULL;
672 }
673
674
675 static void
676 modest_hildon2_window_mgr_set_modal (ModestWindowMgr *self, 
677                                      GtkWindow *window,
678                                      GtkWindow *parent)
679 {
680         g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
681         g_return_if_fail (GTK_IS_WINDOW (window));
682
683         gtk_window_set_modal (window, TRUE);
684         gtk_window_set_transient_for (window, parent);
685         gtk_window_set_destroy_with_parent (window, TRUE);
686 }
687
688 static ModestWindow *
689 modest_hildon2_window_mgr_show_initial_window (ModestWindowMgr *self)
690 {
691         ModestWindow *initial_window = NULL;
692         ModestTnyAccountStore *acc_store;
693
694         /* Always create accounts window. We'll decide later if we
695            want to show it or not, depending the number of accounts */
696         initial_window = MODEST_WINDOW (modest_accounts_window_new ());
697         modest_window_mgr_register_window (self, initial_window, NULL);
698
699         /* If there are less than 2 remote accounts then directly show
700            the folder window and do not show the accounts window */
701         acc_store = modest_runtime_get_account_store ();
702         if (modest_tny_account_store_get_num_remote_accounts (acc_store) < 2) {
703                 ModestAccountMgr *mgr;
704
705                 /* Show first the accounts window to add it to the
706                    stack. This has to be changed when the new
707                    stackable API is available. There will be a method
708                    to show all the windows that will only show the
709                    last one to the user. The current code shows both
710                    windows, one after the other */
711                 gtk_widget_show (GTK_WIDGET (initial_window));
712
713                 initial_window = MODEST_WINDOW (modest_folder_window_new (NULL));
714                 mgr = modest_runtime_get_account_mgr ();
715                 modest_folder_window_set_account (MODEST_FOLDER_WINDOW (initial_window),
716                                                   modest_account_mgr_get_default_account (mgr));
717                 modest_window_mgr_register_window (self, initial_window, NULL);
718         }
719
720         return initial_window;
721 }