commit 5a5b0d31d7bcf97e028ee012c59cebaef9f8beab
Author: Vibha Yadav <ews@linux-qry3.site>
Date:   Wed Oct 5 11:26:28 2011 +0530

    Implementation of get delegation api

diff --git a/src/server/e-ews-connection.c b/src/server/e-ews-connection.c
index 8e02db3..2ad399c 100644
--- a/src/server/e-ews-connection.c
+++ b/src/server/e-ews-connection.c
@@ -432,8 +432,9 @@ ews_response_cb (SoupSession *session, SoupMessage *msg, gpointer data)
 			     subparam = e_soap_parameter_get_next_child (subparam)) {
 				int l = strlen ((char *)subparam->name);
 				if (l < 15 || (strcmp((char *)subparam->name + l - 15, "ResponseMessage") &&
-				    strcmp((char *)subparam->name, "FreeBusyResponse"))) {
-					g_warning ("Unexpected element '%s' in place of ResponseMessage or FreeBusyResponse",
+				    strcmp((char *)subparam->name, "FreeBusyResponse")&&
+						strcmp((char *)subparam->name, "DelegateUserResponseMessageType"))) {
+					g_warning ("Unexpected element '%s' in place of ResponseMessage or FreeBusyResponse or DelegateUserResponseMessageType",
 						   subparam->name);
 					continue;
 				}
@@ -4067,3 +4068,210 @@ e_ews_connection_get_free_busy (EEwsConnection *cnc,
 
 	return result;
 }
+
+static EwsPermissionLevel get_permission_from_string(gchar *permission)
+{
+	g_return_val_if_fail(permission != NULL, NONE);
+
+	if (!g_ascii_strcasecmp (permission, "Editor"))
+		return EDITOR;
+	else if (!g_ascii_strcasecmp (permission, "Author"))
+		return AUTHOR;
+	else if (!g_ascii_strcasecmp (permission, "Reviewer"))
+		return REVIEWER;
+	else if (!g_ascii_strcasecmp (permission, "Custom"))
+		return CUSTOM;
+	else
+		return NONE;
+
+}
+
+static void
+get_delegate_response_cb (ESoapParameter *param, EwsNode *enode)
+{
+	ESoapParameter *subparam, *node, *child;
+	EwsAsyncData *async_data;
+	EwsDelegateData *data;
+	gchar *value;
+
+	async_data = g_simple_async_result_get_op_res_gpointer (enode->simple);
+
+	node = e_soap_parameter_get_first_child_by_name (param, "DelegateUser");
+
+	data = g_new(EwsDelegateData, 1);
+	data->user_id = g_new0(EwsUserId, 1);
+	if(data->user_id == NULL)
+		g_print("NULL value");
+
+	subparam = e_soap_parameter_get_first_child_by_name(node, "UserId");
+
+	/*Parse User Id*/
+	
+	child = e_soap_parameter_get_first_child_by_name(subparam, "SID");
+	data->user_id->sid = g_strdup(e_soap_parameter_get_string_value(child));
+
+	child = e_soap_parameter_get_first_child_by_name(subparam, "PrimarySmtpAddress");
+	data->user_id->primary_smtp_add = g_strdup(e_soap_parameter_get_string_value(child));
+
+	child = e_soap_parameter_get_first_child_by_name(subparam, "DisplayName");
+	data->user_id->display_name = g_strdup(e_soap_parameter_get_string_value(child));
+
+	subparam = e_soap_parameter_get_first_child_by_name(node, "DelegatePermissions");
+	/*Parse Delegate Permissions*/
+
+	child = e_soap_parameter_get_first_child_by_name(subparam, "CalendarFolderPermissionLevel");
+	data->calendar = get_permission_from_string(e_soap_parameter_get_string_value(child));
+
+	child = e_soap_parameter_get_first_child_by_name(subparam, "ContactsFolderPermissionLevel");
+	data->contact = get_permission_from_string(e_soap_parameter_get_string_value(child));
+
+	child = e_soap_parameter_get_first_child_by_name(subparam, "InboxFolderPermissionLevel");
+	data->inbox = get_permission_from_string(e_soap_parameter_get_string_value(child));
+
+	child = e_soap_parameter_get_first_child_by_name(subparam, "TasksFolderPermissionLevel");
+	data->tasks = get_permission_from_string(e_soap_parameter_get_string_value(child));
+
+	child = e_soap_parameter_get_first_child_by_name(subparam, "NotesFolderPermissionLevel");
+	data->notes = get_permission_from_string(e_soap_parameter_get_string_value(child));
+
+	child = e_soap_parameter_get_first_child_by_name(subparam, "JournalFolderPermissionLevel");
+	data->journal = get_permission_from_string(e_soap_parameter_get_string_value(child));
+
+	subparam = e_soap_parameter_get_first_child_by_name(node, "ReceiveCopiesOfMeetingMessages");
+
+	value = e_soap_parameter_get_string_value(subparam);
+	if(!g_ascii_strcasecmp(value, "true"))
+		data->meetingcopies = TRUE;
+	else
+		data->meetingcopies = FALSE;
+
+	subparam = e_soap_parameter_get_first_child_by_name(node, "ViewPrivateItems");
+
+	value = e_soap_parameter_get_string_value(subparam);
+	if(!g_ascii_strcasecmp(value, "true"))
+		data->view_priv_items = TRUE;
+	else
+		data->view_priv_items = FALSE;
+
+	async_data->items = g_slist_append (async_data->items, data);
+	async_data->items_created = g_slist_append (async_data->items_created, data);
+
+	return;
+}
+
+
+/**
+ * e_ews_connection_get_delegate_start
+ * @cnc:
+ * @pri:
+ * @mail_id: mail is for which delegate is requested
+ * @include permission: "true", "false"
+ * @delete_type: "HardDelete", "SoftDelete", "MoveToDeletedItems"
+ * @cb:
+ * @cancellable:
+ * @user_data:
+ **/
+void
+e_ews_connection_get_delegate_start	(EEwsConnection *cnc,
+					 gint pri,
+					 const gchar *mail_id,
+					 const gchar *include_permissions,
+					 GAsyncReadyCallback cb,
+					 GCancellable *cancellable,
+					 gpointer user_data)
+{
+	ESoapMessage *msg;
+	GSimpleAsyncResult *simple;
+	EwsAsyncData *async_data;
+
+	msg = e_ews_message_new_with_header (cnc->priv->uri, "GetDelegate", "IncludePermissions", include_permissions, EWS_EXCHANGE_2007_SP1);
+
+	e_soap_message_start_element (msg, "Mailbox", "messages", NULL);
+
+	e_ews_message_write_string_parameter (msg, "EmailAddress", NULL, mail_id);
+
+	e_soap_message_end_element (msg);
+
+	e_ews_message_write_footer (msg);
+
+      	simple = g_simple_async_result_new (G_OBJECT (cnc),
+                                      cb,
+				      user_data,
+                                      e_ews_connection_get_delegate_start);
+
+	async_data = g_new0 (EwsAsyncData, 1);
+	g_simple_async_result_set_op_res_gpointer (
+		simple, async_data, (GDestroyNotify) async_data_free);
+
+	ews_connection_queue_request (cnc, msg, get_delegate_response_cb, pri, cancellable, simple, cb == ews_sync_reply_cb);
+}
+
+
+gboolean
+e_ews_connection_get_delegate_finish	(EEwsConnection *cnc,
+					 GAsyncResult *result,
+					 EwsDelegateData **get_delegate,
+					 GError **error)
+{
+	GSimpleAsyncResult *simple;
+	EwsAsyncData *async_data;
+
+	g_return_val_if_fail (
+		g_simple_async_result_is_valid (
+		result, G_OBJECT (cnc), e_ews_connection_get_delegate_start),
+		FALSE);
+
+	simple = G_SIMPLE_ASYNC_RESULT (result);
+	async_data = g_simple_async_result_get_op_res_gpointer (simple);
+
+	if (g_simple_async_result_propagate_error (simple, error))
+		return FALSE;
+	*get_delegate = (EwsDelegateData *) async_data->items_created->data;
+	g_slist_free (async_data->items_created);
+
+	return TRUE;
+}
+
+gboolean
+/**
+ * e_ews_connection_get_delegate
+ * @cnc:
+ * @pri:
+ * @mail_id: mail id for which delegate requested
+ * @include_permissions: "true", "false"
+ * @cancellable:
+ * @error:
+ **/
+e_ews_connection_get_delegate	(EEwsConnection *cnc,
+				 gint pri,
+				 const gchar *mail_id,
+				 const gchar *include_permissions,
+				 EwsDelegateData **get_delegate,
+				 GCancellable *cancellable,
+				 GError **error)
+{
+	EwsSyncData *sync_data;
+	gboolean result;
+
+	sync_data = g_new0 (EwsSyncData, 1);
+	sync_data->eflag = e_flag_new ();
+
+	e_ews_connection_get_delegate_start	(cnc, pri, mail_id,
+						 include_permissions,
+						 ews_sync_reply_cb,
+						 cancellable,
+						 (gpointer) sync_data);
+
+	e_flag_wait (sync_data->eflag);
+
+	result = e_ews_connection_get_delegate_finish (cnc, sync_data->res,
+							get_delegate, error);
+
+	e_flag_free (sync_data->eflag);
+	g_object_unref (sync_data->res);
+	g_free (sync_data);
+
+	return result;
+
+}
+
diff --git a/src/server/e-ews-connection.h b/src/server/e-ews-connection.h
index 03d4860..1da569e 100644
--- a/src/server/e-ews-connection.h
+++ b/src/server/e-ews-connection.h
@@ -115,6 +115,30 @@ typedef struct {
 	gchar *filename;
 } EwsOALDetails;
 
+typedef struct{
+	gchar *sid;
+	gchar *primary_smtp_add;
+	gchar *display_name;
+	gchar *distinguished_user;
+	gchar *external_user;
+}EwsUserId;
+
+typedef enum{
+	NONE,
+	EDITOR,
+	REVIEWER,
+	AUTHOR,
+	CUSTOM
+}EwsPermissionLevel;
+
+typedef struct{
+	EwsUserId *user_id;
+	EwsPermissionLevel calendar, contact, inbox, tasks, notes, journal;
+	gboolean meetingcopies;
+	gboolean view_priv_items;
+}EwsDelegateData;
+
+
 GType		e_ews_connection_get_type	(void);
 EEwsConnection *e_ews_connection_new		(const gchar *uri,
 						 const gchar *username,
@@ -664,6 +688,29 @@ gboolean	e_ews_connection_download_oal_file
 						 gpointer progress_data,
 						 GCancellable *cancellable,
 						 GError **error);
+/*Get Delegation*/
+void e_ews_connection_get_delegate_start	(EEwsConnection *cnc,
+						 gint pri,
+						 const gchar *mail_id,
+						 const gchar *include_permissions,
+						 GAsyncReadyCallback cb,
+						 GCancellable *cancellable,
+						 gpointer user_data);
+gboolean e_ews_connection_get_delegate_finish	(EEwsConnection *cnc,
+						 GAsyncResult *result,
+						 EwsDelegateData **get_delegate,
+						 GError **error);
+gboolean e_ews_connection_get_delegate	
+						(EEwsConnection *cnc,
+						 gint pri,
+						 const gchar *mail_id,
+						 const gchar *include_permissions,
+						 EwsDelegateData **get_delegate,
+						 GCancellable *cancellable,
+						 GError **error);
+
+
+
 
 G_END_DECLS
 
diff --git a/src/server/tests/Makefile.am b/src/server/tests/Makefile.am
index 3551727..848772e 100644
--- a/src/server/tests/Makefile.am
+++ b/src/server/tests/Makefile.am
@@ -20,10 +20,11 @@ testews_SOURCES = 				\
 		test-connection.c		\
 		test-syncfolder.c		\
 		test-cuditem.c 			\
-		test-getattachment.c 		\
+		test-getattachment.c		\
 		test-autocompletion.c		\
 		test-createfolder.c		\
 		test-deletefolder.c		\
+		test-getdelegate.c		\
 		test-libews.c 			\
 		utils.h
 
diff --git a/src/server/tests/test-getdelegate.c b/src/server/tests/test-getdelegate.c
new file mode 100644
index 0000000..59b0f64
--- /dev/null
+++ b/src/server/tests/test-getdelegate.c
@@ -0,0 +1,108 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Authors : Vibha Yadav <yvibha@novell.com>
+ *
+ * Copyright (C) 1999-2011 Novell, Inc. (www.novell.com)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program 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 program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
+ * USA
+ */
+
+/* API : e_ews_connection_create_folders */
+
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <glib.h>
+#include <glib/gprintf.h>
+#include "utils.h"
+#include <e-ews-connection.h>
+#include <e-ews-folder.h>
+#include <e-ews-message.h>
+
+void get_delegate_tests_run ();
+
+static GMainLoop *main_loop;
+
+EwsFolderId *folder_id;
+
+static void
+get_delegate_cb (GObject *object, GAsyncResult *res, gpointer data)
+{
+	EEwsConnection *cnc = E_EWS_CONNECTION (object);
+	GError *error = NULL;
+	EwsDelegateData *get_delegate;
+
+	e_ews_connection_get_delegate_finish	(cnc, res, &get_delegate,
+						 &error);
+
+	if (error != NULL) {
+		g_warning ("Unable to get delegate: %s \n", error->message);
+		g_clear_error (&error);
+		goto quit;
+	}
+	if(get_delegate !=NULL && get_delegate->user_id != NULL)
+		g_print ("Delegate is %s", get_delegate->user_id->display_name);
+
+
+quit:
+	g_main_loop_quit(main_loop);
+}
+
+static void
+op_test_get_delegate ()
+{
+	const gchar *username;
+	const gchar *password;
+	const gchar *uri;
+	EEwsConnection *cnc;
+	GCancellable *cancellable;
+
+	cancellable = g_cancellable_new ();
+
+	util_get_login_info_from_env (&username, &password, &uri);
+	g_assert_cmpstr (username, !=, NULL);
+	g_assert_cmpstr (password, !=, NULL);
+	g_assert_cmpstr (uri, !=, NULL);
+
+	cnc = e_ews_connection_new (uri, username, password, NULL, NULL, NULL);
+	g_assert (cnc != NULL);
+	e_ews_connection_get_delegate_start	(cnc, EWS_PRIORITY_MEDIUM, "vibha@g3.com",
+						 "true" ,
+						 get_delegate_cb,
+						 cancellable, NULL);
+
+}
+
+static gboolean
+idle_cb (gpointer data)
+{
+	op_test_get_delegate();
+	return FALSE;
+}
+
+void
+get_delegate_tests_run ()
+{
+	g_type_init ();
+	g_thread_init (NULL);
+
+	main_loop = g_main_loop_new (NULL, TRUE);
+	g_idle_add ((GSourceFunc) idle_cb, NULL);
+	g_main_loop_run (main_loop);
+
+	/* terminate */
+	g_main_loop_unref (main_loop);
+}
+
diff --git a/src/server/tests/test-libews.c b/src/server/tests/test-libews.c
index bbcd26a..0f33032 100644
--- a/src/server/tests/test-libews.c
+++ b/src/server/tests/test-libews.c
@@ -14,6 +14,7 @@ void autocompletion_tests_run ();
 void createfolder_tests_run ();
 void deletefolder_tests_run (gconstpointer data);
 void get_attachments_tests_run ();
+void get_delegate_tests_run ();
 
 static void
 finalize_test_data ()
@@ -43,6 +44,7 @@ int main (int argc, char *argv[])
 	g_test_add_data_func ("/libews/deletefolder", &folder_id, deletefolder_tests_run);
 
 	g_test_add_func ("/libews/cuditem", cuditem_tests_run);
+	g_test_add_func ("/libews/getdelegate", get_delegate_tests_run);
 
 	g_test_add_func ("/libews/getattachment", get_attachments_tests_run);
 
