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);
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_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
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);
259 GList *wins = NULL, *next = NULL;
261 /* delete-event handler actually removes window_list item, */
262 wins = priv->window_list;
264 next = g_list_next (wins);
265 g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
276 compare_msguids (ModestWindow *win,
279 const gchar *msg_uid;
281 if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
284 /* Get message uid from msg window */
285 if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
286 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
287 if (msg_uid && uid &&!strcmp (msg_uid, uid))
290 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
293 if (msg_uid && uid &&!strcmp (msg_uid, uid))
300 modest_hildon1_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
303 ModestHildon1WindowMgrPrivate *priv = NULL;
305 gboolean has_header, has_window = FALSE;
308 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
309 g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
311 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
313 has_header = MODEST_WINDOW_MGR_CLASS (parent_class)->find_registered_header (self, header, win);
315 uid = modest_tny_folder_get_header_unique_id (header);
317 item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
321 if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) &&
322 (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
323 g_debug ("not a valid window!");
325 g_debug ("found a window");
326 *win = MODEST_WINDOW (item->data);
332 return has_header || has_window;
336 modest_hildon1_window_mgr_get_window_list (ModestWindowMgr *self)
338 ModestHildon1WindowMgrPrivate *priv;
340 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
341 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
343 return g_list_copy (priv->window_list);
347 get_show_toolbar_key (GType window_type,
350 const gchar *key = NULL;
352 if (window_type == MODEST_TYPE_MAIN_WINDOW)
354 MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
355 MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
356 else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
358 MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
359 MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
360 else if (window_type == MODEST_TYPE_MSG_EDIT_WINDOW)
362 MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
363 MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
365 g_return_val_if_reached (NULL);
370 #ifndef MODEST_TOOLKIT_GTK
372 on_window_is_topmost (GObject *gobject,
376 ModestHildon1WindowMgr *self;
377 ModestHildon1WindowMgrPrivate *priv;
378 ModestWindow *win = (ModestWindow *) gobject;
380 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data));
382 self = MODEST_HILDON1_WINDOW_MGR (user_data);
383 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
385 if (hildon_window_get_is_topmost (HILDON_WINDOW (win)))
386 priv->current_top = win;
391 on_window_state_event (GtkWidget *widget,
392 GdkEventWindowState *event,
395 ModestHildon1WindowMgr *self;
396 ModestHildon1WindowMgrPrivate *priv;
398 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data), FALSE);
400 self = MODEST_HILDON1_WINDOW_MGR (user_data);
401 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
404 if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
405 g_print ("GDK_WINDOW_STATE_WITHDRAWN\n");
406 if (event->changed_mask & GDK_WINDOW_STATE_ICONIFIED)
407 g_print ("GDK_WINDOW_STATE_ICONIFIED\n");
408 if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED)
409 g_print ("GDK_WINDOW_STATE_MAXIMIZED\n");
410 if (event->changed_mask & GDK_WINDOW_STATE_STICKY)
411 g_print ("GDK_WINDOW_STATE_STICKY\n");
412 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
413 g_print ("GDK_WINDOW_STATE_FULLSCREEN\n");
414 if (event->changed_mask & GDK_WINDOW_STATE_ABOVE)
415 g_print ("GDK_WINDOW_STATE_ABOVE\n");
416 if (event->changed_mask & GDK_WINDOW_STATE_BELOW)
417 g_print ("GDK_WINDOW_STATE_BELOW\n");
419 if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN ||
420 event->changed_mask & GDK_WINDOW_STATE_ABOVE) {
421 priv->current_top = MODEST_WINDOW (widget);
429 modest_hildon1_window_mgr_register_window (ModestWindowMgr *self,
430 ModestWindow *window,
431 ModestWindow *parent)
434 ModestHildon1WindowMgrPrivate *priv;
437 ModestWindow *main_window;
439 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
440 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
442 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
444 win = g_list_find (priv->window_list, window);
446 /* this is for the case we want to register the window and it was already
447 * registered. We leave silently.
452 if (!MODEST_WINDOW_MGR_CLASS (parent_class)->register_window (self, window, parent))
455 /* Add to list. Keep a reference to the window */
456 g_object_ref (window);
457 priv->window_list = g_list_prepend (priv->window_list, window);
459 /* Listen to window state changes. Unfortunately
460 window-state-event does not properly work for the Maemo
461 version, so we need to use is-topmost and the ifdef */
462 #ifndef MODEST_TOOLKIT_GTK
463 priv->window_state_uids =
464 modest_signal_mgr_connect (priv->window_state_uids,
466 "notify::is-topmost",
467 G_CALLBACK (on_window_is_topmost),
470 priv->window_state_uids =
471 modest_signal_mgr_connect (priv->window_state_uids,
473 "window-state-event",
474 G_CALLBACK (on_window_state_event),
477 /* Listen to object destruction */
478 handler_id = g_malloc0 (sizeof (gint));
479 *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
480 g_hash_table_insert (priv->destroy_handlers, window, handler_id);
482 main_window = modest_window_mgr_get_main_window (self, FALSE);
483 /* If there is a msg view window, let the main window listen the msg-changed signal */
484 if (MODEST_IS_MSG_VIEW_WINDOW(window) && main_window) {
486 handler = g_malloc0 (sizeof (gulong));
487 *handler = g_signal_connect (window, "msg-changed",
488 G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed),
490 g_hash_table_insert (priv->viewer_handlers, window, handler);
493 /* Put into fullscreen if needed */
494 if (priv->fullscreen_mode)
495 gtk_window_fullscreen (GTK_WINDOW (window));
497 /* Show/hide toolbar & fullscreen */
498 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
499 modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
505 cancel_window_operations (ModestWindow *window)
507 GSList* pending_ops = NULL;
509 /* cancel open and receive operations */
510 pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (),
512 while (pending_ops != NULL) {
513 ModestMailOperationTypeOperation type;
514 GSList* tmp_list = NULL;
516 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
517 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
518 modest_mail_operation_cancel (pending_ops->data);
520 g_object_unref (G_OBJECT (pending_ops->data));
521 tmp_list = pending_ops;
522 pending_ops = g_slist_next (pending_ops);
523 g_slist_free_1 (tmp_list);
530 on_window_destroy (ModestWindow *window,
532 ModestHildon1WindowMgr *self)
534 gint dialog_response = GTK_RESPONSE_NONE;
535 gboolean no_propagate = FALSE;
537 /* Specific stuff first */
538 if (MODEST_IS_MAIN_WINDOW (window)) {
539 ModestHildon1WindowMgrPrivate *priv;
540 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
542 /* If more than one window already opened */
543 if (g_list_length (priv->window_list) > 1) {
545 /* Present the window if it's not visible now */
546 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window))) {
547 gtk_window_present (GTK_WINDOW (window));
548 priv->current_top = window;
550 /* Create the confirmation dialog MSG-NOT308 */
551 dialog_response = modest_platform_run_confirmation_dialog (
552 GTK_WINDOW (window), _("emev_nc_close_windows"));
554 /* If the user wants to close all the windows */
555 if ((dialog_response == GTK_RESPONSE_OK)
556 || (dialog_response == GTK_RESPONSE_ACCEPT)
557 || (dialog_response == GTK_RESPONSE_YES)) {
558 GList *iter = priv->window_list;
560 if (iter->data != window) {
561 GList *tmp = iter->next;
562 on_window_destroy (MODEST_WINDOW (iter->data),
567 iter = g_list_next (iter);
575 /* Do not unregister it, just hide */
576 gtk_widget_hide_all (GTK_WIDGET (window));
578 /* Cancel pending operations */
579 cancel_window_operations (window);
581 /* Fake the window system, make it think that there is no window */
582 if (modest_window_mgr_num_windows (MODEST_WINDOW_MGR (self)) == 0)
583 g_signal_emit_by_name (self, "window-list-empty");
588 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
589 gboolean sent = FALSE;
590 gint response = GTK_RESPONSE_ACCEPT;
591 sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
592 /* Save currently edited message to Drafts if it was not sent */
593 if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
595 /* Raise the window if it's minimized */
596 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
597 gtk_window_present (GTK_WINDOW (window));
600 modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
601 _("mcen_nc_no_email_message_modified_save_changes"));
603 if (response == GTK_RESPONSE_OK)
604 if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
608 /* Unregister window */
609 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (self), window);
617 disconnect_msg_changed (gpointer key,
622 handler_id = GPOINTER_TO_UINT(value);
624 if (key && G_IS_OBJECT(key))
625 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
629 modest_hildon1_window_mgr_unregister_window (ModestWindowMgr *self,
630 ModestWindow *window)
633 ModestHildon1WindowMgrPrivate *priv;
634 gulong *tmp, handler_id;
636 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
637 g_return_if_fail (MODEST_IS_WINDOW (window));
639 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
641 win = g_list_find (priv->window_list, window);
643 g_warning ("Trying to unregister a window that has not being registered yet");
647 /* If it's the main window unset it */
648 if (MODEST_IS_MAIN_WINDOW (window)) {
649 modest_window_mgr_set_main_window (self, NULL);
651 /* Disconnect all emissions of msg-changed */
652 if (priv->viewer_handlers) {
653 g_hash_table_foreach (priv->viewer_handlers,
654 disconnect_msg_changed,
656 g_hash_table_destroy (priv->viewer_handlers);
657 priv->viewer_handlers = NULL;
661 /* Remove the viewer window handler from the hash table. The
662 HashTable could not exist if the main window was closed
663 when there were other windows remaining */
664 if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
665 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
666 /* If the viewer was created without a main window
667 (for example when opening a message through D-Bus
668 the viewer handlers was not registered */
670 g_signal_handler_disconnect (window, *tmp);
671 g_hash_table_remove (priv->viewer_handlers, window);
675 /* Remove from list & hash table */
676 priv->window_list = g_list_remove_link (priv->window_list, win);
677 tmp = g_hash_table_lookup (priv->destroy_handlers, window);
680 g_hash_table_remove (priv->destroy_handlers, window);
682 /* cancel open and receive operations */
683 cancel_window_operations (window);
685 /* Check if it's the topmost window, and remove the window from the stack.
686 * This is needed for the cases there's no other topmost window that will
687 * replace it in topmost handler.
689 if (window == priv->current_top)
690 priv->current_top = NULL;
692 /* Disconnect the "window-state-event" handler, we won't need it anymore */
693 if (priv->window_state_uids) {
694 #ifndef MODEST_TOOLKIT_GTK
695 priv->window_state_uids =
696 modest_signal_mgr_disconnect (priv->window_state_uids,
698 "notify::is-topmost");
700 priv->window_state_uids =
701 modest_signal_mgr_disconnect (priv->window_state_uids,
703 "window-state-event");
707 /* Disconnect the "delete-event" handler, we won't need it anymore */
708 g_signal_handler_disconnect (window, handler_id);
710 /* Destroy the window */
711 g_object_unref (win->data);
714 MODEST_WINDOW_MGR_CLASS (parent_class)->unregister_window (self, window);
716 /* If there are no more windows registered emit the signal */
717 if (modest_window_mgr_num_windows (self) == 0)
718 g_signal_emit_by_name (self, "window-list-empty");
724 modest_hildon1_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
727 ModestHildon1WindowMgrPrivate *priv;
731 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
733 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
735 /* If there is no change do nothing */
736 if (priv->fullscreen_mode == on)
739 priv->fullscreen_mode = on;
741 conf = modest_runtime_get_conf ();
744 win = priv->window_list;
747 const gchar *key = NULL;
749 /* Getting this from gconf everytime is not that
750 expensive, we'll do it just a few times */
751 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
752 show = modest_conf_get_bool (conf, key, NULL);
754 /* Set fullscreen/unfullscreen */
756 gtk_window_fullscreen (GTK_WINDOW (win->data));
758 gtk_window_unfullscreen (GTK_WINDOW (win->data));
760 /* Show/Hide toolbar */
761 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
763 win = g_list_next (win);
768 modest_hildon1_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
770 ModestHildon1WindowMgrPrivate *priv;
772 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
774 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
776 return priv->fullscreen_mode;
780 modest_hildon1_window_mgr_show_toolbars (ModestWindowMgr *self,
782 gboolean show_toolbars,
785 ModestHildon1WindowMgrPrivate *priv;
787 const gchar *key = NULL;
789 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
791 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
792 conf = modest_runtime_get_conf ();
794 /* If nothing changes then return */
795 key = get_show_toolbar_key (window_type, fullscreen);
796 conf = modest_runtime_get_conf ();
797 if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
801 modest_conf_set_bool (conf, key, show_toolbars, NULL);
803 /* Apply now if the view mode is the right one */
804 if ((fullscreen && priv->fullscreen_mode) ||
805 (!fullscreen && !priv->fullscreen_mode)) {
807 GList *win = priv->window_list;
810 if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
811 modest_window_show_toolbar (MODEST_WINDOW (win->data),
813 win = g_list_next (win);
819 modest_hildon1_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
821 ModestHildon1WindowMgrPrivate *priv;
822 ModestWindow *result;
824 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
825 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
827 result = MODEST_WINDOW_MGR_CLASS (parent_class)->get_main_window (self, FALSE);
828 /* create the main window, if it hasn't been created yet */
829 if (!result && show) {
830 /* modest_window_mgr_register_window will set priv->main_window */
831 result = modest_main_window_new ();
832 modest_window_mgr_register_window (self, result, NULL);
833 gtk_widget_show_all (GTK_WIDGET (result));
834 gtk_window_present (GTK_WINDOW (result));
836 g_debug ("%s: created main window: %p\n", __FUNCTION__, result);
845 modest_hildon1_window_mgr_get_modal (ModestWindowMgr *self)
847 ModestHildon1WindowMgrPrivate *priv;
849 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
850 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
852 return g_queue_peek_head (priv->modal_windows);
857 modest_hildon1_window_mgr_set_modal (ModestWindowMgr *self,
861 GtkWindow *old_modal;
862 ModestHildon1WindowMgrPrivate *priv;
864 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
865 g_return_if_fail (GTK_IS_WINDOW (window));
867 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
868 g_mutex_lock (priv->queue_lock);
869 old_modal = g_queue_peek_head (priv->modal_windows);
870 g_mutex_unlock (priv->queue_lock);
873 /* make us transient wrt the main window then */
874 if (priv->current_top && ((GtkWindow *) priv->current_top != window)) {
875 gtk_window_set_transient_for (window, GTK_WINDOW(priv->current_top));
877 ModestWindow *main_win = modest_window_mgr_get_main_window (self, FALSE);
878 if (GTK_WINDOW(main_win) != window) /* they should not be the same */
879 gtk_window_set_transient_for (window, GTK_WINDOW(main_win));
881 gtk_window_set_modal (window, TRUE);
883 /* un-modalize the old one; the one on top should be the
885 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));
886 gtk_window_set_modal (window, TRUE);
889 /* this will be the new modal window */
890 g_mutex_lock (priv->queue_lock);
891 g_queue_push_head (priv->modal_windows, window);
892 g_mutex_unlock (priv->queue_lock);
894 if (GTK_IS_DIALOG (window)) {
895 /* Note that response is not always enough because it
896 could be captured and removed easily by dialogs but
897 works for most of situations */
898 priv->modal_handler_uids =
899 modest_signal_mgr_connect (priv->modal_handler_uids,
902 G_CALLBACK (on_modal_dialog_close),
904 /* We need this as well because dialogs are often
905 destroyed with gtk_widget_destroy and this one will
906 prevent response from happening */
907 priv->modal_handler_uids =
908 modest_signal_mgr_connect (priv->modal_handler_uids,
911 G_CALLBACK (on_modal_dialog_destroy),
914 priv->modal_handler_uids =
915 modest_signal_mgr_connect (priv->modal_handler_uids,
918 G_CALLBACK (on_modal_window_close),
921 /* Destroy width parent */
922 gtk_window_set_destroy_with_parent (window, TRUE);
928 idle_top_modal (gpointer data)
930 ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
931 ModestHildon1WindowMgrPrivate *priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
934 /* Get the top modal */
935 g_mutex_lock (priv->queue_lock);
936 topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
937 g_mutex_unlock (priv->queue_lock);
941 gdk_threads_enter ();
942 gtk_window_present (topmost);
943 /* It seems that the window looses modality if some
944 other is shown on top of it after set_transient_for
946 gtk_window_set_modal (topmost, TRUE);
947 gdk_threads_leave ();
954 remove_modal_from_queue (GtkWidget *widget,
955 ModestWindowMgr *self)
957 ModestHildon1WindowMgrPrivate *priv;
960 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
962 /* Remove from queue. We don't use remove, because we want to
963 exit if the widget does not belong to the queue */
964 g_mutex_lock (priv->queue_lock);
965 item = g_queue_find (priv->modal_windows, widget);
967 g_warning ("Trying to remove a modal window that is not registered");
968 g_mutex_unlock (priv->queue_lock);
971 g_queue_unlink (priv->modal_windows, item);
972 g_mutex_unlock (priv->queue_lock);
974 /* Disconnect handler */
975 priv->modal_handler_uids =
976 modest_signal_mgr_disconnect (priv->modal_handler_uids,
978 GTK_IS_DIALOG (widget) ?
981 if (GTK_IS_DIALOG (widget))
982 priv->modal_handler_uids =
983 modest_signal_mgr_disconnect (priv->modal_handler_uids,
987 /* Schedule the next one for being shown */
988 g_idle_add (idle_top_modal, self);
992 on_modal_window_close (GtkWidget *widget,
996 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
998 /* Remove modal window from queue */
999 remove_modal_from_queue (widget, self);
1006 on_modal_dialog_close (GtkDialog *dialog,
1010 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1012 /* Remove modal window from queue. Note that if "destroy"
1013 signal was invoked before the response the window could be
1015 remove_modal_from_queue (GTK_WIDGET (dialog), self);
1019 on_modal_dialog_destroy (GtkObject *object,
1022 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1024 /* Remove modal window from queue */
1025 remove_modal_from_queue (GTK_WIDGET (object), self);