/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here. Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * 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 General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include "purpleprotocolcontacts.h"

#include "util.h"

/******************************************************************************
 * GInterface Implementation
 *****************************************************************************/
G_DEFINE_INTERFACE(PurpleProtocolContacts, purple_protocol_contacts,
                   PURPLE_TYPE_PROTOCOL)

static void
purple_protocol_contacts_default_init(G_GNUC_UNUSED PurpleProtocolContactsInterface *iface)
{
}

/******************************************************************************
 * Public API
 *****************************************************************************/
gboolean
purple_protocol_contacts_implements_get_profile(PurpleProtocolContacts *protocol)
{
	PurpleProtocolContactsInterface *iface = NULL;

	g_return_val_if_fail(PURPLE_IS_PROTOCOL_CONTACTS(protocol), FALSE);

	iface = PURPLE_PROTOCOL_CONTACTS_GET_IFACE(protocol);
	if(iface == NULL) {
		return FALSE;
	}

	if(iface->get_profile_async == NULL) {
		return FALSE;
	}

	if(iface->get_profile_finish == NULL) {
		return FALSE;
	}

	return TRUE;
}

void
purple_protocol_contacts_get_profile_async(PurpleProtocolContacts *protocol,
                                           PurpleContact *contact,
                                           GCancellable *cancellable,
                                           GAsyncReadyCallback callback,
                                           gpointer data)
{
	PurpleProtocolContactsInterface *iface = NULL;

	g_return_if_fail(PURPLE_IS_PROTOCOL_CONTACTS(protocol));
	g_return_if_fail(PURPLE_IS_CONTACT(contact));

	iface = PURPLE_PROTOCOL_CONTACTS_GET_IFACE(protocol);
	if(iface != NULL && iface->get_profile_async != NULL) {
		iface->get_profile_async(protocol, contact, cancellable, callback,
		                         data);
	} else {
		g_task_report_new_error(G_OBJECT(protocol), callback, data,
		                        purple_protocol_contacts_get_profile_async,
		                        PURPLE_PROTOCOL_CONTACTS_DOMAIN, 0,
		                        "%s does not implement get_profile_async",
		                        G_OBJECT_TYPE_NAME(protocol));
	}
}

char *
purple_protocol_contacts_get_profile_finish(PurpleProtocolContacts *protocol,
                                            GAsyncResult *result,
                                            GError **error)
{
	PurpleProtocolContactsInterface *iface = NULL;

	g_return_val_if_fail(PURPLE_IS_PROTOCOL_CONTACTS(protocol), NULL);
	g_return_val_if_fail(G_IS_ASYNC_RESULT(result), NULL);

	if(g_async_result_is_tagged(result,
	                            purple_protocol_contacts_get_profile_async))
	{
		return g_task_propagate_pointer(G_TASK(result), error);
	}

	iface = PURPLE_PROTOCOL_CONTACTS_GET_IFACE(protocol);
	if(iface != NULL && iface->get_profile_finish != NULL) {
		return iface->get_profile_finish(protocol, result, error);
	}

	g_warning("purple_protocol_contacts_get_profile_finish without calling "
	          "purple_protocol_contacts_get_profile_async");

	return NULL;
}

gboolean
purple_protocol_contacts_implements_get_action_menu(PurpleProtocolContacts *protocol)
{
	PurpleProtocolContactsInterface *iface = NULL;

	g_return_val_if_fail(PURPLE_IS_PROTOCOL_CONTACTS(protocol), FALSE);

	iface = PURPLE_PROTOCOL_CONTACTS_GET_IFACE(protocol);
	if(iface == NULL) {
		return FALSE;
	}

	if(iface->get_action_menu == NULL) {
		return FALSE;
	}

	return TRUE;
}

BirbActionMenu *
purple_protocol_contacts_get_action_menu(PurpleProtocolContacts *protocol,
                                         PurpleContact *contact)
{
	PurpleProtocolContactsInterface *iface = NULL;

	g_return_val_if_fail(PURPLE_IS_PROTOCOL_CONTACTS(protocol), NULL);
	g_return_val_if_fail(PURPLE_IS_CONTACT(contact), NULL);

	iface = PURPLE_PROTOCOL_CONTACTS_GET_IFACE(protocol);
	if(iface != NULL && iface->get_action_menu != NULL) {
		return iface->get_action_menu(protocol, contact);
	}

	return NULL;
}

gboolean
purple_protocol_contacts_implements_set_alias(PurpleProtocolContacts *protocol)
{
	PurpleProtocolContactsInterface *iface = NULL;

	g_return_val_if_fail(PURPLE_IS_PROTOCOL_CONTACTS(protocol), FALSE);

	iface = PURPLE_PROTOCOL_CONTACTS_GET_IFACE(protocol);
	if(iface == NULL) {
		return FALSE;
	}

	if(iface->set_alias_async == NULL) {
		return FALSE;
	}

	if(iface->set_alias_finish == NULL) {
		return FALSE;
	}

	return TRUE;
}

void
purple_protocol_contacts_set_alias_async(PurpleProtocolContacts *protocol,
                                         PurpleContact *contact,
                                         const char *alias,
                                         GCancellable *cancellable,
                                         GAsyncReadyCallback callback,
                                         gpointer data)
{
	PurpleProtocolContactsInterface *iface = NULL;

	g_return_if_fail(PURPLE_IS_PROTOCOL_CONTACTS(protocol));
	g_return_if_fail(PURPLE_IS_CONTACT(contact));

	iface = PURPLE_PROTOCOL_CONTACTS_GET_IFACE(protocol);
	if(iface != NULL && iface->set_alias_async != NULL) {
		iface->set_alias_async(protocol, contact, alias, cancellable, callback,
		                       data);
	} else {
		g_task_report_new_error(G_OBJECT(protocol), callback, data,
		                        purple_protocol_contacts_set_alias_async,
		                        PURPLE_PROTOCOL_CONTACTS_DOMAIN, 0,
		                        "%s does not implement set_alias_async",
		                        G_OBJECT_TYPE_NAME(protocol));
	}
}

gboolean
purple_protocol_contacts_set_alias_finish(PurpleProtocolContacts *protocol,
                                          GAsyncResult *result,
                                          GError **error)
{
	PurpleProtocolContactsInterface *iface = NULL;

	g_return_val_if_fail(PURPLE_IS_PROTOCOL_CONTACTS(protocol), FALSE);
	g_return_val_if_fail(G_IS_ASYNC_RESULT(result), FALSE);

	if(g_async_result_is_tagged(result,
	                            purple_protocol_contacts_set_alias_async))
	{
		return g_task_propagate_boolean(G_TASK(result), error);
	}

	iface = PURPLE_PROTOCOL_CONTACTS_GET_IFACE(protocol);
	if(iface != NULL && iface->set_alias_finish != NULL) {
		return iface->set_alias_finish(protocol, result, error);
	}

	g_warning("purple_protocol_contacts_set_alias_finish without calling "
	          "purple_protocol_contacts_set_alias_async");

	return FALSE;
}
