pidgin/pidginconversation.c

Thu, 05 Dec 2024 21:27:35 -0600

author
Gary Kramlich <grim@reaperworld.com>
date
Thu, 05 Dec 2024 21:27:35 -0600
changeset 43099
60174e318ecc
parent 43094
4ff60371673f
child 43100
e6df74d36862
permissions
-rw-r--r--

Add a status label to conversations to show online and error status

This puts the typing label in a stack with another label that shows the status.
The visible child of the stack is controlled by whether or not the conversation
is online or has error.

Testing Done:
Tested with /r/3690

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

/*
 * 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 <glib/gi18n-lib.h>

#include <purple.h>

#include "pidginautoadjustment.h"
#include "pidgincontactinfomenu.h"
#include "pidginconversation.h"
#include "pidgininfopane.h"
#include "pidginmessage.h"

#define PIDGIN_CONVERSATION_DATA ("pidgin-conversation")

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

struct _PidginConversation {
	GtkBox parent;

	PurpleConversation *conversation;

	GtkWidget *info_pane;
	GtkWidget *history;
	GtkAdjustment *history_adjustment;

	GtkCustomSorter *memberlist_sorter;

	GtkWidget *input;

	GtkWidget *typing_label;
	GtkWidget *status_label;
};

G_DEFINE_FINAL_TYPE(PidginConversation, pidgin_conversation, GTK_TYPE_BOX)

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
pidgin_conversation_set_conversation(PidginConversation *conversation,
                                     PurpleConversation *purple_conversation)
{
	if(g_set_object(&conversation->conversation, purple_conversation)) {
		if(PURPLE_IS_CONVERSATION(purple_conversation)) {
			g_object_set_data(G_OBJECT(purple_conversation),
			                  PIDGIN_CONVERSATION_DATA, conversation);
		}

		g_object_notify_by_pspec(G_OBJECT(conversation),
		                         properties[PROP_CONVERSATION]);
	}
}

/* This is used to call g_markup_escape_text for the topic before displaying it
 * in its normal label and the tool tip for that label.
 */
static char *
pidgin_conversation_escape_topic(G_GNUC_UNUSED GObject *self,
                                 const char *topic,
                                 G_GNUC_UNUSED gpointer data)
{
	if(topic == NULL) {
		return g_strdup("");
	}

	return g_markup_escape_text(topic, -1);
}

/**
 * pidgin_conversation_send_message:
 * @conversation: The instance.
 *
 * Creates a [class@Purple.Message] from the input widgets of @conversation and
 * sends it.
 *
 * Since: 3.0
 */
static void
pidgin_conversation_send_message(PidginConversation *conversation) {
	PurpleAccount *account = NULL;
	PurpleContactInfo *info = NULL;
	PurpleMessage *message = NULL;
	GtkTextBuffer *buffer = NULL;
	GtkTextIter start;
	GtkTextIter end;
	char *contents = NULL;
	gboolean command_executed = FALSE;

	account = purple_conversation_get_account(conversation->conversation);

	/* Get the contents from the buffer. */
	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(conversation->input));
	gtk_text_buffer_get_start_iter(buffer, &start);
	gtk_text_buffer_get_end_iter(buffer, &end);

	contents = gtk_text_buffer_get_text(buffer, &start, &end, TRUE);

	if(contents != NULL && contents[0] == '/') {
		PurpleCommandManager *manager = NULL;

		manager = purple_command_manager_get_default();
		command_executed = purple_command_manager_find_and_execute(manager,
		                                                           conversation->conversation,
		                                                           contents + 1);
	}

	if(!command_executed) {
		/* Create the message. */
		info = purple_account_get_contact_info(account);
		message = purple_message_new(info, contents);

		/* Send the message and clean up. We don't worry about the callback as we
		 * don't have anything to do in it right now.
		 */
		purple_conversation_send_message_async(conversation->conversation, message,
		                                       NULL, NULL, NULL);

		g_clear_object(&message);
	}

	g_clear_pointer(&contents, g_free);

	gtk_text_buffer_set_text(buffer, "", -1);
}

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static char *
pidgin_conversation_get_status_label(G_GNUC_UNUSED PidginConversation *conversation,
                                     GError *error, gboolean online)
{
	const char *message = NULL;

	if(error != NULL) {
		message = error->message;
	}

	if(purple_strempty(message) && !online) {
		message = _("Conversation offline");
	}

	if(purple_strempty(message)) {
		message = "";
	}

	return g_strdup(message);
}

static GtkWidget *
pidgin_conversation_get_status_page(PidginConversation *conversation,
                                    GError *error, gboolean online)
{
	if(error != NULL || !online) {
		return g_object_ref(conversation->status_label);
	}

	return g_object_ref(conversation->typing_label);
}

