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 void modest_hildon1_window_mgr_close_all_windows (ModestWindowMgr *self);
89 typedef struct _ModestHildon1WindowMgrPrivate ModestHildon1WindowMgrPrivate;
90 struct _ModestHildon1WindowMgrPrivate {
93 GQueue *modal_windows;
95 gboolean fullscreen_mode;
97 GHashTable *destroy_handlers;
98 GHashTable *viewer_handlers;
99 GSList *window_state_uids;
103 GSList *modal_handler_uids;
104 ModestWindow *current_top;
106 #define MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
107 MODEST_TYPE_HILDON1_WINDOW_MGR, \
108 ModestHildon1WindowMgrPrivate))
110 static GObjectClass *parent_class = NULL;
113 modest_hildon1_window_mgr_get_type (void)
115 static GType my_type = 0;
117 static const GTypeInfo my_info = {
118 sizeof(ModestHildon1WindowMgrClass),
119 NULL, /* base init */
120 NULL, /* base finalize */
121 (GClassInitFunc) modest_hildon1_window_mgr_class_init,
122 NULL, /* class finalize */
123 NULL, /* class data */
124 sizeof(ModestHildon1WindowMgr),
126 (GInstanceInitFunc) modest_hildon1_window_mgr_instance_init,
129 my_type = g_type_register_static (MODEST_TYPE_WINDOW_MGR,
130 "ModestHildon1WindowMgr",
137 modest_hildon1_window_mgr_class_init (ModestHildon1WindowMgrClass *klass)
139 GObjectClass *gobject_class;
140 ModestWindowMgrClass *mgr_class;
142 gobject_class = (GObjectClass*) klass;
143 mgr_class = (ModestWindowMgrClass *) klass;
145 parent_class = g_type_class_peek_parent (klass);
146 gobject_class->finalize = modest_hildon1_window_mgr_finalize;
147 mgr_class->register_window = modest_hildon1_window_mgr_register_window;
148 mgr_class->unregister_window = modest_hildon1_window_mgr_unregister_window;
149 mgr_class->set_fullscreen_mode = modest_hildon1_window_mgr_set_fullscreen_mode;
150 mgr_class->get_fullscreen_mode = modest_hildon1_window_mgr_get_fullscreen_mode;
151 mgr_class->show_toolbars = modest_hildon1_window_mgr_show_toolbars;
152 mgr_class->get_main_window = modest_hildon1_window_mgr_get_main_window;
153 mgr_class->get_modal = modest_hildon1_window_mgr_get_modal;
154 mgr_class->set_modal = modest_hildon1_window_mgr_set_modal;
155 mgr_class->find_registered_header = modest_hildon1_window_mgr_find_registered_header;
156 mgr_class->get_window_list = modest_hildon1_window_mgr_get_window_list;
157 mgr_class->close_all_windows = modest_hildon1_window_mgr_close_all_windows;
159 g_type_class_add_private (gobject_class, sizeof(ModestHildon1WindowMgrPrivate));
164 modest_hildon1_window_mgr_instance_init (ModestHildon1WindowMgr *obj)
166 ModestHildon1WindowMgrPrivate *priv;
168 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(obj);
169 priv->window_list = NULL;
170 priv->fullscreen_mode = FALSE;
171 priv->current_top = NULL;
172 priv->window_state_uids = NULL;
174 priv->modal_windows = g_queue_new ();
175 priv->queue_lock = g_mutex_new ();
177 /* Could not initialize it from gconf, singletons are not
179 priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
180 priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
182 priv->closing_time = 0;
184 priv->modal_handler_uids = NULL;
188 modest_hildon1_window_mgr_finalize (GObject *obj)
190 ModestHildon1WindowMgrPrivate *priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(obj);
192 modest_signal_mgr_disconnect_all_and_destroy (priv->window_state_uids);
193 priv->window_state_uids = NULL;
195 if (priv->window_list) {
196 GList *iter = priv->window_list;
197 /* unregister pending windows */
199 ModestWindow *window = (ModestWindow *) iter->data;
200 iter = g_list_next (iter);
201 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), window);
203 g_list_free (priv->window_list);
204 priv->window_list = NULL;
207 /* Free the hash table with the handlers */
208 if (priv->destroy_handlers) {
209 g_hash_table_destroy (priv->destroy_handlers);
210 priv->destroy_handlers = NULL;
213 if (priv->viewer_handlers) {
214 g_hash_table_destroy (priv->viewer_handlers);
215 priv->viewer_handlers = NULL;
218 modest_signal_mgr_disconnect_all_and_destroy (priv->modal_handler_uids);
219 priv->modal_handler_uids = NULL;
221 if (priv->modal_windows) {
222 g_mutex_lock (priv->queue_lock);
223 g_queue_free (priv->modal_windows);
224 priv->modal_windows = NULL;
225 g_mutex_unlock (priv->queue_lock);
227 g_mutex_free (priv->queue_lock);
229 /* Do not unref priv->main_window because it does not hold a
233 G_OBJECT_CLASS(parent_class)->finalize (obj);
237 modest_hildon1_window_mgr_new (void)
239 return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_HILDON1_WINDOW_MGR, NULL));
243 modest_hildon1_window_mgr_close_all_windows (ModestWindowMgr *self)
245 ModestHildon1WindowMgrPrivate *priv = NULL;
246 gboolean ret_value = FALSE;
247 ModestWindow *main_window;
249 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
250 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
252 /* If there is a main window then try to close it, and it will
253 close the others if needed */
254 main_window = modest_window_mgr_get_main_window (self, FALSE);
256 g_signal_emit_by_name (main_window, "delete-event", NULL, &ret_value);
258 GList *wins = NULL, *next = NULL;
260 /* delete-event handler actually removes window_list item, */
261 wins = priv->window_list;
263 next = g_list_next (wins);
264 g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
271 compare_msguids (ModestWindow *win,
274 const gchar *msg_uid;
276 if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
279 /* Get message uid from msg window */
280 if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
281 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
282 if (msg_uid && uid &&!strcmp (msg_uid, uid))
285 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
288 if (msg_uid && uid &&!strcmp (msg_uid, uid))
295 modest_hildon1_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
298 ModestHildon1WindowMgrPrivate *priv = NULL;
300 gboolean has_header, has_window = FALSE;
303 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
304 g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
306 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
308 has_header = MODEST_WINDOW_MGR_CLASS (parent_class)->find_registered_header (self, header, win);
310 uid = modest_tny_folder_get_header_unique_id (header);
312 item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
316 if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) &&
317 (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
318 g_debug ("not a valid window!");
320 g_debug ("found a window");
321 *win = MODEST_WINDOW (item->data);
327 return has_header || has_window;
331 modest_hildon1_window_mgr_get_window_list (ModestWindowMgr *self)
333 ModestHildon1WindowMgrPrivate *priv;
335 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
336 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
338 return g_list_copy (priv->window_list);
342 get_show_toolbar_key (GType window_type,
345 const gchar *key = NULL;
347 if (window_type == MODEST_TYPE_MAIN_WINDOW)
349 MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
350 MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
351 else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
353 MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
354 MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
355 else if (window_type == MODEST_TYPE_MSG_EDIT_WINDOW)
357 MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
358 MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
360 g_return_val_if_reached (NULL);
365 #ifndef MODEST_TOOLKIT_GTK
367 on_window_is_topmost (GObject *gobject,
371 ModestHildon1WindowMgr *self;
372 ModestHildon1WindowMgrPrivate *priv;
373 ModestWindow *win = (ModestWindow *) gobject;
375 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data));
377 self = MODEST_HILDON1_WINDOW_MGR (user_data);
378 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
380 if (hildon_window_get_is_topmost (HILDON_WINDOW (win)))
381 priv->current_top = win;
386 on_window_state_event (GtkWidget *widget,
387 GdkEventWindowState *event,
390 ModestHildon1WindowMgr *self;
391 ModestHildon1WindowMgrPrivate *priv;
393 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data), FALSE);
395 self = MODEST_HILDON1_WINDOW_MGR (user_data);
396 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
399 if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
400 g_print ("GDK_WINDOW_STATE_WITHDRAWN\n");
401 if (event->changed_mask & GDK_WINDOW_STATE_ICONIFIED)
402 g_print ("GDK_WINDOW_STATE_ICONIFIED\n");
403 if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED)
404 g_print ("GDK_WINDOW_STATE_MAXIMIZED\n");
405 if (event->changed_mask & GDK_WINDOW_STATE_STICKY)
406 g_print ("GDK_WINDOW_STATE_STICKY\n");
407 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
408 g_print ("GDK_WINDOW_STATE_FULLSCREEN\n");
409 if (event->changed_mask & GDK_WINDOW_STATE_ABOVE)
410 g_print ("GDK_WINDOW_STATE_ABOVE\n");
411 if (event->changed_mask & GDK_WINDOW_STATE_BELOW)
412 g_print ("GDK_WINDOW_STATE_BELOW\n");
414 if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN ||
415 event->changed_mask & GDK_WINDOW_STATE_ABOVE) {
416 priv->current_top = MODEST_WINDOW (widget);
424 modest_hildon1_window_mgr_register_window (ModestWindowMgr *self,
425 ModestWindow *window,
426 ModestWindow *parent)
429 ModestHildon1WindowMgrPrivate *priv;
432 ModestWindow *main_window;
434 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
435 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
437 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
439 win = g_list_find (priv->window_list, window);
441 /* this is for the case we want to register the window and it was already
442 * registered. We leave silently.
447 if (!MODEST_WINDOW_MGR_CLASS (parent_class)->register_window (self, window, parent))
450 /* Add to list. Keep a reference to the window */
451 g_object_ref (window);
452 priv->window_list = g_list_prepend (priv->window_list, window);
454 /* Listen to window state changes. Unfortunately
455 window-state-event does not properly work for the Maemo
456 version, so we need to use is-topmost and the ifdef */
457 #ifndef MODEST_TOOLKIT_GTK
458 priv->window_state_uids =
459 modest_signal_mgr_connect (priv->window_state_uids,
461 "notify::is-topmost",
462 G_CALLBACK (on_window_is_topmost),
465 priv->window_state_uids =
466 modest_signal_mgr_connect (priv->window_state_uids,
468 "window-state-event",
469 G_CALLBACK (on_window_state_event),
472 /* Listen to object destruction */
473 handler_id = g_malloc0 (sizeof (gint));
474 *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
475 g_hash_table_insert (priv->destroy_handlers, window, handler_id);
477 main_window = modest_window_mgr_get_main_window (self, FALSE);
478 /* If there is a msg view window, let the main window listen the msg-changed signal */
479 if (MODEST_IS_MSG_VIEW_WINDOW(window) && main_window) {
481 handler = g_malloc0 (sizeof (gulong));
482 *handler = g_signal_connect (window, "msg-changed",
483 G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed),
485 g_hash_table_insert (priv->viewer_handlers, window, handler);
488 /* Put into fullscreen if needed */
489 if (priv->fullscreen_mode)
490 gtk_window_fullscreen (GTK_WINDOW (window));
492 /* Show/hide toolbar & fullscreen */
493 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
494 modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
500 cancel_window_operations (ModestWindow *window)
502 GSList* pending_ops = NULL;
504 /* cancel open and receive operations */
505 pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (),
507 while (pending_ops != NULL) {
508 ModestMailOperationTypeOperation type;
509 GSList* tmp_list = NULL;
511 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
512 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
513 modest_mail_operation_cancel (pending_ops->data);
515 g_object_unref (G_OBJECT (pending_ops->data));
516 tmp_list = pending_ops;
517 pending_ops = g_slist_next (pending_ops);
518 g_slist_free_1 (tmp_list);
525 on_window_destroy (ModestWindow *window,
527 ModestHildon1WindowMgr *self)
529 gint dialog_response = GTK_RESPONSE_NONE;
530 gboolean no_propagate = FALSE;
532 /* Specific stuff first */
533 if (MODEST_IS_MAIN_WINDOW (window)) {
534 ModestHildon1WindowMgrPrivate *priv;
535 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
537 /* If more than one window already opened */
538 if (g_list_length (priv->window_list) > 1) {
540 /* Present the window if it's not visible now */
541 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window))) {
542 gtk_window_present (GTK_WINDOW (window));
543 priv->current_top = window;
545 /* Create the confirmation dialog MSG-NOT308 */
546 dialog_response = modest_platform_run_confirmation_dialog (
547 GTK_WINDOW (window), _("emev_nc_close_windows"));
549 /* If the user wants to close all the windows */
550 if ((dialog_response == GTK_RESPONSE_OK)
551 || (dialog_response == GTK_RESPONSE_ACCEPT)
552 || (dialog_response == GTK_RESPONSE_YES)) {
553 GList *iter = priv->window_list;
555 if (iter->data != window) {
556 GList *tmp = iter->next;
557 on_window_destroy (MODEST_WINDOW (iter->data),
562 iter = g_list_next (iter);
570 /* Do not unregister it, just hide */
571 gtk_widget_hide_all (GTK_WIDGET (window));
573 /* Cancel pending operations */
574 cancel_window_operations (window);
576 /* Fake the window system, make it think that there is no window */
577 if (modest_window_mgr_num_windows (MODEST_WINDOW_MGR (self)) == 0)
578 g_signal_emit_by_name (self, "window-list-empty");
583 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
584 gboolean sent = FALSE;
585 gint response = GTK_RESPONSE_ACCEPT;
586 sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
587 /* Save currently edited message to Drafts if it was not sent */
588 if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
590 /* Raise the window if it's minimized */
591 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
592 gtk_window_present (GTK_WINDOW (window));
595 modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
596 _("mcen_nc_no_email_message_modified_save_changes"));
598 if (response == GTK_RESPONSE_OK)
599 if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
603 /* Unregister window */
604 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (self), window);
612 disconnect_msg_changed (gpointer key,
617 handler_id = GPOINTER_TO_UINT(value);
619 if (key && G_IS_OBJECT(key))
620 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
624 modest_hildon1_window_mgr_unregister_window (ModestWindowMgr *self,
625 ModestWindow *window)
628 ModestHildon1WindowMgrPrivate *priv;
629 gulong *tmp, handler_id;
631 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
632 g_return_if_fail (MODEST_IS_WINDOW (window));
634 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
636 win = g_list_find (priv->window_list, window);
638 g_warning ("Trying to unregister a window that has not being registered yet");
642 /* If it's the main window unset it */
643 if (MODEST_IS_MAIN_WINDOW (window)) {
644 modest_window_mgr_set_main_window (self, NULL);
646 /* Disconnect all emissions of msg-changed */
647 if (priv->viewer_handlers) {
648 g_hash_table_foreach (priv->viewer_handlers,
649 disconnect_msg_changed,
651 g_hash_table_destroy (priv->viewer_handlers);
652 priv->viewer_handlers = NULL;
656 /* Remove the viewer window handler from the hash table. The
657 HashTable could not exist if the main window was closed
658 when there were other windows remaining */
659 if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
660 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
661 /* If the viewer was created without a main window
662 (for example when opening a message through D-Bus
663 the viewer handlers was not registered */
665 g_signal_handler_disconnect (window, *tmp);
666 g_hash_table_remove (priv->viewer_handlers, window);
670 /* Remove from list & hash table */
671 priv->window_list = g_list_remove_link (priv->window_list, win);
672 tmp = g_hash_table_lookup (priv->destroy_handlers, window);
675 g_hash_table_remove (priv->destroy_handlers, window);
677 /* cancel open and receive operations */
678 cancel_window_operations (window);
680 /* Check if it's the topmost window, and remove the window from the stack.
681 * This is needed for the cases there's no other topmost window that will
682 * replace it in topmost handler.
684 if (window == priv->current_top)
685 priv->current_top = NULL;
687 /* Disconnect the "window-state-event" handler, we won't need it anymore */
688 if (priv->window_state_uids) {
689 #ifndef MODEST_TOOLKIT_GTK
690 priv->window_state_uids =
691 modest_signal_mgr_disconnect (priv->window_state_uids,
693 "notify::is-topmost");
695 priv->window_state_uids =
696 modest_signal_mgr_disconnect (priv->window_state_uids,
698 "window-state-event");
702 /* Disconnect the "delete-event" handler, we won't need it anymore */
703 g_signal_handler_disconnect (window, handler_id);
705 /* Destroy the window */
706 g_object_unref (win->data);
709 MODEST_WINDOW_MGR_CLASS (parent_class)->unregister_window (self, window);
711 /* If there are no more windows registered emit the signal */
712 if (modest_window_mgr_num_windows (self) == 0)
713 g_signal_emit_by_name (self, "window-list-empty");
719 modest_hildon1_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
722 ModestHildon1WindowMgrPrivate *priv;
726 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
728 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
730 /* If there is no change do nothing */
731 if (priv->fullscreen_mode == on)
734 priv->fullscreen_mode = on;
736 conf = modest_runtime_get_conf ();
739 win = priv->window_list;
742 const gchar *key = NULL;
744 /* Getting this from gconf everytime is not that
745 expensive, we'll do it just a few times */
746 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
747 show = modest_conf_get_bool (conf, key, NULL);
749 /* Set fullscreen/unfullscreen */
751 gtk_window_fullscreen (GTK_WINDOW (win->data));
753 gtk_window_unfullscreen (GTK_WINDOW (win->data));
755 /* Show/Hide toolbar */
756 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
758 win = g_list_next (win);
763 modest_hildon1_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
765 ModestHildon1WindowMgrPrivate *priv;
767 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
769 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
771 return priv->fullscreen_mode;
775 modest_hildon1_window_mgr_show_toolbars (ModestWindowMgr *self,
777 gboolean show_toolbars,
780 ModestHildon1WindowMgrPrivate *priv;
782 const gchar *key = NULL;
784 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
786 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
787 conf = modest_runtime_get_conf ();
789 /* If nothing changes then return */
790 key = get_show_toolbar_key (window_type, fullscreen);
791 conf = modest_runtime_get_conf ();
792 if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
796 modest_conf_set_bool (conf, key, show_toolbars, NULL);
798 /* Apply now if the view mode is the right one */
799 if ((fullscreen && priv->fullscreen_mode) ||
800 (!fullscreen && !priv->fullscreen_mode)) {
802 GList *win = priv->window_list;
805 if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
806 modest_window_show_toolbar (MODEST_WINDOW (win->data),
808 win = g_list_next (win);
814 modest_hildon1_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
816 ModestHildon1WindowMgrPrivate *priv;
817 ModestWindow *result;
819 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
820 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
822 result = MODEST_WINDOW_MGR_CLASS (parent_class)->get_main_window (self, FALSE);
823 /* create the main window, if it hasn't been created yet */
824 if (!result && show) {
825 /* modest_window_mgr_register_window will set priv->main_window */
826 result = modest_main_window_new ();
827 modest_window_mgr_register_window (self, result, NULL);
828 gtk_widget_show_all (GTK_WIDGET (result));
829 gtk_window_present (GTK_WINDOW (result));
831 g_debug ("%s: created main window: %p\n", __FUNCTION__, result);
840 modest_hildon1_window_mgr_get_modal (ModestWindowMgr *self)
842 ModestHildon1WindowMgrPrivate *priv;
844 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
845 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
847 return g_queue_peek_head (priv->modal_windows);
852 modest_hildon1_window_mgr_set_modal (ModestWindowMgr *self,
856 GtkWindow *old_modal;
857 ModestHildon1WindowMgrPrivate *priv;
859 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
860 g_return_if_fail (GTK_IS_WINDOW (window));
862 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
863 g_mutex_lock (priv->queue_lock);
864 old_modal = g_queue_peek_head (priv->modal_windows);
865 g_mutex_unlock (priv->queue_lock);
868 /* make us transient wrt the main window then */
869 if (priv->current_top && ((GtkWindow *) priv->current_top != window)) {
870 gtk_window_set_transient_for (window, GTK_WINDOW(priv->current_top));
872 ModestWindow *main_win = modest_window_mgr_get_main_window (self, FALSE);
873 if (GTK_WINDOW(main_win) != window) /* they should not be the same */
874 gtk_window_set_transient_for (window, GTK_WINDOW(main_win));
876 gtk_window_set_modal (window, TRUE);
878 /* un-modalize the old one; the one on top should be the
880 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));
881 gtk_window_set_modal (window, TRUE);
884 /* this will be the new modal window */
885 g_mutex_lock (priv->queue_lock);
886 g_queue_push_head (priv->modal_windows, window);
887 g_mutex_unlock (priv->queue_lock);
889 if (GTK_IS_DIALOG (window)) {
890 /* Note that response is not always enough because it
891 could be captured and removed easily by dialogs but
892 works for most of situations */
893 priv->modal_handler_uids =
894 modest_signal_mgr_connect (priv->modal_handler_uids,
897 G_CALLBACK (on_modal_dialog_close),
899 /* We need this as well because dialogs are often
900 destroyed with gtk_widget_destroy and this one will
901 prevent response from happening */
902 priv->modal_handler_uids =
903 modest_signal_mgr_connect (priv->modal_handler_uids,
906 G_CALLBACK (on_modal_dialog_destroy),
909 priv->modal_handler_uids =
910 modest_signal_mgr_connect (priv->modal_handler_uids,
913 G_CALLBACK (on_modal_window_close),
916 /* Destroy width parent */
917 gtk_window_set_destroy_with_parent (window, TRUE);
923 idle_top_modal (gpointer data)
925 ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
926 ModestHildon1WindowMgrPrivate *priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
929 /* Get the top modal */
930 g_mutex_lock (priv->queue_lock);
931 topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
932 g_mutex_unlock (priv->queue_lock);
936 gdk_threads_enter ();
937 gtk_window_present (topmost);
938 /* It seems that the window looses modality if some
939 other is shown on top of it after set_transient_for
941 gtk_window_set_modal (topmost, TRUE);
942 gdk_threads_leave ();
949 remove_modal_from_queue (GtkWidget *widget,
950 ModestWindowMgr *self)
952 ModestHildon1WindowMgrPrivate *priv;
955 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
957 /* Remove from queue. We don't use remove, because we want to
958 exit if the widget does not belong to the queue */
959 g_mutex_lock (priv->queue_lock);
960 item = g_queue_find (priv->modal_windows, widget);
962 g_warning ("Trying to remove a modal window that is not registered");
963 g_mutex_unlock (priv->queue_lock);
966 g_queue_unlink (priv->modal_windows, item);
967 g_mutex_unlock (priv->queue_lock);
969 /* Disconnect handler */
970 priv->modal_handler_uids =
971 modest_signal_mgr_disconnect (priv->modal_handler_uids,
973 GTK_IS_DIALOG (widget) ?
976 if (GTK_IS_DIALOG (widget))
977 priv->modal_handler_uids =
978 modest_signal_mgr_disconnect (priv->modal_handler_uids,
982 /* Schedule the next one for being shown */
983 g_idle_add (idle_top_modal, self);
987 on_modal_window_close (GtkWidget *widget,
991 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
993 /* Remove modal window from queue */
994 remove_modal_from_queue (widget, self);
1001 on_modal_dialog_close (GtkDialog *dialog,
1005 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1007 /* Remove modal window from queue. Note that if "destroy"
1008 signal was invoked before the response the window could be
1010 remove_modal_from_queue (GTK_WIDGET (dialog), self);
1014 on_modal_dialog_destroy (GtkObject *object,
1017 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1019 /* Remove modal window from queue */
1020 remove_modal_from_queue (GTK_WIDGET (object), self);