Skip to content

Commit

Permalink
Preferences Dialog: refactored handling of friends list
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Sep 7, 2016
1 parent 4f6c869 commit 5ee75aa
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 90 deletions.
32 changes: 21 additions & 11 deletions src/game_preferences.cpp
Expand Up @@ -48,6 +48,8 @@
static lg::log_domain log_config("config");
#define ERR_CFG LOG_STREAM(err , log_config)

using acquaintances_map = std::map<std::string, preferences::acquaintance>;

namespace {

bool message_private_on = false;
Expand All @@ -60,7 +62,7 @@ std::set<t_translation::t_terrain> encountered_terrains_set;

std::map<std::string, std::vector<std::string> > history_map;

std::map<std::string, preferences::acquaintance> acquaintances;
acquaintances_map acquaintances;

std::vector<std::string> mp_modifications;
bool mp_modifications_initialized = false;
Expand Down Expand Up @@ -278,18 +280,26 @@ std::map<std::string, std::string> get_acquaintances_nice(const std::string& fil
return ac_nice;
}

bool add_friend(const std::string& nick, const std::string& notes) {
if (!utils::isvalid_wildcard(nick)) return false;
acquaintances[nick] = preferences::acquaintance(nick, "friend", notes);
save_acquaintances();
return true;
}
preferences::acquaintance* add_acquaintance(const std::string& nick, const std::string& mode, const std::string& notes)
{
if(!utils::isvalid_wildcard(nick)) {
return nullptr;
}

preferences::acquaintance new_entry(nick, mode, notes);

acquaintances_map::iterator iter;
bool success;

std::tie(iter, success) = acquaintances.emplace(nick, new_entry);

if(!success) {
iter->second = new_entry;
}

bool add_ignore(const std::string& nick, const std::string& reason) {
if (!utils::isvalid_wildcard(nick)) return false;
acquaintances[nick] = preferences::acquaintance(nick, "ignore", reason);
save_acquaintances();
return true;

return &iter->second;
}

bool remove_acquaintance(const std::string& nick) {
Expand Down
3 changes: 1 addition & 2 deletions src/game_preferences.hpp
Expand Up @@ -67,8 +67,7 @@ class acquaintance;

const std::map<std::string, acquaintance> & get_acquaintances();
std::map<std::string, std::string> get_acquaintances_nice(const std::string& filter);
bool add_friend(const std::string& nick, const std::string& notes);
bool add_ignore(const std::string& nick, const std::string& reason);
preferences::acquaintance* 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);
Expand Down
4 changes: 2 additions & 2 deletions src/gui/dialogs/lobby/player_info.cpp
Expand Up @@ -156,14 +156,14 @@ void tlobby_player_info::update_relation()

void tlobby_player_info::add_to_friends_button_callback()
{
preferences::add_friend(info_.name, "");
preferences::add_acquaintance(info_.name, "friend", "");
info_.relation = user_info::FRIEND;
update_relation();
}

void tlobby_player_info::add_to_ignores_button_callback()
{
preferences::add_ignore(info_.name, "");
preferences::add_acquaintance(info_.name, "ignore", "");
info_.relation = user_info::IGNORED;
update_relation();
}
Expand Down
146 changes: 78 additions & 68 deletions src/gui/dialogs/preferences_dialog.cpp
Expand Up @@ -77,7 +77,6 @@ REGISTER_DIALOG(preferences)
tpreferences::tpreferences(CVideo& video, const config& game_cfg, const PREFERENCE_VIEW& initial_view)
: resolutions_(video.get_available_resolutions(true))
, adv_preferences_cfg_()
, friend_names_()
, last_selected_item_(0)
, accl_speeds_(
// IMPORTANT: NEVER have trailing zeroes here, or else the cast from doubles
Expand Down Expand Up @@ -122,104 +121,109 @@ void tpreferences::set_resolution_list(tmenu_button& res_list, CVideo& video)
res_list.set_values(options, current_res);
}

void tpreferences::setup_friends_list(twindow& window)
std::map<std::string, string_map> tpreferences::get_friends_list_row_data(const acquaintance& entry)
{
tlistbox& friends_list = find_widget<tlistbox>(&window, "friends_list", false);
std::map<std::string, string_map> data;
string_map item;

const std::map<std::string, acquaintance>& acquaintances = get_acquaintances();
std::string image = "friend.png";
std::string descriptor = _("friend");
std::string notes;

std::map<std::string, string_map> data;
if(entry.get_status() == "ignore") {
image = "ignore.png";
descriptor = _("ignored");
}

find_widget<tbutton>(&window, "remove", false).set_active(!acquaintances.empty());
if(!entry.get_notes().empty()) {
notes = " <small>(" + entry.get_notes() + ")</small>";
}

friends_list.clear();
friend_names_.clear();
item["use_markup"] = "true";

if(acquaintances.empty()) {
data["friend_icon"]["label"] = "misc/status-neutral.png";
data["friend_name"]["label"] = _("Empty list");
friends_list.add_row(data);
item["label"] = "misc/status-" + image;
data.emplace("friend_icon", item);

return;
}
item["label"] = entry.get_nick() + notes;
data.emplace("friend_name", item);

for(const auto& acquaintence : acquaintances) {
std::string image = "friend.png";
std::string descriptor = _("friend");
std::string notes;
item["label"] = "<small>" + descriptor + "</small>";
data.emplace("friend_status", item);

if(acquaintence.second.get_status() == "ignore") {
image = "ignore.png";
descriptor = _("ignored");
}
return data;
}

if(!acquaintence.second.get_notes().empty()) {
notes = " <small>(" + acquaintence.second.get_notes() + ")</small>";
}
void tpreferences::on_friends_list_select(tlistbox& list, ttext_box& textbox)
{
const int num_friends = get_acquaintances().size();
const int sel = list.get_selected_row();

if(sel < 0 || sel >= num_friends) {
return;
}

data["friend_icon"]["label"] = "misc/status-" + image;
std::map<std::string, acquaintance>::const_iterator who = get_acquaintances().begin();
std::advance(who, sel);

data["friend_name"]["label"] = acquaintence.second.get_nick() + notes;
data["friend_name"]["use_markup"] = "true";
textbox.set_value(who->second.get_nick() + " " + who->second.get_notes());
}

data["friend_status"]["label"] = "<small>" + descriptor + "</small>";
data["friend_status"]["use_markup"] = "true";
friends_list.add_row(data);
void tpreferences::update_friends_list_controls(twindow& window, tlistbox& list)
{
const bool list_empty = list.get_item_count() == 0;

friend_names_.push_back(acquaintence.first);
if(!list_empty) {
list.select_row(std::min(static_cast<int>(list.get_item_count()) - 1, list.get_selected_row()));
}

find_widget<tbutton>(&window, "remove", false).set_active(!list_empty);

find_widget<tlabel>(&window, "no_friends_notice", false).set_visible(
list_empty ? twindow::tvisible::visible : twindow::tvisible::invisible);
}

void tpreferences::add_friend_list_entry(const bool is_friend, ttext_box& textbox, twindow& window)
{
std::string reason;
std::string username = textbox.text();
size_t pos = username.find_first_of(' ');
if(username.empty()) {
gui2::show_transient_message(window.video(), "", _("No username specified"), "", false, false, true);
return;
}

std::string reason;

size_t pos = username.find_first_of(' ');
if(pos != std::string::npos) {
reason = username.substr(pos + 1);
username = username.substr(0, pos);
}

const bool added_sucessfully = is_friend ?
add_friend(username, reason) :
add_ignore(username, reason) ;

if(!added_sucessfully) {
gui2::show_transient_message(window.video(), _("Error"), _("Invalid username"),
std::string(), false, false, true);
acquaintance* entry = add_acquaintance(username, (is_friend ? "friend": "ignore"), reason);
if(!entry) {
gui2::show_transient_message(window.video(), _("Error"), _("Invalid username"), "", false, false, true);
return;
}

textbox.clear();

setup_friends_list(window);
tlistbox& list = find_widget<tlistbox>(&window, "friends_list", false);
list.add_row(get_friends_list_row_data(*entry));

update_friends_list_controls(window, list);
}

void tpreferences::on_friends_list_select(tlistbox& friends, ttext_box& textbox)
void tpreferences::remove_friend_list_entry(tlistbox& friends_list, ttext_box& textbox, twindow& window)
{
const int num_friends = get_acquaintances().size();
const int sel = friends.get_selected_row();

if(sel < 0 || sel >= num_friends) {
return;
}
const int selected_row = std::max(0, friends_list.get_selected_row());

std::map<std::string, acquaintance>::const_iterator who = get_acquaintances().begin();
std::advance(who, sel);

textbox.set_value(who->second.get_nick() + " " + who->second.get_notes());
}
std::advance(who, selected_row);

void tpreferences::remove_friend_list_entry(tlistbox& friends_list,
ttext_box& textbox, twindow& window)
{
std::string to_remove = textbox.text();

const int selected_row = std::max(0, friends_list.get_selected_row());
const std::string to_remove = !textbox.text().empty() ? textbox.text() : who->second.get_nick();

if(to_remove.empty()) {
to_remove = friend_names_[selected_row];
gui2::show_transient_message(window.video(), "", _("No username specified"), "", false, false, true);
return;
}

if(!remove_acquaintance(to_remove)) {
Expand All @@ -229,7 +233,10 @@ void tpreferences::remove_friend_list_entry(tlistbox& friends_list,

textbox.clear();

setup_friends_list(window);
tlistbox& list = find_widget<tlistbox>(&window, "friends_list", false);
list.remove_row(selected_row);

update_friends_list_controls(window, list);
}

// Helper function to get the main grid in each row of the advanced section
Expand Down Expand Up @@ -476,10 +483,15 @@ void tpreferences::post_build(twindow& window)
});

/* FRIENDS LIST */
setup_friends_list(window);
tlistbox& friends_list = find_widget<tlistbox>(&window, "friends_list", false);

friends_list.clear();

for(const auto& entry : get_acquaintances()) {
friends_list.add_row(get_friends_list_row_data(entry.second));
}

ttext_box& textbox = find_widget<ttext_box>(&window, "friend_name_box", false);
tlistbox& friend_list = find_widget<tlistbox>(&window, "friends_list", false);

connect_signal_mouse_left_click(
find_widget<tbutton>(&window, "add_friend", false), std::bind(
Expand All @@ -499,18 +511,16 @@ void tpreferences::post_build(twindow& window)
find_widget<tbutton>(&window, "remove", false), std::bind(
&tpreferences::remove_friend_list_entry,
this,
std::ref(friend_list),
std::ref(friends_list),
std::ref(textbox),
std::ref(window)));

friend_list.set_callback_value_change(std::bind(
friends_list.set_callback_value_change(std::bind(
&tpreferences::on_friends_list_select,
this,
std::ref(friend_list),
std::ref(friends_list),
std::ref(textbox)));

friend_list.select_row(0);

/* ALERTS */
connect_signal_mouse_left_click(
find_widget<tbutton>(&window, "mp_alerts", false),
Expand Down
12 changes: 5 additions & 7 deletions src/gui/dialogs/preferences_dialog.hpp
Expand Up @@ -89,16 +89,15 @@ class tpreferences : public tdialog
/** Initializers */
void initialize_tabs(twindow& window, tlistbox& selector);
void set_resolution_list(tmenu_button& res_list, CVideo& video);
void setup_friends_list(twindow& window);
void setup_hotkey_list(twindow& window);

void add_friend_list_entry(const bool is_friend,
ttext_box& textbox, twindow& window);
std::map<std::string, string_map> get_friends_list_row_data(const acquaintance& entry);

void on_friends_list_select(tlistbox& friends, ttext_box& textbox);
void add_friend_list_entry(const bool is_friend, ttext_box& textbox, twindow& window);
void remove_friend_list_entry(tlistbox& friends_list, ttext_box& textbox, twindow& window);

void remove_friend_list_entry(tlistbox& friends_list,
ttext_box& textbox, twindow& window);
void on_friends_list_select(tlistbox& list, ttext_box& textbox);
void update_friends_list_controls(twindow& window, tlistbox& list);

void set_visible_page(twindow& window, unsigned int page, const std::string& pager_id);

Expand Down Expand Up @@ -126,7 +125,6 @@ class tpreferences : public tdialog

std::vector<std::pair<int,int> > resolutions_;
std::vector<config> adv_preferences_cfg_;
std::vector<std::string> friend_names_;

int last_selected_item_;

Expand Down

0 comments on commit 5ee75aa

Please sign in to comment.