[evolution] Bug 784665 - Reply/Forward fails to pair account alias with recipient



commit 15ef0e2f57ef0fdf58a833a7908f5c62b7e16b9c
Author: Milan Crha <mcrha redhat com>
Date:   Tue Jul 11 12:45:19 2017 +0200

    Bug 784665 - Reply/Forward fails to pair account alias with recipient

 src/e-util/e-mail-identity-combo-box.c |   38 ++++++-
 src/mail/em-composer-utils.c           |  181 +++++++++++++++++--------------
 2 files changed, 132 insertions(+), 87 deletions(-)
---
diff --git a/src/e-util/e-mail-identity-combo-box.c b/src/e-util/e-mail-identity-combo-box.c
index 3715f85..4bd6bb5 100644
--- a/src/e-util/e-mail-identity-combo-box.c
+++ b/src/e-util/e-mail-identity-combo-box.c
@@ -458,7 +458,7 @@ mail_identity_combo_box_build_alias_id (const gchar *identity_uid,
 {
        g_return_val_if_fail (identity_uid != NULL, NULL);
 
-       if (!address)
+       if (!address || !*address)
                return g_strdup (identity_uid);
 
        return g_strconcat (identity_uid, "\n", address, "\n", name, NULL);
@@ -470,6 +470,7 @@ mail_identity_combo_box_add_address (GtkListStore *list_store,
                                     const gchar *name,
                                     const gchar *address,
                                     gboolean is_alias_entry,
+                                    const gchar *stored_alias_name,
                                     const gchar *identity_uid,
                                     const gchar *identity_display_name)
 {
@@ -496,7 +497,7 @@ mail_identity_combo_box_add_address (GtkListStore *list_store,
        if (queue != NULL && g_queue_get_length (queue) > 1)
                g_string_append_printf (string, " (%s)", identity_display_name);
 
-       alias_id = mail_identity_combo_box_build_alias_id (identity_uid, name, address);
+       alias_id = mail_identity_combo_box_build_alias_id (identity_uid, stored_alias_name, address);
 
        gtk_list_store_append (list_store, &iter);
 
@@ -647,7 +648,7 @@ e_mail_identity_combo_box_refresh (EMailIdentityComboBox *combo_box)
                address = e_source_mail_identity_get_address (extension);
 
                mail_identity_combo_box_add_address (GTK_LIST_STORE (tree_model),
-                       address_table, name, address, FALSE, uid, display_name);
+                       address_table, name, address, FALSE, NULL, uid, display_name);
 
                if (!e_mail_identity_combo_box_get_allow_aliases (combo_box))
                        continue;
@@ -666,10 +667,10 @@ e_mail_identity_combo_box_refresh (EMailIdentityComboBox *combo_box)
                                if (camel_internet_address_get (inet_address, ii, &alias_name, 
&alias_address) &&
                                    alias_address && *alias_address) {
                                        if (!alias_name || !*alias_name)
-                                               alias_name = name;
+                                               alias_name = NULL;
 
                                        mail_identity_combo_box_add_address (GTK_LIST_STORE (tree_model),
-                                               address_table, alias_name, alias_address, TRUE, uid, 
display_name);
+                                               address_table, alias_name ? alias_name : name, alias_address, 
TRUE, alias_name, uid, display_name);
                                }
                        }
 
@@ -905,6 +906,33 @@ e_mail_identity_combo_box_set_active_uid (EMailIdentityComboBox *combo_box,
 
        g_free (alias_id);
 
+       if (!found && alias_address && *alias_address) {
+               /* Match the first alias_address, not considering the name at all */
+               GtkTreeModel *model;
+               GtkTreeIter iter;
+
+               model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
+               if (gtk_tree_model_get_iter_first (model, &iter)) {
+                       do {
+                               gchar *uid = NULL, *address = NULL;
+
+                               gtk_tree_model_get (model, &iter,
+                                       E_MAIL_IDENTITY_COMBO_BOX_COLUMN_UID, &uid,
+                                       E_MAIL_IDENTITY_COMBO_BOX_COLUMN_ADDRESS, &address,
+                                       -1);
+
+                               found = g_strcmp0 (uid, identity_uid) == 0 &&
+                                       address && g_ascii_strcasecmp (address, alias_address) == 0;
+
+                               g_free (uid);
+                               g_free (address);
+
+                               if (found)
+                                       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
+                       } while (!found && gtk_tree_model_iter_next (model, &iter));
+               }
+       }
+
        if (!found && alias_address)
                found = gtk_combo_box_set_active_id (GTK_COMBO_BOX (combo_box), identity_uid);
 
diff --git a/src/mail/em-composer-utils.c b/src/mail/em-composer-utils.c
index 9014ad4..2cff9b0 100644
--- a/src/mail/em-composer-utils.c
+++ b/src/mail/em-composer-utils.c
@@ -1327,18 +1327,93 @@ em_utils_composer_print_cb (EMsgComposer *composer,
        }
 }
 
+static gint
+compare_sources_with_uids_order_cb (gconstpointer a,
+                                    gconstpointer b,
+                                    gpointer user_data)
+{
+       ESource *asource = (ESource *) a;
+       ESource *bsource = (ESource *) b;
+       GHashTable *uids_order = user_data;
+       gint aindex, bindex;
+
+       aindex = GPOINTER_TO_INT (g_hash_table_lookup (uids_order, e_source_get_uid (asource)));
+       bindex = GPOINTER_TO_INT (g_hash_table_lookup (uids_order, e_source_get_uid (bsource)));
+
+       if (aindex <= 0)
+               aindex = g_hash_table_size (uids_order);
+       if (bindex <= 0)
+               bindex = g_hash_table_size (uids_order);
+
+       return aindex - bindex;
+}
+
+static void
+sort_sources_by_ui (GList **psources,
+                    gpointer user_data)
+{
+       EShell *shell = user_data;
+       EShellBackend *shell_backend;
+       EMailSession *mail_session;
+       EMailAccountStore *account_store;
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+       GHashTable *uids_order;
+       gint index = 0;
+
+       g_return_if_fail (psources != NULL);
+       g_return_if_fail (E_IS_SHELL (shell));
+
+       /* nothing to sort */
+       if (!*psources || !g_list_next (*psources))
+               return;
+
+       shell_backend = e_shell_get_backend_by_name (shell, "mail");
+       g_return_if_fail (shell_backend != NULL);
+
+       mail_session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+       g_return_if_fail (mail_session != NULL);
+
+       account_store = e_mail_ui_session_get_account_store (E_MAIL_UI_SESSION (mail_session));
+       g_return_if_fail (account_store != NULL);
+
+       model = GTK_TREE_MODEL (account_store);
+       if (!gtk_tree_model_get_iter_first (model, &iter))
+               return;
+
+       uids_order = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+       do {
+               CamelService *service = NULL;
+
+               gtk_tree_model_get (model, &iter, E_MAIL_ACCOUNT_STORE_COLUMN_SERVICE, &service, -1);
+
+               if (service) {
+                       index++;
+                       g_hash_table_insert (uids_order, g_strdup (camel_service_get_uid (service)), 
GINT_TO_POINTER (index));
+                       g_object_unref (service);
+               }
+       } while (gtk_tree_model_iter_next (model, &iter));
+
+       *psources = g_list_sort_with_data (*psources, compare_sources_with_uids_order_cb, uids_order);
+
+       g_hash_table_destroy (uids_order);
+}
+
 /* Composing messages... */
 
 static void
 set_up_new_composer (EMsgComposer *composer,
                     const gchar *subject,
-                    CamelFolder *folder)
+                    CamelFolder *folder,
+                    CamelMimeMessage *message,
+                    const gchar *message_uid)
 {
        EClientCache *client_cache;
        ESourceRegistry *registry;
        EComposerHeaderTable *table;
        ESource *source = NULL;
-       gchar *identity = NULL;
+       gchar *identity = NULL, *identity_name = NULL, *identity_address = NULL;
 
        table = e_msg_composer_get_header_table (composer);
 
@@ -1346,12 +1421,25 @@ set_up_new_composer (EMsgComposer *composer,
        registry = e_client_cache_ref_registry (client_cache);
 
        if (folder != NULL) {
-               CamelStore *store;
                gchar *folder_uri;
                GList *list;
 
-               store = camel_folder_get_parent_store (folder);
-               source = em_utils_ref_mail_identity_for_store (registry, store);
+               if (message) {
+                       EShell *shell;
+
+                       shell = e_msg_composer_get_shell (composer);
+                       source = em_utils_check_send_account_override (shell, message, folder, 
&identity_name, &identity_address);
+                       if (!source)
+                               source = em_utils_guess_mail_identity_with_recipients_and_sort (
+                                       registry, message, folder, message_uid, &identity_name, 
&identity_address, sort_sources_by_ui, shell);
+               }
+
+               if (!source) {
+                       CamelStore *store;
+
+                       store = camel_folder_get_parent_store (folder);
+                       source = em_utils_ref_mail_identity_for_store (registry, store);
+               }
 
                folder_uri = e_mail_folder_uri_from_folder (folder);
 
@@ -1368,11 +1456,13 @@ set_up_new_composer (EMsgComposer *composer,
        }
 
        e_composer_header_table_set_subject (table, subject);
-       e_composer_header_table_set_identity_uid (table, identity, NULL, NULL);
+       e_composer_header_table_set_identity_uid (table, identity, identity_name, identity_address);
 
        em_utils_apply_send_account_override_to_composer (composer, folder);
 
        g_free (identity);
+       g_free (identity_name);
+       g_free (identity_address);
 
        g_object_unref (client_cache);
        g_object_unref (registry);
@@ -1396,7 +1486,7 @@ em_utils_compose_new_message (EMsgComposer *composer,
        if (folder != NULL)
                g_return_if_fail (CAMEL_IS_FOLDER (folder));
 
-       set_up_new_composer (composer, "", folder);
+       set_up_new_composer (composer, "", folder, NULL, NULL);
        composer_set_no_change (composer);
 
        gtk_widget_show (GTK_WIDGET (composer));
@@ -2173,7 +2263,7 @@ forward_non_attached (EMsgComposer *composer,
                gchar *subject;
 
                subject = mail_tool_generate_forward_subject (message);
-               set_up_new_composer (composer, subject, folder);
+               set_up_new_composer (composer, subject, folder, message, uid);
                g_free (subject);
 
                content = camel_medium_get_content (CAMEL_MEDIUM (message));
@@ -2286,7 +2376,7 @@ em_utils_forward_attachment (EMsgComposer *composer,
 
        e_msg_composer_set_is_reply_or_forward (composer, TRUE);
 
-       set_up_new_composer (composer, subject, folder);
+       set_up_new_composer (composer, subject, folder, NULL, NULL);
 
        e_msg_composer_attach (composer, part);
 
@@ -2329,79 +2419,6 @@ em_utils_forward_attachment (EMsgComposer *composer,
        gtk_widget_show (GTK_WIDGET (composer));
 }
 
-static gint
-compare_sources_with_uids_order_cb (gconstpointer a,
-                                    gconstpointer b,
-                                    gpointer user_data)
-{
-       ESource *asource = (ESource *) a;
-       ESource *bsource = (ESource *) b;
-       GHashTable *uids_order = user_data;
-       gint aindex, bindex;
-
-       aindex = GPOINTER_TO_INT (g_hash_table_lookup (uids_order, e_source_get_uid (asource)));
-       bindex = GPOINTER_TO_INT (g_hash_table_lookup (uids_order, e_source_get_uid (bsource)));
-
-       if (aindex <= 0)
-               aindex = g_hash_table_size (uids_order);
-       if (bindex <= 0)
-               bindex = g_hash_table_size (uids_order);
-
-       return aindex - bindex;
-}
-
-static void
-sort_sources_by_ui (GList **psources,
-                    gpointer user_data)
-{
-       EShell *shell = user_data;
-       EShellBackend *shell_backend;
-       EMailSession *mail_session;
-       EMailAccountStore *account_store;
-       GtkTreeModel *model;
-       GtkTreeIter iter;
-       GHashTable *uids_order;
-       gint index = 0;
-
-       g_return_if_fail (psources != NULL);
-       g_return_if_fail (E_IS_SHELL (shell));
-
-       /* nothing to sort */
-       if (!*psources || !g_list_next (*psources))
-               return;
-
-       shell_backend = e_shell_get_backend_by_name (shell, "mail");
-       g_return_if_fail (shell_backend != NULL);
-
-       mail_session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
-       g_return_if_fail (mail_session != NULL);
-
-       account_store = e_mail_ui_session_get_account_store (E_MAIL_UI_SESSION (mail_session));
-       g_return_if_fail (account_store != NULL);
-
-       model = GTK_TREE_MODEL (account_store);
-       if (!gtk_tree_model_get_iter_first (model, &iter))
-               return;
-
-       uids_order = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-
-       do {
-               CamelService *service = NULL;
-
-               gtk_tree_model_get (model, &iter, E_MAIL_ACCOUNT_STORE_COLUMN_SERVICE, &service, -1);
-
-               if (service) {
-                       index++;
-                       g_hash_table_insert (uids_order, g_strdup (camel_service_get_uid (service)), 
GINT_TO_POINTER (index));
-                       g_object_unref (service);
-               }
-       } while (gtk_tree_model_iter_next (model, &iter));
-
-       *psources = g_list_sort_with_data (*psources, compare_sources_with_uids_order_cb, uids_order);
-
-       g_hash_table_destroy (uids_order);
-}
-
 /**
  * em_utils_redirect_message:
  * @composer: an #EMsgComposer


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]