<commit_before>/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */

#include "eas-sync-msg.h"
#include "eas-update-calendar-req.h"



G_DEFINE_TYPE (EasUpdateCalendarReq, eas_update_calendar_req, EAS_TYPE_REQUEST_BASE);

#define EAS_UPDATE_CALENDAR_REQ_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), EAS_TYPE_UPDATE_CALENDAR_REQ, EasUpdateCalendarReqPrivate))

struct _EasUpdateCalendarReqPrivate
{
    EasSyncMsg* sync_msg;
    gchar* account_id;
    gchar* sync_key;
    EasItemType item_type;
    gchar* folder_id;
    GSList *serialised_calendar;
};

static void
eas_update_calendar_req_init (EasUpdateCalendarReq *object)
{
    /* initialization code */
    EasUpdateCalendarReqPrivate *priv;
    g_debug ("eas_update_calendar_req_init++");

    object->priv = priv = EAS_UPDATE_CALENDAR_REQ_PRIVATE (object);

    priv->sync_msg = NULL;
    priv->account_id = NULL;
    priv->sync_key = NULL;
    priv->item_type = EAS_ITEM_LAST;
    priv->folder_id = NULL;
    priv->serialised_calendar = NULL;

    eas_request_base_SetRequestType (&object->parent_instance,
                                     EAS_REQ_UPDATE_CALENDAR);

    g_debug ("eas_update_calendar_req_init++");
}

static void
eas_update_calendar_req_dispose (GObject *object)
{
    EasUpdateCalendarReq *req = (EasUpdateCalendarReq *) object;
    EasUpdateCalendarReqPrivate *priv = req->priv;

    g_debug ("eas_update_calendar_req_dispose++");

	if (priv->sync_msg)
	{
		g_object_unref (priv->sync_msg);
		priv->sync_msg = NULL;
	}

    G_OBJECT_CLASS (eas_update_calendar_req_parent_class)->dispose (object);

    g_debug ("eas_update_calendar_req_dispose--");
}

static void
eas_update_calendar_req_finalize (GObject *object)
{
    EasUpdateCalendarReq *req = (EasUpdateCalendarReq *) object;
    EasUpdateCalendarReqPrivate *priv = req->priv;

    g_debug ("eas_update_calendar_req_finalize++");

    g_free (priv->account_id);

    G_OBJECT_CLASS (eas_update_calendar_req_parent_class)->finalize (object);

    g_debug ("eas_update_calendar_req_finalize--");
}

static void
eas_update_calendar_req_class_init (EasUpdateCalendarReqClass *klass)
{
    GObjectClass* object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (klass, sizeof (EasUpdateCalendarReqPrivate));

    object_class->finalize = eas_update_calendar_req_finalize;
    object_class->dispose = eas_update_calendar_req_dispose;

    g_debug ("eas_update_calendar_req_class_init--");
}


// TODO - update this to take a GSList of serialised calendars? rem to copy the list
EasUpdateCalendarReq *eas_update_calendar_req_new (const gchar* account_id, 
                                                   const gchar *sync_key, 
                                                   const EasItemType item_type, 
                                                   const gchar *folder_id, 
                                                   const GSList* serialised_calendar, 
                                                   DBusGMethodInvocation *context)
{
    EasUpdateCalendarReq* self = g_object_new (EAS_TYPE_UPDATE_CALENDAR_REQ, NULL);
    EasUpdateCalendarReqPrivate *priv = self->priv;

    g_debug ("eas_update_calendar_req_new++");

    priv->sync_key = g_strdup (sync_key);
    priv->folder_id = g_strdup (folder_id);
    priv->serialised_calendar = (GSList *) serialised_calendar;
    priv->account_id = g_strdup (account_id);
    priv->item_type = item_type;

    eas_request_base_SetContext (&self->parent_instance, context);

    g_debug ("eas_update_calendar_req_new--");
    return self;
}

