xeme/xemestream.c

Mon, 13 May 2024 23:31:43 -0500

author
Elliott Sales de Andrade <quantum.analyst@gmail.com>
date
Mon, 13 May 2024 23:31:43 -0500
changeset 48
5d0707ab10a2
parent 41
cb874e13b5ff
permissions
-rw-r--r--

Use g_set_str everywhere

This simplifies some setters.

Testing Done:
Ran `ninja turtles`

Reviewed at https://reviews.imfreedom.org/r/3181/

/*
 * Copyright (C) 2023 Xeme Developers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include "xemecore.h"
#include "xemestream.h"
#include "xemestring.h"

enum {
	PROP_0,
	PROP_CANCELLABLE,
	PROP_ID,
	PROP_TO,
	PROP_FROM,
	PROP_LANGUAGE,
	PROP_VERSION,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

typedef struct {
	GCancellable *cancellable;

	char *id;
	char *to;
	char *from;
	char *language;
	char *version;
} XemeStreamPrivate;

G_DEFINE_TYPE_WITH_PRIVATE(XemeStream, xeme_stream, G_TYPE_OBJECT)

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
xeme_stream_set_cancellable(XemeStream *stream, GCancellable *cancellable) {
	XemeStreamPrivate *priv = NULL;

	g_return_if_fail(XEME_IS_STREAM(stream));

	priv = xeme_stream_get_instance_private(stream);

	if(g_set_object(&priv->cancellable, cancellable)) {
		g_object_notify_by_pspec(G_OBJECT(stream),
		                         properties[PROP_CANCELLABLE]);
	}
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
xeme_stream_finalize(GObject *obj) {
	XemeStream *stream = XEME_STREAM(obj);
	XemeStreamPrivate *priv = NULL;

	priv = xeme_stream_get_instance_private(stream);

	g_clear_object(&priv->cancellable);
	g_clear_pointer(&priv->id, g_free);
	g_clear_pointer(&priv->to, g_free);
	g_clear_pointer(&priv->from, g_free);
	g_clear_pointer(&priv->language, g_free);
	g_clear_pointer(&priv->version, g_free);

	G_OBJECT_CLASS(xeme_stream_parent_class)->finalize(obj);
}

static void
xeme_stream_get_property(GObject *obj, guint param_id, GValue *value,
                         GParamSpec *pspec)
{
	XemeStream *stream = XEME_STREAM(obj);

	switch(param_id) {
	case PROP_CANCELLABLE:
		g_value_set_object(value, xeme_stream_get_cancellable(stream));
		break;
	case PROP_ID:
		g_value_set_string(value, xeme_stream_get_id(stream));
		break;
	case PROP_TO:
		g_value_set_string(value, xeme_stream_get_to(stream));
		break;
	case PROP_FROM:
		g_value_set_string(value, xeme_stream_get_from(stream));
		break;
	case PROP_LANGUAGE:
		g_value_set_string(value, xeme_stream_get_language(stream));
		break;
	case PROP_VERSION:
		g_value_set_string(value, xeme_stream_get_version(stream));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
xeme_stream_set_property(GObject *obj, guint param_id, const GValue *value,
                         GParamSpec *pspec)
{
	XemeStream *stream = XEME_STREAM(obj);

	switch(param_id) {
	case PROP_CANCELLABLE:
		xeme_stream_set_cancellable(stream, g_value_get_object(value));
		break;
	case PROP_ID:
		xeme_stream_set_id(stream, g_value_get_string(value));
		break;
	case PROP_TO:
		xeme_stream_set_to(stream, g_value_get_string(value));
		break;
	case PROP_FROM:
		xeme_stream_set_from(stream, g_value_get_string(value));
		break;
	case PROP_LANGUAGE:
		xeme_stream_set_language(stream, g_value_get_string(value));
		break;
	case PROP_VERSION:
		xeme_stream_set_version(stream, g_value_get_string(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
xeme_stream_init(XemeStream *stream) {
	/* Set our defaults. */
	xeme_stream_set_version(stream, XEME_STREAM_VERSION);
}

