1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 15:53:26 +01:00

improve friendlis sorting function

This commit is contained in:
Jfreegman 2013-11-24 18:22:48 -05:00
parent e625bffbb4
commit 3470a5eef7
2 changed files with 15 additions and 24 deletions

View File

@ -27,39 +27,27 @@ static int friendlist_index[MAX_FRIENDS_NUM] = {0};
int index_name_cmp(const void *n1, const void *n2)
{
return name_compare(friends[*(int *) n1].name, friends[*(int *) n2].name);
int res = name_compare(friends[*(int *) n1].name, friends[*(int *) n2].name);
int k = 100;
/* Use weight to make qsort always put online friends before offline */
res = friends[*(int *) n1].online ? (res - k) : (res + k);
res = friends[*(int *) n2].online ? (res + k) : (res - k);
return res;
}
/* sorts friendlist_index by connection status */
/* sorts friendlist_index first by connection status then alphabetically */
void sort_friendlist_index(void)
{
int on_friends[MAX_FRIENDS_NUM];
int off_friends[MAX_FRIENDS_NUM];
int on_cnt = 0;
int off_cnt = 0;
int i;
/* split friends into online and offline groups */
for (i = 0; i < max_friends_index; ++i) {
if (!friends[i].active)
continue;
if (friends[i].online)
on_friends[on_cnt++] = friends[i].num;
else
off_friends[off_cnt++] = friends[i].num;
if (friends[i].active)
friendlist_index[i] = friends[i].num;
}
/* Sort both groups alphabetically*/
qsort(on_friends, on_cnt, sizeof(int), index_name_cmp);
qsort(off_friends, off_cnt, sizeof(int), index_name_cmp);
/* update friendlist_index, putting online friends before offline friends */
for (i = 0; i < on_cnt; ++i)
friendlist_index[i] = on_friends[i];
for (i = on_cnt; i < num_friends; ++i)
friendlist_index[i] = off_friends[i-on_cnt];
qsort(friendlist_index, num_friends, sizeof(int), index_name_cmp);
}
static void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len)
@ -89,6 +77,7 @@ static void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint
len = strlen(str) + 1;
memcpy(friends[num].name, str, len);
friends[num].namelength = len;
sort_friendlist_index();
}
static void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status)

View File

@ -6,6 +6,8 @@
ToxWindow new_friendlist(void);
void disable_chatwin(int f_num);
int get_friendnum(uint8_t *name);
/* sorts friendlist_index first by connection status then alphabetically */
void sort_friendlist_index(void);
#endif /* end of include guard: FRIENDLIST_H_53I41IM */