view plugins/raw.c @ 11719:109ee3bfeac5

[gaim-migrate @ 14010] SF Patch #1333770 from corfe83 "Many times in gaim we use the function g_slist_remove(list,node->data) to remove an element from a GSList. If we already have the pointer to the node we want to delete, it is faster to send it the pointer to the node to delete rather than the data of the node (we can do this by calling g_slist_delete_link(list,node)). This change was made while looking at glib's documentation and the code in glib's gslist.c. This is because as the remove/delete function traverses each node in the list, it doesn't need to spend an extra memory access to retrieve the data for each element in the node it is traversing and then compare, it can simply compare the pointer. In my tests outside of gaim, this makes a big difference if the node you are deleting is at a high index in the list. However, even if you're deleting the first node, it about breaks even. So, I've found each case in gaim where we are calling g_slist_remove, and we already have the pointer to the appropriate node to delete (this is often the case when we're doing a for or while loop on a GSList). I've then replaced it with the appropriate call to g_slist_delete_link. I, however, didn't do this in situations where we are explicitly removing the first element in the list, because in those situations it is an unnecessary change. There should be no difference in behavior, but just in case I've tried running it with valgrind, which reports the same number of memory leaks after my patch as before my patch. Of course, I can't guarantee that my normal behavior on gaim is hitting all the functions I've changed, but in general testing it Works For Me (tm)." As with the last patch, this one may not have a practical performance impact (or maybe it does, I have no idea), but it's not worse for any case. Given two ways of doing things where one is always at least as fast and may be faster under some cases, I like to prefer that faster way. This doesn't make the code any uglier, so I'm applying. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Sat, 22 Oct 2005 20:48:18 +0000
parents bb0d7b719af2
children 4c05f4dd3ab9
line wrap: on
line source

#define GAIM_PLUGINS
#include "conversation.h"
#include "debug.h"
#include "prpl.h"
#include "version.h"

#include "gtkgaim.h"
#include "gtkplugin.h"
#include "gtkutils.h"

#ifdef MAX
# undef MAX
# undef MIN
#endif

#include "protocols/jabber/jabber.h"
#undef GAIM_PLUGINS
#include "protocols/msn/session.h"

#define RAW_PLUGIN_ID "gtk-raw"

static GtkWidget *window = NULL;
static GaimAccount *account = NULL;
static GaimPlugin *my_plugin = NULL;

static int
window_closed_cb()
{
	gaim_plugin_unload(my_plugin);

	return FALSE;
}

static void
text_sent_cb(GtkEntry *entry)
{
	const char *txt;
	GaimConnection *gc;
	const char *prpl_id;

	if (account == NULL)
		return;

	gc = gaim_account_get_connection(account);

	txt = gtk_entry_get_text(entry);

	prpl_id = gaim_account_get_protocol_id(account);
	
	gaim_debug_misc("raw", "prpl_id = %s\n", prpl_id);
	
	if (strcmp(prpl_id, "prpl-toc") == 0) {
		int *a = (int *)gc->proto_data;
		unsigned short seqno = htons(a[1]++ & 0xffff);
		unsigned short len = htons(strlen(txt) + 1);
		write(*a, "*\002", 2);
		write(*a, &seqno, 2);
		write(*a, &len, 2);
		write(*a, txt, ntohs(len));
		gaim_debug(GAIM_DEBUG_MISC, "raw", "TOC C: %s\n", txt);
		
	} else if (strcmp(prpl_id, "prpl-msn") == 0) {
		MsnSession *session = gc->proto_data;
		char buf[strlen(txt) + 3];

		g_snprintf(buf, sizeof(buf), "%s\r\n", txt);
		msn_servconn_write(session->notification->servconn, buf, strlen(buf));
		
	} else if (strcmp(prpl_id, "prpl-irc") == 0) {
		write(*(int *)gc->proto_data, txt, strlen(txt));
		write(*(int *)gc->proto_data, "\r\n", 2);
		gaim_debug(GAIM_DEBUG_MISC, "raw", "IRC C: %s\n", txt);

	} else if (strcmp(prpl_id, "prpl-jabber") == 0) {
		jabber_send_raw((JabberStream *)gc->proto_data, txt, -1);
	} else {
		gaim_debug_error("raw", "Unknown protocol ID %s\n", prpl_id);
	}

	gtk_entry_set_text(entry, "");
}

static void
account_changed_cb(GtkWidget *dropdown, GaimAccount *new_account,
				   void *user_data)
{
	account = new_account;
}

static gboolean
plugin_load(GaimPlugin *plugin)
{
	GtkWidget *hbox;
	GtkWidget *entry;
	GtkWidget *dropdown;

	/* Setup the window. */
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_container_set_border_width(GTK_CONTAINER(window), 6);

	g_signal_connect(G_OBJECT(window), "delete_event",
					 G_CALLBACK(window_closed_cb), NULL);

	/* Main hbox */
	hbox = gtk_hbox_new(FALSE, 6);
	gtk_container_add(GTK_CONTAINER(window), hbox);

	/* Account drop-down menu. */
	dropdown = gaim_gtk_account_option_menu_new(NULL, FALSE,
			G_CALLBACK(account_changed_cb), NULL, NULL);

	if (gaim_connections_get_all())
		account = (GaimAccount *)gaim_connections_get_all()->data;

	gtk_box_pack_start(GTK_BOX(hbox), dropdown, FALSE, FALSE, 0);

	/* Entry box */
	entry = gtk_entry_new();
	gtk_box_pack_start(GTK_BOX(hbox), entry, FALSE, FALSE, 0);

	g_signal_connect(G_OBJECT(entry), "activate",
					 G_CALLBACK(text_sent_cb), NULL);

	gtk_widget_show_all(window);

	return TRUE;
}

static gboolean
plugin_unload(GaimPlugin *plugin)
{
	if (window)
		gtk_widget_destroy(window);

	window = NULL;

	return TRUE;
}

static GaimPluginInfo info =
{
	GAIM_PLUGIN_MAGIC,
	GAIM_MAJOR_VERSION,
	GAIM_MINOR_VERSION,
	GAIM_PLUGIN_STANDARD,
	GAIM_GTK_PLUGIN_TYPE,
	0,
	NULL,
	GAIM_PRIORITY_DEFAULT,
	RAW_PLUGIN_ID,
	N_("Raw"),
	VERSION,
	N_("Lets you send raw input to text-based protocols."),
	N_("Lets you send raw input to text-based protocols (Jabber, MSN, IRC, "
	   "TOC). Hit 'Enter' in the entry box to send. Watch the debug window."),
	"Eric Warmenhoven <eric@warmenhoven.org>",
	GAIM_WEBSITE,
	plugin_load,
	plugin_unload,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL
};

static void
init_plugin(GaimPlugin *plugin)
{
	my_plugin = plugin;
}

GAIM_INIT_PLUGIN(raw, init_plugin, info)