* update the modest-window-manager; together with the next patch, this
[modest] / src / widgets / modest-window-mgr.c
index e8b583d..1d768e0 100644 (file)
@@ -65,6 +65,7 @@ struct _ModestWindowMgrPrivate {
        gboolean     show_toolbars_fullscreen;
        
        GSList       *windows_that_prevent_hibernation;
+       GSList       *preregistered_uids;
        GHashTable   *destroy_handlers;
 };
 #define MODEST_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
@@ -122,6 +123,8 @@ modest_window_mgr_init (ModestWindowMgr *obj)
        priv->main_window = NULL;
        priv->fullscreen_mode = FALSE;
 
+       priv->preregistered_uids = NULL;
+
        /* Could not initialize it from gconf, singletons are not
           ready yet */
        priv->show_toolbars = FALSE;
@@ -146,6 +149,10 @@ modest_window_mgr_finalize (GObject *obj)
                priv->window_list = NULL;
        }
 
+       g_slist_foreach (priv->preregistered_uids, (GFunc)g_free, NULL);
+       g_slist_free (priv->preregistered_uids);
+
+       
        /* Free the hash table with the handlers */
        if (priv->destroy_handlers) {
                g_hash_table_destroy (priv->destroy_handlers);
@@ -164,6 +171,134 @@ modest_window_mgr_new (void)
        return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_WINDOW_MGR, NULL));
 }
 
+
+
+
+/* do we have uid? */
+static gboolean
+has_uid (GSList *list, const gchar *uid)
+{
+       GSList *cursor = list;
+
+       if (!uid)
+               return FALSE;
+       
+       while (cursor) {
+               if (cursor->data && strcmp (cursor->data, uid) == 0)
+                       return TRUE;
+               cursor = g_slist_next (cursor);
+       }
+       return FALSE;
+}
+
+
+/* remove all from the list have have uid = uid */
+static GSList*
+remove_uid (GSList *list, const gchar *uid)
+{
+       GSList *cursor = list, *start = list;
+       
+       if (!uid)
+               return FALSE;
+       
+       while (cursor) {
+               GSList *next = g_slist_next (cursor);
+               if (cursor->data && strcmp (cursor->data, uid) == 0) {
+                       g_free (cursor->data);
+                       start = g_slist_delete_link (start, cursor);
+               }
+               cursor = next;
+       }
+       return start;
+}
+
+
+static GSList *
+append_uid (GSList *list, const gchar *uid)
+{
+       return g_slist_append (list, g_strdup(uid));
+}
+
+
+
+void 
+modest_window_mgr_register_header (ModestWindowMgr *self,  TnyHeader *header)
+{
+       ModestWindowMgrPrivate *priv;
+       gchar* uid;
+       
+       g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
+       g_return_if_fail (TNY_IS_HEADER(header));
+               
+       priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
+       uid = modest_tny_folder_get_header_unique_id (header);
+       
+       if (!has_uid (priv->preregistered_uids, uid))
+               priv->preregistered_uids = append_uid (priv->preregistered_uids, uid);
+
+       g_free (uid);
+}
+
+
+
+static gint
+compare_msguids (ModestWindow *win,
+                const gchar *uid)
+{
+       const gchar *msg_uid;
+
+       if (!MODEST_IS_MSG_VIEW_WINDOW (win))
+               return 1;
+
+       /* Get message uid from msg window */
+       msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
+       
+       if (msg_uid && uid &&!strcmp (msg_uid, uid))
+               return 0;
+       else
+               return 1;
+}
+
+
+
+gboolean
+modest_window_mgr_find_registered_header (ModestWindowMgr *self, TnyHeader *header,
+                                         ModestWindow **win)
+{
+       ModestWindowMgrPrivate *priv;
+       gchar* uid;
+       gboolean retval = FALSE;
+       GList *item = NULL;
+
+       g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
+       g_return_val_if_fail (TNY_IS_HEADER(header), FALSE);
+       
+       priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
+
+       uid = modest_tny_folder_get_header_unique_id (header);
+       
+       /* first, look for the window */
+       /* note, the UID cannot be in both the window list and the preregistered uid list */
+       if (priv->window_list) {
+               item = g_list_find_custom (priv->window_list, 
+                                          uid, (GCompareFunc) compare_msguids);
+               if (item) 
+                       retval = TRUE;
+               if (win)
+                       *win = item ? MODEST_WINDOW(item->data) : NULL;
+       }
+       
+
+       /* IF It's not in the window list. maybe it's in our uid list... */
+       retval = retval || has_uid (priv->preregistered_uids, uid);
+
+       g_free (uid);
+
+       return retval;
+}
+
+
+
 void 
 modest_window_mgr_register_window (ModestWindowMgr *self, 
                                   ModestWindow *window)
@@ -195,6 +330,14 @@ modest_window_mgr_register_window (ModestWindowMgr *self,
                }
        }
 
+       /* remove from the list of pre-registered uids */
+       if (MODEST_IS_MSG_VIEW_WINDOW(window)) {
+               priv->preregistered_uids = 
+                       remove_uid (priv->preregistered_uids,
+                                   modest_msg_view_window_get_message_uid
+                                   (MODEST_MSG_VIEW_WINDOW (window)));
+       }
+       
        /* Add to list. Keep a reference to the window */
        g_object_ref (window);
        priv->window_list = g_list_prepend (priv->window_list, window);
@@ -336,53 +479,6 @@ modest_window_mgr_unregister_window (ModestWindowMgr *self,
        }
 }
 
-static gint
-compare_msguids (ModestWindow *win,
-                const gchar *uid)
-{
-       const gchar *msg_uid;
-
-       if (!MODEST_IS_MSG_VIEW_WINDOW (win))
-               return 1;
-
-       /* Get message uid from msg window */
-       msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
-
-       if (msg_uid && uid &&!strcmp (msg_uid, uid))
-               return 0;
-       else
-               return 1;
-}
-
-ModestWindow*  
-modest_window_mgr_find_window_by_header (ModestWindowMgr *self, 
-                                        TnyHeader *header)
-{
-       ModestWindowMgrPrivate *priv;
-       GList *win = NULL;
-       gchar *msg_uid;
-
-       g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
-       g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
-
-       priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
-       msg_uid = modest_tny_folder_get_header_unique_id (header);
-
-       /* Look for the window */
-       if (priv->window_list)
-               win = g_list_find_custom (priv->window_list, 
-                                         msg_uid, 
-                                         (GCompareFunc) compare_msguids);
-       /* Free */
-       g_free (msg_uid);
-
-       /* Return the window */
-       if (win)
-               return win->data;
-       else 
-               return NULL;
-}
-
 void
 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
                                       gboolean on)