Fixes NB#66410
[modest] / src / widgets / modest-account-view.c
index 7a828fb..a5ebda0 100644 (file)
@@ -35,6 +35,7 @@
 #include <modest-tny-account.h>
 #include <modest-text-utils.h>
 #include <modest-runtime.h>
+#include <modest-signal-mgr.h>
 
 #include <gtk/gtkcellrenderertoggle.h>
 #include <gtk/gtkcellrenderertext.h>
@@ -48,7 +49,16 @@ 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);
+                                               const gchar* account_name);
+
+static void on_default_account_changed         (ModestAccountMgr *mgr,
+                                               gpointer user_data);
+
+static void on_display_name_changed            (ModestAccountMgr *self, 
+                                               const gchar *account,
+                                               gpointer user_data);
+
+static void modest_account_view_select_first_account (ModestAccountView *account_view);
 
 typedef enum {
        MODEST_ACCOUNT_VIEW_NAME_COLUMN,
@@ -66,8 +76,7 @@ struct _ModestAccountViewPrivate {
        ModestAccountMgr *account_mgr;
 
        /* Signal handlers */
-       gulong acc_inserted_handler, acc_removed_handler,
-               acc_busy_changed_handler, acc_changed_handler;
+       GSList *sig_handlers;
 };
 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                  MODEST_TYPE_ACCOUNT_VIEW, \
@@ -118,11 +127,7 @@ modest_account_view_init (ModestAccountView *obj)
        
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
        
-       priv->account_mgr = NULL; 
-       priv->acc_inserted_handler = 0;
-       priv->acc_removed_handler = 0;
-       priv->acc_busy_changed_handler = 0;
-       priv->acc_changed_handler = 0;
+       priv->sig_handlers = NULL;
 }
 
 static void
@@ -132,26 +137,10 @@ modest_account_view_finalize (GObject *obj)
 
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
 
-       if (priv->account_mgr) {
-               if (g_signal_handler_is_connected (modest_runtime_get_account_store (),
-                                                  priv->acc_inserted_handler))
-                       g_signal_handler_disconnect (modest_runtime_get_account_store (), 
-                                                    priv->acc_inserted_handler);
-
-               if (g_signal_handler_is_connected (modest_runtime_get_account_store (),
-                                                  priv->acc_removed_handler))
-                       g_signal_handler_disconnect (modest_runtime_get_account_store (), 
-                                                    priv->acc_removed_handler);
-               
-               if (g_signal_handler_is_connected (modest_runtime_get_account_store (),
-                                                  priv->acc_changed_handler))
-                       g_signal_handler_disconnect (modest_runtime_get_account_store (), 
-                                                    priv->acc_changed_handler);
-               
-               if (priv->acc_busy_changed_handler)
-                       g_signal_handler_disconnect (priv->account_mgr, priv->acc_busy_changed_handler);
+       /* Disconnect signals */
+       modest_signal_mgr_disconnect_all_and_destroy (priv->sig_handlers);
 
-               
+       if (priv->account_mgr) {        
                g_object_unref (G_OBJECT(priv->account_mgr));
                priv->account_mgr = NULL; 
        }
@@ -159,22 +148,23 @@ 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 the string for the last updated time. Result must NOT be g_freed */
+static const 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;
+       const 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"));
+                       last_updated_string = _("mcen_va_never");
        } else  {
                /* FIXME: There should be a logical name in the UI specs */
-               last_updated_string = g_strdup(_("..."));
+               last_updated_string = _("mcen_va_refreshing");
        }
+       
        return last_updated_string;
 }
 
@@ -186,7 +176,7 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                
        model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
        
-       /* Get the ID of the currently-selected account, 
+       /* 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.
         */
@@ -195,11 +185,11 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
        gtk_list_store_clear (model);
 
        /* Note: We do not show disabled accounts.
-        * Of course, this means that there is no UI to enable or disable 
-        * accounts. That is OK for maemo where no such feature or UI is 
-        * specified, so the "enabled" property is used internally to avoid 
-        * showing unfinished accounts. If a user-visible "enabled" is 
-        * needed in the future, we must use a second property for the 
+        * Of course, this means that there is no UI to enable or disable
+        * accounts. That is OK for maemo where no such feature or UI is
+        * specified, so the "enabled" property is used internally to avoid
+        * showing unfinished accounts. If a user-visible "enabled" is
+        * needed in the future, we must use a second property for the
         * current use instead */
        cursor = account_names = modest_account_mgr_account_names (account_mgr,
                TRUE /* only enabled accounts. */);
