From 12b997c5f34776f34634d4ca155a14586aebc905 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Thu, 23 Mar 2023 07:41:25 +0100 Subject: ui: make it easier to find non covered window types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rewrite `win_get_title()` to using switch without a default case. So the compiler warns us (`enumeration value ‘WIN_XXX’ not handled in switch`) in case we add a new window type (WIN_CHAT, WIN_PRIV etc) and forget to adapt this function. Add assert() in the end to make compiler happy when he realizes there the function might have no return value (`control reaches end of non-void function`). This should ever be reached. Replace the default value of `win_to_string()`, `win_get_tab_identifier()` as well, and replace it with an assert. See discussion started at https://github.com/profanity-im/profanity/pull/1799#discussion_r1142444684 --- src/ui/window.c | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/src/ui/window.c b/src/ui/window.c index 2534dddb..3c9ed4e2 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -302,10 +302,14 @@ win_get_title(ProfWin* window) if (window == NULL) { return strdup(CONS_WIN_TITLE); } - if (window->type == WIN_CONSOLE) { + + switch (window->type) { + case WIN_CONSOLE: + { return strdup(CONS_WIN_TITLE); } - if (window->type == WIN_CHAT) { + case WIN_CHAT: + { ProfChatWin* chatwin = (ProfChatWin*)window; assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK); jabber_conn_status_t conn_status = connection_get_status(); @@ -321,7 +325,8 @@ win_get_title(ProfWin* window) return strdup(chatwin->barejid); } } - if (window->type == WIN_MUC) { + case WIN_MUC: + { ProfMucWin* mucwin = (ProfMucWin*)window; assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK); @@ -339,30 +344,37 @@ win_get_title(ProfWin* window) return g_string_free(title, FALSE); } - if (window->type == WIN_CONFIG) { - ProfConfWin* confwin = (ProfConfWin*)window; - assert(confwin->memcheck == PROFCONFWIN_MEMCHECK); - GString* title = g_string_new(confwin->roomjid); - g_string_append(title, " config"); - if (confwin->form->modified) { - g_string_append(title, " *"); + case WIN_CONFIG: + { + if (window->type == WIN_CONFIG) { + ProfConfWin* confwin = (ProfConfWin*)window; + assert(confwin->memcheck == PROFCONFWIN_MEMCHECK); + GString* title = g_string_new(confwin->roomjid); + g_string_append(title, " config"); + if (confwin->form->modified) { + g_string_append(title, " *"); + } + return g_string_free(title, FALSE); } - return g_string_free(title, FALSE); } - if (window->type == WIN_PRIVATE) { + case WIN_PRIVATE: + { ProfPrivateWin* privatewin = (ProfPrivateWin*)window; assert(privatewin->memcheck == PROFPRIVATEWIN_MEMCHECK); return strdup(privatewin->fulljid); } - if (window->type == WIN_XML) { + case WIN_XML: + { return strdup(XML_WIN_TITLE); } - if (window->type == WIN_PLUGIN) { + case WIN_PLUGIN: + { ProfPluginWin* pluginwin = (ProfPluginWin*)window; assert(pluginwin->memcheck == PROFPLUGINWIN_MEMCHECK); return strdup(pluginwin->tag); } - if (window->type == WIN_VCARD) { + case WIN_VCARD: + { ProfVcardWin* vcardwin = (ProfVcardWin*)window; assert(vcardwin->memcheck == PROFVCARDWIN_MEMCHECK); @@ -378,7 +390,8 @@ win_get_title(ProfWin* window) free(jid); return g_string_free(title, FALSE); } - return NULL; + } + assert(FALSE); } char* @@ -420,9 +433,8 @@ win_get_tab_identifier(ProfWin* window) { return strdup("xmlconsole"); } - default: - return strdup("UNKNOWN"); } + assert(FALSE); } char* @@ -503,9 +515,8 @@ win_to_string(ProfWin* window) ProfVcardWin* vcardwin = (ProfVcardWin*)window; return vcardwin_get_string(vcardwin); } - default: - return NULL; } + assert(FALSE); } void -- cgit 1.4.1-2-gfad0 From ac440e72d7dac25c1114556d863d8bcac40fe216 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Thu, 23 Mar 2023 10:23:51 +0100 Subject: Adapt to new g_string_free() usage Somehow this must have been overlooked when doing e59c401c840f379e64945734969db03b0e55ef22. --- src/ui/chatwin.c | 5 +---- src/ui/mucwin.c | 12 +++--------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c index 399d0fe0..ff425c0f 100644 --- a/src/ui/chatwin.c +++ b/src/ui/chatwin.c @@ -514,10 +514,7 @@ chatwin_get_string(ProfChatWin* chatwin) g_string_append_printf(res, ", %d unread", chatwin->unread); } - char* resstr = res->str; - g_string_free(res, FALSE); - - return resstr; + return g_string_free(res, FALSE); } void diff --git a/src/ui/mucwin.c b/src/ui/mucwin.c index a3d154e0..a4bcc191 100644 --- a/src/ui/mucwin.c +++ b/src/ui/mucwin.c @@ -943,17 +943,11 @@ mucwin_get_string(ProfMucWin* mucwin) { assert(mucwin != NULL); - GString* res = g_string_new("Room "); - g_string_append(res, mucwin->roomjid); - if (mucwin->unread > 0) { - g_string_append_printf(res, ", %d unread", mucwin->unread); + return g_strdup_printf("Room %s, %d unread", mucwin->roomjid, mucwin->unread); + } else { + return g_strdup_printf("Room %s", mucwin->roomjid); } - - char* resstr = res->str; - g_string_free(res, FALSE); - - return resstr; } void -- cgit 1.4.1-2-gfad0 From 732cbbfefc3536d4ce4e16f4eca0a5470f5c18b0 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Thu, 23 Mar 2023 10:28:47 +0100 Subject: Add WIN_VCARD to win_get_tab_identifier() Thanks to 12b997c5f34776f34634d4ca155a14586aebc905 we already found a place where we should add this missing window type. AFAIK there can be only one vcard window. --- src/ui/window.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ui/window.c b/src/ui/window.c index 3c9ed4e2..c1d5edf3 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -433,6 +433,10 @@ win_get_tab_identifier(ProfWin* window) { return strdup("xmlconsole"); } + case WIN_VCARD: + { + return strdup("vcard"); + } } assert(FALSE); } -- cgit 1.4.1-2-gfad0 a> 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307