[evolution-ews] Bug 750564 - Make server settings lookup extensible



commit 7bb721c93be7b58b648a17bbdf76fdb1f2d9ac8b
Author: Milan Crha <mcrha redhat com>
Date:   Thu Aug 3 12:44:16 2017 +0200

    Bug 750564 - Make server settings lookup extensible

 po/POTFILES.in                                |    1 +
 src/configuration/CMakeLists.txt              |    2 +
 src/configuration/e-ews-config-lookup.c       |  236 +++++++++++++++++++++++++
 src/configuration/e-ews-config-lookup.h       |   29 +++
 src/configuration/e-mail-config-ews-backend.c |   12 ++
 src/configuration/module-ews-configuration.c  |    2 +
 6 files changed, 282 insertions(+), 0 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index fd41e5c..50a2671 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -11,6 +11,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-ews-config-lookup.c
 src/configuration/e-ews-config-utils.c
 src/configuration/e-ews-edit-folder-permissions.c
 src/configuration/e-ews-ooo-notificator.c
diff --git a/src/configuration/CMakeLists.txt b/src/configuration/CMakeLists.txt
index ad684df..b6a298f 100644
--- a/src/configuration/CMakeLists.txt
+++ b/src/configuration/CMakeLists.txt
@@ -27,6 +27,8 @@ set(sources
        e-mail-config-ews-offline-options.h
        e-mail-config-ews-ooo-page.c
        e-mail-config-ews-ooo-page.h
+       e-ews-config-lookup.c
+       e-ews-config-lookup.h
        e-ews-config-ui-extension.c
        e-ews-config-ui-extension.h
        e-ews-config-utils.c
diff --git a/src/configuration/e-ews-config-lookup.c b/src/configuration/e-ews-config-lookup.c
new file mode 100644
index 0000000..48bf3f8
--- /dev/null
+++ b/src/configuration/e-ews-config-lookup.c
@@ -0,0 +1,236 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2017 Red Hat, Inc. (www.redhat.com)
+ *
+ * This library is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "evolution-ews-config.h"
+
+#include <glib/gi18n-lib.h>
+
+#include <e-util/e-util.h>
+
+#include "server/camel-ews-settings.h"
+#include "server/e-ews-connection.h"
+#include "e-ews-config-lookup.h"
+
+/* Standard GObject macros */
+#define E_TYPE_EWS_CONFIG_LOOKUP \
+       (e_ews_config_lookup_get_type ())
+#define E_EWS_CONFIG_LOOKUP(obj) \
+       (G_TYPE_CHECK_INSTANCE_CAST \
+       ((obj), E_TYPE_EWS_CONFIG_LOOKUP, EEwsConfigLookup))
+#define E_EWS_CONFIG_LOOKUP_CLASS(cls) \
+       (G_TYPE_CHECK_CLASS_CAST \
+       ((cls), E_TYPE_EWS_CONFIG_LOOKUP, EEwsConfigLookupClass))
+#define E_IS_EWS_CONFIG_LOOKUP(obj) \
+       (G_TYPE_CHECK_INSTANCE_TYPE \
+       ((obj), E_TYPE_EWS_CONFIG_LOOKUP))
+#define E_IS_EWS_CONFIG_LOOKUP_CLASS(cls) \
+       (G_TYPE_CHECK_CLASS_TYPE \
+       ((cls), E_TYPE_EWS_CONFIG_LOOKUP))
+#define E_EWS_CONFIG_LOOKUP_GET_CLASS(obj) \
+       (G_TYPE_INSTANCE_GET_CLASS \
+       ((obj), E_TYPE_EWS_CONFIG_LOOKUP, EEwsConfigLookupClass))
+
+typedef struct _EEwsConfigLookup EEwsConfigLookup;
+typedef struct _EEwsConfigLookupClass EEwsConfigLookupClass;
+
+struct _EEwsConfigLookup {
+       EExtension parent;
+};
+
+struct _EEwsConfigLookupClass {
+       EExtensionClass parent_class;
+};
+
+GType e_ews_config_lookup_get_type (void) G_GNUC_CONST;
+
+G_DEFINE_DYNAMIC_TYPE (EEwsConfigLookup, e_ews_config_lookup, E_TYPE_EXTENSION)
+
+static void
+ews_config_lookup_thread (EConfigLookup *config_lookup,
+                         const ENamedParameters *params,
+                         gpointer user_data,
+                         GCancellable *cancellable)
+{
+       CamelEwsSettings *ews_settings = NULL;
+       ESource *source;
+       const gchar *email_address;
+       const gchar *password;
+       const gchar *extension_name;
+
+       g_return_if_fail (E_IS_CONFIG_LOOKUP (config_lookup));
+       g_return_if_fail (params != NULL);
+
+       email_address = e_named_parameters_get (params, E_CONFIG_LOOKUP_PARAM_EMAIL_ADDRESS);
+
+       if (!email_address || !*email_address)
+               return;
+
+       ews_settings = g_object_new (CAMEL_TYPE_EWS_SETTINGS, NULL);
+       camel_ews_settings_set_email (ews_settings, email_address);
+
+       extension_name = e_source_camel_get_extension_name ("ews");
+       source = e_config_lookup_get_source (config_lookup, E_CONFIG_LOOKUP_SOURCE_COLLECTION);
+       if (source && e_source_has_extension (source, extension_name)) {
+               ESourceCamel *camel_extension;
+               CamelSettings *settings;
+
+               camel_extension = e_source_get_extension (source, extension_name);
+               settings = e_source_camel_get_settings (camel_extension);
+
+               /* Copy only the host url */
+               if (CAMEL_IS_EWS_SETTINGS (settings))
+                       camel_ews_settings_set_hosturl (ews_settings, camel_ews_settings_get_hosturl 
(CAMEL_EWS_SETTINGS (settings)));
+       }
+
+       password = e_named_parameters_get (params, E_CONFIG_LOOKUP_PARAM_PASSWORD);
+
+       if (password &&
+           e_ews_autodiscover_ws_url_sync (ews_settings, email_address, password, cancellable, NULL)) {
+               const gchar *url;
+
+               url = camel_ews_settings_get_hosturl (ews_settings);
+               if (url && *url) {
+                       EConfigLookupResult *lookup_result;
+                       GString *description;
+                       gchar *tmp, *ptr, *user;
+                       SoupURI *suri;
+
+                       tmp = g_strdup (email_address);
+                       ptr = tmp ? strchr (tmp, '@') : NULL;
+                       if (ptr)
+                               *ptr = '\0';
+
+                       if (!tmp || !*tmp) {
+                               g_free (tmp);
+                               tmp = NULL;
+                       }
+
+                       user = tmp;
+
+                       suri = soup_uri_new (url);
+
+                       description = g_string_new ("");
+
+                       if (user && *user)
+                               g_string_append_printf (description, _("User: %s"), user);
+
+                       if (description->len)
+                               g_string_append_c (description, '\n');
+
+                       g_string_append_printf (description, _("Host URL: %s"), url);
+
+                       url = camel_ews_settings_get_oaburl (ews_settings);
+                       if (url && *url) {
+                               g_string_append_c (description, '\n');
+                               g_string_append_printf (description, _("OAB URL: %s"), url);
+                       }
+
+                       lookup_result = e_config_lookup_result_simple_new (E_CONFIG_LOOKUP_RESULT_COLLECTION,
+                               E_CONFIG_LOOKUP_RESULT_PRIORITY_IMAP - 100, /* This is better than IMAP */
+                               "ews",
+                               _("Exchange Web Services"),
+                               description->str);
+
+                       e_config_lookup_result_simple_add_string (lookup_result, extension_name,
+                               "hosturl", camel_ews_settings_get_hosturl (ews_settings));
+
+                       e_config_lookup_result_simple_add_string (lookup_result, extension_name,
+                               "oaburl", camel_ews_settings_get_oaburl (ews_settings));
+
+                       if (user && *user) {
+                               e_config_lookup_result_simple_add_string (lookup_result,
+                                       E_SOURCE_EXTENSION_AUTHENTICATION,
+                                       "user", user);
+                       }
+
+                       if (suri && suri->host && *suri->host) {
+                               e_config_lookup_result_simple_add_string (lookup_result,
+                                       E_SOURCE_EXTENSION_AUTHENTICATION,
+                                       "host", suri->host);
+                       }
+
+                       e_config_lookup_add_result (config_lookup, lookup_result);
+
+                       g_string_free (description, TRUE);
+                       g_free (user);
+                       if (suri)
+                               soup_uri_free (suri);
+               }
+       }
+
+       g_clear_object (&ews_settings);
+}
+
+static void
+ews_config_lookup_run_cb (EConfigLookup *config_lookup,
+                         const ENamedParameters *params,
+                         EActivity *activity,
+                         gpointer user_data)
+{
+       g_return_if_fail (E_IS_CONFIG_LOOKUP (config_lookup));
+       g_return_if_fail (E_IS_EWS_CONFIG_LOOKUP (user_data));
+       g_return_if_fail (E_IS_ACTIVITY (activity));
+
+       e_config_lookup_create_thread (config_lookup, params, activity,
+               ews_config_lookup_thread, NULL, NULL);
+}
+
+static void
+ews_config_lookup_constructed (GObject *object)
+{
+       EConfigLookup *config_lookup;
+
+       /* Chain up to parent's method. */
+       G_OBJECT_CLASS (e_ews_config_lookup_parent_class)->constructed (object);
+
+       config_lookup = E_CONFIG_LOOKUP (e_extension_get_extensible (E_EXTENSION (object)));
+
+       g_signal_connect (config_lookup, "run",
+               G_CALLBACK (ews_config_lookup_run_cb), object);
+}
+
+static void
+e_ews_config_lookup_class_init (EEwsConfigLookupClass *class)
+{
+       GObjectClass *object_class;
+       EExtensionClass *extension_class;
+
+       object_class = G_OBJECT_CLASS (class);
+       object_class->constructed = ews_config_lookup_constructed;
+
+       extension_class = E_EXTENSION_CLASS (class);
+       extension_class->extensible_type = E_TYPE_CONFIG_LOOKUP;
+}
+
+static void
+e_ews_config_lookup_class_finalize (EEwsConfigLookupClass *class)
+{
+}
+
+static void
+e_ews_config_lookup_init (EEwsConfigLookup *extension)
+{
+}
+
+void
+e_ews_config_lookup_type_register (GTypeModule *type_module)
+{
+       /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
+        *     function, so we have to wrap it with a public function in
+        *     order to register types from a separate compilation unit. */
+       e_ews_config_lookup_register_type (type_module);
+}
diff --git a/src/configuration/e-ews-config-lookup.h b/src/configuration/e-ews-config-lookup.h
new file mode 100644
index 0000000..8f29c4a
--- /dev/null
+++ b/src/configuration/e-ews-config-lookup.h
@@ -0,0 +1,29 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2017 Red Hat, Inc. (www.redhat.com)
+ *
+ * This library is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef E_EWS_CONFIG_LOOKUP_H
+#define E_EWS_CONFIG_LOOKUP_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+void e_ews_config_lookup_type_register (GTypeModule *type_module);
+
+G_END_DECLS
+
+#endif /* E_EWS_CONFIG_LOOKUP_H */
diff --git a/src/configuration/e-mail-config-ews-backend.c b/src/configuration/e-mail-config-ews-backend.c
index e36b7ea..3882ce4 100644
--- a/src/configuration/e-mail-config-ews-backend.c
+++ b/src/configuration/e-mail-config-ews-backend.c
@@ -350,6 +350,17 @@ mail_config_ews_backend_setup_defaults (EMailConfigServiceBackend *backend)
 }
 
 static gboolean
