changeset 11496:3f038da50a18

[gaim-migrate @ 13740] Closer to compiling on head committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Sat, 10 Sep 2005 20:47:18 +0000
parents d5e4a3af7934
children 8d0a2b6c192b
files src/protocols/bonjour/bonjour.c src/protocols/bonjour/bonjour.h src/protocols/bonjour/buddy.c src/protocols/bonjour/buddy.h src/protocols/bonjour/dns_sd.c src/protocols/bonjour/dns_sd.h
diffstat 6 files changed, 184 insertions(+), 129 deletions(-) [+]
line wrap: on
line diff
--- a/src/protocols/bonjour/bonjour.c	Sat Sep 10 20:43:25 2005 +0000
+++ b/src/protocols/bonjour/bonjour.c	Sat Sep 10 20:47:18 2005 +0000
@@ -32,7 +32,7 @@
 #include "jabber.h"
 #include "buddy.h"
 
-void bonjour_login(GaimAccount* account)
+void bonjour_login(GaimAccount *account, GaimStatus *status)
 {
 	GaimConnection *gc = gaim_account_get_connection(account);
 	GaimGroup* bonjour_group = NULL;
@@ -63,16 +63,16 @@
 	bd->dns_sd_data->name = (sw_string)gaim_account_get_username(account);
 	bd->dns_sd_data->txtvers = g_strdup("1");
 	bd->dns_sd_data->version = g_strdup("1");
-	bd->dns_sd_data->first = gaim_account_get_string(account, "first", "Juanjo");
-	bd->dns_sd_data->last = gaim_account_get_string(account, "last", "");
+	bd->dns_sd_data->first = g_strdup(gaim_account_get_string(account, "first", "TODO"));
+	bd->dns_sd_data->last = g_strdup(gaim_account_get_string(account, "last", ""));
 	bd->dns_sd_data->port_p2pj = gaim_account_get_int(account, "port", BONJOUR_DEFAULT_PORT_INT);
 	bd->dns_sd_data->phsh = g_strdup("");
 	bd->dns_sd_data->status = g_strdup("avail"); //<-- Check the real status if different from avail
-	bd->dns_sd_data->email = gaim_account_get_string(account, "email", "");
+	bd->dns_sd_data->email = g_strdup(gaim_account_get_string(account, "email", ""));
 	bd->dns_sd_data->vc = g_strdup("");
 	bd->dns_sd_data->jid = g_strdup("");
 	bd->dns_sd_data->AIM = g_strdup("");
-	bd->dns_sd_data->msg = g_strdup(gc->away);
+	bd->dns_sd_data->msg = NULL; /* TODO */
 	
 	bd->dns_sd_data->account = account;
 	bonjour_dns_sd_start(bd->dns_sd_data);
@@ -134,104 +134,145 @@
 	return 1;
 }
 
