[evolution-ews] Bug 795121 - Option for GAL: Use only primary contact email address



commit 271d9a4180bfb99949eb4284c0c151373aa5f4fa
Author: Milan Crha <mcrha redhat com>
Date:   Thu Apr 19 15:50:25 2018 +0200

    Bug 795121 - Option for GAL: Use only primary contact email address

 po/POTFILES.in                        |    1 +
 src/addressbook/e-book-backend-ews.c  |   54 +++++++++++++++++-----
 src/configuration/e-book-config-ews.c |   81 +++++++++++++++++++++++++++++++++
 src/server/e-source-ews-folder.c      |   52 +++++++++++++++++++++-
 src/server/e-source-ews-folder.h      |    5 ++
 5 files changed, 179 insertions(+), 14 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 6fc898a..232dee4 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -12,6 +12,7 @@ src/camel/camel-ews-transport.c
 src/camel/camel-ews-utils.c
 src/collection/e-ews-backend.c
 src/collection/module-ews-backend.c
+src/configuration/e-book-config-ews.c
 src/configuration/e-ews-config-lookup.c
 src/configuration/e-ews-config-utils.c
 src/configuration/e-ews-edit-folder-permissions.c
diff --git a/src/addressbook/e-book-backend-ews.c b/src/addressbook/e-book-backend-ews.c
index 83f1cbf..94fa231 100644
--- a/src/addressbook/e-book-backend-ews.c
+++ b/src/addressbook/e-book-backend-ews.c
@@ -479,7 +479,7 @@ ebews_populate_notes (EBookBackendEws *bbews,
        e_contact_set (contact, E_CONTACT_NOTE, notes);
 }
 
-static void
+static gboolean
 set_email_address (EContact *contact,
                    EContactField field,
                    EEwsItem *item,
@@ -494,19 +494,29 @@ set_email_address (EContact *contact,
        else if (require_smtp_prefix)
                ea = NULL;
 
-       if (ea && *ea)
+       if (ea && *ea) {
                e_contact_set (contact, field, ea);
+               return TRUE;
+       }
+
+       return FALSE;
 }
 
 static void
 ebews_populate_emails_ex (EBookBackendEws *bbews,
                          EContact *contact,
                          EEwsItem *item,
-                         gboolean require_smtp_prefix)
+                         gboolean require_smtp_prefix,
+                         gboolean use_primary_address)
 {
-       set_email_address (contact, E_CONTACT_EMAIL_1, item, "EmailAddress1", require_smtp_prefix);
-       set_email_address (contact, E_CONTACT_EMAIL_2, item, "EmailAddress2", require_smtp_prefix);
-       set_email_address (contact, E_CONTACT_EMAIL_3, item, "EmailAddress3", require_smtp_prefix);
+       if (set_email_address (contact, E_CONTACT_EMAIL_1, item, "EmailAddress1", require_smtp_prefix) && 
use_primary_address)
+               return;
+
+       if (set_email_address (contact, E_CONTACT_EMAIL_2, item, "EmailAddress2", require_smtp_prefix) && 
use_primary_address)
+               return;
+
+       if (set_email_address (contact, E_CONTACT_EMAIL_3, item, "EmailAddress3", require_smtp_prefix) && 
use_primary_address)
+               return;
 }
 
 static void
@@ -516,7 +526,7 @@ ebews_populate_emails (EBookBackendEws *bbews,
                       GCancellable *cancellable,
                       GError **errror)
 {
-       ebews_populate_emails_ex (bbews, contact, item, FALSE);
+       ebews_populate_emails_ex (bbews, contact, item, FALSE, FALSE);
 }
 
 static void
