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