Index: src/libexchangemapi/exchange-mapi-connection.c
===================================================================
--- src/libexchangemapi/exchange-mapi-connection.c.orig
+++ src/libexchangemapi/exchange-mapi-connection.c
@@ -64,6 +64,56 @@ static GStaticRecMutex connect_lock = G_
 #define STREAM_ACCESS_WRITE     0x0001
 #define STREAM_ACCESS_READWRITE 0x0002
 
+
+static gboolean
+ensure_mapi_init_called (void)
+{
+	static gboolean called = FALSE;
+	gchar *profpath;
+	enum MAPISTATUS status;
+
+	LOCK ();
+	if (called) {
+		UNLOCK ();
+		return TRUE;
+	}
+
+	profpath = g_build_filename (g_get_home_dir (), DEFAULT_PROF_PATH, NULL);
+
+	if (!g_file_test (profpath, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
+		/* Create a ProfileStore */
+		status = CreateProfileStore (profpath, LIBMAPI_LDIF_DIR);
+		if (status != MAPI_E_SUCCESS && (status != MAPI_E_NO_ACCESS || !g_file_test (profpath, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) {
+			mapi_errstr ("CreateProfileStore", GetLastError());
+			g_free (profpath);
+
+			UNLOCK ();
+			return FALSE;
+		}
+	}
+
+	status = MAPIInitialize (profpath);
+	if (status == MAPI_E_SESSION_LIMIT) {
+		/* do nothing, the profile store is already initialized */
+		/* but this shouldn't happen */
+		mapi_errstr ("MAPIInitialize", GetLastError());
+	} else if (status != MAPI_E_SUCCESS) {
+		mapi_errstr ("MAPIInitialize", GetLastError());
+		g_free (profpath);
+
+		UNLOCK ();
+		return FALSE;
+	}
+
+	g_free (profpath);
+
+	called = TRUE;
+	UNLOCK ();
+
+	return TRUE;
+}
+
+
 static struct mapi_session *
 mapi_profile_load (const char *profname, const char *password, 
 		   const char *username, const char *server,
@@ -71,36 +121,15 @@ mapi_profile_load (const char *profname,
 {
 	enum MAPISTATUS	retval = MAPI_E_SUCCESS;
 	struct mapi_session *session = NULL;
-	gchar *profpath = NULL;
 	gchar *default_profile_name = NULL;
 	const char *profile = NULL;
 	guint32 debug_log_level = 0;
 
 	d(g_print("\n%s: Entering %s ", G_STRLOC, G_STRFUNC));
 
-	profpath = g_build_filename (g_get_home_dir(), DEFAULT_PROF_PATH, NULL);
-	if (!g_file_test (profpath, G_FILE_TEST_EXISTS)) {
-		g_warning ("\nMAPI profile database @ %s not found ", profpath);
-		/* Note : We reach here when there is a account created and 
-		 ~/.evolution is removed. So we try to recreate mapi profile
-		 with available information */
-		if ((username && *username) || (domain && *domain) ||
-		    (server && *server))
-			exchange_mapi_create_profile (username, password, domain,
-						      server, NULL, NULL, NULL);
-		else
-			goto cleanup;
-	}
-
-	MAPIUninitialize ();
-
-	retval = MAPIInitialize(profpath);
-	if (retval != MAPI_E_SUCCESS) {
-		mapi_errstr("MAPIInitialize", GetLastError());
-		if (retval == MAPI_E_SESSION_LIMIT)
-			g_print("\n%s: %s: Already connected ", G_STRLOC, G_STRFUNC);
+	if (!ensure_mapi_init_called ())
 		goto cleanup;
-	}
+
 
 	if (g_getenv ("MAPI_DEBUG")) {
 		debug_log_level = atoi (g_getenv ("MAPI_DEBUG"));
@@ -127,12 +156,6 @@ mapi_profile_load (const char *profname,
 	}
 
 cleanup:
-	if (retval != MAPI_E_SUCCESS && retval != MAPI_E_SESSION_LIMIT &&
-	    retval != MAPI_E_LOGON_FAILED)
-		MAPIUninitialize ();
-
-	g_free (profpath);
-
 	d(g_print("\n%s: Leaving %s ", G_STRLOC, G_STRFUNC));
 
 	return session;
@@ -171,13 +194,27 @@ exchange_mapi_connection_new_full (const
 }
 
 void
-exchange_mapi_connection_close ()
+exchange_mapi_connection_close (void)
 {
 	LOCK();
+
+	if (global_mapi_session) {
+		mapi_object_t obj_store;
+		enum MAPISTATUS status;
+
+		mapi_object_init (&obj_store);
+
+		/* Open the message store */
+		status = OpenMsgStore (global_mapi_session, &obj_store);
+		if (status != MAPI_E_SUCCESS) {
+			mapi_errstr ("OpenMsgStore", GetLastError());
+		} else {
+			Logoff (&obj_store);
+		}
+	}
 	global_mapi_session = NULL;
-	MAPIUninitialize ();	
+
 	UNLOCK();
-	/* TODO :  Return status. get last error ? */
 }
 
 static gboolean 
@@ -3053,7 +3090,7 @@ exchange_mapi_create_profile (const char
 	enum MAPISTATUS	retval;
 	gboolean result = FALSE; 
 	const gchar *workstation = "localhost";
-	gchar *profname = NULL, *profpath = NULL;
+	gchar *profname = NULL;
 	struct mapi_session *session = NULL;
 
 	/*We need all the params before proceeding.*/
@@ -3064,31 +3101,9 @@ exchange_mapi_create_profile (const char
 
 	LOCK ();
 
-	profpath = g_build_filename (g_get_home_dir(), DEFAULT_PROF_PATH, NULL);
 	profname = g_strdup_printf("%s@%s", username, domain);
 
-	if (!g_file_test (profpath, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
-		/* Create a ProfileStore */
-		retval = CreateProfileStore (profpath, LIBMAPI_LDIF_DIR); 
-		if (retval != MAPI_E_SUCCESS) {
-			manage_mapi_error ("CreateProfileStore", GetLastError(), error_msg);
-			g_free (profpath);
-			g_free (profname);
-			
-			UNLOCK ();
-			return FALSE;
-		}
-	}
-
-	retval = MAPIInitialize(profpath); 
-	if (retval == MAPI_E_SESSION_LIMIT)
-	/* do nothing, the profile store is already initialized */
-		manage_mapi_error ("MAPIInitialize", GetLastError(), error_msg); 
-	else if (retval != MAPI_E_SUCCESS) {
-		manage_mapi_error ("MAPIInitialize", GetLastError(), error_msg);
-		g_free (profpath);
-		g_free (profname);
-		
+	if (!ensure_mapi_init_called ()) {
 		UNLOCK ();
 		return FALSE;
 	}
@@ -3151,13 +3166,9 @@ exchange_mapi_create_profile (const char
 	} else 
 		goto exit;
 
-cleanup: 
-	if (!result)
-		MAPIUninitialize ();
-
-exit:
+ cleanup: 
+ exit:
 	g_free (profname);
-	g_free (profpath);
 
 	UNLOCK ();
 
@@ -3169,23 +3180,10 @@ exchange_mapi_delete_profile (const char
 {
 	enum MAPISTATUS	retval;
 	gboolean result = FALSE; 
-	gchar *profpath = NULL;
 
 	LOCK ();
 
-	profpath = g_build_filename (g_get_home_dir(), DEFAULT_PROF_PATH, NULL);
-	if (!g_file_test (profpath, G_FILE_TEST_EXISTS)) {
-		g_warning ("No need to delete profile. DB itself is missing \n");
-		result = TRUE;
-		goto cleanup; 
-	}
-
-	retval = MAPIInitialize(profpath); 
-	if (retval == MAPI_E_SESSION_LIMIT)
-	/* do nothing, the profile store is already initialized */
-		; 
-	else if (retval != MAPI_E_SUCCESS) {
-		mapi_errstr("MAPIInitialize", GetLastError());
+	if (!ensure_mapi_init_called ()) {
 		goto cleanup; 
 	}
 
@@ -3200,8 +3198,6 @@ exchange_mapi_delete_profile (const char
 	result = TRUE; 
 
 cleanup: 
-	g_free(profpath);
-
 	UNLOCK ();
 
 	return result;