static void
xeme_stream_class_init(XemeStreamClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = xeme_stream_finalize;
	obj_class->get_property = xeme_stream_get_property;
	obj_class->set_property = xeme_stream_set_property;

	/**
	 * XemeStream:cancellable:
	 *
	 * The cancellable for this stream.
	 *
	 * Since: 0.1
	 */
	properties[PROP_CANCELLABLE] = g_param_spec_object(
		"cancellable", "cancellable",
		"The cancellable for this stream.",
		G_TYPE_CANCELLABLE,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

	/**
	 * XemeStream:id:
	 *
	 * The initiator of the stream.
	 *
	 * Since: 0.1
	 */
	properties[PROP_ID] = g_param_spec_string(
		"id", "id",
		"The id for the stream.",
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * XemeStream:to:
	 *
	 * The bare jid of the remote side of this stream.
	 *
	 * Since: 0.1
	 */
	properties[PROP_TO] = g_param_spec_string(
		"to", "to",
		"The bare jid of the remote side of this stream.",
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * XemeStream:from:
	 *
	 * The bare jid of the local side of this stream.
	 *
	 * Since: 0.1
	 */
	properties[PROP_FROM] = g_param_spec_string(
		"from", "from",
		"The bare jid of the local side of this stream.",
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * XemeStream:language:
	 *
	 * The language for this stream.
	 *
	 * Since: 0.1
	 */
	properties[PROP_LANGUAGE] = g_param_spec_string(
		"language", "language",
		"The language for the stream.",
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * XemeStream:version:
	 *
	 * The XMPP version for this stream.
	 *
	 * Since: 0.1
	 */
	properties[PROP_VERSION] = g_param_spec_string(
		"version", "version",
		"The XMPP version for this stream.",
		XEME_STREAM_VERSION,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
GCancellable *
xeme_stream_get_cancellable(XemeStream *stream) {
	XemeStreamPrivate *priv = NULL;

	g_return_val_if_fail(XEME_IS_STREAM(stream), NULL);

	priv = xeme_stream_get_instance_private(stream);

	return priv->cancellable;
}

const char *
xeme_stream_get_id(XemeStream *stream) {
	XemeStreamPrivate *priv = NULL;

	g_return_val_if_fail(XEME_IS_STREAM(stream), NULL);

	priv = xeme_stream_get_instance_private(stream);

	return priv->id;
}

void
xeme_stream_set_id(XemeStream *stream, const char *id) {
	XemeStreamPrivate *priv = NULL;

	g_return_if_fail(XEME_IS_STREAM(stream));

	priv = xeme_stream_get_instance_private(stream);

	if(g_set_str(&priv->id, id)) {
		g_object_notify_by_pspec(G_OBJECT(stream), properties[PROP_ID]);
	}
}

const char *
xeme_stream_get_to(XemeStream *stream) {
	XemeStreamPrivate *priv = NULL;

	g_return_val_if_fail(XEME_IS_STREAM(stream), NULL);

	priv = xeme_stream_get_instance_private(stream);

	return priv->to;
}

void
xeme_stream_set_to(XemeStream *stream, const char *to) {
	XemeStreamPrivate *priv = NULL;

	g_return_if_fail(XEME_IS_STREAM(stream));

	priv = xeme_stream_get_instance_private(stream);

	if(g_set_str(&priv->to, to)) {
		g_object_notify_by_pspec(G_OBJECT(stream), properties[PROP_TO]);
	}
}

const char *
xeme_stream_get_from(XemeStream *stream) {
	XemeStreamPrivate *priv = NULL;

	g_return_val_if_fail(XEME_IS_STREAM(stream), NULL);

	priv = xeme_stream_get_instance_private(stream);

	return priv->from;
}

void
xeme_stream_set_from(XemeStream *stream, const char *from) {
	XemeStreamPrivate *priv = NULL;

	g_return_if_fail(XEME_IS_STREAM(stream));

	priv = xeme_stream_get_instance_private(stream);

	if(g_set_str(&priv->from, from)) {
		g_object_notify_by_pspec(G_OBJECT(stream), properties[PROP_FROM]);
	}
}

const char *
xeme_stream_get_language(XemeStream *stream) {
	XemeStreamPrivate *priv = NULL;

	g_return_val_if_fail(XEME_IS_STREAM(stream), NULL);

	priv = xeme_stream_get_instance_private(stream);

	return priv->language;
}

void
xeme_stream_set_language(XemeStream *stream, const char *language) {
	XemeStreamPrivate *priv = NULL;

	g_return_if_fail(XEME_IS_STREAM(stream));

	priv = xeme_stream_get_instance_private(stream);

	if(g_set_str(&priv->language, language)) {
		g_object_notify_by_pspec(G_OBJECT(stream),
		                         properties[PROP_LANGUAGE]);
	}
}

const char *
xeme_stream_get_version(XemeStream *stream) {
	XemeStreamPrivate *priv = NULL;

	g_return_val_if_fail(XEME_IS_STREAM(stream), NULL);

	priv = xeme_stream_get_instance_private(stream);

	return priv->version;
}

void
xeme_stream_set_version(XemeStream *stream, const char *version) {
	XemeStreamPrivate *priv = NULL;

	g_return_if_fail(XEME_IS_STREAM(stream));

	priv = xeme_stream_get_instance_private(stream);
	if(g_set_str(&priv->version, version)) {
		g_object_notify_by_pspec(G_OBJECT(stream), properties[PROP_VERSION]);
	}
}

mercurial