Mercurial > ~mikael > mcabber > hg
comparison mcabber/src/roster.c @ 1063:4e62941df777
roster.c: refactor some functions
Remove some duplicated code.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Fri, 01 Dec 2006 20:16:18 +0100 |
parents | c0d44a9a99bc |
children | 230dca34dbea |
comparison
equal
deleted
inserted
replaced
1062:9207f2efb921 | 1063:4e62941df777 |
---|---|
112 roster_special.type = ROSTER_TYPE_SPECIAL; | 112 roster_special.type = ROSTER_TYPE_SPECIAL; |
113 } | 113 } |
114 | 114 |
115 /* ### Resources functions ### */ | 115 /* ### Resources functions ### */ |
116 | 116 |
117 static void free_all_resources(GSList **reslist) | 117 static inline void free_resource_data(res *p_res) |
118 { | 118 { |
119 GSList *lip; | 119 if (!p_res) |
120 res *p_res; | 120 return; |
121 | 121 g_free((gchar*)p_res->status_msg); |
122 for ( lip = *reslist; lip ; lip = g_slist_next(lip)) { | 122 g_free((gchar*)p_res->name); |
123 p_res = (res*)lip->data; | 123 g_free((gchar*)p_res->realjid); |
124 g_free((gchar*)p_res->status_msg); | |
125 g_free((gchar*)p_res->name); | |
126 g_free((gchar*)p_res->realjid); | |
127 #ifdef JEP0022 | 124 #ifdef JEP0022 |
128 g_free(p_res->jep22.last_msgid_sent); | 125 g_free(p_res->jep22.last_msgid_sent); |
129 g_free(p_res->jep22.last_msgid_rcvd); | 126 g_free(p_res->jep22.last_msgid_rcvd); |
130 #endif | 127 #endif |
131 #ifdef HAVE_GPGME | 128 #ifdef HAVE_GPGME |
132 g_free(p_res->pgpdata.sign_keyid); | 129 g_free(p_res->pgpdata.sign_keyid); |
133 #endif | 130 #endif |
134 } | 131 g_free(p_res); |
132 } | |
133 | |
134 static void free_all_resources(GSList **reslist) | |
135 { | |
136 GSList *lip; | |
137 | |
138 for (lip = *reslist; lip ; lip = g_slist_next(lip)) | |
139 free_resource_data((res*)lip->data); | |
135 // Free all nodes but the first (which is static) | 140 // Free all nodes but the first (which is static) |
136 g_slist_free(*reslist); | 141 g_slist_free(*reslist); |
137 *reslist = NULL; | 142 *reslist = NULL; |
138 } | 143 } |
139 | 144 |
220 rost->offline_status_message = p_res->status_msg; | 225 rost->offline_status_message = p_res->status_msg; |
221 p_res->status_msg = NULL; | 226 p_res->status_msg = NULL; |
222 } | 227 } |
223 | 228 |
224 // Free allocations and delete resource node | 229 // Free allocations and delete resource node |
225 g_free(p_res->name); | 230 free_resource_data(p_res); |
226 g_free(p_res->status_msg); | |
227 g_free(p_res->realjid); | |
228 #ifdef JEP0022 | |
229 g_free(p_res->jep22.last_msgid_sent); | |
230 g_free(p_res->jep22.last_msgid_rcvd); | |
231 #endif | |
232 #ifdef HAVE_GPGME | |
233 g_free(p_res->pgpdata.sign_keyid); | |
234 #endif | |
235 rost->resource = g_slist_delete_link(rost->resource, p_res_elt); | 231 rost->resource = g_slist_delete_link(rost->resource, p_res_elt); |
236 return; | 232 return; |
237 } | 233 } |
238 | 234 |
239 | 235 |
240 /* ### Roster functions ### */ | 236 /* ### Roster functions ### */ |
237 | |
238 static inline void free_roster_user_data(roster *roster_usr) | |
239 { | |
240 if (!roster_usr) | |
241 return; | |
242 g_free((gchar*)roster_usr->jid); | |
243 g_free((gchar*)roster_usr->name); | |
244 g_free((gchar*)roster_usr->nickname); | |
245 g_free((gchar*)roster_usr->topic); | |
246 g_free((gchar*)roster_usr->offline_status_message); | |
247 free_all_resources(&roster_usr->resource); | |
248 g_free(roster_usr); | |
249 } | |
241 | 250 |
242 // Comparison function used to search in the roster (compares jids and types) | 251 // Comparison function used to search in the roster (compares jids and types) |
243 static gint roster_compare_jid_type(roster *a, roster *b) { | 252 static gint roster_compare_jid_type(roster *a, roster *b) { |
244 if (! (a->type & b->type)) | 253 if (! (a->type & b->type)) |
245 return -1; // arbitrary (but should be != 0, of course) | 254 return -1; // arbitrary (but should be != 0, of course) |
400 if (node) unread_list = g_slist_delete_link(unread_list, node); | 409 if (node) unread_list = g_slist_delete_link(unread_list, node); |
401 // If there is a pending unread message, keep track of it | 410 // If there is a pending unread message, keep track of it |
402 if (roster_usr->flags & ROSTER_FLAG_MSG) | 411 if (roster_usr->flags & ROSTER_FLAG_MSG) |
403 unread_jid_add(roster_usr->jid); | 412 unread_jid_add(roster_usr->jid); |
404 | 413 |
405 // Let's free memory (jid, name, status message) | 414 // Let's free roster_usr memory (jid, name, status message...) |
406 g_free((gchar*)roster_usr->jid); | 415 free_roster_user_data(roster_usr); |
407 g_free((gchar*)roster_usr->name); | |
408 g_free((gchar*)roster_usr->nickname); | |
409 g_free((gchar*)roster_usr->topic); | |
410 g_free((gchar*)roster_usr->offline_status_message); | |
411 free_all_resources(&roster_usr->resource); | |
412 g_free(roster_usr); | |
413 | 416 |
414 // That's a little complex, we need to dereference twice | 417 // That's a little complex, we need to dereference twice |
415 sl_group = ((roster*)sl_user->data)->list; | 418 sl_group = ((roster*)sl_user->data)->list; |
416 sl_group_listptr = &((roster*)(sl_group->data))->list; | 419 sl_group_listptr = &((roster*)(sl_group->data))->list; |
417 *sl_group_listptr = g_slist_delete_link(*sl_group_listptr, sl_user); | 420 *sl_group_listptr = g_slist_delete_link(*sl_group_listptr, sl_user); |
443 while (sl_usr) { | 446 while (sl_usr) { |
444 roster *roster_usr = (roster*)sl_usr->data; | 447 roster *roster_usr = (roster*)sl_usr->data; |
445 // If there is a pending unread message, keep track of it | 448 // If there is a pending unread message, keep track of it |
446 if (roster_usr->flags & ROSTER_FLAG_MSG) | 449 if (roster_usr->flags & ROSTER_FLAG_MSG) |
447 unread_jid_add(roster_usr->jid); | 450 unread_jid_add(roster_usr->jid); |
448 // Free name and jid | 451 // Free roster_usr data (jid, name, status message...) |
449 g_free((gchar*)roster_usr->jid); | 452 free_roster_user_data(roster_usr); |
450 g_free((gchar*)roster_usr->name); | |
451 g_free((gchar*)roster_usr->nickname); | |
452 g_free((gchar*)roster_usr->topic); | |
453 g_free((gchar*)roster_usr->offline_status_message); | |
454 free_all_resources(&roster_usr->resource); | |
455 g_free(roster_usr); | |
456 sl_usr = g_slist_next(sl_usr); | 453 sl_usr = g_slist_next(sl_usr); |
457 } | 454 } |
458 // Free group's users list | 455 // Free group's users list |
459 if (roster_grp->list) | 456 if (roster_grp->list) |
460 g_slist_free(roster_grp->list); | 457 g_slist_free(roster_grp->list); |