From 5f3d2f04f7dba0bb3e01a2ef6ff61a1ca6cec6c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Sun, 5 Oct 2014 05:22:59 +0200 Subject: [PATCH] Added nicklist to group chats. --- src/twc-chat.c | 43 ++++++++++++++++++++++++++++++++--------- src/twc-chat.h | 8 ++++++++ src/twc-profile.c | 1 + src/twc-tox-callbacks.c | 32 ++++++++++++++++++++++++++++++ src/twc-tox-callbacks.h | 7 +++++++ src/twc-utils.c | 7 +++++-- 6 files changed, 87 insertions(+), 11 deletions(-) diff --git a/src/twc-chat.c b/src/twc-chat.c index 2a021a7..ced50aa 100644 --- a/src/twc-chat.c +++ b/src/twc-chat.c @@ -47,8 +47,8 @@ twc_chat_buffer_close_callback(void *data, * Create a new chat. */ struct t_twc_chat * -twc_chat_create(struct t_twc_profile *profile, - const char *name) +twc_chat_new(struct t_twc_profile *profile, + const char *name) { struct t_twc_chat *chat = malloc(sizeof(struct t_twc_chat)); if (!chat) @@ -56,6 +56,7 @@ twc_chat_create(struct t_twc_profile *profile, chat->profile = profile; chat->friend_number = chat->group_number = -1; + chat->nicks = NULL; size_t full_name_size = strlen(profile->name) + 1 + strlen(name) + 1; char *full_name = malloc(full_name_size); @@ -90,7 +91,7 @@ twc_chat_new_friend(struct t_twc_profile *profile, char buffer_name[TOX_CLIENT_ID_SIZE * 2 + 1]; twc_bin2hex(client_id, TOX_CLIENT_ID_SIZE, buffer_name); - struct t_twc_chat *chat = twc_chat_create(profile, buffer_name); + struct t_twc_chat *chat = twc_chat_new(profile, buffer_name); if (chat) chat->friend_number = friend_number; @@ -98,7 +99,7 @@ twc_chat_new_friend(struct t_twc_profile *profile, } /** - * Create a new friend chat. + * Create a new group chat. */ struct t_twc_chat * twc_chat_new_group(struct t_twc_profile *profile, @@ -107,10 +108,19 @@ twc_chat_new_group(struct t_twc_profile *profile, char buffer_name[32]; sprintf(buffer_name, "group_chat_%d", group_number); - struct t_twc_chat *chat = twc_chat_create(profile, buffer_name); + struct t_twc_chat *chat = twc_chat_new(profile, buffer_name); if (chat) chat->group_number = group_number; + chat->nicklist_group = weechat_nicklist_add_group(chat->buffer, NULL, + NULL, NULL, true); + chat->nicks = weechat_hashtable_new(256, + WEECHAT_HASHTABLE_INTEGER, + WEECHAT_HASHTABLE_POINTER, + NULL, NULL); + + weechat_buffer_set(chat->buffer, "nicklist", "1"); + return chat; } @@ -279,8 +289,12 @@ twc_chat_send_message(struct t_twc_chat *chat, } else if (chat->group_number >= 0) { - tox_group_message_send(chat->profile->tox, chat->group_number, - (uint8_t *)message, strlen(message)); + if (message_type == TWC_MESSAGE_TYPE_MESSAGE) + tox_group_message_send(chat->profile->tox, chat->group_number, + (uint8_t *)message, strlen(message)); + else if (message_type == TWC_MESSAGE_TYPE_ACTION) + tox_group_action_send(chat->profile->tox, chat->group_number, + (uint8_t *)message, strlen(message)); } } @@ -308,11 +322,22 @@ twc_chat_buffer_close_callback(void *data, struct t_twc_chat *chat = data; twc_list_remove_with_data(chat->profile->chats, chat); - free(chat); + twc_chat_free(chat); return WEECHAT_RC_OK; } +/** + * Free a chat object. + */ +void +twc_chat_free(struct t_twc_chat *chat) +{ + if (chat->nicks) + weechat_hashtable_free(chat->nicks); + free(chat); +} + /** * Free all chats connected to a profile. */ @@ -324,7 +349,7 @@ twc_chat_free_list(struct t_twc_list *list) { weechat_buffer_set_pointer(chat->buffer, "close_callback", NULL); weechat_buffer_close(chat->buffer); - free(chat); + twc_chat_free(chat); } free(list); diff --git a/src/twc-chat.h b/src/twc-chat.h index bbe3091..a37efdb 100644 --- a/src/twc-chat.h +++ b/src/twc-chat.h @@ -23,6 +23,8 @@ #include #include +struct t_twc_list; + extern const char *twc_tag_unsent_message; extern const char *twc_tag_sent_message; extern const char *twc_tag_received_message; @@ -40,6 +42,9 @@ struct t_twc_chat struct t_gui_buffer *buffer; int32_t friend_number; int32_t group_number; + + struct t_gui_nick_group *nicklist_group; + struct t_hashtable *nicks; }; struct t_twc_chat * @@ -70,6 +75,9 @@ twc_chat_send_message(struct t_twc_chat *chat, void twc_chat_queue_refresh(struct t_twc_chat *chat); +void +twc_chat_free(struct t_twc_chat *chat); + void twc_chat_free_list(struct t_twc_list *list); diff --git a/src/twc-profile.c b/src/twc-profile.c index 0228d90..0367624 100644 --- a/src/twc-profile.c +++ b/src/twc-profile.c @@ -273,6 +273,7 @@ twc_profile_load(struct t_twc_profile *profile) tox_callback_group_invite(profile->tox, twc_group_invite_callback, profile); tox_callback_group_message(profile->tox, twc_group_message_callback, profile); tox_callback_group_action(profile->tox, twc_group_action_callback, profile); + tox_callback_group_namelist_change(profile->tox, twc_group_namelist_change_callback, profile); } /** diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index 51492ea..33f6f4c 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -322,3 +322,35 @@ twc_group_action_callback(Tox *tox, TWC_MESSAGE_TYPE_ACTION); } +void +twc_group_namelist_change_callback(Tox *tox, + int group_number, + int peer_number, + uint8_t change_type, + void *data) +{ + struct t_twc_profile *profile = data; + struct t_twc_chat *chat = twc_chat_search_group(profile, + group_number, + false); + + struct t_gui_nick *nick; + char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); + + if (change_type == TOX_CHAT_CHANGE_PEER_DEL + || change_type == TOX_CHAT_CHANGE_PEER_NAME) + { + nick = weechat_hashtable_get(chat->nicks, &peer_number); + weechat_nicklist_remove_nick(chat->buffer, nick); + weechat_hashtable_remove(chat->nicks, &peer_number); + } + + if (change_type == TOX_CHAT_CHANGE_PEER_ADD + || change_type == TOX_CHAT_CHANGE_PEER_NAME) + { + nick = weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, + name, NULL, NULL, NULL, 1); + weechat_hashtable_set(chat->nicks, &peer_number, nick); + } +} + diff --git a/src/twc-tox-callbacks.h b/src/twc-tox-callbacks.h index 3156e2a..96f6aa8 100644 --- a/src/twc-tox-callbacks.h +++ b/src/twc-tox-callbacks.h @@ -96,5 +96,12 @@ twc_group_action_callback(Tox *tox, uint16_t length, void *data); +void +twc_group_namelist_change_callback(Tox *tox, + int group_number, + int peer_number, + uint8_t change_type, + void *data); + #endif // TOX_WEECHAT_TOX_CALLBACKS_H diff --git a/src/twc-utils.c b/src/twc-utils.c index 75e0b6f..c8f4253 100644 --- a/src/twc-utils.c +++ b/src/twc-utils.c @@ -111,8 +111,11 @@ twc_get_peer_name_nt(Tox *tox, int32_t group_number, int32_t peer_number) { uint8_t name[TOX_MAX_NAME_LENGTH] = {0}; - tox_group_peername(tox, group_number, peer_number, name); - return twc_null_terminate(name, strlen((char *)name)); + int length = tox_group_peername(tox, group_number, peer_number, name); + if (length >= 0) + return twc_null_terminate(name, length); + else + return ""; } /**