Preferences Dialog: fixed friends list row being added even if username was a duplicate

This fixes #2156.

preferences::add_acquaintance replaces the existing friend entry if the given username
has already been added to the acquaintance list, but the prefs dialog always added a
new row unconditionally. I've changed it to only add a new row if the username creates
a new entry, else replace the appropriate exiting row.
This commit is contained in:
Charles Dang 2017-11-03 11:18:31 +11:00
parent e0366b1352
commit 67a43390f8
4 changed files with 32 additions and 8 deletions

View file

@ -108,7 +108,7 @@ void chat_command_handler::do_ignore()
utils::string_map symbols;
symbols["nick"] = get_arg(1);
if (preferences::add_acquaintance(get_arg(1), "ignore", get_data(2))) {
if (preferences::add_acquaintance(get_arg(1), "ignore", get_data(2)).first) {
print(_("ignores list"), VGETTEXT("Added to ignore list: $nick", symbols));
chat_handler_.user_relation_changed(get_arg(1));
}
@ -127,7 +127,7 @@ void chat_command_handler::do_friend()
utils::string_map symbols;
symbols["nick"] = get_arg(1);
if (preferences::add_acquaintance(get_arg(1), "friend", get_data(2))) {
if (preferences::add_acquaintance(get_arg(1), "friend", get_data(2)).first) {
print(_("friends list"), VGETTEXT("Added to friends list: $nick", symbols));
chat_handler_.user_relation_changed(get_arg(1));
}

View file

@ -216,7 +216,11 @@ void preferences_dialog::add_friend_list_entry(const bool is_friend, text_box& t
username = username.substr(0, pos);
}
acquaintance* entry = add_acquaintance(username, (is_friend ? "friend": "ignore"), reason);
acquaintance* entry = nullptr;
bool added_new;
std::tie(entry, added_new) = add_acquaintance(username, (is_friend ? "friend": "ignore"), reason);
if(!entry) {
gui2::show_transient_message(window.video(), _("Error"), _("Invalid username"), "", false, false, true);
return;
@ -225,7 +229,27 @@ void preferences_dialog::add_friend_list_entry(const bool is_friend, text_box& t
textbox.clear();
listbox& list = find_widget<listbox>(&window, "friends_list", false);
list.add_row(get_friends_list_row_data(*entry));
//
// If this is a new entry, just add a new row. If it's not, we find the relevant
// row, remove it, and add a new row with the updated data. Should probably come
// up with a more elegant way to do this... the only reason I'm using the remove
// -and-replace method is to prevent any issues with the widgets' layout sizes.
//
if(added_new) {
list.add_row(get_friends_list_row_data(*entry));
} else {
for(unsigned i = 0; i < list.get_item_count(); ++i) {
grid* row_grid = list.get_row_grid(i);
if(find_widget<label>(row_grid, "friend_name", false).get_label() == entry->get_nick()) {
list.remove_row(i);
list.add_row(get_friends_list_row_data(*entry), i);
break;
}
}
}
update_friends_list_controls(window, list);
}

View file

@ -242,10 +242,10 @@ std::map<std::string, std::string> get_acquaintances_nice(const std::string& fil
return ac_nice;
}
preferences::acquaintance* add_acquaintance(const std::string& nick, const std::string& mode, const std::string& notes)
std::pair<preferences::acquaintance*, bool> add_acquaintance(const std::string& nick, const std::string& mode, const std::string& notes)
{
if(!utils::isvalid_wildcard(nick)) {
return nullptr;
return std::make_pair(nullptr, false);
}
preferences::acquaintance new_entry(nick, mode, notes);
@ -261,7 +261,7 @@ preferences::acquaintance* add_acquaintance(const std::string& nick, const std::
save_acquaintances();
return &iter->second;
return std::make_pair(&iter->second, success);
}
bool remove_acquaintance(const std::string& nick) {

View file

@ -65,7 +65,7 @@ class acquaintance;
const std::map<std::string, acquaintance> & get_acquaintances();
std::map<std::string, std::string> get_acquaintances_nice(const std::string& filter);
preferences::acquaintance* add_acquaintance(const std::string& nick, const std::string& mode, const std::string& notes);
std::pair<preferences::acquaintance*, bool> add_acquaintance(const std::string& nick, const std::string& mode, const std::string& notes);
void add_completed_campaign(const std::string &campaign_id, const std::string &difficulty_level);
bool remove_acquaintance(const std::string& nick);
bool is_friend(const std::string& nick);