/*
 * 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 "purpleprotocoldirectory.h"

G_DEFINE_QUARK(purple-protocol-directory-error, purple_protocol_directory_error)

/******************************************************************************
 * GInterface Implementation
 *****************************************************************************/
G_DEFINE_INTERFACE(PurpleProtocolDirectory, purple_protocol_directory,
                   PURPLE_TYPE_PROTOCOL)

static void
purple_protocol_directory_default_init(G_GNUC_UNUSED PurpleProtocolDirectoryInterface *iface)
{
}

/******************************************************************************
 * Public API
 *****************************************************************************/
gboolean
purple_protocol_directory_implements_search_contacts(PurpleProtocolDirectory *protocol)
{
	PurpleProtocolDirectoryInterface *iface = NULL;

	g_return_val_if_fail(PURPLE_IS_PROTOCOL_DIRECTORY(protocol), FALSE);

	iface = PURPLE_PROTOCOL_DIRECTORY_GET_IFACE(protocol);

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

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

	return TRUE;
}

void
purple_protocol_directory_search_contacts_async(PurpleProtocolDirectory *protocol,
                                                PurpleAccount *account,
                                                const char *search_text,
                                                GCancellable *cancellable,
                                                GAsyncReadyCallback callback,
                                                gpointer data)
{
	PurpleProtocolDirectoryInterface *iface = NULL;

	g_return_if_fail(PURPLE_IS_PROTOCOL_DIRECTORY(protocol));
	g_return_if_fail(PURPLE_IS_ACCOUNT(account));

	iface = PURPLE_PROTOCOL_DIRECTORY_GET_IFACE(protocol);
	if(iface != NULL && iface->search_contacts_async != NULL) {
		iface->search_contacts_async(protocol, account, search_text,
		                             cancellable, callback, data);
    } else {
		g_task_report_new_error(G_OBJECT(protocol), callback, data,
		                        purple_protocol_directory_search_contacts_async,
		                        PURPLE_PROTOCOL_DIRECTORY_ERROR,
		                        PURPLE_PROTOCOL_DIRECTORY_ERROR_SEARCH_CONTACTS_ASYNC_NOT_IMPLEMENTED,
		                        "%s does not implement search_contacts_async",
		                        G_OBJECT_TYPE_NAME(protocol));
    }
}

PurpleContacts *
purple_protocol_directory_search_contacts_finish(PurpleProtocolDirectory *protocol,
                                                 GAsyncResult *result,
                                                 GError **error)
{
	PurpleProtocolDirectoryInterface *iface = NULL;

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

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

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

	g_warning("purple_protocol_directory_search_contacts_finish called "
	          "without calling "
	          "purple_protocol_directory_search_contacts_async");

	return NULL;
}