@@ -1515,6 +1525,7 @@ ebb_ews_convert_contact_to_updatexml_cb (ESoapMessage *msg,
 static EContact *
 ebb_ews_item_to_contact (EBookBackendEws *bbews,
                         EEwsItem *item,
+                        gboolean use_primary_address,
                         GCancellable *cancellable,
                         GError **error)
 {
@@ -1526,6 +1537,10 @@ ebb_ews_item_to_contact (EBookBackendEws *bbews,
        for (ii = 0; ii < G_N_ELEMENTS (mappings); ii++) {
                element_type = mappings[ii].element_type;
 
+               /* Skip email, when the primary is supposed to be used only */
+               if (use_primary_address && mappings[ii].field_id == E_CONTACT_EMAIL_1)
+                       continue;
+
                if (element_type == ELEMENT_TYPE_SIMPLE && !mappings[ii].populate_contact_func) {
                        const gchar *val = mappings[ii].get_simple_prop_func (item);
 
@@ -1556,7 +1571,7 @@ ebb_ews_items_to_contacts (EBookBackendEws *bbews,
                if (e_ews_item_get_item_type (item) == E_EWS_ITEM_TYPE_ERROR)
                        continue;
 
-               contact = ebb_ews_item_to_contact (bbews, item, cancellable, error);
+               contact = ebb_ews_item_to_contact (bbews, item, FALSE, cancellable, error);
 
                attr = e_vcard_attribute_new (NULL, "X-EWS-KIND");
                e_vcard_add_attribute_with_value (E_VCARD (contact), attr, "DT_MAILUSER");
@@ -2586,14 +2601,19 @@ ebb_ews_update_cache_for_expression (EBookBackendEws *bbews,
                                EWS_SEARCH_AD, NULL, TRUE, &mailboxes, &contacts, &includes_last_item, 
cancellable, error);
 
                if (success) {
+                       ESourceEwsFolder *ews_folder;
+                       gboolean use_primary_address;
                        GSList *mlink, *clink;
 
+                       ews_folder = e_source_get_extension (e_backend_get_source (E_BACKEND (bbews)), 
E_SOURCE_EXTENSION_EWS_FOLDER);
+                       use_primary_address = e_source_ews_folder_get_use_primary_address (ews_folder);
+
                        for (mlink = mailboxes, clink = contacts; mlink; mlink = g_slist_next (mlink), clink 
= g_slist_next (clink)) {
                                EwsMailbox *mb = mlink->data;
                                EEwsItem *contact_item = clink ? clink->data : NULL;
                                EBookMetaBackendInfo *nfo;
                                EContact *contact = NULL;
-                               gboolean is_public_dl = FALSE;
+                               gboolean is_public_dl = FALSE, mailbox_address_set = FALSE;
                                const gchar *str;
                                gchar *fake_rev;
 
@@ -2608,7 +2628,7 @@ ebb_ews_update_cache_for_expression (EBookBackendEws *bbews,
                                }
 
                                if (!contact && contact_item && e_ews_item_get_item_type (contact_item) == 
E_EWS_ITEM_TYPE_CONTACT)
-                                       contact = ebb_ews_item_to_contact (bbews, contact_item, cancellable, 
NULL);
+                                       contact = ebb_ews_item_to_contact (bbews, contact_item, 
use_primary_address && !is_public_dl, cancellable, NULL);
 
                                if (!contact)
                                        contact = e_contact_new ();
@@ -2624,12 +2644,19 @@ ebb_ews_update_cache_for_expression (EBookBackendEws *bbews,
 
                                g_free (fake_rev);
 
+                               if (use_primary_address && !is_public_dl && mb->email &&
+                                   (!mb->routing_type || g_ascii_strcasecmp (mb->routing_type, "SMTP") == 
0)) {
+                                       e_contact_set (contact, E_CONTACT_EMAIL_1, mb->email);
+                                       mailbox_address_set = TRUE;
+                               }
+
                                str = e_contact_get_const (contact, E_CONTACT_FULL_NAME);
                                if (!str || !*str)
                                        e_contact_set (contact, E_CONTACT_FULL_NAME, mb->name);
 
                                str = e_contact_get_const (contact, E_CONTACT_EMAIL_1);
-                               if (!str || !*str || (!is_public_dl && contact_item && 
e_ews_item_get_item_type (contact_item) == E_EWS_ITEM_TYPE_CONTACT)) {
+                               if (!str || !*str || (!is_public_dl && contact_item && !mailbox_address_set &&
+                                   e_ews_item_get_item_type (contact_item) == E_EWS_ITEM_TYPE_CONTACT)) {
                                        /* Cleanup first, then re-add only SMTP addresses */
                                        e_contact_set (contact, E_CONTACT_EMAIL_1, NULL);
                                        e_contact_set (contact, E_CONTACT_EMAIL_2, NULL);
@@ -2637,13 +2664,14 @@ ebb_ews_update_cache_for_expression (EBookBackendEws *bbews,
                                        e_contact_set (contact, E_CONTACT_EMAIL_4, NULL);
                                        e_contact_set (contact, E_CONTACT_EMAIL, NULL);
 
-                                       ebews_populate_emails_ex (bbews, contact, contact_item, TRUE);
+                                       ebews_populate_emails_ex (bbews, contact, contact_item, TRUE, 
use_primary_address && !is_public_dl);
                                }
 
                                str = e_contact_get_const (contact, E_CONTACT_EMAIL_1);
                                if (!str || !*str) {
                                        e_contact_set (contact, E_CONTACT_EMAIL_1, mb->email);
-                               } else if (!is_public_dl && mb->email && (!mb->routing_type || 
g_ascii_strcasecmp (mb->routing_type, "SMTP") == 0)) {
+                               } else if (!is_public_dl && !mailbox_address_set && mb->email &&
+                                          (!mb->routing_type || g_ascii_strcasecmp (mb->routing_type, 
"SMTP") == 0)) {
                                        EContactField fields[3] = { E_CONTACT_EMAIL_2, E_CONTACT_EMAIL_3, 
E_CONTACT_EMAIL_4 };
                                        gchar *emails[3];
                                        gint ii, ff = 0;
diff --git a/src/configuration/e-book-config-ews.c b/src/configuration/e-book-config-ews.c
index a2ebfd1..9180a60 100644
--- a/src/configuration/e-book-config-ews.c
+++ b/src/configuration/e-book-config-ews.c
@@ -18,6 +18,11 @@
 
 #include "evolution-ews-config.h"
 
+#include <glib/gi18n-lib.h>
+
+#include "server/camel-ews-settings.h"
+#include "server/e-source-ews-folder.h"
+
 #include "e-book-config-ews.h"
 
 G_DEFINE_DYNAMIC_TYPE (
@@ -25,6 +30,37 @@ G_DEFINE_DYNAMIC_TYPE (
        e_book_config_ews,
        E_TYPE_SOURCE_CONFIG_BACKEND)
 
+static CamelEwsSettings *
+book_config_ews_get_ews_settings (ESourceConfigBackend *backend,
+                                 ESource *source)
+{
+       ESource *collection;
+       ESourceCamel *extension;
+       ESourceRegistry *registry;
+       CamelSettings *settings;
+       const gchar *extension_name;
+
+       if (!source)
+               return NULL;
+
+       registry = e_source_config_get_registry (e_source_config_backend_get_config (backend));
+
+       extension_name = e_source_camel_get_extension_name ("ews");
+       e_source_camel_generate_subtype ("ews", CAMEL_TYPE_EWS_SETTINGS);
+
+       /* The collection settings live in our parent data source. */
+       collection = e_source_registry_find_extension (registry, source, extension_name);
+       if (!collection)
+               return NULL;
+
+       extension = e_source_get_extension (collection, extension_name);
+       settings = e_source_camel_get_settings (extension);
+
+       g_object_unref (collection);
+
+       return CAMEL_EWS_SETTINGS (settings);
+}
+
 static gboolean
 book_config_ews_allow_creation (ESourceConfigBackend *backend)
 {
@@ -32,6 +68,50 @@ book_config_ews_allow_creation (ESourceConfigBackend *backend)
 }
 
 static void
+book_config_ews_insert_widgets (ESourceConfigBackend *backend,
+                               ESource *scratch_source)
+{
+       CamelEwsSettings *ews_settings;
+       ESource *source;
+       gchar *gal_uid;
+       gboolean is_gal;
+
+       if (!scratch_source || !e_source_has_extension (scratch_source, E_SOURCE_EXTENSION_EWS_FOLDER))
+               return;
+
+       source = e_source_config_get_original_source (e_source_config_backend_get_config (backend));
+       if (!source)
+               return;
+
+       ews_settings = book_config_ews_get_ews_settings (backend, source);
+       if (!ews_settings)
+               return;
+
+       gal_uid = camel_ews_settings_dup_gal_uid (ews_settings);
+       is_gal = g_strcmp0 (e_source_get_uid (source), gal_uid) == 0;
+       g_free (gal_uid);
+
+       if (is_gal) {
+               ESourceEwsFolder *ews_folder;
+               GtkWidget *checkbox;
+
+               checkbox = gtk_check_button_new_with_mnemonic (_("Use only _primary contact email address"));
+               gtk_widget_set_tooltip_text (checkbox, _("When checked, the contacts looked up in the online 
Global Address List will contain only the primary email address"));
+               gtk_widget_show (checkbox);
+
+               ews_folder = e_source_get_extension (scratch_source, E_SOURCE_EXTENSION_EWS_FOLDER);
+               gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbox), 
e_source_ews_folder_get_use_primary_address (ews_folder));
+
+               e_binding_bind_property (
+                       checkbox, "active",
+                       ews_folder, "use-primary-address",
+                       G_BINDING_DEFAULT);
+
+               e_source_config_insert_widget (e_source_config_backend_get_config (backend), scratch_source, 
NULL, checkbox);
+       }
+}
+
+static void
 e_book_config_ews_class_init (EBookConfigEwsClass *class)
 {
        EExtensionClass *extension_class;
@@ -43,6 +123,7 @@ e_book_config_ews_class_init (EBookConfigEwsClass *class)
        backend_class = E_SOURCE_CONFIG_BACKEND_CLASS (class);
        backend_class->backend_name = "ews";
        backend_class->allow_creation = book_config_ews_allow_creation;
+       backend_class->insert_widgets = book_config_ews_insert_widgets;
 }
 
 static void
diff --git a/src/server/e-source-ews-folder.c b/src/server/e-source-ews-folder.c
index 6392c23..fccf95d 100644
--- a/src/server/e-source-ews-folder.c
+++ b/src/server/e-source-ews-folder.c
@@ -33,6 +33,7 @@ struct _ESourceEwsFolderPrivate {
        gboolean is_public;
        guint freebusy_weeks_before;
        guint freebusy_weeks_after;
+       gboolean use_primary_address;
 };
 
 enum {
@@ -44,7 +45,8 @@ enum {
        PROP_FOREIGN_MAIL,
        PROP_FREEBUSY_WEEKS_BEFORE,
        PROP_FREEBUSY_WEEKS_AFTER,
-       PROP_PUBLIC
+       PROP_PUBLIC,
+       PROP_USE_PRIMARY_ADDRESS
 };
 
 G_DEFINE_TYPE (
@@ -106,6 +108,12 @@ source_ews_folder_set_property (GObject *object,
                                E_SOURCE_EWS_FOLDER (object),
                                g_value_get_boolean (value));
                        return;
+
+               case PROP_USE_PRIMARY_ADDRESS:
+                       e_source_ews_folder_set_use_primary_address (
+                               E_SOURCE_EWS_FOLDER (object),
+                               g_value_get_boolean (value));
+                       return;
        }
 
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -173,6 +181,13 @@ source_ews_folder_get_property (GObject *object,
                                e_source_ews_folder_get_public (
                                E_SOURCE_EWS_FOLDER (object)));
                        return;
+
+               case PROP_USE_PRIMARY_ADDRESS:
+                       g_value_set_boolean (
+                               value,
+                               e_source_ews_folder_get_use_primary_address (
+                               E_SOURCE_EWS_FOLDER (object)));
+                       return;
        }
 
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -313,6 +328,19 @@ e_source_ews_folder_class_init (ESourceEwsFolderClass *class)
                        G_PARAM_CONSTRUCT |
                        G_PARAM_STATIC_STRINGS |
                        E_SOURCE_PARAM_SETTING));
+
+       g_object_class_install_property (
+               object_class,
+               PROP_USE_PRIMARY_ADDRESS,
+               g_param_spec_boolean (
+                       "use-primary-address",
+                       "Use Primary Address",
+                       "Whether online GAL should use only the primary contact address",
+                       FALSE,
+                       G_PARAM_READWRITE |
+                       G_PARAM_CONSTRUCT |
+                       G_PARAM_STATIC_STRINGS |
+                       E_SOURCE_PARAM_SETTING));
 }
 
 static void
@@ -597,3 +625,25 @@ e_source_ews_folder_set_public (ESourceEwsFolder *extension,
 
        g_object_notify (G_OBJECT (extension), "public");
 }
+
+gboolean
+e_source_ews_folder_get_use_primary_address (ESourceEwsFolder *extension)
+{
+       g_return_val_if_fail (E_IS_SOURCE_EWS_FOLDER (extension), FALSE);
+
+       return extension->priv->use_primary_address;
+}
+
+void
+e_source_ews_folder_set_use_primary_address (ESourceEwsFolder *extension,
+                                            gboolean use_primary_address)
+{
+       g_return_if_fail (E_IS_SOURCE_EWS_FOLDER (extension));
+
+       if ((extension->priv->use_primary_address ? 1 : 0) == (use_primary_address ? 1 : 0))
+               return;
+
+       extension->priv->use_primary_address = use_primary_address;
+
+       g_object_notify (G_OBJECT (extension), "use-primary-address");
+}
diff --git a/src/server/e-source-ews-folder.h b/src/server/e-source-ews-folder.h
index e9c07a1..b8ea27d 100644
--- a/src/server/e-source-ews-folder.h
+++ b/src/server/e-source-ews-folder.h
@@ -104,6 +104,11 @@ void               e_source_ews_folder_set_freebusy_weeks_after
 gboolean       e_source_ews_folder_get_public  (ESourceEwsFolder *extension);
 void           e_source_ews_folder_set_public  (ESourceEwsFolder *extension,
                                                 gboolean is_public);
+gboolean       e_source_ews_folder_get_use_primary_address
+                                               (ESourceEwsFolder *extension);
+void           e_source_ews_folder_set_use_primary_address
+                                               (ESourceEwsFolder *extension,
+                                                gboolean use_primary_address);
 
 G_END_DECLS
 


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