pidgin/pidginconversationmember.c

Sat, 09 Aug 2025 18:12:31 +0800

author
Gong Zhile <gongzl@stu.hebust.edu.cn>
date
Sat, 09 Aug 2025 18:12:31 +0800
branch
gir-dependency
changeset 43305
4ede49515766
parent 43224
323118ff41da
permissions
-rw-r--r--

Add builtin library dependency for introspection

Without specifying, gir defaults to the system pidgin/purple libraries by default,
which fails the build when new symbols were added and gir failed to link for them.

/*
 * Pidgin - Internet Messenger
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Pidgin 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 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 "pidginconversationmember.h"

#include "pidgincolor.h"
#include "pidgincontactinfomenu.h"

struct _PidginConversationMember {
	GtkBox parent;

	PurpleConversationMember *conversation_member;
};

enum {
	PROP_0,
	PROP_CONVERSATION_MEMBER,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static char *
pidgin_conversation_member_get_name_for_display(G_GNUC_UNUSED PidginConversationMember *self,
                                                PurpleConversationMember *conversation_member,
                                                G_GNUC_UNUSED gpointer data)
{
	const char *name_for_display = "";

	if(PURPLE_IS_CONVERSATION_MEMBER(conversation_member)) {
		name_for_display = purple_conversation_member_get_name_for_display(conversation_member);
	}

	return g_strdup(name_for_display);
}

static PangoAttrList *
pidgin_conversation_member_attributes(G_GNUC_UNUSED PidginConversationMember *self,
                                      PurpleConversationMember *conversation_member,
                                      G_GNUC_UNUSED gpointer data)
{
	GdkRGBA rgba;
	PangoAttribute *attr = NULL;
	PangoAttrList *attrs = NULL;
	gboolean color_valid = FALSE;
	const char *custom_color = NULL;

	if(!PURPLE_IS_CONVERSATION_MEMBER(conversation_member)) {
		return NULL;
	}

	custom_color = purple_conversation_member_get_color(conversation_member);

	if(!purple_strempty(custom_color)) {
		color_valid = gdk_rgba_parse(&rgba, custom_color);
	}

	if(!color_valid) {
		const char *name_for_display = NULL;

		name_for_display = purple_conversation_member_get_name_for_display(conversation_member);

		pidgin_color_calculate_for_text(name_for_display, &rgba);
	}

	attrs = pango_attr_list_new();
	attr = pango_attr_foreground_new(0xFFFF * rgba.red,
	                                 0xFFFF * rgba.green,
	                                 0xFFFF * rgba.blue);
	pango_attr_list_insert(attrs, attr);

	return attrs;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PidginConversationMember, pidgin_conversation_member,
                    GTK_TYPE_BOX)

static void
pidgin_conversation_member_finalize(GObject *obj) {
	PidginConversationMember *member = PIDGIN_CONVERSATION_MEMBER(obj);

	g_clear_object(&member->conversation_member);

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

static void
pidgin_conversation_member_get_property(GObject *obj, guint param_id,
                                        GValue *value, GParamSpec *pspec)
{
	PidginConversationMember *member = PIDGIN_CONVERSATION_MEMBER(obj);

	switch(param_id) {
	case PROP_CONVERSATION_MEMBER:
		g_value_set_object(value,
		                   pidgin_conversation_member_get_conversation_member(member));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
pidgin_conversation_member_set_property(GObject *obj, guint param_id,
                                        const GValue *value, GParamSpec *pspec)
{
	PidginConversationMember *member = PIDGIN_CONVERSATION_MEMBER(obj);

	switch(param_id) {
	case PROP_CONVERSATION_MEMBER:
		pidgin_conversation_member_set_conversation_member(member,
		                                                   g_value_get_object(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
pidgin_conversation_member_init(PidginConversationMember *member) {
	gtk_widget_init_template(GTK_WIDGET(member));
}

static void
pidgin_conversation_member_class_init(PidginConversationMemberClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	obj_class->finalize = pidgin_conversation_member_finalize;
	obj_class->get_property = pidgin_conversation_member_get_property;
	obj_class->set_property = pidgin_conversation_member_set_property;

	/**
	 * PidginConversationMember:conversation-member:
	 *
	 * The conversation member which is being displayed.
	 *
	 * Since: 3.0
	 */
	properties[PROP_CONVERSATION_MEMBER] = g_param_spec_object(
		"conversation-member", NULL, NULL,
		PURPLE_TYPE_CONVERSATION_MEMBER,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

	gtk_widget_class_set_template_from_resource(widget_class,
	                                            "/im/pidgin/Pidgin3/conversationmember.ui");

	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_member_get_name_for_display);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_member_attributes);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
GtkWidget *
pidgin_conversation_member_new(PurpleConversationMember *conversation_member) {
	return g_object_new(
		PIDGIN_TYPE_CONVERSATION_MEMBER,
		"conversation-member", conversation_member,
		NULL);
}

PurpleConversationMember *
pidgin_conversation_member_get_conversation_member(PidginConversationMember *member)
{
	g_return_val_if_fail(PIDGIN_IS_CONVERSATION_MEMBER(member), NULL);

	return member->conversation_member;
}

void
pidgin_conversation_member_set_conversation_member(PidginConversationMember *member,
                                                   PurpleConversationMember *conversation_member)
{
	g_return_if_fail(PIDGIN_IS_CONVERSATION_MEMBER(member));

	if(g_set_object(&member->conversation_member, conversation_member)) {
		g_object_notify_by_pspec(G_OBJECT(member),
		                         properties[PROP_CONVERSATION_MEMBER]);
	}
}

mercurial