diff mcabber/src/roster.c @ 210:f64818ba3503

[/trunk] Changeset 222 by mikael * Add /move command * roster: Add buddy_setgroup() * roster.c: Fix a small memory leak * Keep documentation up-to-date
author mikael
date Sun, 08 May 2005 07:02:11 +0000
parents 353a4f8a3f61
children 465d98d2f8e3
line wrap: on
line diff
--- a/mcabber/src/roster.c	Sat May 07 21:21:57 2005 +0000
+++ b/mcabber/src/roster.c	Sun May 08 07:02:11 2005 +0000
@@ -180,6 +180,7 @@
     g_free((gchar*)roster_usr->jid);
   if (roster_usr->name)
     g_free((gchar*)roster_usr->name);
+  g_free(roster_usr);
 
   // That's a little complex, we need to dereference twice
   sl_group = ((roster*)sl_user->data)->list;
@@ -189,12 +190,12 @@
   // We need to rebuild the list
   if (current_buddy)
     buddylist_build();
-  // TODO What we should do, too, is to check if the deleted node is
+  // TODO What we could do, too, is to check if the deleted node is
   // current_buddy, in which case we could move current_buddy to the
   // previous (or next) node.
 }
 
-// Free all roster data.  Call buddylist_build() to free the buddylist.
+// Free all roster data and call buddylist_build() to free the buddylist.
 void roster_free(void)
 {
   GSList *sl_grp = groups;
@@ -475,6 +476,42 @@
   return roster_usr->jid;
 }
 
+//  buddy_setgroup()
+// Change the group of current buddy
+//
+// Warning!  This function changes current_buddy!
+// Warning!  Old buddy is deleted, so you can't acces to its jid/name after
+//           calling this function.
+void buddy_setgroup(gpointer rosterdata, char *newgroupname)
+{
+  roster *roster_usr = rosterdata;
+  GSList **sl_group;
+  GSList *sl_clone;
+  roster *roster_clone;
+
+  // A group has no group :)
+  if (roster_usr->type & ROSTER_TYPE_GROUP) return;
+
+  // Remove the buddy from current group
+  sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list;
+  *sl_group = g_slist_remove(*sl_group, rosterdata);
+  
+  // Add the buddy to its new group; actually we "clone" this buddy...
+  sl_clone = roster_add_user(roster_usr->jid, roster_usr->name,
+          newgroupname, roster_usr->type);
+  roster_clone = (roster*)sl_clone->data;
+  roster_clone->status = roster_usr->status;
+  roster_clone->flags  = roster_usr->flags;
+
+  // Free old buddy
+  if (roster_usr->jid)  g_free((gchar*)roster_usr->jid);
+  if (roster_usr->name) g_free((gchar*)roster_usr->name);
+  g_free(roster_usr);
+
+  buddylist_build();
+  current_buddy = g_list_find(buddylist, roster_clone);
+}
+
 void buddy_setname(gpointer rosterdata, char *newname)
 {
   roster *roster_usr = rosterdata;