@@ -220,24 +210,24 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                if (account_data->store_account) {
 
                        GtkTreeIter iter;
-                       
-                       gchar *last_updated_string = get_last_updated_string(account_mgr, account_data);
+
+                       /* don't free */
+                       const gchar *last_updated_string = get_last_updated_string(account_mgr, account_data);
                        
                        if (account_data->is_enabled) {
+                               const gchar *proto_name;
+
+                               proto_name = modest_protocol_info_get_transport_store_protocol_name (account_data->store_account->proto);
                                gtk_list_store_insert_with_values (
                                        model, &iter, 0,
-                                       MODEST_ACCOUNT_VIEW_NAME_COLUMN,          account_name,
-                                       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_NAME_COLUMN, account_name,
+                                       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, proto_name,
                                        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);
@@ -262,30 +252,34 @@ on_account_busy_changed(ModestAccountMgr *account_mgr,
 {
        GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self)));
        GtkTreeIter iter;
-       g_message(__FUNCTION__);
+       gboolean found = FALSE;
+
        if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
                return;
-       do
-       {
+
+       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))
-               {
+               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)
+                       if (!account_data) {
+                               g_free (cur_name);
                                return;
-                       gchar* last_updated_string = get_last_updated_string(account_mgr, account_data);
+                       }
+                       const 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;
+                       found = TRUE;
                }
-       }
-       while (gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
+               g_free (cur_name);
+
+       } while (!found && gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
 }
 
 static void
@@ -301,7 +295,9 @@ on_account_inserted (TnyAccountStore *account_store,
        self = MODEST_ACCOUNT_VIEW (user_data);
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
 
-       update_account_view (priv->account_mgr, self);
+       /* Do not refresh the view with transport accounts */
+       if (TNY_IS_STORE_ACCOUNT (account))
+               update_account_view (priv->account_mgr, self);
 }
 
 static void
@@ -316,7 +312,15 @@ on_account_removed (TnyAccountStore *account_store,
 
        self = MODEST_ACCOUNT_VIEW (user_data);
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
-
+       
+       gchar *selected_name = modest_account_view_get_selected_account (self);
+       if (selected_name == NULL) {
+               /* we select the first account if none is selected */
+               modest_account_view_select_first_account (self);                
+       } else {
+               g_free (selected_name);
+       }
+       
        update_account_view (priv->account_mgr, self);
 }
 
