2007-07-25 Murray Cumming <murrayc@murrayc.com>
[modest] / src / widgets / modest-account-view.c
index 8846d14..fbac3d9 100644 (file)
@@ -46,6 +46,8 @@ static void modest_account_view_class_init    (ModestAccountViewClass *klass);
 static void modest_account_view_init          (ModestAccountView *obj);
 static void modest_account_view_finalize      (GObject *obj);
 
+static void modest_account_view_select_account (ModestAccountView *account_view, 
+       const gchar* account_name);
 
 typedef enum {
        MODEST_ACCOUNT_VIEW_NAME_COLUMN,
@@ -69,7 +71,7 @@ enum {
 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
 struct _ModestAccountViewPrivate {
        ModestAccountMgr *account_mgr;
-       gulong sig1, sig2;
+       gulong sig1, sig2, sig3;
        
        /* When this is TRUE, we ignore configuration key changes.
         * This is useful when making many changes. */
@@ -142,11 +144,15 @@ modest_account_view_finalize (GObject *obj)
 
        if (priv->account_mgr) {
                if (priv->sig1)
-                       g_signal_handler_disconnect (priv->account_mgr, priv->sig1);
+                       g_signal_handler_disconnect (modest_runtime_get_account_store (), 
+                                                    priv->sig1);
 
                if (priv->sig2)
                        g_signal_handler_disconnect (priv->account_mgr, priv->sig2);
 
+               if (priv->sig3)
+                       g_signal_handler_disconnect (priv->account_mgr, priv->sig3);
+               
                g_object_unref (G_OBJECT(priv->account_mgr));
                priv->account_mgr = NULL; 
        }
@@ -154,7 +160,27 @@ modest_account_view_finalize (GObject *obj)
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
-
+/* Get the string for the last updated time. Result must be g_freed */
+static gchar*
+get_last_updated_string(ModestAccountMgr* account_mgr, ModestAccountData *account_data)
+{
+       /* FIXME: let's assume that 'last update' applies to the store account... */
+       gchar* last_updated_string;
+       time_t last_updated = account_data->store_account->last_updated;
+       if (!modest_account_mgr_account_is_busy(account_mgr, account_data->account_name))
+       {
+               if (last_updated > 0) 
+                               last_updated_string = modest_text_utils_get_display_date(last_updated);
+               else
+                               last_updated_string = g_strdup (_("mcen_va_never"));
+       }
+       else
+       {
+               /* FIXME: There should be a logical name in the UI specs */
+               last_updated_string = g_strdup(_("Refreshing..."));
+       }
+       return last_updated_string;
+}
 
 static void
 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
@@ -162,7 +188,14 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
        GSList *account_names, *cursor;
        GtkListStore *model;
                
-       model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));  
+       model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
+       
+       /* Get the ID of the currently-selected account, 
+        * so we can select it again after rebuilding the list.
+        * Note that the name doesn't change even when the display name changes.
+        */
+       gchar *selected_name = modest_account_view_get_selected_account (view);
+
        gtk_list_store_clear (model);
 
        /* Note: We do not show disabled accounts.
@@ -175,10 +208,8 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
        cursor = account_names = modest_account_mgr_account_names (account_mgr,
                TRUE /* only enabled accounts. */);
        
-       if(account_names == NULL)
-       {
-         printf ("debug: modest_account_mgr_account_names() returned  NULL\n");
-       }
+       if (!account_names)
+               g_warning ("debug: modest_account_mgr_account_names() returned  NULL\n");
 
        while (cursor) {
                gchar *account_name;
@@ -196,15 +227,8 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                if (account_data->store_account) {
 
                        GtkTreeIter iter;
-                       time_t last_updated; 
-                       gchar *last_updated_string;
                        
-                       /* FIXME: let's assume that 'last update' applies to the store account... */
-                       last_updated = account_data->store_account->last_updated;
-                       if (last_updated > 0) 
-                               last_updated_string = modest_text_utils_get_display_date(last_updated);
-                       else
-                               last_updated_string = g_strdup (_("FIXME: Never"));
+                       gchar *last_updated_string = get_last_updated_string(account_mgr, account_data);
                        
                        if (account_data->is_enabled) {
                                gtk_list_store_insert_with_values (
@@ -213,26 +237,34 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                                        MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,  account_data->display_name,
                                        MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,    account_data->is_enabled,
                                        MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,    account_data->is_default,
-       
+
                                        MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
                                        modest_protocol_info_get_transport_store_protocol_name (account_data->store_account->proto),
        
                                        MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,  last_updated_string,
                                        -1);
-                               g_free (last_updated_string);
                        }
+                       g_free (last_updated_string);
                }
 
                modest_account_mgr_free_account_data (account_mgr, account_data);
                cursor = cursor->next;
        }