static void
pidgin_conversation_input_insert_text_cb(G_GNUC_UNUSED GtkTextBuffer *buffer,
                                         G_GNUC_UNUSED const GtkTextIter *iter,
                                         G_GNUC_UNUSED char *text,
                                         G_GNUC_UNUSED int length,
                                         gpointer data)
{
	PidginConversation *conversation = data;

	purple_conversation_set_typing_state(conversation->conversation,
	                                     PURPLE_TYPING_STATE_TYPING);
}

static void
pidgin_conversation_input_delete_range_cb(GtkTextBuffer *buffer,
                                          G_GNUC_UNUSED const GtkTextIter *start,
                                          G_GNUC_UNUSED const GtkTextIter *end,
                                          gpointer data)
{
	PidginConversation *conversation = data;

	if(gtk_text_buffer_get_char_count(buffer) == 0) {
		purple_conversation_set_typing_state(conversation->conversation,
		                                     PURPLE_TYPING_STATE_NONE);
	}
}

static gboolean
pidgin_conversation_input_key_pressed_cb(G_GNUC_UNUSED GtkEventControllerKey *self,
                                         guint keyval,
                                         G_GNUC_UNUSED guint keycode,
                                         GdkModifierType state,
                                         gpointer data)
{
	PidginConversation *conversation = data;
	gboolean handled = TRUE;

	if(keyval == GDK_KEY_Return || keyval == GDK_KEY_KP_Enter) {
		if(state == GDK_SHIFT_MASK || state == GDK_CONTROL_MASK) {
			return FALSE;
		}

		pidgin_conversation_send_message(conversation);
	} else if(keyval == GDK_KEY_Page_Up) {
		pidgin_auto_adjustment_decrement(PIDGIN_AUTO_ADJUSTMENT(conversation->history_adjustment));
	} else if(keyval == GDK_KEY_Page_Down) {
		pidgin_auto_adjustment_increment(PIDGIN_AUTO_ADJUSTMENT(conversation->history_adjustment));
	} else {
		handled = FALSE;
	}

	return handled;
}

static void
pidgin_conversation_detach(PidginConversation *conversation) {
	if(PURPLE_IS_CONVERSATION(conversation->conversation)) {
		gpointer us = NULL;

		us = g_object_get_data(G_OBJECT(conversation->conversation),
		                       PIDGIN_CONVERSATION_DATA);

		if(conversation == us) {
			g_object_set_data(G_OBJECT(conversation->conversation),
			                  PIDGIN_CONVERSATION_DATA, NULL);
		}
	}
}

static void
pidgin_conversation_member_list_context_cb(GtkGestureSingle *self,
                                           G_GNUC_UNUSED gint n_press,
                                           gdouble x,
                                           gdouble y,
                                           gpointer data)
{
	PurpleAccount *account = NULL;
	PurpleContactInfo *info = NULL;
	PurpleConversationMember *member = NULL;
	GtkWidget *parent = NULL;
	GtkListItem *item = data;

	parent = gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(self));

	member = gtk_list_item_get_item(item);
	info = purple_conversation_member_get_contact_info(member);

	/* ConversationMembers are a PurpleAccount for the libpurple user, or in
	 * most cases are PurpleContact for all the other users. Because of this,
	 * we have to do a runtime check to determine which one they are.
	 */
	if(PURPLE_IS_ACCOUNT(info)) {
		account = PURPLE_ACCOUNT(info);
	} else if(PURPLE_IS_CONTACT(info)) {
		account = purple_contact_get_account(PURPLE_CONTACT(info));
	}

	pidgin_contact_info_menu_popup(info, account, parent, x, y);
}

static int
pidgin_conversation_member_list_sort(gconstpointer a, gconstpointer b,
                                     G_GNUC_UNUSED gpointer data)
{
	PurpleConversationMember *member_a = NULL;
	PurpleConversationMember *member_b = NULL;
	PurpleContactInfo *info_a = NULL;
	PurpleContactInfo *info_b = NULL;

	member_a = PURPLE_CONVERSATION_MEMBER((gpointer)a);
	member_b = PURPLE_CONVERSATION_MEMBER((gpointer)b);

	info_a = purple_conversation_member_get_contact_info(member_a);
	info_b = purple_conversation_member_get_contact_info(member_b);

	return purple_contact_info_compare(info_a, info_b);
}

static void
pidgin_conversation_message_setup(G_GNUC_UNUSED GtkSignalListItemFactory *self,
                                  GObject *object,
                                  G_GNUC_UNUSED gpointer data)
{
	gtk_list_item_set_child(GTK_LIST_ITEM(object), pidgin_message_new(NULL));
}

static void
pidgin_conversation_message_bind(G_GNUC_UNUSED GtkSignalListItemFactory *self,
                                 GObject *object,
                                 G_GNUC_UNUSED gpointer data)
{
	PurpleMessage *purple_message = NULL;
	GtkListItem *item = GTK_LIST_ITEM(object);
	GtkWidget *pidgin_message = NULL;

	purple_message = gtk_list_item_get_item(item);
	pidgin_message = gtk_list_item_get_child(item);

	pidgin_message_set_message(PIDGIN_MESSAGE(pidgin_message), purple_message);
}

