Mercurial > ~mikael > mcabber > hg
annotate mcabber/src/roster.c @ 301:efcbd3c1ad15
Free unread_list when the roster is freed
It fixes a bug after a /disconnect
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Mon, 11 Jul 2005 18:52:29 +0100 |
parents | 49e9e02dd6d0 |
children | 45076d02eeef 33b8e801ffa6 |
rev | line source |
---|---|
72 | 1 /* |
2 * roster.c -- Local roster implementation | |
3 * | |
4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or (at | |
9 * your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
19 * USA | |
20 */ | |
21 | |
265 | 22 #define _GNU_SOURCE /* for strcasestr() */ |
72 | 23 #include <string.h> |
24 | |
25 #include "roster.h" | |
26 | |
27 | |
28 /* This is a private structure type for the roster */ | |
29 | |
30 typedef struct { | |
207 | 31 const gchar *name; |
32 const gchar *jid; | |
221 | 33 const gchar *status_msg; |
72 | 34 guint type; |
35 enum imstatus status; | |
36 guint flags; | |
37 // list: user -> points to his group; group -> points to its users list | |
38 GSList *list; | |
39 } roster; | |
40 | |
41 | |
42 /* ### Variables ### */ | |
43 | |
44 static int hide_offline_buddies; | |
45 static GSList *groups; | |
236 | 46 static GSList *unread_list; |
72 | 47 GList *buddylist; |
78 | 48 GList *current_buddy; |
72 | 49 |
50 | |
51 /* ### Roster functions ### */ | |
52 | |
53 // Comparison function used to search in the roster (compares jids and types) | |
54 gint roster_compare_jid_type(roster *a, roster *b) { | |
89 | 55 if (! (a->type & b->type)) |
72 | 56 return -1; // arbitrary (but should be != , of course) |
57 return strcasecmp(a->jid, b->jid); | |
58 } | |
59 | |
60 // Comparison function used to sort the roster (by name) | |
61 gint roster_compare_name(roster *a, roster *b) { | |
62 return strcasecmp(a->name, b->name); | |
63 } | |
64 | |
65 // Finds a roster element (user, group, agent...), by jid or name | |
89 | 66 // If roster_type is 0, returns match of any type. |
72 | 67 // Returns the roster GSList element, or NULL if jid/name not found |
78 | 68 GSList *roster_find(const char *jidname, enum findwhat type, guint roster_type) |
72 | 69 { |
70 GSList *sl_roster_elt = groups; | |
71 GSList *res; | |
72 roster sample; | |
73 GCompareFunc comp; | |
74 | |
192 | 75 if (!jidname) return NULL; |
72 | 76 |
89 | 77 if (!roster_type) |
78 roster_type = ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_GROUP; | |
79 | |
72 | 80 sample.type = roster_type; |
81 if (type == jidsearch) { | |
82 sample.jid = jidname; | |
83 comp = (GCompareFunc)&roster_compare_jid_type; | |
84 } else if (type == namesearch) { | |
85 sample.name = jidname; | |
86 comp = (GCompareFunc)&roster_compare_name; | |
87 } else | |
88 return NULL; // should not happen | |
89 | |
90 while (sl_roster_elt) { | |
91 roster *roster_elt = (roster*)sl_roster_elt->data; | |
92 if (roster_type & ROSTER_TYPE_GROUP) { | |
93 if ((type == namesearch) && !strcasecmp(jidname, roster_elt->name)) | |
94 return sl_roster_elt; | |
95 } else { | |
96 res = g_slist_find_custom(roster_elt->list, &sample, comp); | |
97 if (res) | |
98 return res; | |
99 } | |
100 sl_roster_elt = g_slist_next(sl_roster_elt); | |
101 } | |
102 return NULL; | |
103 } | |
104 | |
105 // Returns pointer to new group, or existing group with that name | |
78 | 106 GSList *roster_add_group(const char *name) |
72 | 107 { |
108 roster *roster_grp; | |
109 // #1 Check name doesn't already exist | |
110 if (!roster_find(name, namesearch, ROSTER_TYPE_GROUP)) { | |
111 // #2 Create the group node | |
112 roster_grp = g_new0(roster, 1); | |
113 roster_grp->name = g_strdup(name); | |
114 roster_grp->type = ROSTER_TYPE_GROUP; | |
115 // #3 Insert (sorted) | |
116 groups = g_slist_insert_sorted(groups, roster_grp, | |
117 (GCompareFunc)&roster_compare_name); | |
118 } | |
119 return roster_find(name, namesearch, ROSTER_TYPE_GROUP); | |
120 } | |
121 | |
122 // Returns a pointer to the new user, or existing user with that name | |
78 | 123 GSList *roster_add_user(const char *jid, const char *name, const char *group, |
124 guint type) | |
72 | 125 { |
126 roster *roster_usr; | |
127 roster *my_group; | |
128 GSList *slist; | |
129 | |
130 if ((type != ROSTER_TYPE_USER) && (type != ROSTER_TYPE_AGENT)) { | |
131 // XXX Error message? | |
132 return NULL; | |
133 } | |
134 | |
78 | 135 // Let's be arbitrary: default group has an empty name (""). |
136 if (!group) group = ""; | |
137 | |
72 | 138 // #1 Check this user doesn't already exist |
192 | 139 slist = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
140 if (slist) return slist; | |
72 | 141 // #2 add group if necessary |
142 slist = roster_add_group(group); | |
143 if (!slist) return NULL; | |
144 my_group = (roster*)slist->data; | |
145 // #3 Create user node | |
146 roster_usr = g_new0(roster, 1); | |
147 roster_usr->jid = g_strdup(jid); | |
78 | 148 if (name) { |
149 roster_usr->name = g_strdup(name); | |
150 } else { | |
151 gchar *p, *str = g_strdup(jid); | |
80 | 152 p = strstr(str, "/"); |
78 | 153 if (p) *p = '\0'; |
154 roster_usr->name = g_strdup(str); | |
155 g_free(str); | |
156 } | |
72 | 157 roster_usr->type = type; //ROSTER_TYPE_USER; |
158 roster_usr->list = slist; // (my_group SList element) | |
159 // #4 Insert node (sorted) | |
160 my_group->list = g_slist_insert_sorted(my_group->list, roster_usr, | |
161 (GCompareFunc)&roster_compare_name); | |
162 return roster_find(jid, jidsearch, type); | |
163 } | |
164 | |
165 // Removes user (jid) from roster, frees allocated memory | |
78 | 166 void roster_del_user(const char *jid) |
72 | 167 { |
168 GSList *sl_user, *sl_group; | |
169 GSList **sl_group_listptr; | |
170 roster *roster_usr; | |
246 | 171 GSList *node; |
72 | 172 |
89 | 173 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
174 if (sl_user == NULL) | |
72 | 175 return; |
176 roster_usr = (roster*)sl_user->data; | |
246 | 177 |
178 // Remove (if present) from unread messages list | |
179 node = g_slist_find(unread_list, roster_usr); | |
180 if (node) unread_list = g_slist_delete_link(unread_list, node); | |
181 | |
182 // Let's free memory (jid, name, status message) | |
221 | 183 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
184 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
185 if (roster_usr->status_msg) g_free((gchar*)roster_usr->status_msg); | |
210 | 186 g_free(roster_usr); |
72 | 187 |
188 // That's a little complex, we need to dereference twice | |
189 sl_group = ((roster*)sl_user->data)->list; | |
190 sl_group_listptr = &((roster*)(sl_group->data))->list; | |
191 *sl_group_listptr = g_slist_delete_link(*sl_group_listptr, sl_user); | |
78 | 192 |
193 // We need to rebuild the list | |
194 if (current_buddy) | |
195 buddylist_build(); | |
210 | 196 // TODO What we could do, too, is to check if the deleted node is |
78 | 197 // current_buddy, in which case we could move current_buddy to the |
198 // previous (or next) node. | |
72 | 199 } |
200 | |
210 | 201 // Free all roster data and call buddylist_build() to free the buddylist. |
164 | 202 void roster_free(void) |
203 { | |
204 GSList *sl_grp = groups; | |
205 | |
301
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
206 // Free unread_list |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
207 if (unread_list) { |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
208 g_slist_free(unread_list); |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
209 unread_list = NULL; |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
210 } |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
211 |
164 | 212 // Walk through groups |
213 while (sl_grp) { | |
214 roster *roster_grp = (roster*)sl_grp->data; | |
215 GSList *sl_usr = roster_grp->list; | |
216 // Walk through this group users | |
217 while (sl_usr) { | |
218 roster *roster_usr = (roster*)sl_usr->data; | |
219 // Free name and jid | |
221 | 220 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
221 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
222 if (roster_usr->status_msg) g_free((gchar*)roster_usr->status_msg); | |
214 | 223 g_free(roster_usr); |
164 | 224 sl_usr = g_slist_next(sl_usr); |
225 } | |
226 // Free group's users list | |
227 if (roster_grp->list) | |
228 g_slist_free(roster_grp->list); | |
229 // Free group's name and jid | |
221 | 230 if (roster_grp->jid) g_free((gchar*)roster_grp->jid); |
231 if (roster_grp->name) g_free((gchar*)roster_grp->name); | |
214 | 232 g_free(roster_grp); |
164 | 233 sl_grp = g_slist_next(sl_grp); |
234 } | |
235 // Free groups list | |
236 if (groups) { | |
237 g_slist_free(groups); | |
238 groups = NULL; | |
239 // Update (i.e. free) buddylist | |
240 if (buddylist) | |
241 buddylist_build(); | |
242 } | |
243 } | |
244 | |
221 | 245 void roster_setstatus(const char *jid, enum imstatus bstat, |
246 const char *status_msg) | |
72 | 247 { |
248 GSList *sl_user; | |
249 roster *roster_usr; | |
250 | |
89 | 251 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
92 | 252 // If we can't find it, we add it |
89 | 253 if (sl_user == NULL) |
92 | 254 sl_user = roster_add_user(jid, NULL, NULL, ROSTER_TYPE_USER); |
72 | 255 |
256 roster_usr = (roster*)sl_user->data; | |
257 roster_usr->status = bstat; | |
221 | 258 if (roster_usr->status_msg) { |
259 g_free((gchar*)roster_usr->status_msg); | |
260 roster_usr->status_msg = NULL; | |
261 } | |
262 if (status_msg) | |
263 roster_usr->status_msg = g_strdup(status_msg); | |
72 | 264 } |
265 | |
78 | 266 // roster_setflags() |
267 // Set one or several flags to value (TRUE/FALSE) | |
80 | 268 void roster_setflags(const char *jid, guint flags, guint value) |
78 | 269 { |
270 GSList *sl_user; | |
271 roster *roster_usr; | |
272 | |
89 | 273 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
274 if (sl_user == NULL) | |
78 | 275 return; |
276 | |
277 roster_usr = (roster*)sl_user->data; | |
278 if (value) | |
279 roster_usr->flags |= flags; | |
280 else | |
281 roster_usr->flags &= ~flags; | |
282 } | |
148 | 283 |
284 // roster_msg_setflag() | |
285 // Set the ROSTER_FLAG_MSG to the given value for the given jid. | |
286 // It will update the buddy's group message flag. | |
236 | 287 // Update the unread messages list too. |
148 | 288 void roster_msg_setflag(const char *jid, guint value) |
289 { | |
290 GSList *sl_user; | |
291 roster *roster_usr, *roster_grp; | |
292 | |
293 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); | |
294 if (sl_user == NULL) | |
295 return; | |
296 | |
297 roster_usr = (roster*)sl_user->data; | |
298 roster_grp = (roster*)roster_usr->list->data; | |
299 if (value) { | |
300 // Message flag is TRUE. This is easy, we just have to set both flags | |
301 // to TRUE... | |
302 roster_usr->flags |= ROSTER_FLAG_MSG; | |
303 roster_grp->flags |= ROSTER_FLAG_MSG; // group | |
236 | 304 // Append the roster_usr to unread_list, but avoid duplicates |
305 if (!g_slist_find(unread_list, roster_usr)) | |
306 unread_list = g_slist_append(unread_list, roster_usr); | |
148 | 307 } else { |
308 // Message flag is FALSE. | |
309 guint msg = FALSE; | |
310 roster_usr->flags &= ~ROSTER_FLAG_MSG; | |
236 | 311 if (unread_list) { |
312 GSList *node = g_slist_find(unread_list, roster_usr); | |
313 if (node) unread_list = g_slist_delete_link(unread_list, node); | |
314 } | |
148 | 315 // For the group value we need to watch all buddies in this group; |
316 // if one is flagged, then the group will be flagged. | |
317 // I will re-use sl_user and roster_usr here, as they aren't used | |
318 // anymore. | |
319 sl_user = roster_grp->list; | |
320 while (sl_user) { | |
321 roster_usr = (roster*)sl_user->data; | |
322 if (roster_usr->flags & ROSTER_FLAG_MSG) { | |
323 msg = TRUE; | |
324 break; | |
325 } | |
326 sl_user = g_slist_next(sl_user); | |
327 } | |
328 if (!msg) | |
329 roster_grp->flags &= ~ROSTER_FLAG_MSG; | |
330 else | |
331 roster_grp->flags |= ROSTER_FLAG_MSG; | |
332 // Actually the "else" part is useless, because the group | |
333 // ROSTER_FLAG_MSG should already be set... | |
334 } | |
335 } | |
336 | |
89 | 337 void roster_settype(const char *jid, guint type) |
338 { | |
339 GSList *sl_user; | |
340 roster *roster_usr; | |
341 | |
342 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) | |
343 return; | |
344 | |
345 roster_usr = (roster*)sl_user->data; | |
346 roster_usr->type = type; | |
347 } | |
348 | |
90 | 349 enum imstatus roster_getstatus(const char *jid) |
350 { | |
351 GSList *sl_user; | |
352 roster *roster_usr; | |
353 | |
354 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); | |
355 if (sl_user == NULL) | |
356 return offline; // Not in the roster, anyway... | |
357 | |
358 roster_usr = (roster*)sl_user->data; | |
359 return roster_usr->status; | |
360 } | |
361 | |
89 | 362 guint roster_gettype(const char *jid) |
363 { | |
364 GSList *sl_user; | |
365 roster *roster_usr; | |
366 | |
367 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) | |
368 return 0; | |
369 | |
370 roster_usr = (roster*)sl_user->data; | |
371 return roster_usr->type; | |
372 } | |
373 | |
141 | 374 inline guint roster_exists(const char *jidname, enum findwhat type, |
375 guint roster_type) | |
376 { | |
377 if (roster_find(jidname, type, roster_type)) | |
378 return TRUE; | |
379 return FALSE; | |
380 } | |
381 | |
72 | 382 |
383 /* ### BuddyList functions ### */ | |
384 | |
120 | 385 // buddylist_set_hide_offline_buddies(hide) |
72 | 386 // "hide" values: 1=hide 0=show_all -1=invert |
120 | 387 void buddylist_set_hide_offline_buddies(int hide) |
72 | 388 { |
389 if (hide < 0) // NEG (invert) | |
390 hide_offline_buddies = !hide_offline_buddies; | |
391 else if (hide == 0) // FALSE (don't hide) | |
392 hide_offline_buddies = 0; | |
393 else // TRUE (hide) | |
394 hide_offline_buddies = 1; | |
395 } | |
396 | |
120 | 397 inline int buddylist_get_hide_offline_buddies(void) |
398 { | |
399 return hide_offline_buddies; | |
400 } | |
401 | |
72 | 402 // buddylist_build() |
403 // Creates the buddylist from the roster entries. | |
404 void buddylist_build(void) | |
405 { | |
406 GSList *sl_roster_elt = groups; | |
407 roster *roster_elt; | |
81 | 408 roster *roster_current_buddy = NULL; |
128 | 409 int shrunk_group; |
72 | 410 |
81 | 411 // We need to remember which buddy is selected. |
412 if (current_buddy) | |
413 roster_current_buddy = BUDDATA(current_buddy); | |
414 current_buddy = NULL; | |
415 | |
72 | 416 // Destroy old buddylist |
417 if (buddylist) { | |
418 g_list_free(buddylist); | |
419 buddylist = NULL; | |
420 } | |
421 | |
422 // Create the new list | |
423 while (sl_roster_elt) { | |
424 GSList *sl_roster_usrelt; | |
425 roster *roster_usrelt; | |
147 | 426 guint pending_group = FALSE; |
72 | 427 roster_elt = (roster*) sl_roster_elt->data; |
428 | |
429 // Add the group now unless hide_offline_buddies is set, | |
430 // in which case we'll add it only if an online buddy belongs to it. | |
147 | 431 // We take care to keep the current_buddy in the list, too. |
432 if (!hide_offline_buddies || roster_elt == roster_current_buddy) | |
72 | 433 buddylist = g_list_append(buddylist, roster_elt); |
434 else | |
147 | 435 pending_group = TRUE; |
72 | 436 |
128 | 437 shrunk_group = roster_elt->flags & ROSTER_FLAG_HIDE; |
438 | |
72 | 439 sl_roster_usrelt = roster_elt->list; |
440 while (sl_roster_usrelt) { | |
441 roster_usrelt = (roster*) sl_roster_usrelt->data; | |
442 | |
443 // Buddy will be added if either: | |
444 // - hide_offline_buddies is FALSE | |
445 // - buddy is not offline | |
446 // - buddy has a lock (for example the buddy window is currently open) | |
447 // - buddy has a pending (non-read) message | |
128 | 448 // - group isn't hidden (shrunk) |
147 | 449 // - this is the current_buddy |
450 if (!hide_offline_buddies || roster_usrelt == roster_current_buddy || | |
72 | 451 (buddy_getstatus((gpointer)roster_usrelt) != offline) || |
452 (buddy_getflags((gpointer)roster_usrelt) & | |
453 (ROSTER_FLAG_LOCK | ROSTER_FLAG_MSG))) { | |
454 // This user should be added. Maybe the group hasn't been added yet? | |
147 | 455 if (pending_group && |
456 (hide_offline_buddies || roster_usrelt == roster_current_buddy)) { | |
72 | 457 // It hasn't been done yet |
458 buddylist = g_list_append(buddylist, roster_elt); | |
459 pending_group = FALSE; | |
460 } | |
461 // Add user | |
128 | 462 // XXX Should we add the user if there is a message and |
463 // the group is shrunk? If so, we'd need to check LOCK flag too, | |
464 // perhaps... | |
465 if (!shrunk_group) | |
466 buddylist = g_list_append(buddylist, roster_usrelt); | |
72 | 467 } |
468 | |
469 sl_roster_usrelt = g_slist_next(sl_roster_usrelt); | |
470 } | |
471 sl_roster_elt = g_slist_next(sl_roster_elt); | |
472 } | |
78 | 473 |
81 | 474 // Check if we can find our saved current_buddy... |
475 if (roster_current_buddy) | |
476 current_buddy = g_list_find(buddylist, roster_current_buddy); | |
78 | 477 // current_buddy initialization |
478 if (!current_buddy || (g_list_position(buddylist, current_buddy) == -1)) | |
81 | 479 current_buddy = g_list_first(buddylist); |
128 | 480 // XXX Maybe we should set update_roster to TRUE there? |
72 | 481 } |
482 | |
483 // buddy_hide_group(roster, hide) | |
484 // "hide" values: 1=hide 0=show_all -1=invert | |
485 void buddy_hide_group(gpointer rosterdata, int hide) | |
486 { | |
207 | 487 roster *roster_usr = rosterdata; |
72 | 488 if (hide > 0) // TRUE (hide) |
207 | 489 roster_usr->flags |= ROSTER_FLAG_HIDE; |
72 | 490 else if (hide < 0) // NEG (invert) |
207 | 491 roster_usr->flags ^= ROSTER_FLAG_HIDE; |
72 | 492 else // FALSE (don't hide) |
207 | 493 roster_usr->flags &= ~ROSTER_FLAG_HIDE; |
72 | 494 } |
495 | |
496 const char *buddy_getjid(gpointer rosterdata) | |
497 { | |
207 | 498 roster *roster_usr = rosterdata; |
499 return roster_usr->jid; | |
500 } | |
501 | |
210 | 502 // buddy_setgroup() |
503 // Change the group of current buddy | |
504 // | |
505 // Warning! This function changes current_buddy! | |
506 // Warning! Old buddy is deleted, so you can't acces to its jid/name after | |
507 // calling this function. | |
508 void buddy_setgroup(gpointer rosterdata, char *newgroupname) | |
509 { | |
510 roster *roster_usr = rosterdata; | |
511 GSList **sl_group; | |
512 GSList *sl_clone; | |
513 roster *roster_clone; | |
514 | |
515 // A group has no group :) | |
516 if (roster_usr->type & ROSTER_TYPE_GROUP) return; | |
517 | |
518 // Remove the buddy from current group | |
519 sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list; | |
520 *sl_group = g_slist_remove(*sl_group, rosterdata); | |
521 | |
522 // Add the buddy to its new group; actually we "clone" this buddy... | |
523 sl_clone = roster_add_user(roster_usr->jid, roster_usr->name, | |
524 newgroupname, roster_usr->type); | |
525 roster_clone = (roster*)sl_clone->data; | |
526 roster_clone->status = roster_usr->status; | |
527 roster_clone->flags = roster_usr->flags; | |
528 | |
529 // Free old buddy | |
221 | 530 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
531 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
532 if (roster_usr->status_msg) g_free((gchar*)roster_usr->status_msg); | |
210 | 533 g_free(roster_usr); |
534 | |
212 | 535 // If new new group is folded, the curren_buddy will be lost, and the |
536 // chat window won't be correctly refreshed. So we make sure it isn't... | |
537 ((roster*)((GSList*)roster_clone->list)->data)->flags &= ~ROSTER_FLAG_HIDE; | |
538 | |
539 // Little trick to have current_body pointing to the cloned buddy | |
540 buddylist = g_list_append(buddylist, roster_clone); | |
541 current_buddy = g_list_find(buddylist, roster_clone); | |
542 | |
210 | 543 buddylist_build(); |
544 } | |
545 | |
207 | 546 void buddy_setname(gpointer rosterdata, char *newname) |
547 { | |
548 roster *roster_usr = rosterdata; | |
209 | 549 GSList **sl_group; |
207 | 550 |
551 // TODO For groups, we need to check for unicity | |
552 // However, renaming a group boils down to moving all its buddies to | |
553 // another group, so calling this function is not really necessary... | |
554 if (roster_usr->type & ROSTER_TYPE_GROUP) return; | |
555 | |
556 if (roster_usr->name) { | |
557 g_free((gchar*)roster_usr->name); | |
558 roster_usr->name = NULL; | |
559 } | |
560 if (newname) | |
561 roster_usr->name = g_strdup(newname); | |
209 | 562 |
563 // We need to resort the group list | |
564 sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list; | |
565 *sl_group = g_slist_sort(*sl_group, (GCompareFunc)&roster_compare_name); | |
566 | |
567 buddylist_build(); | |
72 | 568 } |
569 | |
570 const char *buddy_getname(gpointer rosterdata) | |
571 { | |
207 | 572 roster *roster_usr = rosterdata; |
573 return roster_usr->name; | |
574 } | |
575 | |
576 // buddy_getgroupname() | |
577 // Returns a pointer on buddy's group name. | |
578 const char *buddy_getgroupname(gpointer rosterdata) | |
579 { | |
580 roster *roster_usr = rosterdata; | |
581 | |
582 if (roster_usr->type & ROSTER_TYPE_GROUP) | |
583 return roster_usr->name; | |
584 | |
585 // This is a user | |
586 return ((roster*)((GSList*)roster_usr->list)->data)->name; | |
72 | 587 } |
588 | |
166 | 589 // buddy_getgroup() |
590 // Returns a pointer on buddy's group. | |
591 gpointer buddy_getgroup(gpointer rosterdata) | |
592 { | |
207 | 593 roster *roster_usr = rosterdata; |
166 | 594 |
207 | 595 if (roster_usr->type & ROSTER_TYPE_GROUP) |
166 | 596 return rosterdata; |
597 | |
598 // This is a user | |
207 | 599 return (gpointer)((GSList*)roster_usr->list)->data; |
166 | 600 } |
601 | |
72 | 602 guint buddy_gettype(gpointer rosterdata) |
603 { | |
207 | 604 roster *roster_usr = rosterdata; |
605 return roster_usr->type; | |
72 | 606 } |
607 | |
608 enum imstatus buddy_getstatus(gpointer rosterdata) | |
609 { | |
207 | 610 roster *roster_usr = rosterdata; |
611 return roster_usr->status; | |
72 | 612 } |
613 | |
221 | 614 const char *buddy_getstatusmsg(gpointer rosterdata) |
615 { | |
616 roster *roster_usr = rosterdata; | |
617 return roster_usr->status_msg; | |
618 } | |
619 | |
119 | 620 // buddy_setflags() |
621 // Set one or several flags to value (TRUE/FALSE) | |
622 void buddy_setflags(gpointer rosterdata, guint flags, guint value) | |
623 { | |
207 | 624 roster *roster_usr = rosterdata; |
119 | 625 if (value) |
207 | 626 roster_usr->flags |= flags; |
119 | 627 else |
207 | 628 roster_usr->flags &= ~flags; |
119 | 629 } |
630 | |
72 | 631 guint buddy_getflags(gpointer rosterdata) |
632 { | |
207 | 633 roster *roster_usr = rosterdata; |
634 return roster_usr->flags; | |
72 | 635 } |
636 | |
265 | 637 // buddy_search(string) |
638 // Look for a buddy whose name or jid contains string. | |
639 // Search begins at current_buddy; if no match is found in the the buddylist, | |
640 // return NULL; | |
641 GList *buddy_search(char *string) | |
642 { | |
643 GList *buddy = current_buddy; | |
644 roster *roster_usr; | |
645 if (!buddylist || !current_buddy) return NULL; | |
646 for (;;) { | |
647 buddy = g_list_next(buddy); | |
648 if (!buddy) | |
649 buddy = buddylist; | |
650 | |
651 roster_usr = (roster*)buddy->data; | |
652 if (roster_usr->jid && strcasestr(roster_usr->jid, string)) | |
653 return buddy; | |
654 if (roster_usr->name && strcasestr(roster_usr->name, string)) | |
655 return buddy; | |
656 | |
657 if (buddy == current_buddy) | |
658 return NULL; // Back to the beginning, and no match found | |
659 } | |
660 } | |
661 | |
225 | 662 // compl_list(type) |
663 // Returns a list of jid's or groups. (For commands completion) | |
664 // type: ROSTER_TYPE_USER (jid's) or ROSTER_TYPE_GROUP (group names) | |
665 // The list should be freed by the caller after use. | |
666 GSList *compl_list(guint type) | |
667 { | |
668 GSList *list = NULL; | |
669 GList *buddy = buddylist; | |
670 | |
671 for ( ; buddy ; buddy = g_list_next(buddy)) { | |
672 guint btype = buddy_gettype(BUDDATA(buddy)); | |
673 | |
674 if (type == ROSTER_TYPE_GROUP) { // (group names) | |
675 if (btype == ROSTER_TYPE_GROUP) { | |
676 const char *bname = buddy_getname(BUDDATA(buddy)); | |
677 if ((bname) && (*bname)) | |
678 list = g_slist_append(list, g_strdup(bname)); | |
679 } | |
680 } else { // ROSTER_TYPE_USER (jid) | |
681 const char *bjid = buddy_getjid(BUDDATA(buddy)); | |
682 if (bjid) | |
683 list = g_slist_append(list, g_strdup(bjid)); | |
684 } | |
685 } | |
686 | |
687 return list; | |
688 } | |
689 | |
236 | 690 // unread_msg(rosterdata) |
691 // Return the next buddy with an unread message. If the parameter is NULL, | |
692 // return the first buddy with an unread message. | |
693 gpointer unread_msg(gpointer rosterdata) | |
694 { | |
695 GSList *unread, *next_unread; | |
696 | |
697 if (!unread_list) return NULL; | |
698 // First unread message | |
699 if (!rosterdata) return unread_list->data; | |
700 | |
701 unread = g_slist_find(unread_list, rosterdata); | |
702 if (!unread) return unread_list->data; | |
703 | |
704 next_unread = g_slist_next(unread); | |
705 if (next_unread) return next_unread->data; | |
706 | |
707 return unread_list->data; | |
708 } | |
709 |