+mail_config_ews_backend_auto_configure (EMailConfigServiceBackend *backend,
+                                       EConfigLookup *config_lookup,
+                                       gint *out_priority)
+{
+       return e_mail_config_service_backend_auto_configure_for_kind (backend, config_lookup,
+               E_CONFIG_LOOKUP_RESULT_COLLECTION, NULL,
+               e_mail_config_service_backend_get_collection (backend),
+               out_priority);
+}
+
+static gboolean
 mail_config_ews_backend_check_complete (EMailConfigServiceBackend *backend)
 {
        EMailConfigServicePage *page;
@@ -437,6 +448,7 @@ e_mail_config_ews_backend_class_init (EMailConfigEwsBackendClass *class)
        backend_class->new_collection = mail_config_ews_backend_new_collection;
        backend_class->insert_widgets = mail_config_ews_backend_insert_widgets;
        backend_class->setup_defaults = mail_config_ews_backend_setup_defaults;
+       backend_class->auto_configure = mail_config_ews_backend_auto_configure;
        backend_class->check_complete = mail_config_ews_backend_check_complete;
        backend_class->commit_changes = mail_config_ews_backend_commit_changes;
 }
diff --git a/src/configuration/module-ews-configuration.c b/src/configuration/module-ews-configuration.c
index 0267e6f..935648c 100644
--- a/src/configuration/module-ews-configuration.c
+++ b/src/configuration/module-ews-configuration.c
@@ -32,6 +32,7 @@
 #include "e-mail-config-ews-ooo-page.h"
 #include "e-mail-config-ews-folder-sizes-page.h"
 #include "e-ews-ooo-notificator.h"
+#include "e-ews-config-lookup.h"
 
 #include "e-ews-config-ui-extension.h"
 #include "server/e-source-ews-folder.h"
@@ -57,6 +58,7 @@ e_module_load (GTypeModule *type_module)
        e_mail_config_ews_delegates_page_type_register (type_module);
        e_mail_config_ews_ooo_page_type_register (type_module);
        e_mail_config_ews_folder_sizes_page_type_register (type_module);
+       e_ews_config_lookup_type_register (type_module);
        e_ews_config_ui_extension_type_register (type_module);
        e_ews_ooo_notificator_type_register (type_module);
 


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