1a7e7f01b91340e6220da56055effa8f9af0a968
[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 #include <tny-simple-list.h>
41
42
43 /* 'private'/'protected' functions */
44 static void modest_window_mgr_class_init (ModestWindowMgrClass *klass);
45 static void modest_window_mgr_init       (ModestWindowMgr *obj);
46 static void modest_window_mgr_finalize   (GObject *obj);
47
48 static gboolean modest_window_mgr_register_window_default (ModestWindowMgr *self, 
49                                                            ModestWindow *window,
50                                                            ModestWindow *parent);
51 static void modest_window_mgr_unregister_window_default (ModestWindowMgr *self, 
52                                                          ModestWindow *window);
53 static void modest_window_mgr_set_fullscreen_mode_default (ModestWindowMgr *self,
54                                                            gboolean on);
55 static gboolean modest_window_mgr_get_fullscreen_mode_default (ModestWindowMgr *self);
56 static void modest_window_mgr_show_toolbars_default (ModestWindowMgr *self,
57                                                      GType window_type,
58                                                      gboolean show_toolbars,
59                                                      gboolean fullscreen);
60 static ModestWindow* modest_window_mgr_get_main_window_default (ModestWindowMgr *self, gboolean show);
61 static GtkWindow *modest_window_mgr_get_modal_default (ModestWindowMgr *self);
62 static void modest_window_mgr_set_modal_default (ModestWindowMgr *self, 
63                                                  GtkWindow *window,
64                                                  GtkWindow *parent);
65 static gboolean modest_window_mgr_close_all_windows_default (ModestWindowMgr *self);
66 static gboolean modest_window_mgr_find_registered_header_default (ModestWindowMgr *self, 
67                                                                   TnyHeader *header,
68                                                                   ModestWindow **win);
69 static gboolean modest_window_mgr_find_registered_message_uid_default (ModestWindowMgr *self, 
70                                                                        const gchar *msg_uid,
71                                                                        ModestWindow **win);
72 static GList *modest_window_mgr_get_window_list_default (ModestWindowMgr *self);
73 static ModestWindow *modest_window_mgr_show_initial_window_default (ModestWindowMgr *self);
74 static ModestWindow *modest_window_mgr_get_current_top_default (ModestWindowMgr *self);
75 static gboolean modest_window_mgr_screen_is_on_default (ModestWindowMgr *self);
76 static void modest_window_mgr_create_caches_default (ModestWindowMgr *self);
77 static void modest_window_mgr_on_queue_changed (ModestMailOperationQueue *queue,
78                                                 ModestMailOperation *mail_op,
79                                                 ModestMailOperationQueueNotification type,
80                                                 ModestWindowMgr *self);
81 static void on_mail_operation_started (ModestMailOperation *mail_op,
82                                        gpointer user_data);
83 static void on_mail_operation_finished (ModestMailOperation *mail_op,
84                                         gpointer user_data);
85
86 /* list my signals  */
87 enum {
88         WINDOW_LIST_EMPTY_SIGNAL,
89         PROGRESS_LIST_CHANGED_SIGNAL,
90         NUM_SIGNALS
91 };
92
93 typedef struct _ModestWindowMgrPrivate ModestWindowMgrPrivate;
94 struct _ModestWindowMgrPrivate {
95         guint         banner_counter;
96
97         ModestWindow *main_window;
98
99         GSList       *windows_that_prevent_hibernation;
100         GSList       *preregistered_uids;
101         
102         guint        closing_time;
103
104         GtkWidget    *cached_view;
105         GtkWidget    *cached_editor;
106         guint        idle_load_view_id;
107         guint        idle_load_editor_id;
108
109         guint        queue_change_handler;
110         TnyList      *progress_operations;
111         GSList       *sighandlers;
112 };
113
114 #define MODEST_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
115                                                MODEST_TYPE_WINDOW_MGR, \
116                                                ModestWindowMgrPrivate))
117 /* globals */
118 static GObjectClass *parent_class = NULL;
119
120 /* uncomment the following if you have defined any signals */
121 static guint signals[NUM_SIGNALS] = {0};
122
123 GType
124 modest_window_mgr_get_type (void)
125 {
126         static GType my_type = 0;
127         if (!my_type) {
128                 static const GTypeInfo my_info = {
129                         sizeof(ModestWindowMgrClass),
130                         NULL,           /* base init */
131                         NULL,           /* base finalize */
132                         (GClassInitFunc) modest_window_mgr_class_init,
133                         NULL,           /* class finalize */
134                         NULL,           /* class data */
135                         sizeof(ModestWindowMgr),
136                         1,              /* n_preallocs */
137                         (GInstanceInitFunc) modest_window_mgr_init,
138                         NULL
139                 };
140                 my_type = g_type_register_static (G_TYPE_OBJECT,
141                                                   "ModestWindowMgr",
142                                                   &my_info, 0);
143         }
144         return my_type;
145 }
146
147 static void
148 modest_window_mgr_class_init (ModestWindowMgrClass *klass)
149 {
150         GObjectClass *gobject_class;
151         ModestWindowMgrClass *mgr_class;
152
153         gobject_class = (GObjectClass*) klass;
154         mgr_class = (ModestWindowMgrClass *) klass;
155
156         parent_class            = g_type_class_peek_parent (klass);
157         gobject_class->finalize = modest_window_mgr_finalize;
158         mgr_class->register_window = modest_window_mgr_register_window_default;
159         mgr_class->unregister_window = modest_window_mgr_unregister_window_default;
160         mgr_class->set_fullscreen_mode = modest_window_mgr_set_fullscreen_mode_default;
161         mgr_class->get_fullscreen_mode = modest_window_mgr_get_fullscreen_mode_default;
162         mgr_class->show_toolbars = modest_window_mgr_show_toolbars_default;
163         mgr_class->get_main_window = modest_window_mgr_get_main_window_default;
164         mgr_class->get_modal = modest_window_mgr_get_modal_default;
165         mgr_class->set_modal = modest_window_mgr_set_modal_default;
166         mgr_class->close_all_windows = modest_window_mgr_close_all_windows_default;
167         mgr_class->find_registered_header = modest_window_mgr_find_registered_header_default;
168         mgr_class->find_registered_message_uid = modest_window_mgr_find_registered_message_uid_default;
169         mgr_class->get_window_list = modest_window_mgr_get_window_list_default;
170         mgr_class->show_initial_window = modest_window_mgr_show_initial_window_default;
171         mgr_class->get_current_top = modest_window_mgr_get_current_top_default;
172         mgr_class->screen_is_on = modest_window_mgr_screen_is_on_default;
173         mgr_class->create_caches = modest_window_mgr_create_caches_default;
174
175         g_type_class_add_private (gobject_class, sizeof(ModestWindowMgrPrivate));
176
177
178         /**
179          * ModestWindowMgr::window-list-empty
180          * @self: the #ModestWindowMgr that emits the signal
181          * @user_data: user data set when the signal handler was connected
182          *
183          * Issued whenever the window list becomes empty
184          */
185         signals[WINDOW_LIST_EMPTY_SIGNAL] =
186                 g_signal_new ("window-list-empty",
187                               G_TYPE_FROM_CLASS (gobject_class),
188                               G_SIGNAL_RUN_FIRST,
189                               G_STRUCT_OFFSET (ModestWindowMgrClass, window_list_empty),
190                               NULL, NULL,
191                               g_cclosure_marshal_VOID__VOID,
192                               G_TYPE_NONE, 0);
193
194         /**
195          * ModestWindowMgr::progress-list-changed
196          * @self: the #ModestWindowMgr that emits the signal
197          * @user_data: user data set when the signal handler was connected
198          *
199          * Issued whenever the progress mail operations list becomes changed
200          */
201         signals[PROGRESS_LIST_CHANGED_SIGNAL] =
202                 g_signal_new ("progress-list-changed",
203                               G_TYPE_FROM_CLASS (gobject_class),
204                               G_SIGNAL_RUN_FIRST,
205                               G_STRUCT_OFFSET (ModestWindowMgrClass, progress_list_changed),
206                               NULL, NULL,
207                               g_cclosure_marshal_VOID__VOID,
208                               G_TYPE_NONE, 0);
209 }
210
211 static gboolean
212 idle_load_view (ModestWindowMgr *mgr)
213 {
214         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (mgr);
215         
216         priv->cached_view = g_object_new (MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
217         priv->idle_load_view_id = 0;
218         return FALSE;
219 }
220
221 static gboolean
222 idle_load_editor (ModestWindowMgr *mgr)
223 {
224         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (mgr);
225         
226         priv->cached_editor = g_object_new (MODEST_TYPE_MSG_EDIT_WINDOW, NULL);
227         priv->idle_load_editor_id = 0;
228         return FALSE;
229 }
230
231 static void
232 load_new_view (ModestWindowMgr *self)
233 {
234         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
235         if ((priv->cached_view == NULL) && (priv->idle_load_view_id == 0))
236                 priv->idle_load_view_id = g_timeout_add (2500, (GSourceFunc) idle_load_view, self);
237 }
238
239 static void
240 load_new_editor (ModestWindowMgr *self)
241 {
242         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
243         if ((priv->cached_editor == NULL) && (priv->idle_load_editor_id == 0))
244                 priv->idle_load_editor_id = g_timeout_add (5000, (GSourceFunc) idle_load_editor, self);
245 }
246
247 static void
248 modest_window_mgr_init (ModestWindowMgr *obj)
249 {
250         ModestWindowMgrPrivate *priv;
251
252         priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
253         priv->banner_counter = 0;
254         priv->main_window = NULL;
255
256         priv->preregistered_uids = NULL;
257
258         priv->closing_time = 0;
259
260         priv->cached_view = NULL;
261         priv->cached_editor = NULL;
262
263         priv->windows_that_prevent_hibernation = NULL;
264
265         priv->queue_change_handler = 0;
266         priv->progress_operations = TNY_LIST (tny_simple_list_new ());
267 }
268
269 static void
270 modest_window_mgr_finalize (GObject *obj)
271 {
272         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
273
274         if (priv->idle_load_view_id > 0) {
275                 g_source_remove (priv->idle_load_view_id);
276                 priv->idle_load_view_id = 0;
277         }
278         
279         if (priv->idle_load_editor_id > 0) {
280                 g_source_remove (priv->idle_load_editor_id);
281                 priv->idle_load_editor_id = 0;
282         }
283         
284         if (priv->cached_view) {
285                 gtk_widget_destroy (priv->cached_view);
286                 priv->cached_view = NULL;
287         }
288         if (priv->cached_editor) {
289                 gtk_widget_destroy (priv->cached_editor);
290                 priv->cached_editor = NULL;
291         }
292
293         if (priv->windows_that_prevent_hibernation) {
294                 g_slist_free (priv->windows_that_prevent_hibernation);
295                 priv->cached_editor = NULL;
296         }
297
298         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
299         priv->sighandlers = NULL;
300
301         if (priv->queue_change_handler > 0) {
302                 g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
303                                              priv->queue_change_handler);
304                 priv->queue_change_handler = 0;
305         }
306
307         if (priv->progress_operations) {
308                 g_object_unref (priv->progress_operations);
309                 priv->progress_operations = NULL;
310         }
311
312         g_slist_foreach (priv->preregistered_uids, (GFunc)g_free, NULL);
313         g_slist_free (priv->preregistered_uids);
314
315         G_OBJECT_CLASS(parent_class)->finalize (obj);
316 }
317
318 ModestWindowMgr*
319 modest_window_mgr_new (void)
320 {
321         return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_WINDOW_MGR, NULL));
322 }
323
324
325
326
327 /* do we have uid? */
328 static gboolean
329 has_uid (GSList *list, const gchar *uid)
330 {
331         GSList *cursor = list;
332
333         if (!uid)
334                 return FALSE;
335         
336         while (cursor) {
337                 if (cursor->data && strcmp (cursor->data, uid) == 0)
338                         return TRUE;
339                 cursor = g_slist_next (cursor);
340         }
341         return FALSE;
342 }
343
344
345 /* remove all from the list have have uid = uid */
346 static GSList*
347 remove_uid (GSList *list, const gchar *uid)
348 {
349         GSList *cursor = list, *start = list;
350         
351         if (!uid)
352                 return list;
353         
354         while (cursor) {
355                 GSList *next = g_slist_next (cursor);
356                 if (cursor->data && strcmp (cursor->data, uid) == 0) {
357                         g_free (cursor->data);
358                         start = g_slist_delete_link (start, cursor);
359                 }
360                 cursor = next;
361         }
362         return start;
363 }
364
365
366 static GSList *
367 append_uid (GSList *list, const gchar *uid)
368 {
369         return g_slist_append (list, g_strdup(uid));
370 }
371
372
373
374 void 
375 modest_window_mgr_register_header (ModestWindowMgr *self,  TnyHeader *header, const gchar *alt_uid)
376 {
377         ModestWindowMgrPrivate *priv;
378         gchar* uid;
379         
380         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
381         g_return_if_fail (TNY_IS_HEADER(header));
382                 
383         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
384         if (alt_uid != NULL) {
385                 uid = g_strdup (alt_uid);
386         } else {
387                 uid = modest_tny_folder_get_header_unique_id (header);
388         }
389
390         if (!has_uid (priv->preregistered_uids, uid)) {
391                 MODEST_DEBUG_BLOCK(g_debug ("registering new uid %s", uid););
392                 priv->preregistered_uids = append_uid (priv->preregistered_uids, uid);
393         } else
394                 MODEST_DEBUG_BLOCK(g_debug ("already had uid %s", uid););
395         
396         g_free (uid);
397 }
398
399 void 
400 modest_window_mgr_unregister_header (ModestWindowMgr *self,  TnyHeader *header)
401 {
402         ModestWindowMgrPrivate *priv;
403         gchar* uid;
404         
405         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
406         g_return_if_fail (TNY_IS_HEADER(header));
407                 
408         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
409         uid = modest_tny_folder_get_header_unique_id (header);
410
411         if (!has_uid (priv->preregistered_uids, uid)) {
412                 MODEST_DEBUG_BLOCK(g_debug ("trying to unregister non-existing uid %s", uid););
413                 priv->preregistered_uids = append_uid (priv->preregistered_uids, uid);
414         } else
415                 MODEST_DEBUG_BLOCK(g_debug ("unregistering uid %s", uid););
416         
417         if (has_uid (priv->preregistered_uids, uid)) {
418                 priv->preregistered_uids = remove_uid (priv->preregistered_uids, uid);
419                 if (has_uid (priv->preregistered_uids, uid))
420                         g_debug ("BUG: uid %s NOT removed", uid);
421                 else
422                         MODEST_DEBUG_BLOCK(g_debug ("uid %s removed", uid););
423         }
424                 
425         g_free (uid);
426 }
427
428
429 #define MODEST_WINDOW_HELP_ID_PARAM "help-id"
430
431 void
432 modest_window_mgr_register_help_id (ModestWindowMgr *self, GtkWindow *win, const gchar* help_id)
433 {
434         /* we don't need 'self', but for API consistency... */
435         g_return_if_fail (self && MODEST_IS_WINDOW_MGR(self));
436
437         g_return_if_fail (win && GTK_IS_WINDOW(win));
438         g_return_if_fail (help_id);
439         
440         g_object_set_data_full (G_OBJECT(win), MODEST_WINDOW_HELP_ID_PARAM,
441                                 g_strdup(help_id), g_free);
442 }
443
444
445 const gchar*
446 modest_window_mgr_get_help_id (ModestWindowMgr *self, GtkWindow *win)
447 {
448         /* we don't need 'self', but for API consistency... */
449         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR (self), NULL);
450         g_return_val_if_fail (win, NULL);
451
452         return g_object_get_data (G_OBJECT(win), MODEST_WINDOW_HELP_ID_PARAM);
453 }
454
455 gboolean
456 modest_window_mgr_close_all_windows (ModestWindowMgr *self)
457 {
458         return MODEST_WINDOW_MGR_GET_CLASS (self)->close_all_windows (self);
459 }
460
461 static gboolean
462 modest_window_mgr_close_all_windows_default (ModestWindowMgr *self)
463 {
464         return TRUE;
465 }
466
467
468 gboolean
469 modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
470                                                   ModestWindow **win)
471 {
472         return MODEST_WINDOW_MGR_GET_CLASS (self)->find_registered_header (self, header, win);
473 }
474
475 static gboolean
476 modest_window_mgr_find_registered_header_default (ModestWindowMgr *self, TnyHeader *header,
477                                                   ModestWindow **win)
478 {
479         gchar* uid = NULL;
480
481         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
482         g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
483
484         uid = modest_tny_folder_get_header_unique_id (header);
485
486         return modest_window_mgr_find_registered_message_uid (self, uid, win);
487 }
488
489 gboolean
490 modest_window_mgr_find_registered_message_uid (ModestWindowMgr *self, const gchar *msg_uid,
491                                                ModestWindow **win)
492 {
493         return MODEST_WINDOW_MGR_GET_CLASS (self)->find_registered_message_uid (self, msg_uid, win);
494 }
495
496 static gboolean
497 modest_window_mgr_find_registered_message_uid_default (ModestWindowMgr *self, const gchar *msg_uid,
498                                                        ModestWindow **win)
499 {
500         ModestWindowMgrPrivate *priv = NULL;
501         gchar* uid = NULL;
502         gboolean has_header = FALSE;
503
504         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
505         g_return_val_if_fail (msg_uid && msg_uid[0] != '\0', FALSE);
506
507         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
508
509         if (win)
510                 *win = NULL;
511
512         has_header = has_uid (priv->preregistered_uids, msg_uid);
513         g_free (uid);
514
515         return has_header;
516 }
517
518 GList *
519 modest_window_mgr_get_window_list (ModestWindowMgr *self)
520 {
521         return MODEST_WINDOW_MGR_GET_CLASS (self)->get_window_list (self);
522 }
523
524 static GList *
525 modest_window_mgr_get_window_list_default (ModestWindowMgr *self)
526 {
527         return NULL;
528 }
529
530 gboolean
531 modest_window_mgr_register_window (ModestWindowMgr *self, 
532                                    ModestWindow *window,
533                                    ModestWindow *parent)
534 {
535         return MODEST_WINDOW_MGR_GET_CLASS (self)->register_window (self, window, parent);
536 }
537
538 static gboolean
539 modest_window_mgr_register_window_default (ModestWindowMgr *self, 
540                                            ModestWindow *window,
541                                            ModestWindow *parent)
542 {
543         ModestWindowMgrPrivate *priv;
544
545         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
546         g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
547
548         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
549
550         /* We set up the queue change handler */
551         if (priv->queue_change_handler == 0) {
552                 priv->queue_change_handler = g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
553                                                                "queue-changed",
554                                                                G_CALLBACK (modest_window_mgr_on_queue_changed),
555                                                                self);
556         }
557
558         /* Check that it's not a second main window */
559         if (MODEST_IS_MAIN_WINDOW (window)) {
560                 if (priv->main_window) {
561                         g_warning ("%s: trying to register a second main window",
562                                    __FUNCTION__);
563                         return FALSE;
564                 } else {
565                         priv->main_window = window;
566                 }
567         }
568
569         /* remove from the list of pre-registered uids */
570         if (MODEST_IS_MSG_VIEW_WINDOW(window)) {
571                 const gchar *uid = modest_msg_view_window_get_message_uid
572                         (MODEST_MSG_VIEW_WINDOW (window));
573
574                 if (!has_uid (priv->preregistered_uids, uid)) 
575                         g_debug ("weird: no uid for window (%s)", uid);
576                 
577                 MODEST_DEBUG_BLOCK(g_debug ("registering window for %s", uid ? uid : "<none>"););
578                 
579                 priv->preregistered_uids = 
580                         remove_uid (priv->preregistered_uids,
581                                     modest_msg_view_window_get_message_uid
582                                     (MODEST_MSG_VIEW_WINDOW (window)));
583         } else if (MODEST_IS_MSG_EDIT_WINDOW(window)) {
584                 const gchar *uid = modest_msg_edit_window_get_message_uid
585                         (MODEST_MSG_EDIT_WINDOW (window));
586                 
587                 MODEST_DEBUG_BLOCK(g_debug ("registering window for %s", uid););
588
589                 priv->preregistered_uids = 
590                         remove_uid (priv->preregistered_uids,
591                                     modest_msg_edit_window_get_message_uid
592                                     (MODEST_MSG_EDIT_WINDOW (window)));
593         }
594
595         return TRUE;
596 }
597
598 void 
599 modest_window_mgr_unregister_window (ModestWindowMgr *self, 
600                                      ModestWindow *window)
601 {
602         MODEST_WINDOW_MGR_GET_CLASS (self)->unregister_window (self, window);
603 }
604
605 static void 
606 modest_window_mgr_unregister_window_default (ModestWindowMgr *self, 
607                                              ModestWindow *window)
608 {
609         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
610         g_return_if_fail (MODEST_IS_WINDOW (window));
611
612         /* Save state */
613         modest_window_save_state (window);
614
615         /* Disconnect all the window signals */
616         modest_window_disconnect_signals (window);
617         
618         /* Destroy the window */
619         gtk_widget_destroy (GTK_WIDGET (window));
620 }
621
622
623
624 void
625 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
626                                        gboolean on)
627 {
628         MODEST_WINDOW_MGR_GET_CLASS (self)->set_fullscreen_mode (self, on);
629 }
630
631 static void
632 modest_window_mgr_set_fullscreen_mode_default (ModestWindowMgr *self,
633                                                gboolean on)
634 {
635         return;
636 }
637
638 gboolean
639 modest_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
640 {
641         return MODEST_WINDOW_MGR_GET_CLASS (self)->get_fullscreen_mode (self);
642 }
643
644 static gboolean
645 modest_window_mgr_get_fullscreen_mode_default (ModestWindowMgr *self)
646 {
647         return FALSE;
648 }
649
650 void 
651 modest_window_mgr_show_toolbars (ModestWindowMgr *self,
652                                  GType window_type,
653                                  gboolean show_toolbars,
654                                  gboolean fullscreen)
655 {
656         return MODEST_WINDOW_MGR_GET_CLASS (self)->show_toolbars (self, window_type, show_toolbars, fullscreen);
657 }
658
659 static void 
660 modest_window_mgr_show_toolbars_default (ModestWindowMgr *self,
661                                          GType window_type,
662                                          gboolean show_toolbars,
663                                          gboolean fullscreen)
664 {
665         return;
666 }
667
668 void
669 modest_window_mgr_set_main_window (ModestWindowMgr *self, ModestWindow *win)
670 {
671         ModestWindowMgrPrivate *priv;
672         
673         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
674
675         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
676         priv->main_window = win;
677 }
678
679 ModestWindow*  
680 modest_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
681 {
682         return MODEST_WINDOW_MGR_GET_CLASS (self)->get_main_window (self, show);
683 }
684
685 static ModestWindow*  
686 modest_window_mgr_get_main_window_default (ModestWindowMgr *self, gboolean show)
687 {
688         ModestWindowMgrPrivate *priv;
689         
690         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
691
692         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
693         if (priv->main_window)
694                 return priv->main_window;
695
696         if (show) 
697                 return modest_main_window_new ();
698         else return NULL;
699 }
700
701
702 gboolean
703 modest_window_mgr_main_window_exists  (ModestWindowMgr *self)
704 {
705         ModestWindowMgrPrivate *priv;
706         
707         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
708         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
709
710         return priv->main_window != NULL;
711 }
712
713
714 GtkWindow *
715 modest_window_mgr_get_modal (ModestWindowMgr *self)
716 {
717         return MODEST_WINDOW_MGR_GET_CLASS (self)->get_modal (self);
718 }
719
720 static GtkWindow *
721 modest_window_mgr_get_modal_default (ModestWindowMgr *self)
722 {
723         return NULL;
724 }
725
726
727 void
728 modest_window_mgr_set_modal (ModestWindowMgr *self, 
729                              GtkWindow *window,
730                              GtkWindow *parent)
731 {
732         MODEST_WINDOW_MGR_GET_CLASS (self)->set_modal (self, window, parent);
733 }
734
735 static void
736 modest_window_mgr_set_modal_default (ModestWindowMgr *self, 
737                                      GtkWindow *window,
738                                      GtkWindow *parent)
739 {
740         return;
741 }
742
743
744 static void
745 on_nonhibernating_window_hide(GtkWidget *widget, gpointer user_data)
746 {
747         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
748         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
749         
750         /* Forget this window,
751          * so hibernation will be allowed again if no windows are remembered: */
752         priv->windows_that_prevent_hibernation =
753                 g_slist_remove (priv->windows_that_prevent_hibernation, GTK_WINDOW(widget));
754 }
755
756 static void
757 on_nonhibernating_window_show(GtkWidget *widget, gpointer user_data)
758 {
759         ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
760         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
761         
762         GtkWindow *window = GTK_WINDOW (widget);
763         
764         priv->windows_that_prevent_hibernation = 
765                         g_slist_append (priv->windows_that_prevent_hibernation, window);
766         
767         /* Allow hibernation again when the window has been hidden: */
768         g_signal_connect (window, "hide", 
769                 G_CALLBACK (on_nonhibernating_window_hide), self);
770 }
771
772 void
773 modest_window_mgr_prevent_hibernation_while_window_is_shown (ModestWindowMgr *self,
774                                                                   GtkWindow *window)
775 {
776         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
777         
778         if (GTK_WIDGET_VISIBLE(window)) {
779                 on_nonhibernating_window_show (GTK_WIDGET (window), self);
780         } else {
781                 /* Wait for it to be shown: */
782                 g_signal_connect (window, "show", 
783                         G_CALLBACK (on_nonhibernating_window_show), self);      
784         }
785 }
786
787 gboolean
788 modest_window_mgr_get_hibernation_is_prevented (ModestWindowMgr *self)
789 {
790         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
791         
792         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
793         
794         /* Prevent hibernation if any open windows are currently 
795          * preventing hibernation: */
796         return (g_slist_length (priv->windows_that_prevent_hibernation) > 0);
797 }
798
799
800 void
801 modest_window_mgr_save_state_for_all_windows (ModestWindowMgr *self)
802 {
803         GList *window_list;
804         GList *node;
805         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
806         
807         /* Iterate over all windows */
808         window_list = modest_window_mgr_get_window_list (self);
809         node = window_list;
810         while (node) {
811                 ModestWindow *window = MODEST_WINDOW (node->data);
812                 if (window) {
813                         /* This calls the vfunc, 
814                          * so each window can do its own thing: */
815                         modest_window_save_state (window);
816                 }
817
818                 node = g_list_next (node);
819         }
820         g_list_free (window_list);
821 }
822
823 guint
824 modest_window_mgr_get_num_windows (ModestWindowMgr *self)
825 {
826         ModestWindowMgrPrivate *priv;
827         gint num_windows = 0;
828         GList *window_list;
829
830         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), -1);
831
832         priv =  MODEST_WINDOW_MGR_GET_PRIVATE(self);
833
834         window_list = modest_window_mgr_get_window_list (self);
835
836         if (window_list) {
837                 num_windows = g_list_length (window_list);
838                 g_list_free (window_list);
839         }
840
841         /* Do not take into account the main window if it was hidden */
842         if (num_windows && priv->main_window && !GTK_WIDGET_VISIBLE (priv->main_window))
843                 num_windows--;
844
845         return num_windows + priv->banner_counter;
846 }
847
848 GtkWidget *   
849 modest_window_mgr_get_msg_edit_window (ModestWindowMgr *self)
850 {
851         GtkWidget *result;
852         ModestWindowMgrPrivate *priv;
853
854         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), NULL);
855
856         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
857                 
858         if (priv->cached_editor) {
859                 result = priv->cached_editor;
860                 priv->cached_editor = NULL;
861                 load_new_editor (self);
862         } else {
863                 result = g_object_new (MODEST_TYPE_MSG_EDIT_WINDOW, NULL);
864         }
865
866         return result;
867 }
868
869 GtkWidget *   
870 modest_window_mgr_get_msg_view_window (ModestWindowMgr *self)
871 {
872         GtkWidget *result;
873         ModestWindowMgrPrivate *priv;
874
875         g_return_val_if_fail (self && MODEST_IS_WINDOW_MGR(self), NULL);
876         
877         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
878
879         if (priv->cached_view) {
880                 result = priv->cached_view;
881                 priv->cached_view = NULL;
882                 load_new_view (self);
883         } else {
884                 result = g_object_new (MODEST_TYPE_MSG_VIEW_WINDOW, NULL);
885         }
886
887         return result;
888 }
889
890 void
891 modest_window_mgr_register_banner (ModestWindowMgr *self)
892 {
893         ModestWindowMgrPrivate *priv;
894
895         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
896         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
897
898         priv->banner_counter++;
899 }
900
901 void
902 modest_window_mgr_unregister_banner (ModestWindowMgr *self)
903 {
904         ModestWindowMgrPrivate *priv;
905
906         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
907         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
908
909         priv->banner_counter--;
910         if (modest_window_mgr_get_num_windows (self) == 0)
911                 g_signal_emit (self, signals[WINDOW_LIST_EMPTY_SIGNAL], 0);
912 }
913
914 ModestWindow *
915 modest_window_mgr_show_initial_window (ModestWindowMgr *self)
916 {
917         ModestWindow *window = NULL;
918
919         /* Call the children */
920         window = MODEST_WINDOW_MGR_GET_CLASS (self)->show_initial_window (self);
921
922         if (window) {
923                 ModestAccountMgr *mgr;
924
925                 /* Show the initial window */
926                 gtk_widget_show (GTK_WIDGET (window));
927
928                 /* If there are no accounts then show the account wizard */
929                 mgr = modest_runtime_get_account_mgr();
930                 if (!modest_account_mgr_has_accounts (mgr, TRUE)) {
931                         if (!modest_ui_actions_run_account_setup_wizard (window)) {
932                                 g_debug ("%s: couldn't show account setup wizard", __FUNCTION__);
933                         }
934                 }
935         }
936
937         return window;
938 }
939
940 static ModestWindow *
941 modest_window_mgr_show_initial_window_default (ModestWindowMgr *self)
942 {
943         /* By default it returns the main window creating it if
944            needed */
945         return modest_window_mgr_get_main_window (self, TRUE);
946 }
947
948
949 ModestWindow *
950 modest_window_mgr_get_current_top (ModestWindowMgr *self) 
951 {
952         return MODEST_WINDOW_MGR_GET_CLASS (self)->get_current_top (self);
953 }
954
955
956 static ModestWindow *
957 modest_window_mgr_get_current_top_default (ModestWindowMgr *self)
958 {
959         g_return_val_if_reached (NULL);
960 }
961
962 gboolean
963 modest_window_mgr_screen_is_on (ModestWindowMgr *self)
964 {
965         return MODEST_WINDOW_MGR_GET_CLASS (self)->screen_is_on (self);
966 }
967
968 static gboolean
969 modest_window_mgr_screen_is_on_default (ModestWindowMgr *self)
970 {
971         /* Default implementation is assuming screen is always on */
972
973         return TRUE;
974 }
975
976 void
977 modest_window_mgr_create_caches (ModestWindowMgr *mgr)
978 {
979         MODEST_WINDOW_MGR_GET_CLASS (mgr)->create_caches (mgr);
980 }
981
982 static void
983 modest_window_mgr_create_caches_default (ModestWindowMgr *self)
984 {
985         load_new_editor (self);
986         load_new_view (self);
987 }
988
989 static gboolean
990 tny_list_find (TnyList *list, GObject *item)
991 {
992         TnyIterator *iterator;
993         gboolean found = FALSE;
994
995         for (iterator = tny_list_create_iterator (list);
996              !tny_iterator_is_done (iterator) && !found;
997              tny_iterator_next (iterator)) {
998                 GObject *current = tny_iterator_get_current (iterator);
999                 if (current == item)
1000                         found = TRUE;
1001                 g_object_unref (current);
1002         }
1003         g_object_unref (iterator);
1004         
1005         return found;
1006 }
1007
1008 static void
1009 modest_window_mgr_on_queue_changed (ModestMailOperationQueue *queue,
1010                                     ModestMailOperation *mail_op,
1011                                     ModestMailOperationQueueNotification type,
1012                                     ModestWindowMgr *self)
1013 {       
1014         ModestWindowMgrPrivate *priv;
1015
1016         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1017
1018         /* We register to track progress */
1019         if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED) {
1020                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
1021                                                                G_OBJECT (mail_op),
1022                                                                "operation-started",
1023                                                                G_CALLBACK (on_mail_operation_started),
1024                                                                self);
1025                 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
1026                                                                G_OBJECT (mail_op),
1027                                                                "operation-finished",
1028                                                                G_CALLBACK (on_mail_operation_finished),
1029                                                                self);
1030         } else if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED) {
1031                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
1032                                                                   G_OBJECT (mail_op),
1033                                                                   "operation-started");
1034                 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
1035                                                                   G_OBJECT (mail_op),
1036                                                                   "operation-finished");
1037                 if (tny_list_find  (priv->progress_operations, G_OBJECT (mail_op))) {
1038                         tny_list_remove (priv->progress_operations, G_OBJECT (mail_op));
1039                         g_signal_emit (self, signals[PROGRESS_LIST_CHANGED_SIGNAL], 0);
1040                 }
1041         }
1042 }
1043
1044 static void 
1045 on_mail_operation_started (ModestMailOperation *mail_op,
1046                            gpointer user_data)
1047 {
1048         ModestWindowMgr *self;
1049         ModestWindowMgrPrivate *priv;
1050         ModestMailOperationTypeOperation op_type;
1051
1052         self = MODEST_WINDOW_MGR (user_data);
1053         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1054
1055         /* First we check if the operation is a send receive operation,
1056          * If now, we don't handle this */
1057         op_type = modest_mail_operation_get_type_operation (mail_op);
1058         if (op_type != MODEST_MAIL_OPERATION_TYPE_SEND &&
1059             op_type != MODEST_MAIL_OPERATION_TYPE_SEND_AND_RECEIVE) {
1060                 return;
1061         }
1062
1063         if (!tny_list_find (priv->progress_operations, G_OBJECT (mail_op))) {
1064                 tny_list_prepend (priv->progress_operations, G_OBJECT (mail_op));
1065                 g_signal_emit (self, signals[PROGRESS_LIST_CHANGED_SIGNAL], 0);
1066         }
1067 }
1068
1069 static void 
1070 on_mail_operation_finished (ModestMailOperation *mail_op,
1071                             gpointer user_data)
1072 {
1073         ModestWindowMgr *self;
1074         ModestWindowMgrPrivate *priv;
1075
1076         self = MODEST_WINDOW_MGR (user_data);
1077         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1078
1079         if (tny_list_find (priv->progress_operations, G_OBJECT (mail_op))) {
1080                 tny_list_remove (priv->progress_operations, G_OBJECT (mail_op));
1081                 g_signal_emit (self, signals[PROGRESS_LIST_CHANGED_SIGNAL], 0);
1082         }
1083 }
1084
1085 TnyList *
1086 modest_window_mgr_get_progress_operations (ModestWindowMgr *self)
1087 {
1088         ModestWindowMgrPrivate *priv;
1089
1090         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1091
1092         return tny_list_copy (priv->progress_operations);
1093 }
1094
1095 gboolean 
1096 modest_window_mgr_has_progress_operation (ModestWindowMgr *self)
1097 {
1098         ModestWindowMgrPrivate *priv;
1099
1100         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1101
1102         return tny_list_get_length (priv->progress_operations) > 0;
1103 }
1104
1105 gboolean 
1106 modest_window_mgr_has_progress_operation_on_account (ModestWindowMgr *self,
1107                                                      const gchar *account_name)
1108 {
1109         ModestWindowMgrPrivate *priv;
1110         gint account_ops = 0;
1111         TnyIterator *iterator;
1112
1113         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
1114
1115         if (account_name == NULL)
1116                 return 0;
1117
1118         for (iterator = tny_list_create_iterator (priv->progress_operations);
1119              !tny_iterator_is_done (iterator);
1120              tny_iterator_next (iterator)) {
1121                 ModestMailOperation *mail_op; 
1122                 TnyAccount *account;
1123
1124                 mail_op= MODEST_MAIL_OPERATION (tny_iterator_get_current (iterator));
1125                 account = modest_mail_operation_get_account (mail_op);
1126
1127                 if (account != NULL) {
1128                         const gchar *current_name;
1129
1130                         current_name = tny_account_get_id (account);
1131                         if (current_name && strcmp (current_name, account_name) == 0)
1132                                 account_ops ++;
1133                         g_object_unref (account);
1134                 }
1135
1136                 g_object_unref (mail_op);
1137         }
1138         g_object_unref (iterator);
1139
1140         return account_ops;
1141 }
1142