From ea62c3f29366f0f7b4b47bf0e52ee34882c33897 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Tue, 4 Jun 2019 11:16:24 +0200 Subject: Determine chat window names beforehand Save the name for displaying the windows in the statusbar inside the tab object. So far we calculated them repeatedly and this created issues when we lost the connection. Regards https://github.com/profanity-im/profanity/issues/1083 --- src/ui/statusbar.c | 57 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/ui/statusbar.c b/src/ui/statusbar.c index d0ce7309..46320a04 100644 --- a/src/ui/statusbar.c +++ b/src/ui/statusbar.c @@ -57,6 +57,7 @@ typedef struct _status_bar_tab_t { win_type_t window_type; char *identifier; gboolean highlight; + char *display_name; } StatusBarTab; typedef struct _status_bar_t { @@ -94,6 +95,7 @@ status_bar_init(void) StatusBarTab *console = malloc(sizeof(StatusBarTab)); console->window_type = WIN_CONSOLE; console->identifier = strdup("console"); + console->display_name = NULL; g_hash_table_insert(statusbar->tabs, GINT_TO_POINTER(1), console); statusbar->current_tab = 1; @@ -182,6 +184,24 @@ status_bar_active(const int win, win_type_t wintype, char *identifier) tab->identifier = strdup(identifier); tab->highlight = FALSE; tab->window_type = wintype; + tab->display_name = NULL; + + if (tab->window_type == WIN_CHAT) { + PContact contact = roster_get_contact(tab->identifier); + if (contact && p_contact_name(contact)) { + tab->display_name = strdup(p_contact_name(contact)); + } else { + char *pref = prefs_get_string(PREF_STATUSBAR_CHAT); + if (g_strcmp0("user", pref) == 0) { + Jid *jidp = jid_create(tab->identifier); + tab->display_name = strdup(jidp->localpart); + jid_destroy(jidp); + } else { + tab->display_name = strdup(tab->identifier); + } + } + } + g_hash_table_replace(statusbar->tabs, GINT_TO_POINTER(true_win), tab); status_bar_draw(); @@ -199,6 +219,24 @@ status_bar_new(const int win, win_type_t wintype, char* identifier) tab->identifier = strdup(identifier); tab->highlight = TRUE; tab->window_type = wintype; + tab->display_name = NULL; + + if (tab->window_type == WIN_CHAT) { + PContact contact = roster_get_contact(tab->identifier); + if (contact && p_contact_name(contact)) { + tab->display_name = strdup(p_contact_name(contact)); + } else { + char *pref = prefs_get_string(PREF_STATUSBAR_CHAT); + if (g_strcmp0("user", pref) == 0) { + Jid *jidp = jid_create(tab->identifier); + tab->display_name = strdup(jidp->localpart); + jid_destroy(jidp); + } else { + tab->display_name = strdup(tab->identifier); + } + } + } + g_hash_table_replace(statusbar->tabs, GINT_TO_POINTER(true_win), tab); status_bar_draw(); @@ -461,8 +499,12 @@ _destroy_tab(StatusBarTab *tab) if (tab->identifier) { free(tab->identifier); } + if (tab->display_name) { + free(tab->display_name); + } free(tab); } + tab = NULL; } static int @@ -520,19 +562,8 @@ _display_name(StatusBarTab *tab) } else if (tab->window_type == WIN_PLUGIN) { fullname = strdup(tab->identifier); } else if (tab->window_type == WIN_CHAT) { - PContact contact = roster_get_contact(tab->identifier); - if (contact && p_contact_name(contact)) { - fullname = strdup(p_contact_name(contact)); - } else { - char *pref = prefs_get_string(PREF_STATUSBAR_CHAT); - if (g_strcmp0("user", pref) == 0) { - Jid *jidp = jid_create(tab->identifier); - char *user = strdup(jidp->localpart); - jid_destroy(jidp); - fullname = user; - } else { - fullname = strdup(tab->identifier); - } + if (tab && tab->display_name) { + fullname = strdup(tab->display_name); } } else if (tab->window_type == WIN_MUC) { char *pref = prefs_get_string(PREF_STATUSBAR_ROOM); -- cgit 1.4.1-2-gfad0 From 2d00444702661d7f5b989b2a7556aafeab4309fd Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Tue, 4 Jun 2019 14:50:25 +0200 Subject: statusbar: reduce duplicate code status_bar_new() and status_bar_active() are almost identical. Let's use one helper function to not duplicate code. I thought about renaming both functions into one and adding another parameter but didn't come up with a good name for the function that clearly describes what it does. So staying with current names + helper functions. --- src/ui/statusbar.c | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/ui/statusbar.c b/src/ui/statusbar.c index 46320a04..4570be50 100644 --- a/src/ui/statusbar.c +++ b/src/ui/statusbar.c @@ -173,7 +173,7 @@ status_bar_inactive(const int win) } void -status_bar_active(const int win, win_type_t wintype, char *identifier) +_create_tab(const int win, win_type_t wintype, char *identifier, gboolean highlight) { int true_win = win; if (true_win == 0) { @@ -182,7 +182,7 @@ status_bar_active(const int win, win_type_t wintype, char *identifier) StatusBarTab *tab = malloc(sizeof(StatusBarTab)); tab->identifier = strdup(identifier); - tab->highlight = FALSE; + tab->highlight = highlight; tab->window_type = wintype; tab->display_name = NULL; @@ -208,38 +208,15 @@ status_bar_active(const int win, win_type_t wintype, char *identifier) } void -status_bar_new(const int win, win_type_t wintype, char* identifier) +status_bar_active(const int win, win_type_t wintype, char *identifier) { - int true_win = win; - if (true_win == 0) { - true_win = 10; - } - - StatusBarTab *tab = malloc(sizeof(StatusBarTab)); - tab->identifier = strdup(identifier); - tab->highlight = TRUE; - tab->window_type = wintype; - tab->display_name = NULL; - - if (tab->window_type == WIN_CHAT) { - PContact contact = roster_get_contact(tab->identifier); - if (contact && p_contact_name(contact)) { - tab->display_name = strdup(p_contact_name(contact)); - } else { - char *pref = prefs_get_string(PREF_STATUSBAR_CHAT); - if (g_strcmp0("user", pref) == 0) { - Jid *jidp = jid_create(tab->identifier); - tab->display_name = strdup(jidp->localpart); - jid_destroy(jidp); - } else { - tab->display_name = strdup(tab->identifier); - } - } - } - - g_hash_table_replace(statusbar->tabs, GINT_TO_POINTER(true_win), tab); + _create_tab(win, wintype, identifier, FALSE); +} - status_bar_draw(); +void +status_bar_new(const int win, win_type_t wintype, char* identifier) +{ + _create_tab(win, wintype, identifier, TRUE); } void -- cgit 1.4.1-2-gfad0 From b210fb3603c4b52d478dd81810c6b4c38210bfd6 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Tue, 4 Jun 2019 16:02:56 +0200 Subject: statusbar: check if roster exists We destory the roster in ev_disconnect_cleanup(). Adding a function to test if the roster has been destroyed and testing for it in the statusbar. So now when the connection is lost 'Lost connection' is printed in all open windows. We can then reconnect with `/connect accountname`. Should fix https://github.com/profanity-im/profanity/issues/1083 --- src/ui/statusbar.c | 6 ++++-- src/xmpp/roster_list.c | 8 ++++++++ src/xmpp/roster_list.h | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/ui/statusbar.c b/src/ui/statusbar.c index 4570be50..0e24ab3f 100644 --- a/src/ui/statusbar.c +++ b/src/ui/statusbar.c @@ -187,7 +187,10 @@ _create_tab(const int win, win_type_t wintype, char *identifier, gboolean highli tab->display_name = NULL; if (tab->window_type == WIN_CHAT) { - PContact contact = roster_get_contact(tab->identifier); + PContact contact = NULL; + if (roster_exists()) { + contact = roster_get_contact(tab->identifier); + } if (contact && p_contact_name(contact)) { tab->display_name = strdup(p_contact_name(contact)); } else { @@ -601,5 +604,4 @@ _display_name(StatusBarTab *tab) g_free(trimmed); return trimmedname; - } diff --git a/src/xmpp/roster_list.c b/src/xmpp/roster_list.c index 81a51581..7954e3de 100644 --- a/src/xmpp/roster_list.c +++ b/src/xmpp/roster_list.c @@ -707,3 +707,11 @@ roster_process_pending_presence(void) g_slist_free(roster_pending_presence); roster_pending_presence = NULL; } + +gboolean +roster_exists(void) { + if (roster != NULL) { + return TRUE; + } + return FALSE; +} diff --git a/src/xmpp/roster_list.h b/src/xmpp/roster_list.h index 57a932be..93f1253e 100644 --- a/src/xmpp/roster_list.h +++ b/src/xmpp/roster_list.h @@ -73,5 +73,6 @@ char* roster_get_msg_display_name(const char *const barejid, const char *const r gint roster_compare_name(PContact a, PContact b); gint roster_compare_presence(PContact a, PContact b); void roster_process_pending_presence(void); +gboolean roster_exists(void); #endif -- cgit 1.4.1-2-gfad0