-void bonjour_set_status(GaimConnection* connection, const char* state, const char* message)
+void bonjour_set_status(GaimAccount *account, GaimStatus *status)
 {
-	char* status_dns_sd = NULL;
-	char* stripped = NULL;
+	GaimConnection *gc;
+	BonjourData *bd;
+	gboolean disconnected;
+	GaimStatusType *type;
+	int primitive;
+	GaimPresence *presence;
+	const char *message, *bonjour_status;
 
-	if(message) {
-		stripped = g_strdup(message);
-	} else {
-		stripped = g_strdup("");
+	disconnected = gaim_account_is_disconnected(account);
+	type = gaim_status_get_type(status);
+	primitive = gaim_status_type_get_primitive(type);
+	presence = gaim_account_get_presence(account);
+
+	if (primitive != GAIM_STATUS_OFFLINE && disconnected)
+	{
+		gaim_account_connect(account);
+		return;
+	}
+	if (primitive == GAIM_STATUS_OFFLINE && !disconnected)
+	{
+		gaim_account_disconnect(account);
+		return;
 	}
 
-	if(connection->away){
-		g_free(connection->away);
-	}
-	connection->away = stripped;
-	
-	if (g_ascii_strcasecmp(state, _("Online")) == 0) {
-		status_dns_sd = g_strdup("avail");
-	} else if (g_ascii_strcasecmp(state, _("Away")) == 0) {
-		status_dns_sd = g_strdup("away");
-	} else if (g_ascii_strcasecmp(state, _("Do Not Disturb")) == 0) {
-		status_dns_sd = g_strdup("dnd");
-	} else if (g_ascii_strcasecmp(state, _("Custom")) == 0) {
-		status_dns_sd = g_strdup("away");
+	if (!gaim_account_is_connected(account))
+		/* TODO: Does this mean we're connecting? */
+		return;
+
+	message = gaim_status_get_attr_string(status, "message");
+	if (message == NULL)
+		message = "";
+
+	/*
+	 * The three possible status for Bonjour are
+	 *   -available ("avail")
+	 *   -idle ("away")
+	 *   -away ("dnd")
+	 * Each of them can have an optional message.
+	 */
+	if (primitive == GAIM_STATUS_AVAILABLE) {
+		bonjour_status = "avail";
+	} else if (gaim_presence_is_idle(presence)) {
+		bonjour_status = "away";
+	} else {
+		bonjour_status = "dnd";
 	}
 
-	if (status_dns_sd != NULL) {
-		bonjour_dns_sd_send_status(((BonjourData*)(connection->proto_data))->dns_sd_data, 
-			status_dns_sd, stripped);
-	}
+	gc = gaim_account_get_connection(account);
+	bd = gc->proto_data;
+	bonjour_dns_sd_send_status(bd->dns_sd_data, bonjour_status, message);
 }
 
-static GList* bonjour_status_types(GaimConnection* connection)
+static GList *
+bonjour_status_types(GaimAccount *account)
 {
-	GList *types = NULL;
+	GList *status_types = NULL;
+	GaimStatusType *type;
+
+	g_return_val_if_fail(account != NULL, NULL);
+
+	type = gaim_status_type_new_full(GAIM_STATUS_OFFLINE,
+									 BONJOUR_STATUS_ID_OFFLINE,
+									 _("Offline"), TRUE, TRUE, FALSE);
+	status_types = g_list_append(status_types, type);
 
-	types = g_list_append(types, _("Online"));
-	types = g_list_append(types, _("Away"));
-	types = g_list_append(types, _("Do Not Disturb"));
-	types = g_list_append(types, GAIM_AWAY_CUSTOM);
+	type = gaim_status_type_new_with_attrs(GAIM_STATUS_AVAILABLE,
+										   BONJOUR_STATUS_ID_AVAILABLE,
+										   _("Available"), TRUE, TRUE, FALSE,
+										   "message", _("Message"),
+										   gaim_value_new(GAIM_TYPE_STRING), NULL);
+	status_types = g_list_append(status_types, type);
 
-	return types;
+	type = gaim_status_type_new_with_attrs(GAIM_STATUS_AWAY,
+										   BONJOUR_STATUS_ID_AWAY,
+										   _("Away"), TRUE, TRUE, FALSE,
+										   "message", _("Message"),
+										   gaim_value_new(GAIM_TYPE_STRING), NULL);
+	status_types = g_list_append(status_types, type);
+
+	return status_types;
 }
 
-static void bonjour_convo_closed(GaimConnection* connection, const char* who)
+static void bonjour_convo_closed(GaimConnection *connection, const char *who)
 {
 	GaimBuddy* buddy = gaim_find_buddy(connection->account, who);
 	
 	bonjour_jabber_close_conversation(((BonjourData*)(connection->proto_data))->jabber_data, buddy);
 }
 
-static void bonjour_list_emblems(GaimBuddy *b, char **se, char **sw,
-		char **nw, char **ne)
+static void bonjour_list_emblems(GaimBuddy *buddy,
+								 const char **se, const char **sw,
+								 const char **nw,const char **ne)
 {
-	switch (b->uc) {
-		case BONJOUR_STATE_AWAY:
-			*se = "away";
-			break;
-		case BONJOUR_STATE_DND:
-			*se = "dnd";
-			break;
-		case BONJOUR_STATE_ERROR:
-			*se = "error";
-			break;
-	}
+	GaimPresence *presence;
+
+	presence = gaim_buddy_get_presence(buddy);
+
+	if (!gaim_presence_is_available(presence))
+		*se = "away";
+}
+
+static char *bonjour_status_text(GaimBuddy *buddy)
+{
+	GaimPresence *presence;
+
+	presence = gaim_buddy_get_presence(buddy);
+
+	if (gaim_presence_is_available(presence))
+		return g_strdup("");
+	else
+		return g_strdup("Away");
 }
 
-static char* bonjour_status_text(GaimBuddy *b)
-{
-	BonjourBuddy* bb = (BonjourBuddy*)b->proto_data;
-	
-	if (bb->msg != NULL) {
-		return g_strdup(bb->msg);
-	} else {
-		return g_strdup("");
-	}
-}
-
-static char* bonjour_tooltip_text(GaimBuddy *b)
+static char *bonjour_tooltip_text(GaimBuddy *buddy)
 {
-	char* status = NULL;
-	
-	switch (b->uc) {
-		case BONJOUR_STATE_AVAILABLE:
-			status = g_strdup(_("Online"));
-			break;
-		case BONJOUR_STATE_AWAY:
-			status = g_strdup(_("Away"));
-			break;
-		case BONJOUR_STATE_DND:
-			status = g_strdup(_("Do Not Disturb"));
-			break;
-		case BONJOUR_STATE_ERROR:
-			status = g_strdup("Error");
-			break;
-	}
-	
-	return g_strconcat("\n<b>Status: </b>", status, NULL);
+	GString *ret;
+	GaimPresence *presence;
+	GaimStatus *status;
+	const char *status_description;
+	const char *message;
+
+	presence = gaim_buddy_get_presence(buddy);
+	status = gaim_presence_get_active_status(presence);
+	message = gaim_status_get_attr_string(status, "message");
+
+	if (gaim_presence_is_available(presence))
+		status_description = gaim_status_get_name(status);
+	else if (gaim_presence_is_idle(presence))
+		status_description = _("Idle");
+	else
+		status_description = gaim_status_get_name(status);
+
+	ret = g_string_new("");
+	g_string_append_printf(ret, _("<b>Status:</b> %s"), status_description);
+	g_string_append_printf(ret, _("<b>Message:</b> %s"), message);
+
+	return g_string_free(ret, FALSE);
 }
 
 static GaimPlugin *my_protocol = NULL;
--- a/src/protocols/bonjour/bonjour.h	Sat Sep 10 20:43:25 2005 +0000
+++ b/src/protocols/bonjour/bonjour.h	Sat Sep 10 20:47:18 2005 +0000
@@ -35,10 +35,9 @@
 #define BONJOUR_PROTOCOL_NAME "bonjour"
 #define BONJOUR_ICON_NAME "bonjour"
 
-#define BONJOUR_STATE_AWAY  		(0x02 | UC_UNAVAILABLE)
-#define BONJOUR_STATE_AVAILABLE  	(0x04)
-#define BONJOUR_STATE_DND   		(0x10 | UC_UNAVAILABLE)
-#define BONJOUR_STATE_ERROR 		(0x20 | UC_UNAVAILABLE)
+#define BONJOUR_STATUS_ID_OFFLINE   "offline"
+#define BONJOUR_STATUS_ID_AVAILABLE "available"
+#define BONJOUR_STATUS_ID_AWAY      "away"
 
 typedef struct _bonjour_data{
 	BonjourDnsSd* dns_sd_data;
--- a/src/protocols/bonjour/buddy.c	Sat Sep 10 20:43:25 2005 +0000
+++ b/src/protocols/bonjour/buddy.c	Sat Sep 10 20:47:18 2005 +0000
@@ -79,46 +79,59 @@
 }
 
 /**
- * If the buddy doesn't previoulsy exists, it is created. Else, its data is changed (???)
+ * If the buddy does not yet exist, then create it and add it to
+ * our buddy list.  In either case we set the correct status for
+ * the buddy.
  */
-void bonjour_buddy_add_to_gaim(BonjourBuddy* buddy, GaimAccount* account)
+void
+bonjour_buddy_add_to_gaim(GaimAccount *account, BonjourBuddy *bonjour_buddy)
 {
-	GaimBuddy* gb = gaim_find_buddy(account, buddy->name);
-	GaimGroup* bonjour_group = gaim_find_group(BONJOUR_GROUP_NAME);
-	gchar* buddy_alias = NULL;
-	gint buddy_status;
+	GaimBuddy *buddy;
+	GaimGroup *group;
+	const char *status_id, *first, *last;
+	char *alias;
 
-	// Create the alias for the buddy using the first and the last name
-	buddy_alias = g_strconcat(buddy->first, " ", buddy->last, NULL);
+	/* Translate between the Bonjour status and the Gaim status */
+	if (g_ascii_strcasecmp("dnd", bonjour_buddy->status) == 0)
+		status_id = BONJOUR_STATUS_ID_AWAY;
+	else
+		status_id = BONJOUR_STATUS_ID_AVAILABLE;
+
+	/*
+	 * TODO: Figure out the idle time by getting the "away"
+	 * field from the DNS SD.
+	 */
 
-	// Transformation between the bonjour status and Gaim status
-	if (g_ascii_strcasecmp("avail", buddy->status) == 0) {
-		buddy_status = BONJOUR_STATE_AVAILABLE;
-	} else if (g_ascii_strcasecmp("away", buddy->status) == 0) {
-		buddy_status = BONJOUR_STATE_AWAY;
-	} else if (g_ascii_strcasecmp("dnd", buddy->status) == 0) {
-		buddy_status = BONJOUR_STATE_DND;
-	} else {
-		buddy_status = BONJOUR_STATE_ERROR;
+	/* Create the alias for the buddy using the first and the last name */
+	first = bonjour_buddy->first;
+	last = bonjour_buddy->last;
+	alias = g_strdup_printf("%s%s%s",
+							(first && *first ? first : ""),
+							(first && *first && last && *last ? " " : ""),
+							(last && *last ? last : ""));
+
+	/* Make sure the Bonjour group exists in our buddy list */
+	group = gaim_find_group(BONJOUR_GROUP_NAME); /* Use the buddy's domain, instead? */
+	if (group == NULL)
+	{
+		group = gaim_group_new(BONJOUR_GROUP_NAME);
+		gaim_blist_add_group(group, NULL);
 	}
-	
-	if (gb != NULL) {
-		// The buddy already exists
-		serv_got_update(account->gc, gb->name, TRUE, gb->evil, gb->signon, gb->idle, buddy_status);
-	} else {
-		// We have to create the buddy
-		gb = gaim_buddy_new(account, buddy->name, buddy_alias);
-		gb->node.flags = GAIM_BLIST_NODE_FLAG_NO_SAVE;
-		gb->proto_data = buddy;
-		gaim_blist_add_buddy(gb, NULL, bonjour_group, NULL);
-		gaim_blist_server_alias_buddy(gb, buddy_alias);
-		gaim_blist_update_buddy_status(gb, buddy_status);
-		gaim_blist_update_buddy_presence(gb, TRUE);
-		gaim_blist_update_buddy_signon(gb, 0);
-		gaim_blist_update_buddy_idle(gb, 0);
-		gaim_blist_update_buddy_evil(gb, 0);
-		g_free(buddy_alias);
+
+	/* Make sure the buddy exists in our buddy list */
+	buddy = gaim_find_buddy(account, bonjour_buddy->name);
+	if (buddy == NULL)
+	{
+		buddy = gaim_buddy_new(account, bonjour_buddy->name, alias);
+		gaim_blist_node_set_flags((GaimBlistNode *)buddy, GAIM_BLIST_NODE_FLAG_NO_SAVE);
+		gaim_blist_add_buddy(buddy, NULL, group, NULL);
 	}
+
+	/* Set the user's status */
+	gaim_prpl_got_user_status(account, buddy->name, status_id, NULL);
+	gaim_prpl_got_user_idle(account, buddy->name, FALSE, 0);
+
+	g_free(alias);
 }
 
 /**
--- a/src/protocols/bonjour/buddy.h	Sat Sep 10 20:43:25 2005 +0000
+++ b/src/protocols/bonjour/buddy.h	Sat Sep 10 20:47:18 2005 +0000
@@ -54,8 +54,8 @@
 /**
  * If the buddy doesn't previoulsy exists, it is created. Else, its data is changed (???)
  */
-void bonjour_buddy_add_to_gaim(BonjourBuddy* buddy, GaimAccount* account);
- 
+void bonjour_buddy_add_to_gaim(GaimAccount *account, BonjourBuddy *buddy);
+
 /**
  * Deletes a buddy from memory.
  */
--- a/src/protocols/bonjour/dns_sd.c	Sat Sep 10 20:43:25 2005 +0000
+++ b/src/protocols/bonjour/dns_sd.c	Sat Sep 10 20:47:18 2005 +0000
@@ -142,9 +142,8 @@
 		return SW_DISCOVERY_E_UNKNOWN;
 	}
 	
-	// Look for the buddy within the buddy list, if exist, change the status information, 
-	// else create a new buddy within the group Bonjour
-	bonjour_buddy_add_to_gaim(buddy, account);
+	/* Add or update the buddy in our buddy list */
+	bonjour_buddy_add_to_gaim(account, buddy);
 	
 	// Free all the temporal strings
 	g_free(txtvers);
@@ -305,16 +304,19 @@
 void bonjour_dns_sd_free(BonjourDnsSd* data)
 {
 	g_free(data->session);
+	g_free(data->first);
+	g_free(data->last);
+	g_free(data->email);
 	g_free(data);
 }
 
 /**
  * Send a new dns-sd packet updating our status.
  */
-void bonjour_dns_sd_send_status(BonjourDnsSd* data, char* status, const char* status_message)
+void bonjour_dns_sd_send_status(BonjourDnsSd *data, const char *status, const char* status_message)
 {
-	if (data->status) g_free(data->status);
-	if (data->msg) g_free(data->msg);
+	g_free(data->status);
+	g_free(data->msg);
 
 	data->status = g_strdup(status);
 	data->msg = g_strdup(status_message);
@@ -324,8 +326,8 @@
 }
 
 /**
- * Advertise our presence within the dns-sd daemon and start browsing for other 
- * bonjour peers.
+ * Advertise our presence within the dns-sd daemon and start browsing
+ * for other bonjour peers.
  */
 void bonjour_dns_sd_start(BonjourDnsSd* data)
 {	
--- a/src/protocols/bonjour/dns_sd.h	Sat Sep 10 20:43:25 2005 +0000
+++ b/src/protocols/bonjour/dns_sd.h	Sat Sep 10 20:47:18 2005 +0000
@@ -66,7 +66,7 @@
 /**
  * Send a new dns-sd packet updating our status.
  */
-void bonjour_dns_sd_send_status(BonjourDnsSd* data, char* status, const char* status_message);
+void bonjour_dns_sd_send_status(BonjourDnsSd *data, const char *status, const char *status_message);
 
 /**
  * Advertise our presence within the dns-sd daemon and start browsing for other