From bb2257973e90c9d96ae981f5c682e8a7f5852369 Mon Sep 17 00:00:00 2001 From: JFreegman Date: Sun, 30 Jun 2019 14:51:13 -0400 Subject: [PATCH] Load conference titles on startup for saved conferences (#43) --- src/chat_commands.c | 2 +- src/global_commands.c | 2 +- src/group_commands.c | 2 +- src/groupchat.c | 3 ++- src/groupchat.h | 2 +- src/misc_tools.c | 4 ++++ src/toxic.c | 18 +++++++++++++++++- 7 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index eb53241..602015a 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -132,7 +132,7 @@ void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar return; } - if (init_groupchat_win(prompt, m, groupnum, type) == -1) { + if (init_groupchat_win(prompt, m, groupnum, type, NULL, 0) == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); tox_conference_delete(m, groupnum, NULL); return; diff --git a/src/global_commands.c b/src/global_commands.c index f443921..45fdec8 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -353,7 +353,7 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg return; } - if (init_groupchat_win(prompt, m, groupnum, type) == -1) { + if (init_groupchat_win(prompt, m, groupnum, type, NULL, 0) == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); tox_conference_delete(m, groupnum, NULL); return; diff --git a/src/group_commands.c b/src/group_commands.c index 9ceffea..2dc542d 100644 --- a/src/group_commands.c +++ b/src/group_commands.c @@ -36,7 +36,7 @@ void cmd_set_title(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg if (argc < 1) { size_t tlen = tox_conference_get_title_size(m, self->num, &err); - if (err != TOX_ERR_CONFERENCE_TITLE_OK) { + if (err != TOX_ERR_CONFERENCE_TITLE_OK || tlen >= sizeof(title)) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Title is not set"); return; } diff --git a/src/groupchat.c b/src/groupchat.c index b21e918..744a867 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -129,7 +129,7 @@ static void kill_groupchat_window(ToxWindow *self) del_window(self); } -int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t type) +int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t type, const char *title, size_t title_length) { if (groupnum > MAX_GROUPCHAT_NUM) { return -1; @@ -146,6 +146,7 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t typ groupchats[i].start_time = get_unix_time(); set_active_window_index(groupchats[i].chatwin); + set_window_title(self, title, title_length); if (i == max_groupchat_index) { ++max_groupchat_index; diff --git a/src/groupchat.h b/src/groupchat.h index bc19034..43d06d7 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -56,7 +56,7 @@ typedef struct { /* Frees all Toxic associated data structures for a groupchat (does not call tox_conference_delete() ) */ void free_groupchat(ToxWindow *self, Tox *m, uint32_t groupnum); -int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t type); +int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t type, const char *title, size_t title_length); /* destroys and re-creates groupchat window with or without the peerlist */ void redraw_groupchat_win(ToxWindow *self); diff --git a/src/misc_tools.c b/src/misc_tools.c index bb5324f..78bc9cb 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -539,6 +539,10 @@ int check_file_signature(const char *signature, size_t size, FILE *fp) /* sets window title in tab bar. */ void set_window_title(ToxWindow *self, const char *title, int len) { + if (len <= 0 || !title) { + return; + } + char cpy[TOXIC_MAX_NAME_LENGTH + 1]; if (self->is_groupchat) { /* keep groupnumber in title */ diff --git a/src/toxic.c b/src/toxic.c index 67fc2c3..bf53ed1 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -344,7 +344,23 @@ static void load_groups(ToxWindow *prompt, Tox *m) continue; } - if (init_groupchat_win(prompt, m, groupnum, type) == -1) { + Tox_Err_Conference_Title t_err; + size_t length = tox_conference_get_title_size(m, groupnum, &t_err); + uint8_t title[MAX_STR_SIZE]; + + if (t_err != TOX_ERR_CONFERENCE_TITLE_OK || length >= sizeof(title)) { + length = 0; + } else { + tox_conference_get_title(m, groupnum, title, &t_err); + + if (t_err != TOX_ERR_CONFERENCE_TITLE_OK) { + length = 0; + } + } + + title[length] = 0; + + if (init_groupchat_win(prompt, m, groupnum, type, (const char *) title, length) == -1) { tox_conference_delete(m, groupnum, NULL); continue; }