gboolean 
eas_update_calendar_req_Activate (EasUpdateCalendarReq *self, GError **error)
{
    EasUpdateCalendarReqPrivate *priv = self->priv;
    xmlDoc *doc = NULL;
	gboolean success = FALSE;

    g_debug ("eas_update_calendar_req_Activate++");
    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	g_debug ("eas_update_calendar_req_Activate1");
    //create sync msg object
    priv->sync_msg = eas_sync_msg_new (priv->sync_key, priv->account_id, priv->folder_id, priv->item_type);

	g_debug ("eas_update_calendar_req_Activate2");
    //build request msg
    doc = eas_sync_msg_build_message (priv->sync_msg, FALSE, NULL, priv->serialised_calendar, NULL);

	g_debug ("eas_update_calendar_req_Activate3");
    success = eas_connection_send_request (eas_request_base_GetConnection (&self->parent_instance),
                                           "Sync",
                                           doc,
                                           (struct _EasRequestBase *) self,
                                           error);

	g_debug ("eas_update_calendar_req_Activate4");
	if(!success)
	{
		g_assert(error == NULL || (!success && *error != NULL));
	}

    g_debug ("eas_update_calendar_req_Activate--");
	return success;
}


gboolean eas_update_calendar_req_MessageComplete (EasUpdateCalendarReq *self, 
                                              xmlDoc* doc, 
                                              GError* error)
{
	GError *local_error = NULL;
    EasUpdateCalendarReqPrivate *priv = self->priv;
	gchar *ret_sync_key = NULL;
    g_debug ("eas_update_calendar_req_MessageComplete++");

	if (error)
	{
		local_error = error;
		goto finish;
	}

    if (FALSE == eas_sync_msg_parse_response (priv->sync_msg, doc, &local_error))
	{
		goto finish;
	}
	ret_sync_key = g_strdup (eas_sync_msg_get_syncKey (priv->sync_msg));

finish:
	if (local_error)
    {
        dbus_g_method_return_error (eas_request_base_GetContext (&self->parent_instance), local_error);
        g_error_free (local_error);
    }
    else
    {
        dbus_g_method_return (eas_request_base_GetContext (&self->parent_instance),
                              ret_sync_key);
    }
	// We must always free doc and release the semaphore
    xmlFree (doc);
    g_free(ret_sync_key);

    g_debug ("eas_update_calendar_req_MessageComplete--");
	return TRUE;
}

<commit_msg>Code review changes to eas-update-calendar-req<commit_after>/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */

#include "eas-sync-msg.h"
#include "eas-update-calendar-req.h"


G_DEFINE_TYPE (EasUpdateCalendarReq, eas_update_calendar_req, EAS_TYPE_REQUEST_BASE);

#define EAS_UPDATE_CALENDAR_REQ_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), EAS_TYPE_UPDATE_CALENDAR_REQ, EasUpdateCalendarReqPrivate))

struct _EasUpdateCalendarReqPrivate
{
    EasSyncMsg* sync_msg;
    gchar* account_id;
    gchar* sync_key;
    EasItemType item_type;
    gchar* folder_id;
    GSList *serialised_calendar;
};

static void
eas_update_calendar_req_init (EasUpdateCalendarReq *object)
{
    /* initialization code */
    EasUpdateCalendarReqPrivate *priv;
    g_debug ("eas_update_calendar_req_init++");

    object->priv = priv = EAS_UPDATE_CALENDAR_REQ_PRIVATE (object);

    priv->sync_msg = NULL;
    priv->account_id = NULL;
    priv->sync_key = NULL;
    priv->item_type = EAS_ITEM_LAST;
    priv->folder_id = NULL;
    priv->serialised_calendar = NULL;

    eas_request_base_SetRequestType (&object->parent_instance,
                                     EAS_REQ_UPDATE_CALENDAR);

    g_debug ("eas_update_calendar_req_init++");
}

static void
eas_update_calendar_req_dispose (GObject *object)
{
    EasUpdateCalendarReq *req = (EasUpdateCalendarReq *) object;
    EasUpdateCalendarReqPrivate *priv = req->priv;

    g_debug ("eas_update_calendar_req_dispose++");

	if (priv->sync_msg)
	{
		g_object_unref (priv->sync_msg);
		priv->sync_msg = NULL;
	}

    G_OBJECT_CLASS (eas_update_calendar_req_parent_class)->dispose (object);

    g_debug ("eas_update_calendar_req_dispose--");
}

static void
eas_update_calendar_req_finalize (GObject *object)
{
    EasUpdateCalendarReq *req = (EasUpdateCalendarReq *) object;
    EasUpdateCalendarReqPrivate *priv = req->priv;

    g_debug ("eas_update_calendar_req_finalize++");

    g_free (priv->account_id);

    G_OBJECT_CLASS (eas_update_calendar_req_parent_class)->finalize (object);

    g_debug ("eas_update_calendar_req_finalize--");
}