@@ -328,8 +332,6 @@ on_account_changed (TnyAccountStore *account_store,
 {
        ModestAccountView *self = NULL;
        ModestAccountViewPrivate *priv = NULL;
-       TnyTransportAccount *transport_account = NULL;
-       ModestTnySendQueue *send_queue = NULL;
 
        g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
        g_return_if_fail (account);
@@ -338,93 +340,44 @@ on_account_changed (TnyAccountStore *account_store,
        self = MODEST_ACCOUNT_VIEW (user_data);
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
        
-       g_warning ("account changed: %s", tny_account_get_id(account));
-       
        /* Update account view */
-       update_account_view (priv->account_mgr, self);
-
-       /* Get transport account */
-       const gchar *modest_account_name = 
-                       modest_tny_account_get_parent_modest_account_name_for_server_account (account);
-       g_return_if_fail (modest_account_name);
-               
-       transport_account = (TnyTransportAccount*)
-               modest_tny_account_store_get_transport_account_for_open_connection (modest_runtime_get_account_store(),
-                                                                                   modest_account_name);
-
-       /* Restart send queue */
-       if (transport_account) {        
-               g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT(transport_account));
-               send_queue = modest_runtime_get_send_queue (transport_account);
-               g_return_if_fail (MODEST_IS_TNY_SEND_QUEUE(send_queue));
-               modest_tny_send_queue_try_to_send (send_queue);
-               
-               g_object_unref (transport_account);
-       }
-}
-
-
-
-static gboolean
-find_default_account(ModestAccountView *self, GtkTreeIter *iter)
-{
-       GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
-       gboolean result;
-       for (result = gtk_tree_model_get_iter_first(model, iter);
-            result == TRUE; result = gtk_tree_model_iter_next(model, iter))
-       {
-               gboolean is_default;
-               gtk_tree_model_get (model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default, -1);
-               if(is_default)
-                       return TRUE;
-       }
-
-       return FALSE;
+/*     update_account_view (priv->account_mgr, self); */
 }
 
 static void
-on_account_default_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
-                          ModestAccountView *self)
+on_account_default_toggled (GtkCellRendererToggle *cell_renderer, 
+                           gchar *path,
+                           ModestAccountView *self)
 {
+       ModestAccountViewPrivate *priv;
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+       gchar *account_name = NULL;
 
        g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
 
-       gboolean is_default = gtk_cell_renderer_toggle_get_active (cell_renderer);
-       if (is_default) {
-               /* Do not allow an account to be marked non-default.
-                * Only allow this to be changed by setting another account to default: */
-               gtk_cell_renderer_toggle_set_active (cell_renderer, TRUE);
+       /* If it's active then do nothing, no need to reenable it as
+          default account */
+       if (gtk_cell_renderer_toggle_get_active (cell_renderer))
                return;
-       }
 
-       ModestAccountViewPrivate *priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
-       GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
-       
-       GtkTreeIter iter;
-       if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
-               g_printerr ("modest: cannot find iterator\n");
-               return;
-       }
-       
-       gchar *account_name = NULL;
-       gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name,
-                           -1);
+       priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
+       model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));  
+       gtk_tree_model_get_iter_from_string (model, &iter, path);
        
-       /* Set this previously-non-default account as the default: */
-       if (modest_account_mgr_set_default_account (priv->account_mgr, account_name))
-       {
-               /* Explicitely set default column because we are ignoring gconf changes */
-               GtkTreeIter old_default_iter;
-               if (find_default_account (self, &old_default_iter)) {
-                       gtk_list_store_set (GTK_LIST_STORE (model), &old_default_iter,
-                                           MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
-               } else {
-                       g_warning ("%s: Did not find old default account in view", __FUNCTION__);
-               }
-
-               gtk_list_store_set (GTK_LIST_STORE (model), &iter,
-                                   MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
-       }
+       gtk_tree_model_get (model, &iter, 
+                           MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
+                           &account_name, -1);
+
+       /* Set this previously-non-default account as the
+          default. We're not updating here the value of the
+          DEFAULT_COLUMN because we'll do it in the
+          "default_account_changed" signal handler. We do it like
+          this because that way the signal handler is useful also
+          when we're inserting a new account and there is no other
+          one defined, in that case the change of account is provoked
+          by the account mgr and not by a signal toggle.*/
+       modest_account_mgr_set_default_account (priv->account_mgr, account_name);
 
        g_free (account_name);
 }
@@ -490,8 +443,12 @@ init_view (ModestAccountView *self)
        g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL);
 #endif /* MODEST_HAVE_HILDON0_WIDGETS */
 
-       g_signal_connect (G_OBJECT(toggle_renderer), "toggled", G_CALLBACK(on_account_default_toggled),
-                         self);
+       priv->sig_handlers = 
+               modest_signal_mgr_connect (priv->sig_handlers,
+                                          G_OBJECT(toggle_renderer), 
+                                          "toggled", 
+                                          G_CALLBACK(on_account_default_toggled),
+                                          self);
        
        /* account name */
        column =  gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text",
@@ -512,21 +469,42 @@ init_view (ModestAccountView *self)
         */                     
        gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
 