-       g_slist_free (account_names);
+
+       modest_account_mgr_free_account_names (account_names);
+       account_names = NULL;
+       
+       /* Try to re-select the same account: */
+       if (selected_name) {
+               modest_account_view_select_account (view, selected_name);
+               g_free (selected_name);
+       }
 }
 
 
 static void
 on_account_changed (ModestAccountMgr *account_mgr,
-                   const gchar* account, const gchar* key,
+                   const gchar* account, GSList *keys,
                    gboolean server_account, ModestAccountView *self)
 {      
        /* Never update the view in response to gconf changes.
@@ -244,23 +276,54 @@ on_account_changed (ModestAccountMgr *account_mgr,
         * notification does not happen so long after the key was set.
         * (We have no way to know when the last key was set, to do a final update)..
         */
-        return;
         
-       ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
-       
-       if (!priv->block_conf_updates)
-               update_account_view (account_mgr, self);
+       update_account_view (account_mgr, self);
 }
 
+static void
+on_account_busy_changed(ModestAccountMgr *account_mgr, const gchar *account_name,
+                                                                                               gboolean busy, ModestAccountView *self)
+{
+       GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self)));
+       GtkTreeIter iter;
+       g_message(__FUNCTION__);
+       if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
+               return;
+       do
+       {
+               gchar* cur_name;
+               gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
+                                                                                        &cur_name, -1);
+               if (g_str_equal(cur_name, account_name))
+               {
+                       ModestAccountData* account_data = 
+                               modest_account_mgr_get_account_data (account_mgr, account_name);
+                       if (!account_data)
+                               return;
+                       gchar* last_updated_string = get_last_updated_string(account_mgr, account_data);
+                       gtk_list_store_set(model, &iter, 
+                                          MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
+                                          -1);
+                       g_free (last_updated_string);
+                       modest_account_mgr_free_account_data (account_mgr, account_data);
+                       return;
+               }
+       }
+       while (gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
+}
 
 static void
-on_account_removed (ModestAccountMgr *account_mgr,
-                   const gchar* account, gboolean server_account,
-                   ModestAccountView *self)
+on_account_removed (TnyAccountStore *account_store, 
+                   TnyAccount *account,
+                   gpointer user_data)
 {
-       ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
-       if (!priv->block_conf_updates)
-               on_account_changed (account_mgr, account, NULL, server_account, self);
+       ModestAccountView *self;
+       ModestAccountViewPrivate *priv;
+
+       self = MODEST_ACCOUNT_VIEW (user_data);
+       priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
+
+       update_account_view (priv->account_mgr, MODEST_ACCOUNT_VIEW (user_data));
 }
 
 
@@ -435,10 +498,12 @@ init_view (ModestAccountView *self)
         */                     
        gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
 
-       priv->sig1 = g_signal_connect (G_OBJECT(priv->account_mgr),"account_removed",
+       priv->sig1 = g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),"account_removed",
                                       G_CALLBACK(on_account_removed), self);
        priv->sig2 = g_signal_connect (G_OBJECT(priv->account_mgr), "account_changed",
                                       G_CALLBACK(on_account_changed), self);
+       priv->sig3 = g_signal_connect (G_OBJECT(priv->account_mgr), "account_busy_changed",
+                                                        G_CALLBACK(on_account_busy_changed), self);
 }
 
 
@@ -515,7 +580,7 @@ on_model_foreach_select_account(GtkTreeModel *model,
        return FALSE; /* Keep walking the tree. */
 }
 
-void modest_account_view_select_account (ModestAccountView *account_view, 
+static void modest_account_view_select_account (ModestAccountView *account_view, 
        const gchar* account_name)
 {      
        /* Create a state instance so we can send two items of data to the signal handler: */
@@ -531,18 +596,3 @@ void modest_account_view_select_account (ModestAccountView *account_view,
        g_free (state);
 }
 
-
-
-void modest_account_view_block_conf_updates (ModestAccountView *account_view)
-{
-       ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(account_view);
-       priv->block_conf_updates = TRUE;
-}
-
-void modest_account_view_unblock_conf_updates (ModestAccountView *account_view)
-{
-       ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(account_view);
-       priv->block_conf_updates = FALSE;
-       
-       update_account_view (modest_runtime_get_account_mgr(), account_view);
-}