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);
89 static gboolean modest_hildon1_window_mgr_screen_is_on (ModestWindowMgr *self);
91 typedef struct _ModestHildon1WindowMgrPrivate ModestHildon1WindowMgrPrivate;
92 struct _ModestHildon1WindowMgrPrivate {
95 GQueue *modal_windows;
97 gboolean fullscreen_mode;
99 GHashTable *destroy_handlers;
100 GHashTable *viewer_handlers;
101 GSList *window_state_uids;
105 GSList *modal_handler_uids;
106 ModestWindow *current_top;
108 #define MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
109 MODEST_TYPE_HILDON1_WINDOW_MGR, \
110 ModestHildon1WindowMgrPrivate))
112 static GObjectClass *parent_class = NULL;
115 modest_hildon1_window_mgr_get_type (void)
117 static GType my_type = 0;
119 static const GTypeInfo my_info = {
120 sizeof(ModestHildon1WindowMgrClass),
121 NULL, /* base init */
122 NULL, /* base finalize */
123 (GClassInitFunc) modest_hildon1_window_mgr_class_init,
124 NULL, /* class finalize */
125 NULL, /* class data */
126 sizeof(ModestHildon1WindowMgr),
128 (GInstanceInitFunc) modest_hildon1_window_mgr_instance_init,
131 my_type = g_type_register_static (MODEST_TYPE_WINDOW_MGR,
132 "ModestHildon1WindowMgr",
139 modest_hildon1_window_mgr_class_init (ModestHildon1WindowMgrClass *klass)
141 GObjectClass *gobject_class;
142 ModestWindowMgrClass *mgr_class;
144 gobject_class = (GObjectClass*) klass;
145 mgr_class = (ModestWindowMgrClass *) klass;
147 parent_class = g_type_class_peek_parent (klass);
148 gobject_class->finalize = modest_hildon1_window_mgr_finalize;
149 mgr_class->register_window = modest_hildon1_window_mgr_register_window;
150 mgr_class->unregister_window = modest_hildon1_window_mgr_unregister_window;
151 mgr_class->set_fullscreen_mode = modest_hildon1_window_mgr_set_fullscreen_mode;
152 mgr_class->get_fullscreen_mode = modest_hildon1_window_mgr_get_fullscreen_mode;
153 mgr_class->show_toolbars = modest_hildon1_window_mgr_show_toolbars;
154 mgr_class->get_main_window = modest_hildon1_window_mgr_get_main_window;
155 mgr_class->get_modal = modest_hildon1_window_mgr_get_modal;
156 mgr_class->set_modal = modest_hildon1_window_mgr_set_modal;
157 mgr_class->find_registered_header = modest_hildon1_window_mgr_find_registered_header;
158 mgr_class->get_window_list = modest_hildon1_window_mgr_get_window_list;
159 mgr_class->close_all_windows = modest_hildon1_window_mgr_close_all_windows;
160 mgr_class->get_current_top = modest_hildon1_window_mgr_get_current_top;
161 mgr_class->screen_is_on = modest_hildon1_window_mgr_screen_is_on;
163 g_type_class_add_private (gobject_class, sizeof(ModestHildon1WindowMgrPrivate));
168 modest_hildon1_window_mgr_instance_init (ModestHildon1WindowMgr *obj)
170 ModestHildon1WindowMgrPrivate *priv;
172 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(obj);
173 priv->window_list = NULL;
174 priv->fullscreen_mode = FALSE;
175 priv->current_top = NULL;
176 priv->window_state_uids = NULL;
178 priv->modal_windows = g_queue_new ();
179 priv->queue_lock = g_mutex_new ();
181 /* Could not initialize it from gconf, singletons are not
183 priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
184 priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
186 priv->closing_time = 0;
188 priv->modal_handler_uids = NULL;
192 modest_hildon1_window_mgr_finalize (GObject *obj)
194 ModestHildon1WindowMgrPrivate *priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE(obj);
196 modest_signal_mgr_disconnect_all_and_destroy (priv->window_state_uids);
197 priv->window_state_uids = NULL;
199 if (priv->window_list) {
200 GList *iter = priv->window_list;
201 /* unregister pending windows */
203 ModestWindow *window = (ModestWindow *) iter->data;
204 iter = g_list_next (iter);
205 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), window);
207 g_list_free (priv->window_list);
208 priv->window_list = NULL;
211 /* Free the hash table with the handlers */
212 if (priv->destroy_handlers) {
213 g_hash_table_destroy (priv->destroy_handlers);
214 priv->destroy_handlers = NULL;
217 if (priv->viewer_handlers) {
218 g_hash_table_destroy (priv->viewer_handlers);
219 priv->viewer_handlers = NULL;
222 modest_signal_mgr_disconnect_all_and_destroy (priv->modal_handler_uids);
223 priv->modal_handler_uids = NULL;
225 if (priv->modal_windows) {
226 g_mutex_lock (priv->queue_lock);
227 g_queue_free (priv->modal_windows);
228 priv->modal_windows = NULL;
229 g_mutex_unlock (priv->queue_lock);
231 g_mutex_free (priv->queue_lock);
233 /* Do not unref priv->main_window because it does not hold a
237 G_OBJECT_CLASS(parent_class)->finalize (obj);
241 modest_hildon1_window_mgr_new (void)
243 return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_HILDON1_WINDOW_MGR, NULL));
247 modest_hildon1_window_mgr_close_all_windows (ModestWindowMgr *self)
249 ModestHildon1WindowMgrPrivate *priv = NULL;
250 gboolean ret_value = FALSE;
251 ModestWindow *main_window;
253 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
254 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
256 /* If there is a main window then try to close it, and it will
257 close the others if needed */
258 main_window = modest_window_mgr_get_main_window (self, FALSE);
260 g_signal_emit_by_name (main_window, "delete-event", NULL, &ret_value);
263 GList *wins = NULL, *next = NULL;
265 /* delete-event handler actually removes window_list item, */
266 wins = priv->window_list;
268 next = g_list_next (wins);
269 g_signal_emit_by_name (G_OBJECT (wins->data), "delete-event", NULL, &ret_value);
280 compare_msguids (ModestWindow *win,
283 const gchar *msg_uid;
285 if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
288 /* Get message uid from msg window */
289 if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
290 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
291 if (msg_uid && uid &&!strcmp (msg_uid, uid))
294 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
297 if (msg_uid && uid &&!strcmp (msg_uid, uid))
304 modest_hildon1_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
307 ModestHildon1WindowMgrPrivate *priv = NULL;
309 gboolean has_header, has_window = FALSE;
312 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
313 g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
315 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
317 has_header = MODEST_WINDOW_MGR_CLASS (parent_class)->find_registered_header (self, header, win);
319 uid = modest_tny_folder_get_header_unique_id (header);
321 item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
325 if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) &&
326 (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
327 g_debug ("not a valid window!");
329 g_debug ("found a window");
330 *win = MODEST_WINDOW (item->data);
336 return has_header || has_window;
340 modest_hildon1_window_mgr_get_window_list (ModestWindowMgr *self)
342 ModestHildon1WindowMgrPrivate *priv;
344 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
345 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
347 return g_list_copy (priv->window_list);
351 get_show_toolbar_key (GType window_type,
354 const gchar *key = NULL;
356 if (window_type == MODEST_TYPE_MAIN_WINDOW)
358 MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
359 MODEST_CONF_MAIN_WINDOW_SHOW_TOOLBAR;
360 else if (window_type == MODEST_TYPE_MSG_VIEW_WINDOW)
362 MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
363 MODEST_CONF_MSG_VIEW_WINDOW_SHOW_TOOLBAR;
364 else if (window_type == MODEST_TYPE_MSG_EDIT_WINDOW)
366 MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR_FULLSCREEN :
367 MODEST_CONF_EDIT_WINDOW_SHOW_TOOLBAR;
369 g_return_val_if_reached (NULL);
374 #ifndef MODEST_TOOLKIT_GTK
376 on_window_is_topmost (GObject *gobject,
380 ModestHildon1WindowMgr *self;
381 ModestHildon1WindowMgrPrivate *priv;
382 ModestWindow *win = (ModestWindow *) gobject;
384 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data));
386 self = MODEST_HILDON1_WINDOW_MGR (user_data);
387 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
389 if (hildon_window_get_is_topmost (HILDON_WINDOW (win)))
390 priv->current_top = win;
395 on_window_state_event (GtkWidget *widget,
396 GdkEventWindowState *event,
399 ModestHildon1WindowMgr *self;
400 ModestHildon1WindowMgrPrivate *priv;
402 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (user_data), FALSE);
404 self = MODEST_HILDON1_WINDOW_MGR (user_data);
405 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
408 if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
409 g_print ("GDK_WINDOW_STATE_WITHDRAWN\n");
410 if (event->changed_mask & GDK_WINDOW_STATE_ICONIFIED)
411 g_print ("GDK_WINDOW_STATE_ICONIFIED\n");
412 if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED)
413 g_print ("GDK_WINDOW_STATE_MAXIMIZED\n");
414 if (event->changed_mask & GDK_WINDOW_STATE_STICKY)
415 g_print ("GDK_WINDOW_STATE_STICKY\n");
416 if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
417 g_print ("GDK_WINDOW_STATE_FULLSCREEN\n");
418 if (event->changed_mask & GDK_WINDOW_STATE_ABOVE)
419 g_print ("GDK_WINDOW_STATE_ABOVE\n");
420 if (event->changed_mask & GDK_WINDOW_STATE_BELOW)
421 g_print ("GDK_WINDOW_STATE_BELOW\n");
423 if (event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN ||
424 event->changed_mask & GDK_WINDOW_STATE_ABOVE) {
425 priv->current_top = MODEST_WINDOW (widget);
433 modest_hildon1_window_mgr_register_window (ModestWindowMgr *self,
434 ModestWindow *window,
435 ModestWindow *parent)
438 ModestHildon1WindowMgrPrivate *priv;
441 ModestWindow *main_window;
443 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
444 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
446 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
448 win = g_list_find (priv->window_list, window);
450 /* this is for the case we want to register the window and it was already
451 * registered. We leave silently.
456 if (!MODEST_WINDOW_MGR_CLASS (parent_class)->register_window (self, window, parent))
459 /* Add to list. Keep a reference to the window */
460 g_object_ref (window);
461 priv->window_list = g_list_prepend (priv->window_list, window);
463 /* Listen to window state changes. Unfortunately
464 window-state-event does not properly work for the Maemo
465 version, so we need to use is-topmost and the ifdef */
466 #ifndef MODEST_TOOLKIT_GTK
467 priv->window_state_uids =
468 modest_signal_mgr_connect (priv->window_state_uids,
470 "notify::is-topmost",
471 G_CALLBACK (on_window_is_topmost),
474 priv->window_state_uids =
475 modest_signal_mgr_connect (priv->window_state_uids,
477 "window-state-event",
478 G_CALLBACK (on_window_state_event),
481 /* Listen to object destruction */
482 handler_id = g_malloc0 (sizeof (gint));
483 *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
484 g_hash_table_insert (priv->destroy_handlers, window, handler_id);
486 main_window = modest_window_mgr_get_main_window (self, FALSE);
487 /* If there is a msg view window, let the main window listen the msg-changed signal */
488 if (MODEST_IS_MSG_VIEW_WINDOW(window) && main_window) {
490 handler = g_malloc0 (sizeof (gulong));
491 *handler = g_signal_connect (window, "msg-changed",
492 G_CALLBACK (modest_main_window_on_msg_view_window_msg_changed),
494 g_hash_table_insert (priv->viewer_handlers, window, handler);
497 /* Put into fullscreen if needed */
498 if (priv->fullscreen_mode)
499 gtk_window_fullscreen (GTK_WINDOW (window));
501 /* Show/hide toolbar & fullscreen */
502 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (window), priv->fullscreen_mode);
503 modest_window_show_toolbar (window, modest_conf_get_bool (modest_runtime_get_conf (), key, NULL));
509 cancel_window_operations (ModestWindow *window)
511 GSList* pending_ops = NULL;
513 /* cancel open and receive operations */
514 pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (),
516 while (pending_ops != NULL) {
517 ModestMailOperationTypeOperation type;
518 GSList* tmp_list = NULL;
520 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
521 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
522 modest_mail_operation_cancel (pending_ops->data);
524 g_object_unref (G_OBJECT (pending_ops->data));
525 tmp_list = pending_ops;
526 pending_ops = g_slist_next (pending_ops);
527 g_slist_free_1 (tmp_list);
534 on_window_destroy (ModestWindow *window,
536 ModestHildon1WindowMgr *self)
538 gint dialog_response = GTK_RESPONSE_NONE;
539 gboolean no_propagate = FALSE;
541 /* Specific stuff first */
542 if (MODEST_IS_MAIN_WINDOW (window)) {
543 ModestHildon1WindowMgrPrivate *priv;
544 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
546 /* If more than one window already opened */
547 if (g_list_length (priv->window_list) > 1) {
549 /* Present the window if it's not visible now */
550 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window))) {
551 gtk_window_present (GTK_WINDOW (window));
552 priv->current_top = window;
554 /* Create the confirmation dialog MSG-NOT308 */
555 dialog_response = modest_platform_run_confirmation_dialog (
556 GTK_WINDOW (window), _("emev_nc_close_windows"));
558 /* If the user wants to close all the windows */
559 if ((dialog_response == GTK_RESPONSE_OK)
560 || (dialog_response == GTK_RESPONSE_ACCEPT)
561 || (dialog_response == GTK_RESPONSE_YES)) {
562 GList *iter = priv->window_list;
564 if (iter->data != window) {
565 GList *tmp = iter->next;
566 on_window_destroy (MODEST_WINDOW (iter->data),
571 iter = g_list_next (iter);
579 /* Do not unregister it, just hide */
580 gtk_widget_hide_all (GTK_WIDGET (window));
582 /* Cancel pending operations */
583 cancel_window_operations (window);
585 /* Fake the window system, make it think that there is no window */
586 if (modest_window_mgr_get_num_windows (MODEST_WINDOW_MGR (self)) == 0)
587 g_signal_emit_by_name (self, "window-list-empty");
592 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
593 gboolean sent = FALSE;
594 gint response = GTK_RESPONSE_ACCEPT;
595 sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
596 /* Save currently edited message to Drafts if it was not sent */
597 if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
599 /* Raise the window if it's minimized */
600 if (!gtk_window_has_toplevel_focus (GTK_WINDOW (window)))
601 gtk_window_present (GTK_WINDOW (window));
604 modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
605 _("mcen_nc_no_email_message_modified_save_changes"));
607 if (response == GTK_RESPONSE_OK)
608 if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
612 /* Unregister window */
613 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (self), window);
621 disconnect_msg_changed (gpointer key,
626 handler_id = GPOINTER_TO_UINT(value);
628 if (key && G_IS_OBJECT(key))
629 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
633 modest_hildon1_window_mgr_unregister_window (ModestWindowMgr *self,
634 ModestWindow *window)
637 ModestHildon1WindowMgrPrivate *priv;
638 gulong *tmp, handler_id;
640 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
641 g_return_if_fail (MODEST_IS_WINDOW (window));
643 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
645 win = g_list_find (priv->window_list, window);
647 g_warning ("Trying to unregister a window that has not being registered yet");
651 /* If it's the main window unset it */
652 if (MODEST_IS_MAIN_WINDOW (window)) {
653 modest_window_mgr_set_main_window (self, NULL);
655 /* Disconnect all emissions of msg-changed */
656 if (priv->viewer_handlers) {
657 g_hash_table_foreach (priv->viewer_handlers,
658 disconnect_msg_changed,
660 g_hash_table_destroy (priv->viewer_handlers);
661 priv->viewer_handlers = NULL;
665 /* Remove the viewer window handler from the hash table. The
666 HashTable could not exist if the main window was closed
667 when there were other windows remaining */
668 if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
669 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
670 /* If the viewer was created without a main window
671 (for example when opening a message through D-Bus
672 the viewer handlers was not registered */
674 g_signal_handler_disconnect (window, *tmp);
675 g_hash_table_remove (priv->viewer_handlers, window);
679 /* Remove from list & hash table */
680 priv->window_list = g_list_remove_link (priv->window_list, win);
681 tmp = g_hash_table_lookup (priv->destroy_handlers, window);
684 g_hash_table_remove (priv->destroy_handlers, window);
686 /* cancel open and receive operations */
687 cancel_window_operations (window);
689 /* Check if it's the topmost window, and remove the window from the stack.
690 * This is needed for the cases there's no other topmost window that will
691 * replace it in topmost handler.
693 if (window == priv->current_top)
694 priv->current_top = NULL;
696 /* Disconnect the "window-state-event" handler, we won't need it anymore */
697 if (priv->window_state_uids) {
698 #ifndef MODEST_TOOLKIT_GTK
699 priv->window_state_uids =
700 modest_signal_mgr_disconnect (priv->window_state_uids,
702 "notify::is-topmost");
704 priv->window_state_uids =
705 modest_signal_mgr_disconnect (priv->window_state_uids,
707 "window-state-event");
711 /* Disconnect the "delete-event" handler, we won't need it anymore */
712 g_signal_handler_disconnect (window, handler_id);
714 /* Destroy the window */
715 g_object_unref (win->data);
718 MODEST_WINDOW_MGR_CLASS (parent_class)->unregister_window (self, window);
720 /* If there are no more windows registered emit the signal */
721 if (modest_window_mgr_get_num_windows (self) == 0)
722 g_signal_emit_by_name (self, "window-list-empty");
728 modest_hildon1_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
731 ModestHildon1WindowMgrPrivate *priv;
735 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
737 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
739 /* If there is no change do nothing */
740 if (priv->fullscreen_mode == on)
743 priv->fullscreen_mode = on;
745 conf = modest_runtime_get_conf ();
748 win = priv->window_list;
751 const gchar *key = NULL;
753 /* Getting this from gconf everytime is not that
754 expensive, we'll do it just a few times */
755 key = get_show_toolbar_key (G_TYPE_FROM_INSTANCE (win->data), on);
756 show = modest_conf_get_bool (conf, key, NULL);
758 /* Set fullscreen/unfullscreen */
760 gtk_window_fullscreen (GTK_WINDOW (win->data));
762 gtk_window_unfullscreen (GTK_WINDOW (win->data));
764 /* Show/Hide toolbar */
765 modest_window_show_toolbar (MODEST_WINDOW (win->data), show);
767 win = g_list_next (win);
772 modest_hildon1_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
774 ModestHildon1WindowMgrPrivate *priv;
776 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), FALSE);
778 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
780 return priv->fullscreen_mode;
784 modest_hildon1_window_mgr_show_toolbars (ModestWindowMgr *self,
786 gboolean show_toolbars,
789 ModestHildon1WindowMgrPrivate *priv;
791 const gchar *key = NULL;
793 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
795 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
796 conf = modest_runtime_get_conf ();
798 /* If nothing changes then return */
799 key = get_show_toolbar_key (window_type, fullscreen);
800 conf = modest_runtime_get_conf ();
801 if (modest_conf_get_bool (conf, key, NULL) == show_toolbars)
805 modest_conf_set_bool (conf, key, show_toolbars, NULL);
807 /* Apply now if the view mode is the right one */
808 if ((fullscreen && priv->fullscreen_mode) ||
809 (!fullscreen && !priv->fullscreen_mode)) {
811 GList *win = priv->window_list;
814 if (G_TYPE_FROM_INSTANCE (win->data) == window_type)
815 modest_window_show_toolbar (MODEST_WINDOW (win->data),
817 win = g_list_next (win);
823 modest_hildon1_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
825 ModestHildon1WindowMgrPrivate *priv;
826 ModestWindow *result;
828 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
829 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
831 result = MODEST_WINDOW_MGR_CLASS (parent_class)->get_main_window (self, FALSE);
832 /* create the main window, if it hasn't been created yet */
833 if (!result && show) {
834 /* modest_window_mgr_register_window will set priv->main_window */
835 result = modest_main_window_new ();
836 modest_window_mgr_register_window (self, result, NULL);
837 gtk_widget_show_all (GTK_WIDGET (result));
838 gtk_window_present (GTK_WINDOW (result));
840 g_debug ("%s: created main window: %p\n", __FUNCTION__, result);
849 modest_hildon1_window_mgr_get_modal (ModestWindowMgr *self)
851 ModestHildon1WindowMgrPrivate *priv;
853 g_return_val_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self), NULL);
854 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
856 return g_queue_peek_head (priv->modal_windows);
861 modest_hildon1_window_mgr_set_modal (ModestWindowMgr *self,
865 GtkWindow *old_modal;
866 ModestHildon1WindowMgrPrivate *priv;
868 g_return_if_fail (MODEST_IS_HILDON1_WINDOW_MGR (self));
869 g_return_if_fail (GTK_IS_WINDOW (window));
871 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
872 g_mutex_lock (priv->queue_lock);
873 old_modal = g_queue_peek_head (priv->modal_windows);
874 g_mutex_unlock (priv->queue_lock);
877 /* make us transient wrt the main window then */
878 if (priv->current_top && ((GtkWindow *) priv->current_top != window)) {
879 gtk_window_set_transient_for (window, GTK_WINDOW(priv->current_top));
881 ModestWindow *main_win = modest_window_mgr_get_main_window (self, FALSE);
882 if (GTK_WINDOW(main_win) != window) /* they should not be the same */
883 gtk_window_set_transient_for (window, GTK_WINDOW(main_win));
885 gtk_window_set_modal (window, TRUE);
887 /* un-modalize the old one; the one on top should be the
889 gtk_window_set_transient_for (window, GTK_WINDOW(old_modal));
890 gtk_window_set_modal (window, TRUE);
893 /* this will be the new modal window */
894 g_mutex_lock (priv->queue_lock);
895 g_queue_push_head (priv->modal_windows, window);
896 g_mutex_unlock (priv->queue_lock);
898 if (GTK_IS_DIALOG (window)) {
899 /* Note that response is not always enough because it
900 could be captured and removed easily by dialogs but
901 works for most of situations */
902 priv->modal_handler_uids =
903 modest_signal_mgr_connect (priv->modal_handler_uids,
906 G_CALLBACK (on_modal_dialog_close),
908 /* We need this as well because dialogs are often
909 destroyed with gtk_widget_destroy and this one will
910 prevent response from happening */
911 priv->modal_handler_uids =
912 modest_signal_mgr_connect (priv->modal_handler_uids,
915 G_CALLBACK (on_modal_dialog_destroy),
918 priv->modal_handler_uids =
919 modest_signal_mgr_connect (priv->modal_handler_uids,
922 G_CALLBACK (on_modal_window_close),
925 /* Destroy width parent */
926 gtk_window_set_destroy_with_parent (window, TRUE);
932 idle_top_modal (gpointer data)
934 ModestWindowMgr *self = MODEST_WINDOW_MGR (data);
935 ModestHildon1WindowMgrPrivate *priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
938 /* Get the top modal */
939 g_mutex_lock (priv->queue_lock);
940 topmost = (GtkWindow *) g_queue_peek_head (priv->modal_windows);
941 g_mutex_unlock (priv->queue_lock);
945 gdk_threads_enter ();
946 gtk_window_present (topmost);
947 /* It seems that the window looses modality if some
948 other is shown on top of it after set_transient_for
950 gtk_window_set_modal (topmost, TRUE);
951 gdk_threads_leave ();
958 remove_modal_from_queue (GtkWidget *widget,
959 ModestWindowMgr *self)
961 ModestHildon1WindowMgrPrivate *priv;
964 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
966 /* Remove from queue. We don't use remove, because we want to
967 exit if the widget does not belong to the queue */
968 g_mutex_lock (priv->queue_lock);
969 item = g_queue_find (priv->modal_windows, widget);
971 g_warning ("Trying to remove a modal window that is not registered");
972 g_mutex_unlock (priv->queue_lock);
975 g_queue_unlink (priv->modal_windows, item);
976 g_mutex_unlock (priv->queue_lock);
978 /* Disconnect handler */
979 priv->modal_handler_uids =
980 modest_signal_mgr_disconnect (priv->modal_handler_uids,
982 GTK_IS_DIALOG (widget) ?
985 if (GTK_IS_DIALOG (widget))
986 priv->modal_handler_uids =
987 modest_signal_mgr_disconnect (priv->modal_handler_uids,
991 /* Schedule the next one for being shown */
992 g_idle_add (idle_top_modal, self);
996 on_modal_window_close (GtkWidget *widget,
1000 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1002 /* Remove modal window from queue */
1003 remove_modal_from_queue (widget, self);
1010 on_modal_dialog_close (GtkDialog *dialog,
1014 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1016 /* Remove modal window from queue. Note that if "destroy"
1017 signal was invoked before the response the window could be
1019 remove_modal_from_queue (GTK_WIDGET (dialog), self);
1023 on_modal_dialog_destroy (GtkObject *object,
1026 ModestWindowMgr *self = MODEST_WINDOW_MGR (user_data);
1028 /* Remove modal window from queue */
1029 remove_modal_from_queue (GTK_WIDGET (object), self);
1032 static ModestWindow*
1033 modest_hildon1_window_mgr_get_current_top (ModestWindowMgr *self)
1035 ModestHildon1WindowMgrPrivate *priv;
1037 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
1039 return priv->current_top;
1043 modest_hildon1_window_mgr_screen_is_on (ModestWindowMgr *self)
1045 ModestHildon1WindowMgrPrivate *priv;
1046 ModestWindow *main_window;
1048 priv = MODEST_HILDON1_WINDOW_MGR_GET_PRIVATE (self);
1050 main_window = modest_window_mgr_get_main_window (self, FALSE);
1051 if (main_window && MODEST_IS_MAIN_WINDOW (main_window)) {
1052 return modest_main_window_screen_is_on (MODEST_MAIN_WINDOW (main_window));