commit e407363fa954d6e5d8f8b2d65113ba812a3b5f0a
Author: Johnny Jacob <jjohnny@novell.com>
Date:   Wed Jun 24 18:32:21 2009 +0530

    Bug 585615 – Exchange MAPI account setup gets wrong userid in database
    
        * exchange-mapi-account-setup.c (create_profile_callback): Added. Callback for
        e.*create_profile. If ProcessNetworkProfile finds more than one match for
        username, a pop dialog allows the user to select the right one.
    
        * exchange-mapi-connection.c (exchange_mapi_create_profile): Added support
        for ProcessNetworkProfile callback data.

	Modified src/account-setup-eplugin/ChangeLog
diff --git a/src/account-setup-eplugin/ChangeLog b/src/account-setup-eplugin/ChangeLog
index e7bf14d..0f4a445 100644
--- a/src/account-setup-eplugin/ChangeLog
+++ b/src/account-setup-eplugin/ChangeLog
@@ -1,3 +1,9 @@
+2009-06-24  Johnny Jacob  <jjohnny@novell.com>
+
+	* exchange-mapi-account-setup.c (create_profile_callback): Callback for
+	e.*create_profile. If ProcessNetworkProfile finds more than one match for
+	username, a pop dialog allows the user to select the right one.
+
 2009-03-19  Milan Crha  <mcrha@redhat.com>
 
 	** Fix for bug #574784
	Modified src/account-setup-eplugin/exchange-mapi-account-listener.c
diff --git a/src/account-setup-eplugin/exchange-mapi-account-listener.c b/src/account-setup-eplugin/exchange-mapi-account-listener.c
index 206826c..3e613f5 100644
--- a/src/account-setup-eplugin/exchange-mapi-account-listener.c
+++ b/src/account-setup-eplugin/exchange-mapi-account-listener.c
@@ -598,7 +598,7 @@ create_profile_entry (CamelURL *url)
 		g_free (key);
 
 		if (password)
-			status = exchange_mapi_create_profile (url->user, password, camel_url_get_param (url, "domain"), url->host, NULL);
+		  status = exchange_mapi_create_profile (url->user, password, camel_url_get_param (url, "domain"), url->host, NULL, NULL, NULL);
 
 		++attempts; 
 	}
	Modified src/account-setup-eplugin/exchange-mapi-account-setup.c
diff --git a/src/account-setup-eplugin/exchange-mapi-account-setup.c b/src/account-setup-eplugin/exchange-mapi-account-setup.c
index 19349f3..ecb14d6 100644
--- a/src/account-setup-eplugin/exchange-mapi-account-setup.c
+++ b/src/account-setup-eplugin/exchange-mapi-account-setup.c
@@ -1,3 +1,4 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -94,6 +95,79 @@ exchange_mapi_accounts_peek_config_listener ()
 	return config_listener; 
 }
 