-       priv->acc_removed_handler = g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),
-                                                     "account_removed",
-                                                     G_CALLBACK(on_account_removed), self);
-
-       priv->acc_inserted_handler = g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),
-                                                      "account_inserted",
-                                                      G_CALLBACK(on_account_inserted), self);
-
-       priv->acc_inserted_handler = g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),
-                                                      "account_changed",
-                                                      G_CALLBACK(on_account_changed), self);
-
-       priv->acc_busy_changed_handler = g_signal_connect (G_OBJECT(priv->account_mgr),
-                                                          "account_busy_changed",
-                                                          G_CALLBACK(on_account_busy_changed), self);
+       priv->sig_handlers = 
+               modest_signal_mgr_connect (priv->sig_handlers, 
+                                          G_OBJECT (modest_runtime_get_account_store ()),
+                                          "account_removed",
+                                          G_CALLBACK(on_account_removed), 
+                                          self);
+       priv->sig_handlers = 
+               modest_signal_mgr_connect (priv->sig_handlers, 
+                                          G_OBJECT (modest_runtime_get_account_store ()),
+                                          "account_inserted",
+                                          G_CALLBACK(on_account_inserted), 
+                                          self);
+       priv->sig_handlers = 
+               modest_signal_mgr_connect (priv->sig_handlers, 
+                                          G_OBJECT (modest_runtime_get_account_store ()),
+                                          "account_changed",
+                                          G_CALLBACK(on_account_changed), 
+                                          self);
+       priv->sig_handlers = 
+               modest_signal_mgr_connect (priv->sig_handlers, 
+                                          G_OBJECT(priv->account_mgr),
+                                          "account_busy_changed",
+                                          G_CALLBACK(on_account_busy_changed), 
+                                          self);
+       priv->sig_handlers = 
+               modest_signal_mgr_connect (priv->sig_handlers, 
+                                          G_OBJECT(priv->account_mgr),
+                                          "default_account_changed",
+                                          G_CALLBACK(on_default_account_changed), 
+                                          self);
+       priv->sig_handlers = 
+               modest_signal_mgr_connect (priv->sig_handlers, 
+                                          G_OBJECT(priv->account_mgr),
+                                          "display_name_changed",
+                                          G_CALLBACK(on_display_name_changed), 
+                                          self);
 }
 
 
@@ -602,8 +580,9 @@ on_model_foreach_select_account(GtkTreeModel *model,
        return FALSE; /* Keep walking the tree. */
 }
 
-static void modest_account_view_select_account (ModestAccountView *account_view, 
-       const gchar* account_name)
+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: */
        ForEachData *state = g_new0 (ForEachData, 1);
@@ -618,3 +597,65 @@ static void modest_account_view_select_account (ModestAccountView *account_view,
        g_free (state);
 }
 
+static void
+modest_account_view_select_first_account (ModestAccountView *account_view)
+{
+       GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (account_view));
+       g_return_if_fail (model != NULL);
+
+       GtkTreeIter iter;
+       gtk_tree_model_get_iter_first (model, &iter);
+
+       GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (account_view));
+       g_return_if_fail (selection != NULL);
+
+       gtk_tree_selection_select_iter (selection, &iter);
+}
+
+static void
+on_default_account_changed (ModestAccountMgr *mgr,
+                           gpointer user_data)
+{
+       GtkTreeIter iter;
+       gchar *default_account_name;
+       GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
+
+       if (!gtk_tree_model_get_iter_first(model, &iter))
+               return;
+
+       default_account_name = modest_account_mgr_get_default_account (mgr);
+
+       do {
+               gboolean is_default;
+               gchar *name;
+
+               gtk_tree_model_get (model, &iter, 
+                                   MODEST_ACCOUNT_VIEW_NAME_COLUMN, &name,
+                                   MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default, 
+                                   -1);
+
+               /* Update the default account column */
+               if ((default_account_name != NULL) && (!strcmp (name, default_account_name)))
+                       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
+                                           MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
+               else
+                       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
+                                           MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
+
+               g_free (name);
+
+       } while (gtk_tree_model_iter_next(model, &iter));
+
+       /* Free and force a redraw */
+       g_free (default_account_name);
+       gtk_widget_queue_draw (GTK_WIDGET (user_data));
+}
+
+static void 
+on_display_name_changed (ModestAccountMgr *mgr, 
+                        const gchar *account,
+                        gpointer user_data)
+{
+       /* Update the view */
+       update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));
+}