* src/widgets/modest-combo-box.[ch]:
[modest] / src / widgets / modest-window-mgr.c
1 /* Copyright (c) 2006,2007 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 "modest-window-mgr.h"
32 #include "modest-runtime.h"
33 #include "modest-tny-folder.h"
34 #include "modest-ui-actions.h"
35 #include "modest-platform.h"
36 #include "widgets/modest-main-window.h"
37 #include "widgets/modest-msg-edit-window.h"
38 #include "widgets/modest-msg-view-window.h"
39 #include "modest-debug.h"
40
41
42 /* 'private'/'protected' functions */
43 static void modest_window_mgr_class_init (ModestWindowMgrClass *klass);
44 static void modest_window_mgr_init       (ModestWindowMgr *obj);
45 static void modest_window_mgr_finalize   (GObject *obj);
46
47 static gboolean on_window_destroy        (ModestWindow *window,
48                                           GdkEvent *event,
49                                           ModestWindowMgr *self);
50
51 static gboolean on_modal_window_close    (GtkWidget *widget,
52                                           GdkEvent *event,
53                                           gpointer user_data);
54
55 static void     on_modal_dialog_close    (GtkDialog *dialog,
56                                           gint arg1,
57                                           gpointer user_data);
58
59 static const gchar* get_show_toolbar_key (GType window_type,
60                                           gboolean fullscreen);
61
62 /* list my signals  */
63 enum {
64         WINDOW_LIST_EMPTY_SIGNAL,
65         NUM_SIGNALS
66 };
67
68 typedef struct _ModestWindowMgrPrivate ModestWindowMgrPrivate;
69 struct _ModestWindowMgrPrivate {
70         GList        *window_list;
71
72         ModestWindow *main_window;
73
74         GMutex       *queue_lock;
75         GQueue       *modal_windows;
76         
77         gboolean     fullscreen_mode;
78         
79         GSList       *windows_that_prevent_hibernation;
80         GSList       *preregistered_uids;
81         GHashTable   *destroy_handlers;
82         GHashTable   *viewer_handlers;
83         
84         guint        closing_time;
85
86         GSList       *modal_handler_uids;
87         GtkWidget    *cached_view;
88         GtkWidget    *cached_editor;
89         guint        idle_load_view_id;
90         guint        idle_load_editor_id;
91 };
92 #define MODEST_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
93                                                MODEST_TYPE_WINDOW_MGR, \
94                                                ModestWindowMgrPrivate))
95 /* globals */
96 static GObjectClass *parent_class = NULL;
97
98 /* uncomment the following if you have defined any signals */
99 static guint signals[NUM_SIGNALS] = {0};
100
101 GType
102 modest_window_mgr_get_type (void)
103 {
104         static GType my_type = 0;
105         if (!my_type) {
106                 static const GTypeInfo my_info = {
107                         sizeof(ModestWindowMgrClass),
108                         NULL,           /* base init */
109                         NULL,           /* base finalize */
110                         (GClassInitFunc) modest_window_mgr_class_init,
111                         NULL,           /* class finalize */
112                         NULL,           /* class data */
113                         sizeof(ModestWindowMgr),
114                         1,              /* n_preallocs */
115                         (GInstanceInitFunc) modest_window_mgr_init,
116                         NULL
117                 };
118                 my_type = g_type_register_static (G_TYPE_OBJECT,
119                                                   "ModestWindowMgr",
120                                                   &my_info, 0);
121         }
122         return my_type;
123 }
124
125 static void
126 modest_window_mgr_class_init (ModestWindowMgrClass *klass)
127 {
128         GObjectClass *gobject_class;
129         gobject_class = (GObjectClass*) klass;
130
131         parent_class            = g_type_class_peek_parent (klass);
132         gobject_class->finalize = modest_window_mgr_finalize;
133
134         g_type_class_add_private (gobject_class, sizeof(ModestWindowMgrPrivate));
135
136
137         /**
138          * ModestWindowMgr::window-list-empty
139          * @self: the #ModestWindowMgr that emits the signal
140          * @user_data: user data set when the signal handler was connected
141          *
142          * Issued whenever the window list becomes empty
143          */
144         signals[WINDOW_LIST_EMPTY_SIGNAL] =
145                 g_signal_new ("window-list-empty",
146                               G_TYPE_FROM_CLASS (gobject_class),
147                               G_SIGNAL_RUN_FIRST,
148                               G_STRUCT_OFFSET (ModestWindowMgrClass, window_list_empty),
149                               NULL, NULL,
150                               g_cclosure_marshal_VOID__VOID,
151                               G_TYPE_NONE, 0);
152 }
153
154 static gboolean
155 idle_load_view (ModestWindowMgr *mgr)
156 {
157         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (mgr);
158         
159         priv->cached_view = g_object_new (MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
160         priv->idle_load_view_id = 0;
161         return FALSE;
162 }
163
164 static gboolean
165 idle_load_editor (ModestWindowMgr *mgr)
166 {
167         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (mgr);
168         
169         priv->cached_editor = g_object_new (MODEST_TYPE_MSG_EDIT_WINDOW, NULL);
170         priv->idle_load_editor_id = 0;
171         return FALSE;
172 }
173
174 static void
175 load_new_view (ModestWindowMgr *self)
176 {
177         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
178         if ((priv->cached_view == NULL) && (priv->idle_load_view_id == 0))
179                 priv->idle_load_view_id = g_idle_add ((GSourceFunc) idle_load_view, self);
180 }
181
182 static void
183 load_new_editor (ModestWindowMgr *self)
184 {
185         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
186         if ((priv->cached_editor == NULL) && (priv->idle_load_editor_id == 0))
187                 priv->idle_load_editor_id = g_idle_add ((GSourceFunc) idle_load_editor, self);
188 }
189
190 static void
191 modest_window_mgr_init (ModestWindowMgr *obj)
192 {
193         ModestWindowMgrPrivate *priv;
194
195         priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
196         priv->window_list = NULL;
197         priv->main_window = NULL;
198         priv->fullscreen_mode = FALSE;
199
200         priv->modal_windows = g_queue_new ();
201         priv->queue_lock = g_mutex_new ();
202         
203         priv->preregistered_uids = NULL;
204
205         /* Could not initialize it from gconf, singletons are not
206            ready yet */
207         priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);   
208         priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
209
210         priv->closing_time = 0;
211
212         priv->modal_handler_uids = NULL;
213         priv->cached_view = NULL;
214         priv->cached_editor = NULL;
215 }
216
217 static void
218 modest_window_mgr_finalize (GObject *obj)
219 {
220         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
221
222         if (priv->idle_load_view_id > 0) {
223                 g_source_remove (priv->idle_load_view_id);
224                 priv->idle_load_view_id = 0;
225         }
226         
227         if (priv->idle_load_editor_id > 0) {
228                 g_source_remove (priv->idle_load_editor_id);
229                 priv->idle_load_editor_id = 0;
230         }
231         
232         if (priv->cached_view) {
233                 gtk_widget_destroy (priv->cached_view);
234                 priv->cached_view = NULL;
235         }
236         if (priv->cached_editor) {
237                 gtk_widget_destroy (priv->cached_editor);
238                 priv->cached_editor = NULL;
239         }
240
241         if (priv->window_list) {
242                 GList *iter = priv->window_list;
243                 /* unregister pending windows */
244                 while (iter) {
245                         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), 
246                                                              MODEST_WINDOW (iter->data));
247                         iter = g_list_next (iter);
248                 }
249                 g_list_free (priv->window_list);
250                 priv->window_list = NULL;
251         }
252
253         g_slist_foreach (priv->preregistered_uids, (GFunc)g_free, NULL);
254         g_slist_free (priv->preregistered_uids);
255
256         
257         /* Free the hash table with the handlers */
258         if (priv->destroy_handlers) {
259                 g_hash_table_destroy (priv->destroy_handlers);
260                 priv->destroy_handlers = NULL;
261         }
262
263         if (priv->viewer_handlers) {
264                 g_hash_table_destroy (priv->viewer_handlers);
265                 priv->viewer_handlers = NULL;
266         }
267
268         modest_signal_mgr_disconnect_all_and_destroy (priv->modal_handler_uids);
269
270         if (priv->modal_windows) {
271                 g_mutex_lock (priv->queue_lock);
272                 g_queue_free (priv->modal_windows);
273                 priv->modal_windows = NULL;
274                 g_mutex_unlock (priv->queue_lock);
275         }
276         g_mutex_free (priv->queue_lock);
277         
278         /* Do not unref priv->main_window because it does not hold a
279            new reference */
280
281         
282         G_OBJECT_CLASS(parent_class)->finalize (obj);
283 }
284
285 ModestWindowMgr*
286 modest_window_mgr_new (void)
287 {
288         return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_WINDOW_MGR, NULL));
289 }
290
291
292
293
294 /* do we have uid? */
295 static gboolean
296 has_uid (GSList *list, const gchar *uid)
297 {
298         GSList *cursor = list;
299
300         if (!uid)
301                 return FALSE;
302         
303         while (cursor) {
304                 if (cursor->data && strcmp (cursor->data, uid) == 0)
305                         return TRUE;
306                 cursor = g_slist_next (cursor);
307         }
308         return FALSE;
309 }
310
311
312 /* remove all from the list have have uid = uid */
313 static GSList*
314 remove_uid (GSList *list, const gchar *uid)
315 {
316         GSList *cursor = list, *start = list;
317         
318         if (!uid)
319                 return FALSE;
320         
321         while (cursor) {
322                 GSList *next = g_slist_next (cursor);
323                 if (cursor->data && strcmp (cursor->data, uid) == 0) {
324                         g_free (cursor->data);
325                         start = g_slist_delete_link (start, cursor);
326                 }
327                 cursor = next;
328         }
329         return start;
330 }
331
332
333 static GSList *
334 append_uid (GSList *list, const gchar *uid)
335 {
336         return g_slist_append (list, g_strdup(uid));
337 }
338
339
340
341 void 
342 modest_window_mgr_register_header (ModestWindowMgr *self,  TnyHeader *header, const gchar *alt_uid)
343 {
344         ModestWindowMgrPrivate *priv;
345         gchar* uid;
346         
347         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
348         g_return_if_fail (TNY_IS_HEADER(header));
349                 
350         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
351         uid = modest_tny_folder_get_header_unique_id (header);
352
353         if (uid == NULL)
354                 uid = g_strdup (alt_uid);
355         
356         if (!has_uid (priv->preregistered_uids, uid)) {
357                 g_debug ("registering new uid %s", uid);
358                 priv->preregistered_uids = append_uid (priv->preregistered_uids, uid);
359         } else
360                 g_debug ("already had uid %s", uid);
361         
362         g_free (uid);
363 }
364
365 void 
366 modest_window_mgr_unregister_header (ModestWindowMgr *self,  TnyHeader *header)
367 {
368         ModestWindowMgrPrivate *priv;
369         gchar* uid;
370         
371         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
372         g_return_if_fail (TNY_IS_HEADER(header));
373                 
374         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
375         uid = modest_tny_folder_get_header_unique_id (header);
376
377         if (!has_uid (priv->preregistered_uids, uid)) {
378                 g_debug ("trying to unregister non-existing uid %s", uid);
379                 priv->preregistered_uids = append_uid (priv->preregistered_uids, uid);
380         } else
381                 g_debug ("unregistering uid %s", uid);
382         
383         if (has_uid (priv->preregistered_uids, uid)) {
384                 priv->preregistered_uids = remove_uid (priv->preregistered_uids, uid);
385                 if (has_uid (priv->preregistered_uids, uid))
386                         g_debug ("BUG: uid %s NOT removed", uid);
387                 else
388                         g_debug ("uid %s removed", uid);
389         }
390                 
391         g_free (uid);
392 }
393
394
395 #define MODEST_WINDOW_HELP_ID_PARAM "help-id"
396
397 void
398 modest_window_mgr_register_help_id (ModestWindowMgr *self, GtkWindow *win, const gchar* help_id)
399 {
400         /* we don't need 'self', but for API consistency... */
401         g_return_if_fail (self && MODEST_IS_WINDOW_MGR(self));
402
403         g_return_if_fail (win && GTK_IS_WINDOW(win));
404         g_return_if_fail (help_id);
405         
406         g_object_set_data_full (G_OBJECT(win), MODEST_WINDOW_HELP_ID_PARAM,
407                                 g_strdup(help_id), g_free);
408 }
409
410
411 const gchar*
412 modest_window_mgr_get_help_id (ModestWindowMgr *self, GtkWindow *win)
413 {
414         const gchar* help_id = NULL;
415
416         /* we don't need 'self', but for API consistency... */
417         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), NULL);
418         
419         g_return_val_if_fail (win, NULL);
420         g_return_val_if_fail (GTK_IS_WINDOW(win), NULL);
421         
422         if (MODEST_IS_MAIN_WINDOW (win)) {
423                 GtkWidget *folder_view;
424                 TnyFolderStore *folder_store;
425                 
426                 /* Get selected folder */
427                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
428                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
429                 folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
430
431                 /* Switch help_id */
432                 if (folder_store && TNY_IS_FOLDER (folder_store)) {
433                         help_id = modest_tny_folder_get_help_id (TNY_FOLDER (folder_store));
434                         if (!help_id)
435                                 g_warning ("%s: BUG: did not get a valid help_id", __FUNCTION__);
436                 }
437                 if (folder_store)
438                         g_object_unref (folder_store);
439         }
440
441         if (!help_id)
442                 help_id = g_object_get_data (G_OBJECT(win), MODEST_WINDOW_HELP_ID_PARAM);
443                 
444         return help_id;
445 }
446
447 static gint
448 compare_msguids (ModestWindow *win,
449                  const gchar *uid)
450 {
451         const gchar *msg_uid;
452
453         if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
454                 return 1;
455
456         /* Get message uid from msg window */
457         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
458                 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
459                 if (msg_uid && uid &&!strcmp (msg_uid, uid))
460                         return 0;
461         } else {
462                 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
463         }
464         
465         if (msg_uid && uid &&!strcmp (msg_uid, uid))
466                 return 0;
467         else
468                 return 1;
469 }
470
471 void
472 modest_window_mgr_close_all_windows (ModestWindowMgr *self)
473 {
474         ModestWindowMgrPrivate *priv = NULL;
475         GList *wins = NULL;
476         gboolean ret_value = FALSE;
477         
478         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
479         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
480         
481         /* delete-event handler already removes window_list item, */
482         /* so no next its required on this loop  */
483         wins = priv->window_list;
484         while (wins) {          
485                 g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
486
487                 wins = priv->window_list;
488         }
489 }
490
491
492 gboolean
493 modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
494                                           ModestWindow **win)
495 {
496         ModestWindowMgrPrivate *priv = NULL;
497         gchar* uid = NULL;
498         gboolean has_header, has_window = FALSE;
499         GList *item = NULL;
500
501         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
502         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
503         
504         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
505
506         uid = modest_tny_folder_get_header_unique_id (header);
507         
508         if (win)
509                 *win = NULL;
510
511         has_header = has_uid (priv->preregistered_uids, uid);
512         
513         item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
514         if (item) {
515                 has_window = TRUE;
516                 if (win) {
517                         if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) && 
518                             (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
519                                 g_debug ("not a valid window!");
520                         else {
521                                 g_debug ("found a window");
522                                 *win = MODEST_WINDOW (item->data);
523                         }
524                 }
525         }
526         g_free (uid);
527         
528         return has_header || has_window;
529 }
530
531 static const gchar *
532 get_show_toolbar_key (GType window_type,
533                       gboolean fullscreen)
534 {
535         const gchar *key = NULL;
536
537         if (window_type == MODEST_TYPE_MAIN_WINDOW)
538                 key = (fullscreen) ? 
539                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
540                         MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
541         else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
542                 key = (fullscreen) ? 
543                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
544                         MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
545         else if (window_type ==  MODEST_TYPE_MSG_EDIT_WINDOW)
546                 key = (fullscreen) ? 
547                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
548                         MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
549         else
550                 g_return_val_if_reached (NULL);
551
552         return key;
553 }
554
555 void 
556 modest_window_mgr_register_window (ModestWindowMgr *self, 
557                                    ModestWindow *window)
558 {
559         GList *win;
560         ModestWindowMgrPrivate *priv;
561         gint *handler_id;
562         const gchar *key;
563
564         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
565         g_return_if_fail (GTK_IS_WINDOW (window));
566
567         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
568
569         win = g_list_find (priv->window_list, window);
570         if (win) {
571                 /* this is for the case we want to register the window and it was already
572                  * registered. We leave silently.
573                  */
574                 return;
575         }
576         
577         /* Check that it's not a second main window */
578         if (MODEST_IS_MAIN_WINDOW (window)) {
579                 if (priv->main_window) {
580                         g_warning ("%s: trying to register a second main window",
581                                    __FUNCTION__);
582                         return;
583                 } else {
584                         priv->main_window = window;
585                         load_new_view (self);
586                         load_new_editor (self);
587                 }
588         }
589
590         /* remove from the list of pre-registered uids */
591         if (MODEST_IS_MSG_VIEW_WINDOW(window)) {
592                 const gchar *uid = modest_msg_view_window_get_message_uid
593                         (MODEST_MSG_VIEW_WINDOW (window));
594
595                 if (!has_uid (priv->preregistered_uids, uid)) 
596                         g_debug ("weird: no uid for window (%s)", uid);
597                 
598                 g_debug ("registering window for %s", uid ? uid : "<none>");
599                 
600                 priv->preregistered_uids = 
601                         remove_uid (priv->preregistered_uids,
602                                     modest_msg_view_window_get_message_uid
603                                     (MODEST_MSG_VIEW_WINDOW (window)));
604         } else if (MODEST_IS_MSG_EDIT_WINDOW(window)) {
605                 const gchar *uid = modest_msg_edit_window_get_message_uid
606                         (MODEST_MSG_EDIT_WINDOW (window));
607                 
608                 g_debug ("registering window for %s", uid);
609
610                 priv->preregistered_uids = 
611                         remove_uid (priv->preregistered_uids,
612                                     modest_msg_edit_window_get_message_uid
613                                     (MODEST_MSG_EDIT_WINDOW (window)));
614         }
615         
616         /* Add to list. Keep a reference to the window */
617         g_object_ref (window);
618         priv->window_list = g_list_prepend (priv->window_list, window);
619
620         /* Listen to object destruction */
621         handler_id = g_malloc0 (sizeof (gint));
622         *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
623         g_hash_table_insert (priv->destroy_handlers, window, handler_id);
624
625         /* If there is a msg view window, let the main window listen the msg-changed signal */
626         if (MODEST_IS_MSG_VIEW_WINDOW(window) && priv->main_window) {
627                 gulong *handler;
628                 handler = g_malloc0 (sizeof (gulong));
629                 *handler = g_signal_connect (window, "msg-changed", 
630                                              G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed), 
631                                              priv->main_window);
632                 g_hash_table_insert (priv->viewer_handlers, window, handler);
633         }
634
635         /* Put into fullscreen if needed */
636         if (priv->fullscreen_mode)
637                 gtk_window_fullscreen (GTK_WINDOW (window));
638
639         /* Show/hide toolbar & fullscreen */    
640         key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
641         modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
642 }
643
644 static gboolean
645 on_window_destroy (ModestWindow *window, 
646                    GdkEvent *event,
647                    ModestWindowMgr *self)
648 {
649         gint dialog_response = GTK_RESPONSE_NONE;
650
651         /* Specific stuff first */
652         if (MODEST_IS_MAIN_WINDOW (window)) {
653                 ModestWindowMgrPrivate *priv;
654                 priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
655
656                 /* If more than one window already opened */
657                 if (g_list_length (priv->window_list) > 1) {
658
659                         /* Create the confirmation dialog MSG-NOT308 */
660                         dialog_response = modest_platform_run_confirmation_dialog (
661                                         GTK_WINDOW (window), _("emev_nc_close_windows"));
662
663                         /* If the user wants to close all the windows */
664                         if ((dialog_response == GTK_RESPONSE_OK) 
665                                         || (dialog_response == GTK_RESPONSE_ACCEPT) 
666                                         || (dialog_response == GTK_RESPONSE_YES))
667                                 {
668                                         GList *iter = priv->window_list;
669                                         do {
670                                                 if (iter->data != window) {
671                                                         GList *tmp = iter->next;
672                                                         on_window_destroy (MODEST_WINDOW (iter->data),
673                                                                         event,
674                                                                         self);
675                                                         iter = tmp;
676                                                 } else {
677                                                         iter = g_list_next (iter);
678                                                 }
679                                         } while (iter);
680                                 }
681                         else
682                                 {
683                                         return TRUE;
684                                 }
685                 }
686         }
687         else {
688                 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
689                         gboolean sent = FALSE;
690                         gint response = GTK_RESPONSE_ACCEPT;
691                         sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
692                         /* Save currently edited message to Drafts if it was not sent */
693                         if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
694
695                                 /* Raise the window if it's minimized */
696                                 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
697                                         gtk_window_present (GTK_WINDOW (window));
698                                 
699                                 response =
700                                         modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
701                                                                                  _("mcen_nc_no_email_message_modified_save_changes"));
702                                 /* Save to drafts */
703                                 if (response != GTK_RESPONSE_CANCEL)                            
704                                         modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window));                            
705                         }
706                 }
707         }
708
709         /* Unregister window */
710         modest_window_mgr_unregister_window (self, window);
711         
712         return FALSE;
713 }
714
715 static void
716 disconnect_msg_changed (gpointer key, 
717                         gpointer value, 
718                         gpointer user_data)
719 {
720         guint handler_id;
721         handler_id = GPOINTER_TO_UINT(value);
722         
723         if (key && G_IS_OBJECT(key))
724                 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
725 }
726
727 void 
728 modest_window_mgr_unregister_window (ModestWindowMgr *self, 
729                                      ModestWindow *window)
730 {
731         GList *win;
732         ModestWindowMgrPrivate *priv;
733         gulong *tmp, handler_id;
734
735         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
736         g_return_if_fail (MODEST_IS_WINDOW (window));
737
738         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
739
740         win = g_list_find (priv->window_list, window);
741         if (!win) {
742                 g_warning ("Trying to unregister a window that has not being registered yet");
743                 return;
744         }
745
746         /* If it's the main window unset it */
747         if (priv->main_window == window) {
748                 priv->main_window = NULL;
749
750                 /* Disconnect all emissions of msg-changed */
751                 if (priv->viewer_handlers) {
752                         g_hash_table_foreach (priv->viewer_handlers, 
753                                               disconnect_msg_changed, 
754                                               NULL);
755                         g_hash_table_destroy (priv->viewer_handlers);
756                         priv->viewer_handlers = NULL;
757                 }
758         }
759
760         /* Remove the viewer window handler from the hash table. The
761            HashTable could not exist if the main window was closeed
762            when there were other windows remaining */
763         if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
764                 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
765                 /* If the viewer was created without a main window
766                    (for example when opening a message through D-Bus
767                    the viewer handlers was not registered */
768                 if (tmp) {
769                         g_signal_handler_disconnect (window, *tmp);
770                         g_hash_table_remove (priv->viewer_handlers, window);
771                 }
772         }
773
774         /* Save state */
775         modest_window_save_state (window);
776
777         /* Remove from list & hash table */
778         priv->window_list = g_list_remove_link (priv->window_list, win);
779         tmp = g_hash_table_lookup (priv->destroy_handlers, window);
780         handler_id = *tmp;
781         g_hash_table_remove (priv->destroy_handlers, window);
782
783         /* Disconnect the "delete-event" handler, we won't need it anymore */
784         g_signal_handler_disconnect (window, handler_id);
785
786         /* Disconnect all the window signals */
787         modest_window_disconnect_signals (window);
788         
789         /* Destroy the window */
790         gtk_widget_destroy (win->data);
791         
792         /* If there are no more windows registered emit the signal */
793         if (priv->window_list == NULL)
794                 g_signal_emit (self, signals[WINDOW_LIST_EMPTY_SIGNAL], 0);
795 }
796
797
798
799 void
800 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
801                                        gboolean on)
802 {
803         ModestWindowMgrPrivate *priv;
804         GList *win = NULL;
805         ModestConf *conf;
806
807         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
808
809         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
810
811         /* If there is no change do nothing */
812         if (priv->fullscreen_mode == on)
813                 return;
814
815         priv->fullscreen_mode = on;
816
817         conf = modest_runtime_get_conf ();
818
819         /* Update windows */
820         win = priv->window_list;
821         while (win) {
822                 gboolean show;
823                 const gchar *key = NULL;
824
825                 /* Getting this from gconf everytime is not that
826                    expensive, we'll do it just a few times */
827                 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
828                 show = modest_conf_get_bool (conf, key, NULL);
829
830                 /* Set fullscreen/unfullscreen */
831                 if (on)
832                         gtk_window_fullscreen (GTK_WINDOW (win->data));
833                 else
834                         gtk_window_unfullscreen (GTK_WINDOW (win->data));
835
836                 /* Show/Hide toolbar */
837                 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
838
839                 win = g_list_next (win);
840         }
841 }
842
843 gboolean
844 modest_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
845 {
846         ModestWindowMgrPrivate *priv;
847
848         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
849
850         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
851
852         return priv->fullscreen_mode;
853 }
854
855 void 
856 modest_window_mgr_show_toolbars (ModestWindowMgr *self,
857                                  GType window_type,
858                                  gboolean show_toolbars,
859                                  gboolean fullscreen)
860 {
861         ModestWindowMgrPrivate *priv;
862         ModestConf *conf;
863         const gchar *key = NULL;
864
865         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
866
867         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
868         conf = modest_runtime_get_conf ();
869
870         /* If nothing changes then return */
871         key = get_show_toolbar_key (window_type, fullscreen);
872         conf = modest_runtime_get_conf ();
873         if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
874                 return;
875
876         /* Save in conf */
877         modest_conf_set_bool (conf, key, show_toolbars, NULL);
878
879         /* Apply now if the view mode is the right one */
880         if ((fullscreen && priv->fullscreen_mode) ||
881             (!fullscreen && !priv->fullscreen_mode)) {
882
883                 GList *win = priv->window_list;
884
885                 while (win) {
886                         if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
887                                 modest_window_show_toolbar (MODEST_WINDOW (win->data),
888                                                             show_toolbars);
889                         win = g_list_next (win);
890                 }
891         }
892 }
893
894 ModestWindow*  
895 modest_window_mgr_get_main_window (ModestWindowMgr *self, gboolean create)
896 {
897         ModestWindowMgrPrivate *priv;
898         
899         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
900         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
901         
902         /* create the main window, if it hasn't been created yet */
903         if (!priv->main_window && create) {
904                 /* modest_window_mgr_register_window will set priv->main_window */
905                 modest_window_mgr_register_window (self, modest_main_window_new ());
906                 MODEST_DEBUG_BLOCK(
907                         g_debug ("%s: created main window: %p\n", __FUNCTION__, priv->main_window);
908                 );
909         }
910         
911         return priv->main_window;
912 }
913
914
915 gboolean
916 modest_window_mgr_main_window_exists  (ModestWindowMgr *self)
917 {
918         ModestWindowMgrPrivate *priv;
919         
920         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
921         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
922
923         return priv->main_window != NULL;
924 }
925
926
927 GtkWindow *
928 modest_window_mgr_get_modal (ModestWindowMgr *self)
929 {
930         ModestWindowMgrPrivate *priv;
931         
932         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
933         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
934
935         return g_queue_peek_head (priv->modal_windows);
936 }
937
938
939 void
940 modest_window_mgr_set_modal (ModestWindowMgr *self, 
941                              GtkWindow *window)
942 {
943         GtkWindow *old_modal;
944         ModestWindowMgrPrivate *priv;
945
946         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
947         g_return_if_fail (GTK_IS_WINDOW (window));
948
949         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
950         g_mutex_lock (priv->queue_lock);
951         old_modal = g_queue_peek_head (priv->modal_windows);
952         g_mutex_unlock (priv->queue_lock);
953
954         if (!old_modal) {       
955                 gtk_window_set_modal (window, TRUE);
956         } else {
957                 /* un-modalize the old one; the one on top should be the
958                  * modal one */
959                 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));   
960                 gtk_window_set_modal (window, TRUE);
961         }
962
963         /* this will be the new modal window */
964         g_mutex_lock (priv->queue_lock);
965         g_queue_push_head (priv->modal_windows, window);
966         g_mutex_unlock (priv->queue_lock);
967
968         if (GTK_IS_DIALOG (window))
969                 /* Note that response is not always enough because it
970                    could be captured and removed easily by dialogs but
971                    works for most of situations */
972                 priv->modal_handler_uids = 
973                         modest_signal_mgr_connect (priv->modal_handler_uids, 
974                                                    G_OBJECT (window), 
975                                                    "response",
976                                                    G_CALLBACK (on_modal_dialog_close), 
977                                                    self);
978         else
979                 priv->modal_handler_uids = 
980                         modest_signal_mgr_connect (priv->modal_handler_uids, 
981                                                    G_OBJECT (window), 
982                                                    "delete-event",
983                                                    G_CALLBACK (on_modal_window_close), 
984                                                    self);
985 }
986
987
988 static void
989 on_nonhibernating_window_hide(GtkWidget *widget, gpointer user_data)
990 {
991         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
992         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
993         
994         /* Forget this window,
995          * so hibernation will be allowed again if no windows are remembered: */
996         priv->windows_that_prevent_hibernation =
997                 g_slist_remove (priv->windows_that_prevent_hibernation, GTK_WINDOW(widget));
998 }
999
1000 static void
1001 on_nonhibernating_window_show(GtkWidget *widget, gpointer user_data)
1002 {
1003         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1004         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1005         
1006         GtkWindow *window = GTK_WINDOW (widget);
1007         
1008         priv->windows_that_prevent_hibernation = 
1009                         g_slist_append (priv->windows_that_prevent_hibernation, window);
1010         
1011         /* Allow hibernation again when the window has been hidden: */
1012         g_signal_connect (window, "hide", 
1013                 G_CALLBACK (on_nonhibernating_window_hide), self);
1014 }
1015
1016 void
1017 modest_window_mgr_prevent_hibernation_while_window_is_shown (ModestWindowMgr *self,
1018                                                                   GtkWindow *window)
1019 {
1020         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1021         
1022         if (GTK_WIDGET_VISIBLE(window)) {
1023                 on_nonhibernating_window_show (GTK_WIDGET (window), self);
1024         } else {
1025                 /* Wait for it to be shown: */
1026                 g_signal_connect (window, "show", 
1027                         G_CALLBACK (on_nonhibernating_window_show), self);      
1028         }
1029 }
1030
1031 gboolean
1032 modest_window_mgr_get_hibernation_is_prevented (ModestWindowMgr *self)
1033 {
1034         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
1035         
1036         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1037         
1038         /* Prevent hibernation if any open windows are currently 
1039          * preventing hibernation: */
1040         return (g_slist_length (priv->windows_that_prevent_hibernation) > 0);
1041 }
1042
1043
1044 void
1045 modest_window_mgr_save_state_for_all_windows (ModestWindowMgr *self)
1046 {
1047         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
1048         
1049         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1050
1051         /* Iterate over all windows */
1052         GList *win = priv->window_list;
1053         while (win) {
1054                 ModestWindow *window = MODEST_WINDOW (win->data);
1055                 if (window) {
1056                         /* This calls the vfunc, 
1057                          * so each window can do its own thing: */
1058                         modest_window_save_state (window);
1059                 }       
1060                 
1061                 win = g_list_next (win);
1062         }
1063 }
1064
1065 static gboolean
1066 idle_top_modal (gpointer data)
1067 {
1068         ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
1069         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1070         GtkWindow *topmost;
1071
1072         /* Get the top modal */
1073         g_mutex_lock (priv->queue_lock);
1074         topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
1075         g_mutex_unlock (priv->queue_lock);
1076
1077         /* Show it */
1078         if (topmost)
1079                 gtk_window_present (topmost);
1080
1081         return FALSE;
1082 }
1083
1084 static void
1085 remove_modal_from_queue (GtkWidget *widget,
1086                          ModestWindowMgr *self)
1087 {
1088         ModestWindowMgrPrivate *priv;
1089         GList *item = NULL;
1090
1091         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1092
1093         /* Remove from queue. We don't use remove, because we want to
1094            exit if the widget does not belong to the queue */
1095         g_mutex_lock (priv->queue_lock);
1096         item = g_queue_find (priv->modal_windows, widget);
1097         if (!item) {
1098                 g_warning ("Trying to remove a modal window that is not registered");
1099                 g_mutex_unlock (priv->queue_lock);
1100                 return;
1101         }
1102         g_queue_unlink (priv->modal_windows, item);
1103         g_mutex_unlock (priv->queue_lock);
1104
1105         /* Disconnect handler */
1106         priv->modal_handler_uids = 
1107                 modest_signal_mgr_disconnect (priv->modal_handler_uids, 
1108                                               G_OBJECT (widget),
1109                                               GTK_IS_DIALOG (widget) ? 
1110                                               "response" : 
1111                                               "destroy-event");
1112
1113         /* Schedule the next one for being shown */
1114         g_idle_add (idle_top_modal, self);
1115 }
1116
1117 static gboolean
1118 on_modal_window_close (GtkWidget *widget,
1119                        GdkEvent *event,
1120                        gpointer user_data)
1121 {
1122         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1123
1124         /* Remove modal window from queue */
1125         remove_modal_from_queue (widget, self);
1126
1127         /* Continue */
1128         return FALSE;
1129 }
1130
1131 static void
1132 on_modal_dialog_close (GtkDialog *dialog,
1133                        gint arg1,
1134                        gpointer user_data)
1135 {
1136         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1137
1138         /* Remove modal window from queue */
1139         remove_modal_from_queue (GTK_WIDGET (dialog), self);
1140 }
1141
1142 gint 
1143 modest_window_mgr_num_windows (ModestWindowMgr *self)
1144 {
1145         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
1146         gint num_windows = 0;
1147
1148         if (priv->window_list)
1149                 num_windows = g_list_length (priv->window_list);
1150
1151         return num_windows;
1152 }
1153
1154 GtkWidget *   
1155 modest_window_mgr_get_msg_edit_window (ModestWindowMgr *self)
1156 {
1157         GtkWidget *result;
1158         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
1159
1160         if (priv->cached_editor) {
1161                 result = priv->cached_editor;
1162                 priv->cached_editor = NULL;
1163                 load_new_editor (self);
1164         } else {
1165                 result = g_object_new (MODEST_TYPE_MSG_EDIT_WINDOW, NULL);
1166         }
1167
1168         return result;
1169 }
1170
1171 GtkWidget *   
1172 modest_window_mgr_get_msg_view_window (ModestWindowMgr *self)
1173 {
1174         GtkWidget *result;
1175         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
1176
1177         if (priv->cached_view) {
1178                 result = priv->cached_view;
1179                 priv->cached_view = NULL;
1180                 load_new_view (self);
1181         } else {
1182                 result = g_object_new (MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
1183         }
1184
1185         return result;
1186 }