+enum {
+  COL_MAPI_NAME = 0,
+  COL_MAPI_INDEX,
+  COLS_MAX
+};
+
+/* Callback for ProcessNetworkProfile. If we have more than one username, 
+ we need to let the user select. */
+static uint32_t
+create_profile_callback (struct SRowSet *rowset, gpointer data)
+{
+	struct SPropValue *lpProp;
+	gint response;
+	guint32	i, index = 0;
+	GtkTreeIter iter;
+	GtkListStore *store;
+	GtkCellRenderer *renderer;
+	GtkTreeSelection *selection;
+	GtkWidget *dialog, *view;
+	GtkVBox *vbox;
+
+	/* NOTE: A good way would be display the list of username entries */
+	/* using GtkEntryCompletion in the username gtkentry. But plugins */
+	/* as of now does not have access to it */
+
+	/*TODO : Fix strings*/
+	dialog = gtk_dialog_new_with_buttons (_("Select username"),
+					      NULL, GTK_DIALOG_MODAL,
+					      GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
+					      GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
+					      NULL);
+
+	/*Tree View */
+	view = gtk_tree_view_new ();
+	renderer = gtk_cell_renderer_text_new ();
+	gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
+						     -1, _("User Name"), renderer,
+						     "text", COL_MAPI_NAME, NULL);
+
+	/* Model for TreeView */
+	store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
+	gtk_tree_view_set_model (GTK_TREE_VIEW (view), GTK_TREE_MODEL (store));
+
+	for (i = 0; i < rowset->cRows; i++) {
+		lpProp = get_SPropValue_SRow(&(rowset->aRow[i]), PR_DISPLAY_NAME);
+		if (lpProp && lpProp->value.lpszA) {
+		  gtk_list_store_append (store, &iter);
+		  /* Preserve the index inside the store*/
+		  gtk_list_store_set (store, &iter, COL_MAPI_NAME, lpProp->value.lpszA, 
+				      COL_MAPI_INDEX, i, -1);
+		}
+	}
+
+	/* Pack the TreeView into dialog's content area */
+	vbox = (GtkVBox *)gtk_dialog_get_content_area (GTK_DIALOG (dialog));
+	gtk_box_pack_start (GTK_BOX (vbox), view, TRUE, TRUE, 6);
+	gtk_widget_show_all (GTK_WIDGET (vbox));
+
+	response = gtk_dialog_run (GTK_DIALOG (dialog));
+	if (response == GTK_RESPONSE_ACCEPT) {
+	       /* Get the index from the selected value */
+		selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
+		gtk_tree_selection_get_selected (selection, GTK_TREE_MODEL (&store), &iter);
+		gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, COL_MAPI_INDEX, 
+				    &index, -1);
+	} else /* If we return a value > available, we are canceling the login.*/
+	       index = rowset->cRows + 1;
+
+	gtk_widget_destroy (dialog);
+
+	return index;
+}
+
 static void
 validate_credentials (GtkWidget *widget, EConfig *config)
 {
@@ -126,7 +200,10 @@ validate_credentials (GtkWidget *widget, EConfig *config)
 	/*Can there be a account without password ?*/
 	if (password && *password && domain_name && *domain_name && *url->user && *url->host) {
 		char *error_msg = NULL;
-		gboolean status = exchange_mapi_create_profile (url->user, password, domain_name, url->host, &error_msg);
+		gboolean status = exchange_mapi_create_profile (url->user, password, domain_name,
+								url->host, &error_msg, 
+								(mapi_profile_callback_t) create_profile_callback,
+								NULL);
 		if (status) {
 			/* Things are successful */
 			gchar *profname = NULL, *uri = NULL; 
	Modified src/libexchangemapi/exchange-mapi-connection.c
diff --git a/src/libexchangemapi/exchange-mapi-connection.c b/src/libexchangemapi/exchange-mapi-connection.c
index a7cf929..83045f3 100644
--- a/src/libexchangemapi/exchange-mapi-connection.c
+++ b/src/libexchangemapi/exchange-mapi-connection.c
@@ -2880,8 +2880,11 @@ manage_mapi_error (const char *context, uint32_t error_id, char **error_msg)
 	}
 }
 
+
 gboolean
-exchange_mapi_create_profile (const char *username, const char *password, const char *domain, const char *server, char **error_msg)
+exchange_mapi_create_profile (const char *username, const char *password, const char *domain,
+			      const char *server, char **error_msg,
+			      mapi_profile_callback_t callback, gpointer data)
 {
 	enum MAPISTATUS	retval;
 	gboolean result = FALSE; 
@@ -2928,7 +2931,7 @@ exchange_mapi_create_profile (const char *username, const char *password, const
 
 	/* Delete any existing profiles with the same profilename */
 	retval = DeleteProfile(profname); 
-	/* don't bother to check error - it would be valid if we got an error */
+	/* don't bother to check errolar - it would be valid if we got an error */
 
 	retval = CreateProfile(profname, username, password, 0); 
 	if (retval != MAPI_E_SUCCESS) {
@@ -2956,14 +2959,14 @@ exchange_mapi_create_profile (const char *username, const char *password, const
 			manage_mapi_error ("DeleteProfile", GetLastError(), error_msg);
 		goto cleanup; 
 	}
-	d(g_print("succeeded \n"));
+	d(g_print("MapiLogonProvider : succeeded \n"));
 
-	retval = ProcessNetworkProfile(session, username, NULL, NULL); 
+	retval = ProcessNetworkProfile(session, username, callback, NULL); 
 	if (retval != MAPI_E_SUCCESS) {
 		manage_mapi_error ("ProcessNetworkProfile", GetLastError(), error_msg);
 		goto cleanup; 
 	}
-
+	d(g_print("ProcessNetworkProfile : succeeded \n"));
 	/* Set it as the default profile. Is this needed? */
 	retval = SetDefaultProfile(profname); 
 	if (retval != MAPI_E_SUCCESS) {
	Modified src/libexchangemapi/exchange-mapi-connection.h
diff --git a/src/libexchangemapi/exchange-mapi-connection.h b/src/libexchangemapi/exchange-mapi-connection.h
index ca13543..365ae28 100644
--- a/src/libexchangemapi/exchange-mapi-connection.h
+++ b/src/libexchangemapi/exchange-mapi-connection.h
@@ -188,7 +188,9 @@ uint32_t
 exchange_mapi_util_create_named_prop (uint32_t olFolder, mapi_id_t fid, 
 				      const char *named_prop_name, uint32_t ptype);
 
-gboolean exchange_mapi_create_profile (const char *username, const char *password, const char *domain, const char *server, char **error_msg);
+gboolean exchange_mapi_create_profile (const char *username, const char *password,
+				       const char *domain, const char *server,
+				       char **error_msg, mapi_profile_callback_t cb, gpointer data);
 gboolean exchange_mapi_delete_profile (const char *profile);
 
 #endif