static void
eas_update_calendar_req_class_init (EasUpdateCalendarReqClass *klass)
{
    GObjectClass* object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (klass, sizeof (EasUpdateCalendarReqPrivate));

    object_class->finalize = eas_update_calendar_req_finalize;
    object_class->dispose = eas_update_calendar_req_dispose;

    g_debug ("eas_update_calendar_req_class_init--");
}


// TODO - update this to take a GSList of serialised calendars? rem to copy the list
EasUpdateCalendarReq *eas_update_calendar_req_new (const gchar* account_id, 
                                                   const gchar *sync_key, 
                                                   const EasItemType item_type, 
                                                   const gchar *folder_id, 
                                                   const GSList* serialised_calendar, 
                                                   DBusGMethodInvocation *context)
{
    EasUpdateCalendarReq* self = g_object_new (EAS_TYPE_UPDATE_CALENDAR_REQ, NULL);
    EasUpdateCalendarReqPrivate *priv = self->priv;

    g_debug ("eas_update_calendar_req_new++");

	g_assert (sync_key);
    g_assert (folder_id);
    g_assert (serialised_calendar);
	
    priv->sync_key = g_strdup (sync_key);
    priv->folder_id = g_strdup (folder_id);
    priv->serialised_calendar = (GSList *) serialised_calendar;
    priv->account_id = g_strdup (account_id);
    priv->item_type = item_type;

    eas_request_base_SetContext (&self->parent_instance, context);

    g_debug ("eas_update_calendar_req_new--");
    return self;
}

gboolean 
eas_update_calendar_req_Activate (EasUpdateCalendarReq *self, GError **error)
{
    EasUpdateCalendarReqPrivate *priv = self->priv;
    xmlDoc *doc = NULL;
	gboolean success = FALSE;

    g_debug ("eas_update_calendar_req_Activate++");
    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	g_debug ("eas_update_calendar_req_Activate1");
    //create sync msg object
    priv->sync_msg = eas_sync_msg_new (priv->sync_key, priv->account_id, priv->folder_id, priv->item_type);

	g_debug ("eas_update_calendar_req_Activate2");
    //build request msg
    doc = eas_sync_msg_build_message (priv->sync_msg, FALSE, NULL, priv->serialised_calendar, NULL);

	g_debug ("eas_update_calendar_req_Activate3");
    success = eas_connection_send_request (eas_request_base_GetConnection (&self->parent_instance),
                                           "Sync",
                                           doc,
                                           (struct _EasRequestBase *) self,
                                           error);

	g_debug ("eas_update_calendar_req_Activate4");
	if(!success)
	{
		g_assert(error == NULL || (!success && *error != NULL));
	}

    g_debug ("eas_update_calendar_req_Activate--");
	return success;
}


gboolean eas_update_calendar_req_MessageComplete (EasUpdateCalendarReq *self, 
                                              xmlDoc* doc, 
                                              GError* error)
{
	GError *local_error = NULL;
    EasUpdateCalendarReqPrivate *priv = self->priv;
	gchar *ret_sync_key = NULL;
    g_debug ("eas_update_calendar_req_MessageComplete++");

	if (error)
	{
		local_error = error;
		goto finish;
	}

    if (FALSE == eas_sync_msg_parse_response (priv->sync_msg, doc, &local_error))
	{
		goto finish;
	}
	ret_sync_key = g_strdup (eas_sync_msg_get_syncKey (priv->sync_msg));

finish:
	if (local_error)
    {
        dbus_g_method_return_error (eas_request_base_GetContext (&self->parent_instance), local_error);
        g_error_free (local_error);
    }
    else
    {
        dbus_g_method_return (eas_request_base_GetContext (&self->parent_instance),
                              ret_sync_key);
    }
	// We must always free doc and release the semaphore
    xmlFreeDoc (doc);
    g_free(ret_sync_key);

    g_debug ("eas_update_calendar_req_MessageComplete--");
	return TRUE;
}


{'fraction_non_alphanumeric': 0.09622908515774578, 'fraction_numerical': 0.0009989178390077415, 'mean_word_length': 3.075305291723202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5552938', 'n_tokens_mistral': 3914, 'n_tokens_neox': 3753, 'n_words': 684}