Show as active (color) the accounts that have new messages
[modest] / src / widgets / modest-window-mgr.c
index 1dbd75f..95113a7 100644 (file)
@@ -33,6 +33,7 @@
 #include "modest-tny-folder.h"
 #include "modest-ui-actions.h"
 #include "modest-platform.h"
+#include "modest-defs.h"
 #include "widgets/modest-main-window.h"
 #include "widgets/modest-msg-edit-window.h"
 #include "widgets/modest-msg-view-window.h"
@@ -82,6 +83,7 @@ static void on_mail_operation_started (ModestMailOperation *mail_op,
                                       gpointer user_data);
 static void on_mail_operation_finished (ModestMailOperation *mail_op,
                                        gpointer user_data);
+static gboolean modest_window_mgr_close_all_but_initial_default (ModestWindowMgr *self);
 
 /* list my signals  */
 enum {
@@ -171,6 +173,7 @@ modest_window_mgr_class_init (ModestWindowMgrClass *klass)
        mgr_class->get_current_top = modest_window_mgr_get_current_top_default;
        mgr_class->screen_is_on = modest_window_mgr_screen_is_on_default;
        mgr_class->create_caches = modest_window_mgr_create_caches_default;
+       mgr_class->close_all_but_initial = modest_window_mgr_close_all_but_initial_default;
 
        g_type_class_add_private (gobject_class, sizeof(ModestWindowMgrPrivate));
 
@@ -233,7 +236,7 @@ load_new_view (ModestWindowMgr *self)
 {
        ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
        if ((priv->cached_view == NULL) && (priv->idle_load_view_id == 0))
-               priv->idle_load_view_id = g_idle_add ((GSourceFunc) idle_load_view, self);
+               priv->idle_load_view_id = g_timeout_add (2500, (GSourceFunc) idle_load_view, self);
 }
 
 static void
@@ -241,7 +244,7 @@ load_new_editor (ModestWindowMgr *self)
 {
        ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
        if ((priv->cached_editor == NULL) && (priv->idle_load_editor_id == 0))
-               priv->idle_load_editor_id = g_idle_add ((GSourceFunc) idle_load_editor, self);
+               priv->idle_load_editor_id = g_timeout_add (5000, (GSourceFunc) idle_load_editor, self);
 }
 
 static void
@@ -483,7 +486,10 @@ modest_window_mgr_find_registered_header_default (ModestWindowMgr *self, TnyHead
 
        uid = modest_tny_folder_get_header_unique_id (header);
 
-       return modest_window_mgr_find_registered_message_uid (self, uid, win);
+       if (uid)
+               return modest_window_mgr_find_registered_message_uid (self, uid, win);
+       else
+               return FALSE;
 }
 
 gboolean
@@ -532,7 +538,26 @@ modest_window_mgr_register_window (ModestWindowMgr *self,
                                   ModestWindow *window,
                                   ModestWindow *parent)
 {
-       return MODEST_WINDOW_MGR_GET_CLASS (self)->register_window (self, window, parent);
+       gboolean no_windows, retval;
+
+       no_windows = (modest_window_mgr_get_num_windows (self) == 0);
+
+       retval = MODEST_WINDOW_MGR_GET_CLASS (self)->register_window (self, window, parent);
+
+       if  (no_windows) {
+               /* If this is the first registered window then reset the
+                  status of the TnyDevice as it might be forced to be offline
+                  when modest is running in the background (see
+                  modest_tny_account_store_new()) */
+               if (tny_device_is_forced (modest_runtime_get_device ()))
+                       tny_device_reset (modest_runtime_get_device ());
+
+               /* Do also allow modest to shutdown when the
+                  application is closed */
+               modest_runtime_set_allow_shutdown (TRUE);
+       }
+
+       return retval;
 }
 
 static gboolean
@@ -571,19 +596,18 @@ modest_window_mgr_register_window_default (ModestWindowMgr *self,
                const gchar *uid = modest_msg_view_window_get_message_uid
                        (MODEST_MSG_VIEW_WINDOW (window));
 
-               if (!has_uid (priv->preregistered_uids, uid)) 
-                       g_debug ("weird: no uid for window (%s)", uid);
-               
                MODEST_DEBUG_BLOCK(g_debug ("registering window for %s", uid ? uid : "<none>"););
-               
-               priv->preregistered_uids = 
-                       remove_uid (priv->preregistered_uids,
-                                   modest_msg_view_window_get_message_uid
-                                   (MODEST_MSG_VIEW_WINDOW (window)));
+
+               if (has_uid (priv->preregistered_uids, uid)) {
+                       priv->preregistered_uids = 
+                               remove_uid (priv->preregistered_uids,
+                                           modest_msg_view_window_get_message_uid
+                                           (MODEST_MSG_VIEW_WINDOW (window)));
+               }
        } else if (MODEST_IS_MSG_EDIT_WINDOW(window)) {
                const gchar *uid = modest_msg_edit_window_get_message_uid
                        (MODEST_MSG_EDIT_WINDOW (window));
-               
+
                MODEST_DEBUG_BLOCK(g_debug ("registering window for %s", uid););
 
                priv->preregistered_uids = 
@@ -1140,3 +1164,64 @@ modest_window_mgr_has_progress_operation_on_account (ModestWindowMgr *self,
        return account_ops;
 }
 
+/* 'Protected method' must be only called by children */
+gboolean
+_modest_window_mgr_close_active_modals (ModestWindowMgr *self)
+{
+       GtkWidget *modal;
+
+       /* Exit if there are no windows */
+       if (!modest_window_mgr_get_num_windows (self)) {
+               g_warning ("%s: there are no windows to close", __FUNCTION__);
+               return FALSE;
+       }
+
+       /* Check that there is no active modal dialog */
+       modal = (GtkWidget *) modest_window_mgr_get_modal (self);
+       while (modal && GTK_IS_DIALOG (modal)) {
+               GtkWidget *parent;
+
+#if defined(MODEST_TOOLKIT_HILDON2) || defined(MODEST_TOOLKIT_HILDON)
+#include <hildon/hildon.h>
+               /* If it's a hildon note then don't try to close it as
+                  this is the default behaviour of WM, delete event
+                  is not issued for this kind of notes as we want the
+                  user to always click on a button */
+               if (HILDON_IS_NOTE (modal)) {
+                       gtk_window_present (GTK_WINDOW (modal));
+                       return FALSE;
+               }
+#endif
+
+               /* Get the parent */
+               parent = (GtkWidget *) gtk_window_get_transient_for (GTK_WINDOW (modal));
+
+               /* Try to close it */
+               gtk_dialog_response (GTK_DIALOG (modal), GTK_RESPONSE_DELETE_EVENT);
+
+               /* Maybe the dialog was not closed, because a close
+                  confirmation dialog for example. Then ignore the
+                  register process */
+               if (GTK_IS_WINDOW (modal)) {
+                       gtk_window_present (GTK_WINDOW (modal));
+                       return FALSE;
+               }
+
+               /* Get next modal */
+               modal = parent;
+       }
+       return TRUE;
+}
+
+gboolean
+modest_window_mgr_close_all_but_initial (ModestWindowMgr *self)
+{
+       return MODEST_WINDOW_MGR_GET_CLASS (self)->close_all_but_initial (self);
+}
+
+static gboolean
+modest_window_mgr_close_all_but_initial_default (ModestWindowMgr *self)
+{
+       /* Empty default implementation */
+       return FALSE;
+}