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-hildon2-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"
42 #include <hildon/hildon-program.h>
44 /* 'private'/'protected' functions */
45 static void modest_hildon2_window_mgr_class_init (ModestHildon2WindowMgrClass *klass);
46 static void modest_hildon2_window_mgr_instance_init (ModestHildon2WindowMgr *obj);
47 static void modest_hildon2_window_mgr_finalize (GObject *obj);
49 static gboolean on_window_destroy (ModestWindow *window,
51 ModestHildon2WindowMgr *self);
53 static gboolean modest_hildon2_window_mgr_register_window (ModestWindowMgr *self,
55 ModestWindow *parent);
56 static void modest_hildon2_window_mgr_unregister_window (ModestWindowMgr *self,
57 ModestWindow *window);
58 static void modest_hildon2_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
60 static gboolean modest_hildon2_window_mgr_get_fullscreen_mode (ModestWindowMgr *self);
61 static void modest_hildon2_window_mgr_show_toolbars (ModestWindowMgr *self,
63 gboolean show_toolbars,
65 static ModestWindow* modest_hildon2_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show);
66 static GtkWindow *modest_hildon2_window_mgr_get_modal (ModestWindowMgr *self);
67 static void modest_hildon2_window_mgr_set_modal (ModestWindowMgr *self,
70 static gboolean modest_hildon2_window_mgr_find_registered_header (ModestWindowMgr *self,
73 static GList *modest_hildon2_window_mgr_get_window_list (ModestWindowMgr *self);
74 static gboolean modest_hildon2_window_mgr_close_all_windows (ModestWindowMgr *self);
75 static gboolean window_can_close (ModestWindow *window);
76 static gboolean window_has_modals (ModestWindow *window);
78 typedef struct _ModestHildon2WindowMgrPrivate ModestHildon2WindowMgrPrivate;
79 struct _ModestHildon2WindowMgrPrivate {
82 GQueue *modal_windows;
84 gboolean fullscreen_mode;
86 GHashTable *destroy_handlers;
87 GHashTable *viewer_handlers;
88 GSList *window_state_uids;
92 GSList *modal_handler_uids;
93 ModestWindow *current_top;
95 #define MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
96 MODEST_TYPE_HILDON2_WINDOW_MGR, \
97 ModestHildon2WindowMgrPrivate))
99 static GObjectClass *parent_class = NULL;
102 modest_hildon2_window_mgr_get_type (void)
104 static GType my_type = 0;
106 static const GTypeInfo my_info = {
107 sizeof(ModestHildon2WindowMgrClass),
108 NULL, /* base init */
109 NULL, /* base finalize */
110 (GClassInitFunc) modest_hildon2_window_mgr_class_init,
111 NULL, /* class finalize */
112 NULL, /* class data */
113 sizeof(ModestHildon2WindowMgr),
115 (GInstanceInitFunc) modest_hildon2_window_mgr_instance_init,
118 my_type = g_type_register_static (MODEST_TYPE_WINDOW_MGR,
119 "ModestHildon2WindowMgr",
126 modest_hildon2_window_mgr_class_init (ModestHildon2WindowMgrClass *klass)
128 GObjectClass *gobject_class;
129 ModestWindowMgrClass *mgr_class;
131 gobject_class = (GObjectClass*) klass;
132 mgr_class = (ModestWindowMgrClass *) klass;
134 parent_class = g_type_class_peek_parent (klass);
135 gobject_class->finalize = modest_hildon2_window_mgr_finalize;
136 mgr_class->register_window = modest_hildon2_window_mgr_register_window;
137 mgr_class->unregister_window = modest_hildon2_window_mgr_unregister_window;
138 mgr_class->set_fullscreen_mode = modest_hildon2_window_mgr_set_fullscreen_mode;
139 mgr_class->get_fullscreen_mode = modest_hildon2_window_mgr_get_fullscreen_mode;
140 mgr_class->show_toolbars = modest_hildon2_window_mgr_show_toolbars;
141 mgr_class->get_main_window = modest_hildon2_window_mgr_get_main_window;
142 mgr_class->get_modal = modest_hildon2_window_mgr_get_modal;
143 mgr_class->set_modal = modest_hildon2_window_mgr_set_modal;
144 mgr_class->find_registered_header = modest_hildon2_window_mgr_find_registered_header;
145 mgr_class->get_window_list = modest_hildon2_window_mgr_get_window_list;
146 mgr_class->close_all_windows = modest_hildon2_window_mgr_close_all_windows;
148 g_type_class_add_private (gobject_class, sizeof(ModestHildon2WindowMgrPrivate));
153 modest_hildon2_window_mgr_instance_init (ModestHildon2WindowMgr *obj)
155 ModestHildon2WindowMgrPrivate *priv;
157 priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(obj);
158 priv->window_list = NULL;
159 priv->fullscreen_mode = FALSE;
160 priv->current_top = NULL;
161 priv->window_state_uids = NULL;
163 priv->modal_windows = g_queue_new ();
164 priv->queue_lock = g_mutex_new ();
166 /* Could not initialize it from gconf, singletons are not
168 priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
169 priv->viewer_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
171 priv->closing_time = 0;
173 priv->modal_handler_uids = NULL;
177 modest_hildon2_window_mgr_finalize (GObject *obj)
179 ModestHildon2WindowMgrPrivate *priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE(obj);
181 modest_signal_mgr_disconnect_all_and_destroy (priv->window_state_uids);
182 priv->window_state_uids = NULL;
184 if (priv->window_list) {
185 GList *iter = priv->window_list;
186 /* unregister pending windows */
188 ModestWindow *window = (ModestWindow *) iter->data;
189 iter = g_list_next (iter);
190 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), window);
192 g_list_free (priv->window_list);
193 priv->window_list = NULL;
196 /* Free the hash table with the handlers */
197 if (priv->destroy_handlers) {
198 g_hash_table_destroy (priv->destroy_handlers);
199 priv->destroy_handlers = NULL;
202 if (priv->viewer_handlers) {
203 g_hash_table_destroy (priv->viewer_handlers);
204 priv->viewer_handlers = NULL;
207 modest_signal_mgr_disconnect_all_and_destroy (priv->modal_handler_uids);
208 priv->modal_handler_uids = NULL;
210 if (priv->modal_windows) {
211 g_mutex_lock (priv->queue_lock);
212 g_queue_free (priv->modal_windows);
213 priv->modal_windows = NULL;
214 g_mutex_unlock (priv->queue_lock);
216 g_mutex_free (priv->queue_lock);
218 /* Do not unref priv->main_window because it does not hold a
222 G_OBJECT_CLASS(parent_class)->finalize (obj);
226 modest_hildon2_window_mgr_new (void)
228 return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_HILDON2_WINDOW_MGR, NULL));
232 modest_hildon2_window_mgr_close_all_windows (ModestWindowMgr *self)
234 ModestHildon2WindowMgrPrivate *priv = NULL;
235 gboolean ret_value = FALSE;
236 HildonStackableWindow *window;
237 HildonProgram *program;
238 gboolean failed = FALSE;
240 g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
241 priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
243 program = hildon_program_get_instance ();
245 while ((window = hildon_program_peek_window_stack (program)) != NULL) {
246 g_signal_emit_by_name (G_OBJECT (window), "delete-event", NULL, &ret_value);
247 if (ret_value == TRUE) {
257 compare_msguids (ModestWindow *win,
260 const gchar *msg_uid;
262 if ((!MODEST_IS_MSG_EDIT_WINDOW (win)) && (!MODEST_IS_MSG_VIEW_WINDOW (win)))
265 /* Get message uid from msg window */
266 if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
267 msg_uid = modest_msg_edit_window_get_message_uid (MODEST_MSG_EDIT_WINDOW (win));
268 if (msg_uid && uid &&!strcmp (msg_uid, uid))
271 msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
274 if (msg_uid && uid &&!strcmp (msg_uid, uid))
281 modest_hildon2_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
284 ModestHildon2WindowMgrPrivate *priv = NULL;
286 gboolean has_header, has_window = FALSE;
289 g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
290 g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
292 priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
294 has_header = MODEST_WINDOW_MGR_CLASS (parent_class)->find_registered_header (self, header, win);
296 uid = modest_tny_folder_get_header_unique_id (header);
298 item = g_list_find_custom (priv->window_list, uid, (GCompareFunc) compare_msguids);
302 if ((!MODEST_IS_MSG_VIEW_WINDOW(item->data)) &&
303 (!MODEST_IS_MSG_EDIT_WINDOW (item->data)))
304 g_debug ("not a valid window!");
306 g_debug ("found a window");
307 *win = MODEST_WINDOW (item->data);
313 return has_header || has_window;
317 modest_hildon2_window_mgr_get_window_list (ModestWindowMgr *self)
319 ModestHildon2WindowMgrPrivate *priv;
321 g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
322 priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
324 return g_list_copy (priv->window_list);
328 modest_hildon2_window_mgr_register_window (ModestWindowMgr *self,
329 ModestWindow *window,
330 ModestWindow *parent)
333 ModestHildon2WindowMgrPrivate *priv;
335 HildonProgram *program;
336 GtkWidget *current_top;
338 g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), FALSE);
339 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
341 priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
343 program = hildon_program_get_instance ();
344 win = g_list_find (priv->window_list, window);
346 /* this is for the case we want to register the window and it was already
347 * registered. We leave silently, telling the operation was succesful.
349 gtk_window_present (GTK_WINDOW (win));
353 if (!MODEST_WINDOW_MGR_CLASS (parent_class)->register_window (self, window, parent))
356 /* Add to list. Keep a reference to the window */
357 g_object_ref (window);
358 priv->window_list = g_list_prepend (priv->window_list, window);
360 current_top = GTK_WIDGET (hildon_program_peek_window_stack (program));
362 /* policy is: if no parent, then we close all up message views, but no account/folder/header window */
364 if (!parent || ((GtkWidget *) parent != current_top)) {
365 gboolean close_canceled = FALSE;
367 if ((current_top != NULL) &&
368 window_has_modals (MODEST_WINDOW (current_top))) {
369 /* Window on top but it has opened dialogs */
372 while (current_top && !close_canceled) {
373 current_top = GTK_WIDGET (hildon_program_peek_window_stack (program));
374 if (MODEST_IS_MSG_VIEW_WINDOW (current_top) || MODEST_IS_MSG_EDIT_WINDOW (current_top)) {
375 g_signal_emit_by_name (G_OBJECT (current_top), "delete-event", NULL, &close_canceled);
386 /* Listen to object destruction */
387 handler_id = g_malloc0 (sizeof (gint));
388 *handler_id = g_signal_connect (window, "delete-event", G_CALLBACK (on_window_destroy), self);
389 g_hash_table_insert (priv->destroy_handlers, window, handler_id);
391 /* Show toolbar always */
392 modest_window_show_toolbar (window, TRUE);
396 /* Add to list. Keep a reference to the window */
397 priv->window_list = g_list_remove (priv->window_list, window);
398 g_object_unref (window);
399 current_top = GTK_WIDGET (hildon_program_peek_window_stack (program));
401 gtk_window_present (GTK_WINDOW (current_top));
406 cancel_window_operations (ModestWindow *window)
408 GSList* pending_ops = NULL;
410 /* cancel open and receive operations */
411 pending_ops = modest_mail_operation_queue_get_by_source (modest_runtime_get_mail_operation_queue (),
413 while (pending_ops != NULL) {
414 ModestMailOperationTypeOperation type;
415 GSList* tmp_list = NULL;
417 type = modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (pending_ops->data));
418 if (type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || type == MODEST_MAIL_OPERATION_TYPE_OPEN) {
419 modest_mail_operation_cancel (pending_ops->data);
421 g_object_unref (G_OBJECT (pending_ops->data));
422 tmp_list = pending_ops;
423 pending_ops = g_slist_next (pending_ops);
424 g_slist_free_1 (tmp_list);
429 window_has_modals (ModestWindow *window)
433 gboolean retvalue = FALSE;
435 /* First we fetch all toplevels */
436 toplevels = gtk_window_list_toplevels ();
437 for (node = toplevels; node != NULL; node = g_list_next (node)) {
438 if (GTK_IS_WINDOW (node->data) &&
439 gtk_window_get_transient_for (GTK_WINDOW (node->data)) == GTK_WINDOW (window)) {
444 g_list_free (toplevels);
449 window_can_close (ModestWindow *window)
451 /* An editor can be always closed no matter the dialogs it has
453 if (MODEST_IS_MSG_EDIT_WINDOW (window))
456 return !window_has_modals (window);
460 on_window_destroy (ModestWindow *window,
462 ModestHildon2WindowMgr *self)
464 gboolean no_propagate = FALSE;
466 if (!window_can_close (window)) {
469 /* Specific stuff first */
470 if (MODEST_IS_MAIN_WINDOW (window)) {
471 ModestHildon2WindowMgrPrivate *priv;
472 ModestMainWindowContentsStyle style;
473 priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
475 /* If we're on header view, then just go to folder view and don't close */
476 style = modest_main_window_get_contents_style (MODEST_MAIN_WINDOW (window));
477 if (style == MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS) {
478 modest_main_window_set_contents_style (MODEST_MAIN_WINDOW (window),
479 MODEST_MAIN_WINDOW_CONTENTS_STYLE_FOLDERS);
483 /* Do not unregister it, just hide */
484 gtk_widget_hide_all (GTK_WIDGET (window));
486 /* Cancel pending operations */
487 cancel_window_operations (window);
489 /* Fake the window system, make it think that there is no window */
490 if (modest_window_mgr_num_windows (MODEST_WINDOW_MGR (self)) == 0)
491 g_signal_emit_by_name (self, "window-list-empty");
496 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
497 gboolean sent = FALSE;
498 sent = modest_msg_edit_window_get_sent (MODEST_MSG_EDIT_WINDOW (window));
499 /* Save currently edited message to Drafts if it was not sent */
500 if (!sent && modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
502 if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
506 /* Unregister window */
507 modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (self), window);
508 no_propagate = FALSE;
515 disconnect_msg_changed (gpointer key,
520 handler_id = GPOINTER_TO_UINT(value);
522 if (key && G_IS_OBJECT(key))
523 g_signal_handler_disconnect (G_OBJECT (key), handler_id);
527 modest_hildon2_window_mgr_unregister_window (ModestWindowMgr *self,
528 ModestWindow *window)
531 ModestHildon2WindowMgrPrivate *priv;
532 gulong *tmp, handler_id;
534 g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
535 g_return_if_fail (MODEST_IS_WINDOW (window));
537 priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
539 win = g_list_find (priv->window_list, window);
541 g_warning ("Trying to unregister a window that has not being registered yet");
545 /* If it's the main window unset it */
546 if (MODEST_IS_MAIN_WINDOW (window)) {
547 modest_window_mgr_set_main_window (self, NULL);
549 /* Disconnect all emissions of msg-changed */
550 if (priv->viewer_handlers) {
551 g_hash_table_foreach (priv->viewer_handlers,
552 disconnect_msg_changed,
554 g_hash_table_destroy (priv->viewer_handlers);
555 priv->viewer_handlers = NULL;
559 /* Remove the viewer window handler from the hash table. The
560 HashTable could not exist if the main window was closed
561 when there were other windows remaining */
562 if (MODEST_IS_MSG_VIEW_WINDOW (window) && priv->viewer_handlers) {
563 tmp = (gulong *) g_hash_table_lookup (priv->viewer_handlers, window);
564 /* If the viewer was created without a main window
565 (for example when opening a message through D-Bus
566 the viewer handlers was not registered */
568 g_signal_handler_disconnect (window, *tmp);
569 g_hash_table_remove (priv->viewer_handlers, window);
573 /* Remove from list & hash table */
574 priv->window_list = g_list_remove_link (priv->window_list, win);
575 tmp = g_hash_table_lookup (priv->destroy_handlers, window);
578 g_hash_table_remove (priv->destroy_handlers, window);
580 /* cancel open and receive operations */
581 cancel_window_operations (window);
583 /* Check if it's the topmost window, and remove the window from the stack.
584 * This is needed for the cases there's no other topmost window that will
585 * replace it in topmost handler.
587 if (window == priv->current_top)
588 priv->current_top = NULL;
590 /* Disconnect the "window-state-event" handler, we won't need it anymore */
591 if (priv->window_state_uids) {
592 #ifndef MODEST_TOOLKIT_GTK
593 priv->window_state_uids =
594 modest_signal_mgr_disconnect (priv->window_state_uids,
596 "notify::is-topmost");
598 priv->window_state_uids =
599 modest_signal_mgr_disconnect (priv->window_state_uids,
601 "window-state-event");
605 /* Disconnect the "delete-event" handler, we won't need it anymore */
606 g_signal_handler_disconnect (window, handler_id);
608 /* Destroy the window */
609 g_object_unref (win->data);
612 MODEST_WINDOW_MGR_CLASS (parent_class)->unregister_window (self, window);
614 /* If there are no more windows registered emit the signal */
615 if (modest_window_mgr_num_windows (self) == 0)
616 g_signal_emit_by_name (self, "window-list-empty");
622 modest_hildon2_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
625 g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
631 modest_hildon2_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
637 modest_hildon2_window_mgr_show_toolbars (ModestWindowMgr *self,
639 gboolean show_toolbars,
642 g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
648 modest_hildon2_window_mgr_get_main_window (ModestWindowMgr *self, gboolean show)
650 ModestHildon2WindowMgrPrivate *priv;
651 ModestWindow *result;
653 g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
654 priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
656 /* TODO: make this return NULL always */
658 result = MODEST_WINDOW_MGR_CLASS (parent_class)->get_main_window (self, FALSE);
659 /* create the main window, if it hasn't been created yet */
660 if (!result && show) {
661 /* modest_window_mgr_register_window will set priv->main_window */
662 result = modest_main_window_new ();
663 /* We have to remove all other windows */
664 if (!modest_window_mgr_close_all_windows (self)) {
665 gtk_widget_destroy (GTK_WIDGET (result));
668 if (!modest_window_mgr_register_window (self, result, NULL)) {
669 gtk_widget_destroy (GTK_WIDGET (result));
673 g_debug ("%s: created main window: %p\n", __FUNCTION__, result);
677 gtk_widget_show_all (GTK_WIDGET (result));
678 gtk_window_present (GTK_WINDOW (result));
686 modest_hildon2_window_mgr_get_modal (ModestWindowMgr *self)
688 ModestHildon2WindowMgrPrivate *priv;
689 GList *toplevel_list;
692 g_return_val_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self), NULL);
693 priv = MODEST_HILDON2_WINDOW_MGR_GET_PRIVATE (self);
696 toplevel_list = gtk_window_list_toplevels ();
697 while (toplevel_list) {
698 if (gtk_window_is_active (toplevel_list->data)) {
699 toplevel = toplevel_list->data;
702 toplevel_list = g_list_next (toplevel_list);
710 modest_hildon2_window_mgr_set_modal (ModestWindowMgr *self,
714 g_return_if_fail (MODEST_IS_HILDON2_WINDOW_MGR (self));
715 g_return_if_fail (GTK_IS_WINDOW (window));
717 gtk_window_set_modal (window, TRUE);
718 gtk_window_set_transient_for (window, parent);
719 gtk_window_set_destroy_with_parent (window, TRUE);