From 2a02bb10e5b818c9ffabf0c4499a53ba63002b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Mon, 10 Jul 2023 07:31:28 +0200 Subject: [PATCH] core: fix renaming of options with command `/item rename` (closes #1978) The options `weechat.custom_bar_item.xxx.*` are now properly renamed to the new item name. This fixes a save issue (item saved with old name in config) and a crash if a new item is created with the old name. --- ChangeLog.adoc | 1 + src/gui/gui-bar-item-custom.c | 41 +++++++++++++++++++-- tests/unit/gui/test-gui-bar-item-custom.cpp | 3 ++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/ChangeLog.adoc b/ChangeLog.adoc index e85aef664..c29efcbc1 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -27,6 +27,7 @@ New features:: Bug fixes:: + * core: fix renaming of options with command `/item rename` (issue #1978) * core: don't send "key_pressed" signal again for the same key press (issue #1976) * core: don't send "key_combo_*" signals for incomplete keys (issue #1976) * core: add key ctrl-backspace in /help key (issue #1975) diff --git a/src/gui/gui-bar-item-custom.c b/src/gui/gui-bar-item-custom.c index 2864f3cb2..64065c1fe 100644 --- a/src/gui/gui-bar-item-custom.c +++ b/src/gui/gui-bar-item-custom.c @@ -443,10 +443,18 @@ gui_bar_item_custom_new (const char *name, const char *conditions, name, GUI_BAR_ITEM_CUSTOM_OPTION_CONDITIONS, conditions); + if (!option_conditions) + return NULL; + option_content = gui_bar_item_custom_create_option ( name, GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT, content); + if (!option_content) + { + config_file_option_free (option_conditions, 0); + return NULL; + } new_bar_item_custom = gui_bar_item_custom_new_with_options ( name, @@ -517,21 +525,48 @@ int gui_bar_item_custom_rename (struct t_gui_bar_item_custom *item, const char *new_name) { + char *old_name, *option_name; + int i, length; + if (!item || !gui_bar_item_custom_name_valid (new_name)) return 0; if (gui_bar_item_custom_search (new_name)) return 0; + old_name = strdup (item->name); + if (!old_name) + return 0; + + length = strlen (new_name) + 128; + option_name = malloc (length); + if (!option_name) + { + free (old_name); + return 0; + } + free (item->bar_item->name); item->bar_item->name = strdup (new_name); - gui_bar_item_update (item->name); - gui_bar_item_update (item->bar_item->name); - free (item->name); item->name = strdup (new_name); + for (i = 0; i < GUI_BAR_ITEM_CUSTOM_NUM_OPTIONS; i++) + { + snprintf (option_name, length, + "%s.%s", + new_name, + gui_bar_item_custom_option_string[i]); + config_file_option_rename (item->options[i], option_name); + } + + gui_bar_item_update (old_name); + gui_bar_item_update (item->name); + + free (old_name); + free (option_name); + return 1; } diff --git a/tests/unit/gui/test-gui-bar-item-custom.cpp b/tests/unit/gui/test-gui-bar-item-custom.cpp index 4ac1a849c..f3c221851 100644 --- a/tests/unit/gui/test-gui-bar-item-custom.cpp +++ b/tests/unit/gui/test-gui-bar-item-custom.cpp @@ -455,6 +455,9 @@ TEST(GuiBarItemCustom, Rename) CHECK(new_item->bar_item); STRCMP_EQUAL("test3", new_item->bar_item->name); + STRCMP_EQUAL("test3.conditions", new_item->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONDITIONS]->name); + STRCMP_EQUAL("test3.content", new_item->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT]->name); + gui_bar_item_custom_free (new_item); gui_bar_item_custom_free (new_item2); }