1 /* Copyright (c) 2008, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
31 #include "modest-hildon1-window-mgr.h"
32 #include "modest-msg-edit-window.h"
33 #include "modest-main-window.h"
34 #include "modest-conf.h"
35 #include "modest-defs.h"
36 #include "modest-signal-mgr.h"
37 #include "modest-runtime.h"
38 #include "modest-platform.h"
39 #include "modest-ui-actions.h"
40 #include "modest-debug.h"
41 #include "modest-tny-folder.h"
43 /* 'private'/'protected' functions */
44 static void modest_hildon1_window_mgr_class_init (ModestHildon1WindowMgrClass *klass);
45 static void modest_hildon1_window_mgr_instance_init (ModestHildon1WindowMgr *obj);
46 static void modest_hildon1_window_mgr_finalize (GObject *obj);
48 static gboolean on_window_destroy (ModestWindow *window,
50 ModestHildon1WindowMgr *self);
52 static gboolean on_modal_window_close (GtkWidget *widget,
56 static void on_modal_dialog_destroy (GtkObject *object,
59 static void on_modal_dialog_close (GtkDialog *dialog,
63 static const gchar* get_show_toolbar_key (GType window_type,
66 static gboolean modest_hildon1_window_mgr_register_window (ModestWindowMgr *self,
68 ModestWindow *parent);
69 static void modest_hildon1_window_mgr_unregister_window (ModestWindowMgr *self,
70 ModestWindow *window);
71 static void modest_hildon1_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
73 static gboolean modest_hildon1_window_mgr_get_fullscreen_mode (ModestWindowMgr *self);
74 static void modest_hildon1_window_mgr_show_toolbars (ModestWindowMgr *self,
76 gboolean show_toolbars,
78 static ModestWindow* modest_hildon1_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show);
79 static GtkWindow *modest_hildon1_window_mgr_get_modal (ModestWindowMgr *self);
80 static void modest_hildon1_window_mgr_set_modal (ModestWindowMgr *self,
83 static gboolean modest_hildon1_window_mgr_find_registered_header (ModestWindowMgr *self,
86 static GList *modest_hildon1_window_mgr_get_window_list (ModestWindowMgr *self);
87 static gboolean modest_hildon1_window_mgr_close_all_windows (ModestWindowMgr *self);
88 static ModestWindow *modest_hildon1_window_mgr_get_current_top (ModestWindowMgr *self);
90 typedef struct _ModestHildon1WindowMgrPrivate ModestHildon1WindowMgrPrivate;
91 struct _ModestHildon1WindowMgrPrivate {
94 GQueue *modal_windows;
96 gboolean fullscreen_mode;
98 GHashTable *destroy_handlers;
99 GHashTable *viewer_handlers;
100 GSList *window_state_uids;
104 GSList *modal_handler_uids;
105 ModestWindow *current_top;
107 #define MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
108 MODEST_TYPE_HILDON1_WINDOW_MGR, \
109 ModestHildon1WindowMgrPrivate))
111 static GObjectClass *parent_class = NULL;
114 modest_hildon1_window_mgr_get_type (void)
116 static GType my_type = 0;
118 static const GTypeInfo my_info = {
119 sizeof(ModestHildon1WindowMgrClass),
120 NULL, /* base init */
121 NULL, /* base finalize */
122 (GClassInitFunc) modest_hildon1_window_mgr_class_init,
123 NULL, /* class finalize */
124 NULL, /* class data */
125 sizeof(ModestHildon1WindowMgr),
127 (GInstanceInitFunc) modest_hildon1_window_mgr_instance_init,
130 my_type = g_type_register_static (MODEST_TYPE_WINDOW_MGR,
131 "ModestHildon1WindowMgr",
138 modest_hildon1_window_mgr_class_init (ModestHildon1WindowMgrClass *klass)
140 GObjectClass *gobject_class;
141 ModestWindowMgrClass *mgr_class;
143 gobject_class = (GObjectClass*) klass;
144 mgr_class = (ModestWindowMgrClass *) klass;
146 parent_class = g_type_class_peek_parent (klass);
147 gobject_class->finalize = modest_hildon1_window_mgr_finalize;
148 mgr_class->register_window = modest_hildon1_window_mgr_register_window;
149 mgr_class->unregister_window = modest_hildon1_window_mgr_unregister_window;
150 mgr_class->set_fullscreen_mode = modest_hildon1_window_mgr_set_fullscreen_mode;
151 mgr_class->get_fullscreen_mode = modest_hildon1_window_mgr_get_fullscreen_mode;
152 mgr_class->show_toolbars = modest_hildon1_window_mgr_show_toolbars;
153 mgr_class->get_main_window = modest_hildon1_window_mgr_get_main_window;
154 mgr_class->get_modal = modest_hildon1_window_mgr_get_modal;
155 mgr_class->set_modal = modest_hildon1_window_mgr_set_modal;
156 mgr_class->find_registered_header = modest_hildon1_window_mgr_find_registered_header;
157 mgr_class->get_window_list = modest_hildon1_window_mgr_get_window_list;
158 mgr_class->close_all_windows = modest_hildon1_window_mgr_close_all_windows;
159 mgr_class->get_current_top = modest_hildon1_window_mgr_get_current_top;
161 g_type_class_add_private (gobject_class, sizeof(ModestHildon1WindowMgrPrivate));
166 modest_hildon1_window_mgr_instance_init (ModestHildon1WindowMgr *obj)
168 ModestHildon1WindowMgrPrivate *priv;
170 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(obj);
171 priv->window_list = NULL;
172 priv->fullscreen_mode = FALSE;
173 priv->current_top = NULL;
174 priv->window_state_uids = NULL;
176 priv->modal_windows = g_queue_new ();
177 priv->queue_lock = g_mutex_new ();
179 /* Could not initialize it from gconf, singletons are not
181 priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
182 priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
184 priv->closing_time = 0;
186 priv->modal_handler_uids = NULL;
190 modest_hildon1_window_mgr_finalize (GObject *obj)
192 ModestHildon1WindowMgrPrivate *priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(obj);
194 modest_signal_mgr_disconnect_all_and_destroy (priv->window_state_uids);
195 priv->window_state_uids = NULL;
197 if (priv->window_list) {
198 GList *iter = priv->window_list;
199 /* unregister pending windows */
201 ModestWindow *window = (ModestWindow *) iter->data;
202 iter = g_list_next (iter);
203 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), window);
205 g_list_free (priv->window_list);
206 priv->window_list = NULL;
209 /* Free the hash table with the handlers */
210 if (priv->destroy_handlers) {
211 g_hash_table_destroy (priv->destroy_handlers);
212 priv->destroy_handlers = NULL;
215 if (priv->viewer_handlers) {
216 g_hash_table_destroy (priv->viewer_handlers);
217 priv->viewer_handlers = NULL;
220 modest_signal_mgr_disconnect_all_and_destroy (priv->modal_handler_uids);
221 priv->modal_handler_uids = NULL;
223 if (priv->modal_windows) {
224 g_mutex_lock (priv->queue_lock);
225 g_queue_free (priv->modal_windows);
226 priv->modal_windows = NULL;
227 g_mutex_unlock (priv->queue_lock);
229 g_mutex_free (priv->queue_lock);
231 /* Do not unref priv->main_window because it does not hold a
235 G_OBJECT_CLASS(parent_class)->finalize (obj);
239 modest_hildon1_window_mgr_new (void)
241 return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_HILDON1_WINDOW_MGR, NULL));
245 modest_hildon1_window_mgr_close_all_windows (ModestWindowMgr *self)
247 ModestHildon1WindowMgrPrivate *priv = NULL;
248 gboolean ret_value = FALSE;
249 ModestWindow *main_window;
251 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
252 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
254 /* If there is a main window then try to close it, and it will
255 close the others if needed */
256 main_window = modest_window_mgr_get_main_window (self, FALSE);
258 g_signal_emit_by_name (main_window, "delete-event", NULL, &ret_value);
261 GList *wins = NULL, *next = NULL;
263 /* delete-event handler actually removes window_list item, */
264 wins = priv->window_list;
266 next = g_list_next (wins);
267 g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
278 compare_msguids (ModestWindow *win,
281 const gchar *msg_uid;
283 if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
286 /* Get message uid from msg window */
287 if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
288 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
289 if (msg_uid && uid &&!strcmp (msg_uid, uid))
292 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
295 if (msg_uid && uid &&!strcmp (msg_uid, uid))
302 modest_hildon1_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
305 ModestHildon1WindowMgrPrivate *priv = NULL;
307 gboolean has_header, has_window = FALSE;
310 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
311 g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
313 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
315 has_header = MODEST_WINDOW_MGR_CLASS (parent_class)->find_registered_header (self, header, win);
317 uid = modest_tny_folder_get_header_unique_id (header);
319 item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
323 if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) &&
324 (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
325 g_debug ("not a valid window!");
327 g_debug ("found a window");
328 *win = MODEST_WINDOW (item->data);
334 return has_header || has_window;
338 modest_hildon1_window_mgr_get_window_list (ModestWindowMgr *self)
340 ModestHildon1WindowMgrPrivate *priv;
342 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
343 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
345 return g_list_copy (priv->window_list);
349 get_show_toolbar_key (GType window_type,
352 const gchar *key = NULL;
354 if (window_type == MODEST_TYPE_MAIN_WINDOW)
356 MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
357 MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
358 else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
360 MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
361 MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
362 else if (window_type == MODEST_TYPE_MSG_EDIT_WINDOW)
364 MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
365 MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
367 g_return_val_if_reached (NULL);
372 #ifndef MODEST_TOOLKIT_GTK
374 on_window_is_topmost (GObject *gobject,
378 ModestHildon1WindowMgr *self;
379 ModestHildon1WindowMgrPrivate *priv;
380 ModestWindow *win = (ModestWindow *) gobject;
382 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data));
384 self = MODEST_HILDON1_WINDOW_MGR (user_data);
385 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
387 if (hildon_window_get_is_topmost (HILDON_WINDOW (win)))
388 priv->current_top = win;
393 on_window_state_event (GtkWidget *widget,
394 GdkEventWindowState *event,
397 ModestHildon1WindowMgr *self;
398 ModestHildon1WindowMgrPrivate *priv;
400 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data), FALSE);
402 self = MODEST_HILDON1_WINDOW_MGR (user_data);
403 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
406 if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
407 g_print ("GDK_WINDOW_STATE_WITHDRAWN\n");
408 if (event->changed_mask & GDK_WINDOW_STATE_ICONIFIED)
409 g_print ("GDK_WINDOW_STATE_ICONIFIED\n");
410 if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED)
411 g_print ("GDK_WINDOW_STATE_MAXIMIZED\n");
412 if (event->changed_mask & GDK_WINDOW_STATE_STICKY)
413 g_print ("GDK_WINDOW_STATE_STICKY\n");
414 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
415 g_print ("GDK_WINDOW_STATE_FULLSCREEN\n");
416 if (event->changed_mask & GDK_WINDOW_STATE_ABOVE)
417 g_print ("GDK_WINDOW_STATE_ABOVE\n");
418 if (event->changed_mask & GDK_WINDOW_STATE_BELOW)
419 g_print ("GDK_WINDOW_STATE_BELOW\n");
421 if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN ||
422 event->changed_mask & GDK_WINDOW_STATE_ABOVE) {
423 priv->current_top = MODEST_WINDOW (widget);
431 modest_hildon1_window_mgr_register_window (ModestWindowMgr *self,
432 ModestWindow *window,
433 ModestWindow *parent)
436 ModestHildon1WindowMgrPrivate *priv;
439 ModestWindow *main_window;
441 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
442 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
444 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
446 win = g_list_find (priv->window_list, window);
448 /* this is for the case we want to register the window and it was already
449 * registered. We leave silently.
454 if (!MODEST_WINDOW_MGR_CLASS (parent_class)->register_window (self, window, parent))
457 /* Add to list. Keep a reference to the window */
458 g_object_ref (window);
459 priv->window_list = g_list_prepend (priv->window_list, window);
461 /* Listen to window state changes. Unfortunately
462 window-state-event does not properly work for the Maemo
463 version, so we need to use is-topmost and the ifdef */
464 #ifndef MODEST_TOOLKIT_GTK
465 priv->window_state_uids =
466 modest_signal_mgr_connect (priv->window_state_uids,
468 "notify::is-topmost",
469 G_CALLBACK (on_window_is_topmost),
472 priv->window_state_uids =
473 modest_signal_mgr_connect (priv->window_state_uids,
475 "window-state-event",
476 G_CALLBACK (on_window_state_event),
479 /* Listen to object destruction */
480 handler_id = g_malloc0 (sizeof (gint));
481 *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
482 g_hash_table_insert (priv->destroy_handlers, window, handler_id);
484 main_window = modest_window_mgr_get_main_window (self, FALSE);
485 /* If there is a msg view window, let the main window listen the msg-changed signal */
486 if (MODEST_IS_MSG_VIEW_WINDOW(window) && main_window) {
488 handler = g_malloc0 (sizeof (gulong));
489 *handler = g_signal_connect (window, "msg-changed",
490 G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed),
492 g_hash_table_insert (priv->viewer_handlers, window, handler);
495 /* Put into fullscreen if needed */
496 if (priv->fullscreen_mode)
497 gtk_window_fullscreen (GTK_WINDOW (window));
499 /* Show/hide toolbar & fullscreen */
500 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
501 modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
507 cancel_window_operations (ModestWindow *window)
509 GSList* pending_ops = NULL;
511 /* cancel open and receive operations */
512 pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (),
514 while (pending_ops != NULL) {
515 ModestMailOperationTypeOperation type;
516 GSList* tmp_list = NULL;
518 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
519 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
520 modest_mail_operation_cancel (pending_ops->data);
522 g_object_unref (G_OBJECT (pending_ops->data));
523 tmp_list = pending_ops;
524 pending_ops = g_slist_next (pending_ops);
525 g_slist_free_1 (tmp_list);
532 on_window_destroy (ModestWindow *window,
534 ModestHildon1WindowMgr *self)
536 gint dialog_response = GTK_RESPONSE_NONE;
537 gboolean no_propagate = FALSE;
539 /* Specific stuff first */
540 if (MODEST_IS_MAIN_WINDOW (window)) {
541 ModestHildon1WindowMgrPrivate *priv;
542 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
544 /* If more than one window already opened */
545 if (g_list_length (priv->window_list) > 1) {
547 /* Present the window if it's not visible now */
548 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window))) {
549 gtk_window_present (GTK_WINDOW (window));
550 priv->current_top = window;
552 /* Create the confirmation dialog MSG-NOT308 */
553 dialog_response = modest_platform_run_confirmation_dialog (
554 GTK_WINDOW (window), _("emev_nc_close_windows"));
556 /* If the user wants to close all the windows */
557 if ((dialog_response == GTK_RESPONSE_OK)
558 || (dialog_response == GTK_RESPONSE_ACCEPT)
559 || (dialog_response == GTK_RESPONSE_YES)) {
560 GList *iter = priv->window_list;
562 if (iter->data != window) {
563 GList *tmp = iter->next;
564 on_window_destroy (MODEST_WINDOW (iter->data),
569 iter = g_list_next (iter);
577 /* Do not unregister it, just hide */
578 gtk_widget_hide_all (GTK_WIDGET (window));
580 /* Cancel pending operations */
581 cancel_window_operations (window);
583 /* Fake the window system, make it think that there is no window */
584 if (modest_window_mgr_get_num_windows (MODEST_WINDOW_MGR (self)) == 0)
585 g_signal_emit_by_name (self, "window-list-empty");
590 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
591 gboolean sent = FALSE;
592 gint response = GTK_RESPONSE_ACCEPT;
593 sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
594 /* Save currently edited message to Drafts if it was not sent */
595 if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
597 /* Raise the window if it's minimized */
598 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
599 gtk_window_present (GTK_WINDOW (window));
602 modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
603 _("mcen_nc_no_email_message_modified_save_changes"));
605 if (response == GTK_RESPONSE_OK)
606 if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
610 /* Unregister window */
611 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (self), window);
619 disconnect_msg_changed (gpointer key,
624 handler_id = GPOINTER_TO_UINT(value);
626 if (key && G_IS_OBJECT(key))
627 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
631 modest_hildon1_window_mgr_unregister_window (ModestWindowMgr *self,
632 ModestWindow *window)
635 ModestHildon1WindowMgrPrivate *priv;
636 gulong *tmp, handler_id;
638 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
639 g_return_if_fail (MODEST_IS_WINDOW (window));
641 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
643 win = g_list_find (priv->window_list, window);
645 g_warning ("Trying to unregister a window that has not being registered yet");
649 /* If it's the main window unset it */
650 if (MODEST_IS_MAIN_WINDOW (window)) {
651 modest_window_mgr_set_main_window (self, NULL);
653 /* Disconnect all emissions of msg-changed */
654 if (priv->viewer_handlers) {
655 g_hash_table_foreach (priv->viewer_handlers,
656 disconnect_msg_changed,
658 g_hash_table_destroy (priv->viewer_handlers);
659 priv->viewer_handlers = NULL;
663 /* Remove the viewer window handler from the hash table. The
664 HashTable could not exist if the main window was closed
665 when there were other windows remaining */
666 if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
667 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
668 /* If the viewer was created without a main window
669 (for example when opening a message through D-Bus
670 the viewer handlers was not registered */
672 g_signal_handler_disconnect (window, *tmp);
673 g_hash_table_remove (priv->viewer_handlers, window);
677 /* Remove from list & hash table */
678 priv->window_list = g_list_remove_link (priv->window_list, win);
679 tmp = g_hash_table_lookup (priv->destroy_handlers, window);
682 g_hash_table_remove (priv->destroy_handlers, window);
684 /* cancel open and receive operations */
685 cancel_window_operations (window);
687 /* Check if it's the topmost window, and remove the window from the stack.
688 * This is needed for the cases there's no other topmost window that will
689 * replace it in topmost handler.
691 if (window == priv->current_top)
692 priv->current_top = NULL;
694 /* Disconnect the "window-state-event" handler, we won't need it anymore */
695 if (priv->window_state_uids) {
696 #ifndef MODEST_TOOLKIT_GTK
697 priv->window_state_uids =
698 modest_signal_mgr_disconnect (priv->window_state_uids,
700 "notify::is-topmost");
702 priv->window_state_uids =
703 modest_signal_mgr_disconnect (priv->window_state_uids,
705 "window-state-event");
709 /* Disconnect the "delete-event" handler, we won't need it anymore */
710 g_signal_handler_disconnect (window, handler_id);
712 /* Destroy the window */
713 g_object_unref (win->data);
716 MODEST_WINDOW_MGR_CLASS (parent_class)->unregister_window (self, window);
718 /* If there are no more windows registered emit the signal */
719 if (modest_window_mgr_get_num_windows (self) == 0)
720 g_signal_emit_by_name (self, "window-list-empty");
726 modest_hildon1_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
729 ModestHildon1WindowMgrPrivate *priv;
733 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
735 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
737 /* If there is no change do nothing */
738 if (priv->fullscreen_mode == on)
741 priv->fullscreen_mode = on;
743 conf = modest_runtime_get_conf ();
746 win = priv->window_list;
749 const gchar *key = NULL;
751 /* Getting this from gconf everytime is not that
752 expensive, we'll do it just a few times */
753 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
754 show = modest_conf_get_bool (conf, key, NULL);
756 /* Set fullscreen/unfullscreen */
758 gtk_window_fullscreen (GTK_WINDOW (win->data));
760 gtk_window_unfullscreen (GTK_WINDOW (win->data));
762 /* Show/Hide toolbar */
763 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
765 win = g_list_next (win);
770 modest_hildon1_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
772 ModestHildon1WindowMgrPrivate *priv;
774 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
776 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
778 return priv->fullscreen_mode;
782 modest_hildon1_window_mgr_show_toolbars (ModestWindowMgr *self,
784 gboolean show_toolbars,
787 ModestHildon1WindowMgrPrivate *priv;
789 const gchar *key = NULL;
791 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
793 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
794 conf = modest_runtime_get_conf ();
796 /* If nothing changes then return */
797 key = get_show_toolbar_key (window_type, fullscreen);
798 conf = modest_runtime_get_conf ();
799 if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
803 modest_conf_set_bool (conf, key, show_toolbars, NULL);
805 /* Apply now if the view mode is the right one */
806 if ((fullscreen && priv->fullscreen_mode) ||
807 (!fullscreen && !priv->fullscreen_mode)) {
809 GList *win = priv->window_list;
812 if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
813 modest_window_show_toolbar (MODEST_WINDOW (win->data),
815 win = g_list_next (win);
821 modest_hildon1_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
823 ModestHildon1WindowMgrPrivate *priv;
824 ModestWindow *result;
826 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
827 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
829 result = MODEST_WINDOW_MGR_CLASS (parent_class)->get_main_window (self, FALSE);
830 /* create the main window, if it hasn't been created yet */
831 if (!result && show) {
832 /* modest_window_mgr_register_window will set priv->main_window */
833 result = modest_main_window_new ();
834 modest_window_mgr_register_window (self, result, NULL);
835 gtk_widget_show_all (GTK_WIDGET (result));
836 gtk_window_present (GTK_WINDOW (result));
838 g_debug ("%s: created main window: %p\n", __FUNCTION__, result);
847 modest_hildon1_window_mgr_get_modal (ModestWindowMgr *self)
849 ModestHildon1WindowMgrPrivate *priv;
851 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
852 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
854 return g_queue_peek_head (priv->modal_windows);
859 modest_hildon1_window_mgr_set_modal (ModestWindowMgr *self,
863 GtkWindow *old_modal;
864 ModestHildon1WindowMgrPrivate *priv;
866 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
867 g_return_if_fail (GTK_IS_WINDOW (window));
869 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
870 g_mutex_lock (priv->queue_lock);
871 old_modal = g_queue_peek_head (priv->modal_windows);
872 g_mutex_unlock (priv->queue_lock);
875 /* make us transient wrt the main window then */
876 if (priv->current_top && ((GtkWindow *) priv->current_top != window)) {
877 gtk_window_set_transient_for (window, GTK_WINDOW(priv->current_top));
879 ModestWindow *main_win = modest_window_mgr_get_main_window (self, FALSE);
880 if (GTK_WINDOW(main_win) != window) /* they should not be the same */
881 gtk_window_set_transient_for (window, GTK_WINDOW(main_win));
883 gtk_window_set_modal (window, TRUE);
885 /* un-modalize the old one; the one on top should be the
887 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));
888 gtk_window_set_modal (window, TRUE);
891 /* this will be the new modal window */
892 g_mutex_lock (priv->queue_lock);
893 g_queue_push_head (priv->modal_windows, window);
894 g_mutex_unlock (priv->queue_lock);
896 if (GTK_IS_DIALOG (window)) {
897 /* Note that response is not always enough because it
898 could be captured and removed easily by dialogs but
899 works for most of situations */
900 priv->modal_handler_uids =
901 modest_signal_mgr_connect (priv->modal_handler_uids,
904 G_CALLBACK (on_modal_dialog_close),
906 /* We need this as well because dialogs are often
907 destroyed with gtk_widget_destroy and this one will
908 prevent response from happening */
909 priv->modal_handler_uids =
910 modest_signal_mgr_connect (priv->modal_handler_uids,
913 G_CALLBACK (on_modal_dialog_destroy),
916 priv->modal_handler_uids =
917 modest_signal_mgr_connect (priv->modal_handler_uids,
920 G_CALLBACK (on_modal_window_close),
923 /* Destroy width parent */
924 gtk_window_set_destroy_with_parent (window, TRUE);
930 idle_top_modal (gpointer data)
932 ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
933 ModestHildon1WindowMgrPrivate *priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
936 /* Get the top modal */
937 g_mutex_lock (priv->queue_lock);
938 topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
939 g_mutex_unlock (priv->queue_lock);
943 gdk_threads_enter ();
944 gtk_window_present (topmost);
945 /* It seems that the window looses modality if some
946 other is shown on top of it after set_transient_for
948 gtk_window_set_modal (topmost, TRUE);
949 gdk_threads_leave ();
956 remove_modal_from_queue (GtkWidget *widget,
957 ModestWindowMgr *self)
959 ModestHildon1WindowMgrPrivate *priv;
962 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
964 /* Remove from queue. We don't use remove, because we want to
965 exit if the widget does not belong to the queue */
966 g_mutex_lock (priv->queue_lock);
967 item = g_queue_find (priv->modal_windows, widget);
969 g_warning ("Trying to remove a modal window that is not registered");
970 g_mutex_unlock (priv->queue_lock);
973 g_queue_unlink (priv->modal_windows, item);
974 g_mutex_unlock (priv->queue_lock);
976 /* Disconnect handler */
977 priv->modal_handler_uids =
978 modest_signal_mgr_disconnect (priv->modal_handler_uids,
980 GTK_IS_DIALOG (widget) ?
983 if (GTK_IS_DIALOG (widget))
984 priv->modal_handler_uids =
985 modest_signal_mgr_disconnect (priv->modal_handler_uids,
989 /* Schedule the next one for being shown */
990 g_idle_add (idle_top_modal, self);
994 on_modal_window_close (GtkWidget *widget,
998 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1000 /* Remove modal window from queue */
1001 remove_modal_from_queue (widget, self);
1008 on_modal_dialog_close (GtkDialog *dialog,
1012 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1014 /* Remove modal window from queue. Note that if "destroy"
1015 signal was invoked before the response the window could be
1017 remove_modal_from_queue (GTK_WIDGET (dialog), self);
1021 on_modal_dialog_destroy (GtkObject *object,
1024 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1026 /* Remove modal window from queue */
1027 remove_modal_from_queue (GTK_WIDGET (object), self);
1030 static ModestWindow*
1031 modest_hildon1_window_mgr_get_current_top (ModestWindowMgr *self)
1033 ModestHildon1WindowMgrPrivate *priv;
1035 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
1037 return priv->current_top;