/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * This program 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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib/gi18n-lib.h>

#include "purplebonjourconnection.h"

#include "purplebonjourconstants.h"
#include "purplebonjourcore.h"

struct _PurpleBonjourConnection {
	PurpleConnection parent;

	GSocketService *service;
};

G_DEFINE_DYNAMIC_TYPE_EXTENDED(PurpleBonjourConnection, purple_bonjour_connection,
                               PURPLE_TYPE_CONNECTION, G_TYPE_FLAG_FINAL, {})

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static gboolean
purple_bonjour_connection_incoming_cb(G_GNUC_UNUSED GSocketService *service,
                                      GSocketConnection *socket_connection,
                                      G_GNUC_UNUSED GObject *source_object,
                                      G_GNUC_UNUSED gpointer data)
{
	GError *error = NULL;

	if(!g_io_stream_close(G_IO_STREAM(socket_connection), NULL, &error)) {
		g_warning("failed to close stream: %s", error->message);
	}

	g_clear_error(&error);

	return TRUE;
}

/******************************************************************************
 * PurpleConnection Implementation
 *****************************************************************************/
static gboolean
purple_bonjour_connection_connect(PurpleConnection *purple_connection,
                                  GError **error)
{
	PurpleBonjourConnection *connection = NULL;
	PurpleAccount *account = NULL;
	gboolean success = FALSE;

	g_return_val_if_fail(PURPLE_BONJOUR_IS_CONNECTION(purple_connection),
	                     FALSE);

	connection = PURPLE_BONJOUR_CONNECTION(purple_connection);
	account = purple_connection_get_account(purple_connection);

	/* Create the service and add the listeners. */
	connection->service = g_socket_service_new();

	success = g_socket_listener_add_any_inet_port(G_SOCKET_LISTENER(connection->service),
	                                              G_OBJECT(account),
	                                              error);
	if(!success) {
		g_clear_object(&connection->service);

		return FALSE;
	}

	g_signal_connect_object(connection->service,
	                        "incoming",
	                        G_CALLBACK(purple_bonjour_connection_incoming_cb),
	                        connection,
	                        G_CONNECT_DEFAULT);

	/* We added our listener successfully, so start the service. */
	g_socket_service_start(connection->service);

	purple_account_ready(account);

	return TRUE;
}

static gboolean
purple_bonjour_connection_disconnect(PurpleConnection *purple_connection,
                                     G_GNUC_UNUSED GError **error)
{
	PurpleBonjourConnection *connection = NULL;

	g_return_val_if_fail(PURPLE_BONJOUR_IS_CONNECTION(purple_connection),
	                     FALSE);

	connection = PURPLE_BONJOUR_CONNECTION(purple_connection);

	if(G_IS_SOCKET_SERVICE(connection->service)) {
		g_socket_service_stop(connection->service);
		g_socket_listener_close(G_SOCKET_LISTENER(connection->service));
		g_clear_object(&connection->service);
	}

	return TRUE;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
purple_bonjour_connection_init(G_GNUC_UNUSED PurpleBonjourConnection *connection) {
}

static void
purple_bonjour_connection_class_init(PurpleBonjourConnectionClass *klass) {
	PurpleConnectionClass *connection_class = PURPLE_CONNECTION_CLASS(klass);

	connection_class->connect = purple_bonjour_connection_connect;
	connection_class->disconnect = purple_bonjour_connection_disconnect;
}

static void
purple_bonjour_connection_class_finalize(G_GNUC_UNUSED PurpleBonjourConnectionClass *klass) {
}

/******************************************************************************
 * Internal API
 *****************************************************************************/
void
purple_bonjour_connection_register(GPluginNativePlugin *plugin) {
	purple_bonjour_connection_register_type(G_TYPE_MODULE(plugin));
}
