Fri, 09 Sep 2005 12:34:27 +0000
[gaim-migrate @ 13719]
Commit the Bonjour code from oldstatus to HEAD. It hasn't been
updated for the new status code yet.
| 11477 | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify | |
| 3 | * it under the terms of the GNU General Public License as published by | |
| 4 | * the Free Software Foundation; either version 2 of the License, or | |
| 5 | * (at your option) any later version. | |
| 6 | * | |
| 7 | * This program is distributed in the hope that it will be useful, | |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 10 | * GNU Library General Public License for more details. | |
| 11 | * | |
| 12 | * You should have received a copy of the GNU General Public License | |
| 13 | * along with this program; if not, write to the Free Software | |
| 14 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 15 | */ | |
| 16 | ||
| 17 | ||
| 18 | #include <glib.h> | |
| 19 | #include <stdlib.h> | |
| 20 | ||
| 21 | #include "buddy.h" | |
| 22 | #include "account.h" | |
| 23 | #include "blist.h" | |
| 24 | #include "bonjour.h" | |
| 25 | #include "debug.h" | |
| 26 | ||
| 27 | /** | |
| 28 | * Creates a new buddy. | |
| 29 | */ | |
| 30 | BonjourBuddy* bonjour_buddy_new(gchar* name, gchar* first, gint port_p2pj, | |
| 31 | gchar* phsh, gchar* status, gchar* email, gchar* last, gchar* jid, gchar* AIM, | |
| 32 | gchar* vc, gchar* ip, gchar* msg) | |
| 33 | { | |
| 34 | BonjourBuddy* buddy = malloc(sizeof(BonjourBuddy)); | |
| 35 | ||
| 36 | buddy->name = g_strdup(name); | |
| 37 | buddy->first = g_strdup(first); | |
| 38 | buddy->port_p2pj = port_p2pj; | |
| 39 | buddy->phsh = g_strdup(phsh); | |
| 40 | buddy->status = g_strdup(status); | |
| 41 | buddy->email = g_strdup(email); | |
| 42 | buddy->last = g_strdup(last); | |
| 43 | buddy->jid = g_strdup(jid); | |
| 44 | buddy->AIM = g_strdup(AIM); | |
| 45 | buddy->vc = g_strdup(vc); | |
| 46 | buddy->ip = g_strdup(ip); | |
| 47 | buddy->msg = g_strdup(msg); | |
| 48 | buddy->conversation = NULL; | |
| 49 | ||
| 50 | return buddy; | |
| 51 | } | |
| 52 | ||
| 53 | /** | |
| 54 | * Check if all the compulsory buddy data is present. | |
| 55 | */ | |
| 56 | gboolean bonjour_buddy_check(BonjourBuddy* buddy) | |
| 57 | { | |
| 58 | if(buddy->name == NULL){ | |
| 59 | return FALSE; | |
| 60 | } | |
| 61 | ||
| 62 | if(buddy->first == NULL){ | |
| 63 | return FALSE; | |
| 64 | } | |
| 65 | ||
| 66 | if(buddy->last == NULL){ | |
| 67 | return FALSE; | |
| 68 | } | |
| 69 | ||
| 70 | if(buddy->port_p2pj == -1){ | |
| 71 | return FALSE; | |
| 72 | } | |
| 73 | ||
| 74 | if(buddy->status == NULL){ | |
| 75 | return FALSE; | |
| 76 | } | |
| 77 | ||
| 78 | return TRUE; | |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * If the buddy doesn't previoulsy exists, it is created. Else, its data is changed (???) | |
| 83 | */ | |
| 84 | void bonjour_buddy_add_to_gaim(BonjourBuddy* buddy, GaimAccount* account) | |
| 85 | { | |
| 86 | GaimBuddy* gb = gaim_find_buddy(account, buddy->name); | |
| 87 | GaimGroup* bonjour_group = gaim_find_group(BONJOUR_GROUP_NAME); | |
| 88 | gchar* buddy_alias = NULL; | |
| 89 | gint buddy_status; | |
| 90 | ||
| 91 | // Create the alias for the buddy using the first and the last name | |
| 92 | buddy_alias = g_strconcat(buddy->first, " ", buddy->last, NULL); | |
| 93 | ||
| 94 | // Transformation between the bonjour status and Gaim status | |
| 95 | if (g_ascii_strcasecmp("avail", buddy->status) == 0) { | |
| 96 | buddy_status = BONJOUR_STATE_AVAILABLE; | |
| 97 | } else if (g_ascii_strcasecmp("away", buddy->status) == 0) { | |
| 98 | buddy_status = BONJOUR_STATE_AWAY; | |
| 99 | } else if (g_ascii_strcasecmp("dnd", buddy->status) == 0) { | |
| 100 | buddy_status = BONJOUR_STATE_DND; | |
| 101 | } else { | |
| 102 | buddy_status = BONJOUR_STATE_ERROR; | |
| 103 | } | |
| 104 | ||
| 105 | if (gb != NULL) { | |
| 106 | // The buddy already exists | |
| 107 | serv_got_update(account->gc, gb->name, TRUE, gb->evil, gb->signon, gb->idle, buddy_status); | |
| 108 | } else { | |
| 109 | // We have to create the buddy | |
| 110 | gb = gaim_buddy_new(account, buddy->name, buddy_alias); | |
| 111 | gb->node.flags = GAIM_BLIST_NODE_FLAG_NO_SAVE; | |
| 112 | gb->proto_data = buddy; | |
| 113 | gaim_blist_add_buddy(gb, NULL, bonjour_group, NULL); | |
| 114 | gaim_blist_server_alias_buddy(gb, buddy_alias); | |
| 115 | gaim_blist_update_buddy_status(gb, buddy_status); | |
| 116 | gaim_blist_update_buddy_presence(gb, TRUE); | |
| 117 | gaim_blist_update_buddy_signon(gb, 0); | |
| 118 | gaim_blist_update_buddy_idle(gb, 0); | |
| 119 | gaim_blist_update_buddy_evil(gb, 0); | |
| 120 | g_free(buddy_alias); | |
| 121 | } | |
| 122 | } | |
| 123 | ||
| 124 | /** | |
| 125 | * Deletes a buddy from memory. | |
| 126 | */ | |
| 127 | void bonjour_buddy_delete(BonjourBuddy* buddy) | |
| 128 | { | |
| 129 | g_free(buddy->name); | |
| 130 | g_free(buddy->first); | |
| 131 | g_free(buddy->phsh); | |
| 132 | g_free(buddy->status); | |
| 133 | g_free(buddy->email); | |
| 134 | g_free(buddy->last); | |
| 135 | g_free(buddy->jid); | |
| 136 | g_free(buddy->AIM); | |
| 137 | g_free(buddy->vc); | |
| 138 | g_free(buddy->ip); | |
| 139 | g_free(buddy->msg); | |
| 140 | ||
| 141 | if (buddy->conversation != NULL) { | |
| 142 | g_free(buddy->conversation->buddy_name); | |
| 143 | g_free(buddy->conversation); | |
| 144 | } | |
| 145 | ||
| 146 | free(buddy); | |
| 147 | } |