static void
pidgin_conversation_message_unbind(G_GNUC_UNUSED GtkSignalListItemFactory *self,
                                   GObject *object,
                                   G_GNUC_UNUSED gpointer data)
{
	GtkListItem *item = GTK_LIST_ITEM(object);
	GtkWidget *message = NULL;

	message = gtk_list_item_get_child(item);

	pidgin_message_set_message(PIDGIN_MESSAGE(message), NULL);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
pidgin_conversation_dispose(GObject *obj) {
	PidginConversation *conversation = PIDGIN_CONVERSATION(obj);

	pidgin_conversation_detach(conversation);

	g_clear_object(&conversation->conversation);

	G_OBJECT_CLASS(pidgin_conversation_parent_class)->dispose(obj);
}

static void
pidgin_conversation_get_property(GObject *obj, guint param_id, GValue *value,
                                 GParamSpec *pspec)
{
	PidginConversation *conversation = PIDGIN_CONVERSATION(obj);

	switch(param_id) {
	case PROP_CONVERSATION:
		g_value_set_object(value,
		                   pidgin_conversation_get_conversation(conversation));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
pidgin_conversation_set_property(GObject *obj, guint param_id,
                                 const GValue *value, GParamSpec *pspec)
{
	PidginConversation *conversation = PIDGIN_CONVERSATION(obj);

	switch(param_id) {
	case PROP_CONVERSATION:
		pidgin_conversation_set_conversation(conversation,
		                                     g_value_get_object(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
		break;
	}
}

static void
pidgin_conversation_init(PidginConversation *conversation) {
	gtk_widget_init_template(GTK_WIDGET(conversation));

	gtk_custom_sorter_set_sort_func(conversation->memberlist_sorter,
	                                pidgin_conversation_member_list_sort,
	                                NULL, NULL);
}

static void
pidgin_conversation_class_init(PidginConversationClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	obj_class->dispose = pidgin_conversation_dispose;
	obj_class->get_property = pidgin_conversation_get_property;
	obj_class->set_property = pidgin_conversation_set_property;

	/**
	 * PidginConversation:conversation:
	 *
	 * The [class@Purple.Conversation] that this conversation is displaying.
	 *
	 * Since: 3.0
	 */
	properties[PROP_CONVERSATION] = g_param_spec_object(
		"conversation", NULL, NULL,
		PURPLE_TYPE_CONVERSATION,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

	/* Template stuff. */
	gtk_widget_class_set_template_from_resource(
	    widget_class,
	    "/im/pidgin/Pidgin3/Conversations/conversation.ui"
	);

	gtk_widget_class_bind_template_child(widget_class, PidginConversation,
	                                     info_pane);
	gtk_widget_class_bind_template_child(widget_class, PidginConversation,
	                                     history);
	gtk_widget_class_bind_template_child(widget_class, PidginConversation,
	                                     history_adjustment);
	gtk_widget_class_bind_template_child(widget_class, PidginConversation,
	                                     memberlist_sorter);
	gtk_widget_class_bind_template_child(widget_class, PidginConversation,
	                                     input);
	gtk_widget_class_bind_template_child(widget_class, PidginConversation,
	                                     typing_label);
	gtk_widget_class_bind_template_child(widget_class, PidginConversation,
	                                     status_label);

	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_escape_topic);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_get_status_label);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_get_status_page);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_input_insert_text_cb);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_input_delete_range_cb);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_input_key_pressed_cb);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_member_list_context_cb);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_message_setup);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_message_bind);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_conversation_message_unbind);
}

/******************************************************************************
 * API
 *****************************************************************************/
GtkWidget *
pidgin_conversation_new(PurpleConversation *conversation) {
	g_return_val_if_fail(PURPLE_IS_CONVERSATION(conversation), NULL);

	return g_object_new(
		PIDGIN_TYPE_CONVERSATION,
		"conversation", conversation,
		NULL);
}

GtkWidget *
pidgin_conversation_from_purple_conversation(PurpleConversation *conversation)
{
	g_return_val_if_fail(PURPLE_IS_CONVERSATION(conversation), NULL);

	return g_object_get_data(G_OBJECT(conversation), PIDGIN_CONVERSATION_DATA);
}

PurpleConversation *
pidgin_conversation_get_conversation(PidginConversation *conversation) {
	g_return_val_if_fail(PIDGIN_IS_CONVERSATION(conversation), NULL);

	return conversation->conversation;
}

void
pidgin_conversation_close(PidginConversation *conversation) {
	g_return_if_fail(PIDGIN_IS_CONVERSATION(conversation));

	pidgin_conversation_detach(conversation);
}

mercurial