mark PurpleImageClass as private
[pidgin-git.git] / libpurple / protocols / gg / blist.c
blob6b36aa6b488343c10b0e974ef8a7828c26ab3a3b
1 /**
2 * @file buddylist.c
4 * purple
6 * Copyright (C) 2005 Bartosz Oler <bartosz@bzimage.us>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
24 #include <libgadu.h>
25 #include <debug.h>
27 #include "gg.h"
28 #include "utils.h"
29 #include "blist.h"
31 #define F_FIRSTNAME 0
32 #define F_LASTNAME 1
33 /* #define F_ 2 */
34 #define F_NICKNAME 3
35 #define F_PHONE 4
36 #define F_GROUP 5
37 #define F_UIN 6
39 /* void ggp_buddylist_send(PurpleConnection *gc) {{{ */
40 /* this is for for notify purposes, not synchronizing buddy list */
41 void ggp_buddylist_send(PurpleConnection *gc)
43 GGPInfo *info = purple_connection_get_protocol_data(gc);
44 PurpleAccount *account = purple_connection_get_account(gc);
45 GSList *buddies;
46 uin_t *userlist;
47 gchar *types;
48 int i = 0, ret = 0;
49 int size;
51 buddies = purple_blist_find_buddies(account, NULL);
53 size = g_slist_length(buddies);
54 userlist = g_new(uin_t, size);
55 types = g_new(gchar, size);
57 for (buddies = purple_blist_find_buddies(account, NULL); buddies;
58 buddies = g_slist_delete_link(buddies, buddies), ++i)
60 PurpleBuddy *buddy = buddies->data;
61 const gchar *name = purple_buddy_get_name(buddy);
63 userlist[i] = ggp_str_to_uin(name);
64 types[i] = GG_USER_NORMAL;
65 purple_debug_info("gg", "ggp_buddylist_send: adding %d\n",
66 userlist[i]);
69 ret = gg_notify_ex(info->session, userlist, types, size);
70 purple_debug_info("gg", "send: ret=%d; size=%d\n", ret, size);
72 if (userlist) {
73 g_free(userlist);
74 g_free(types);
77 /* }}} */
79 /* void ggp_buddylist_load(PurpleConnection *gc, char *buddylist) {{{ */
80 void ggp_buddylist_load(PurpleConnection *gc, char *buddylist)
82 PurpleBuddy *buddy;
83 PurpleGroup *group;
84 gchar **users_tbl;
85 int i;
86 char *utf8buddylist = ggp_convert_from_cp1250(buddylist);
88 /* Don't limit the number of records in a buddylist. */
89 users_tbl = g_strsplit(utf8buddylist, "\r\n", -1);
91 for (i = 0; users_tbl[i] != NULL; i++) {
92 gchar **data_tbl;
93 gchar *name, *show, *g;
95 if (!*users_tbl[i])
96 continue;
98 data_tbl = g_strsplit(users_tbl[i], ";", 8);
99 if (g_strv_length(data_tbl) < 8) {
100 purple_debug_warning("gg",
101 "Something is wrong on line %d of the buddylist. Skipping.\n",
102 i + 1);
103 continue;
106 show = data_tbl[F_NICKNAME];
107 name = data_tbl[F_UIN];
108 if ('\0' == *name || !atol(name)) {
109 purple_debug_warning("gg",
110 "Identifier on line %d of the buddylist is not a number. Skipping.\n",
111 i + 1);
112 continue;
115 if ('\0' == *show) {
116 show = name;
119 purple_debug_info("gg", "got buddy: name=%s; show=%s\n", name, show);
121 if (purple_blist_find_buddy(purple_connection_get_account(gc), name)) {
122 g_strfreev(data_tbl);
123 continue;
126 g = g_strdup("Gadu-Gadu");
128 if ('\0' != *(data_tbl[F_GROUP])) {
129 /* XXX: Probably buddy should be added to all the groups. */
130 /* Hard limit to at most 50 groups */
131 gchar **group_tbl = g_strsplit(data_tbl[F_GROUP], ",", 50);
132 if (g_strv_length(group_tbl) > 0) {
133 g_free(g);
134 g = g_strdup(group_tbl[0]);
136 g_strfreev(group_tbl);
139 buddy = purple_buddy_new(purple_connection_get_account(gc),
140 name, *show == '\0' ? NULL : show);
142 if (!(group = purple_blist_find_group(g))) {
143 group = purple_group_new(g);
144 purple_blist_add_group(group, NULL);
147 purple_blist_add_buddy(buddy, NULL, group, NULL);
148 g_free(g);
150 g_strfreev(data_tbl);
152 g_strfreev(users_tbl);
153 g_free(utf8buddylist);
155 ggp_buddylist_send(gc);
157 /* }}} */
159 /* char *ggp_buddylist_dump(PurpleAccount *account) {{{ */
160 char *ggp_buddylist_dump(PurpleAccount *account)
162 GSList *buddies;
163 GString *buddylist = g_string_sized_new(1024);
164 char *ptr;
166 for (buddies = purple_blist_find_buddies(account, NULL); buddies;
167 buddies = g_slist_delete_link(buddies, buddies))
169 PurpleBuddy *buddy = buddies->data;
170 PurpleGroup *group = purple_buddy_get_group(buddy);
171 const char *bname = purple_buddy_get_name(buddy);
172 const char *gname = purple_group_get_name(group);
173 const char *alias = purple_buddy_get_alias(buddy);
175 if (alias == NULL)
176 alias = bname;
178 g_string_append_printf(buddylist,
179 "%s;%s;%s;%s;%s;%s;%s;%s%s\r\n",
180 alias, alias, alias, alias,
181 "", gname, bname, "", "");
184 ptr = ggp_convert_to_cp1250(buddylist->str);
185 g_string_free(buddylist, TRUE);
186 return ptr;
188 /* }}} */
190 const char * ggp_buddylist_get_buddy_name(PurpleConnection *gc, const uin_t uin)
192 const char *uin_s = ggp_uin_to_str(uin);
193 PurpleBuddy *buddy = purple_blist_find_buddy(
194 purple_connection_get_account(gc), uin_s);
196 if (buddy != NULL)
197 return purple_buddy_get_alias(buddy);
198 else
199 return uin_s;
202 /* vim: set ts=8 sts=0 sw=8 noet: */