From d0029a806b8b6d244a6a3ec5b243303a8203cdd5 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 7 Mar 2024 10:31:22 -0500 Subject: Added connection history --- src/connection_dialog.cpp | 46 ++++++++++++++++++++++++++++++++++++++-------- src/connection_dialog.h | 4 ++++ src/tracker_config.cpp | 32 ++++++++++++++++++++++++++------ src/tracker_config.h | 17 ++++++++++++++--- src/tracker_frame.cpp | 21 ++++++++++++++++++--- 5 files changed, 100 insertions(+), 20 deletions(-) diff --git a/src/connection_dialog.cpp b/src/connection_dialog.cpp index 9dd9984..64fee98 100644 --- a/src/connection_dialog.cpp +++ b/src/connection_dialog.cpp @@ -4,12 +4,15 @@ ConnectionDialog::ConnectionDialog() : wxDialog(nullptr, wxID_ANY, "Connect to Archipelago") { - server_box_ = new wxTextCtrl(this, -1, GetTrackerConfig().ap_server, wxDefaultPosition, - {300, -1}); - player_box_ = new wxTextCtrl(this, -1, GetTrackerConfig().ap_player, wxDefaultPosition, - {300, -1}); - password_box_ = new wxTextCtrl(this, -1, GetTrackerConfig().ap_password, - wxDefaultPosition, {300, -1}); + server_box_ = + new wxTextCtrl(this, -1, GetTrackerConfig().connection_details.ap_server, + wxDefaultPosition, {300, -1}); + player_box_ = + new wxTextCtrl(this, -1, GetTrackerConfig().connection_details.ap_player, + wxDefaultPosition, {300, -1}); + password_box_ = new wxTextCtrl( + this, -1, GetTrackerConfig().connection_details.ap_password, + wxDefaultPosition, {300, -1}); wxFlexGridSizer* form_sizer = new wxFlexGridSizer(2, 10, 10); @@ -26,15 +29,42 @@ ConnectionDialog::ConnectionDialog() wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT)); form_sizer->Add(password_box_, wxSizerFlags().Expand()); + history_list_ = new wxListBox(this, -1); + for (const ConnectionDetails& details : GetTrackerConfig().connection_history) { + wxString display_text; + display_text << details.ap_player; + display_text << " ("; + display_text << details.ap_server; + display_text << ")"; + + history_list_->Append(display_text); + } + + history_list_->Bind(wxEVT_LISTBOX, &ConnectionDialog::OnOldConnectionChosen, this); + + wxBoxSizer* mid_sizer = new wxBoxSizer(wxHORIZONTAL); + mid_sizer->Add(form_sizer, wxSizerFlags().Proportion(3).Expand()); + mid_sizer->AddSpacer(10); + mid_sizer->Add(history_list_, wxSizerFlags().Proportion(2).Expand()); + wxBoxSizer* top_sizer = new wxBoxSizer(wxVERTICAL); top_sizer->Add(new wxStaticText( this, -1, "Enter the details to connect to Archipelago."), wxSizerFlags().Align(wxALIGN_LEFT).DoubleBorder()); - top_sizer->Add(form_sizer, wxSizerFlags().DoubleBorder().Expand()); - top_sizer->Add(CreateButtonSizer(wxOK | wxCANCEL), wxSizerFlags().Center()); + top_sizer->Add(mid_sizer, wxSizerFlags().DoubleBorder().Expand()); + top_sizer->Add(CreateButtonSizer(wxOK | wxCANCEL), wxSizerFlags().Border().Center()); SetSizerAndFit(top_sizer); Center(); server_box_->SetFocus(); } + +void ConnectionDialog::OnOldConnectionChosen(wxCommandEvent& e) { + if (e.IsSelection()) { + const ConnectionDetails& details = GetTrackerConfig().connection_history.at(e.GetSelection()); + server_box_->SetValue(details.ap_server); + player_box_->SetValue(details.ap_player); + password_box_->SetValue(details.ap_password); + } +} diff --git a/src/connection_dialog.h b/src/connection_dialog.h index 5d2703d..9fe62fd 100644 --- a/src/connection_dialog.h +++ b/src/connection_dialog.h @@ -8,6 +8,7 @@ #endif #include +#include class ConnectionDialog : public wxDialog { public: @@ -22,9 +23,12 @@ class ConnectionDialog : public wxDialog { } private: + void OnOldConnectionChosen(wxCommandEvent& e); + wxTextCtrl* server_box_; wxTextCtrl* player_box_; wxTextCtrl* password_box_; + wxListBox* history_list_; }; #endif /* end of include guard: CONNECTION_DIALOG_H_E526D0E7 */ diff --git a/src/tracker_config.cpp b/src/tracker_config.cpp index b0f4ac4..85164d5 100644 --- a/src/tracker_config.cpp +++ b/src/tracker_config.cpp @@ -10,13 +10,23 @@ void TrackerConfig::Load() { try { YAML::Node file = YAML::LoadFile(filename_); - ap_server = file["ap_server"].as(); - ap_player = file["ap_player"].as(); - ap_password = file["ap_password"].as(); + connection_details.ap_server = file["ap_server"].as(); + connection_details.ap_player = file["ap_player"].as(); + connection_details.ap_password = file["ap_password"].as(); asked_to_check_for_updates = file["asked_to_check_for_updates"].as(); should_check_for_updates = file["should_check_for_updates"].as(); hybrid_areas = file["hybrid_areas"].as(); show_hunt_panels = file["show_hunt_panels"].as(); + + if (file["connection_history"]) { + for (const auto& connection : file["connection_history"]) { + connection_history.push_back(ConnectionDetails{ + .ap_server = connection["ap_server"].as(), + .ap_player = connection["ap_player"].as(), + .ap_password = connection["ap_password"].as() + }); + } + } } catch (const std::exception&) { // It's fine if the file can't be loaded. } @@ -24,13 +34,23 @@ void TrackerConfig::Load() { void TrackerConfig::Save() { YAML::Node output; - output["ap_server"] = ap_server; - output["ap_player"] = ap_player; - output["ap_password"] = ap_password; + output["ap_server"] = connection_details.ap_server; + output["ap_player"] = connection_details.ap_player; + output["ap_password"] = connection_details.ap_password; output["asked_to_check_for_updates"] = asked_to_check_for_updates; output["should_check_for_updates"] = should_check_for_updates; output["hybrid_areas"] = hybrid_areas; output["show_hunt_panels"] = show_hunt_panels; + + output.remove("connection_history"); + for (const ConnectionDetails& details : connection_history) { + YAML::Node connection; + connection["ap_server"] = details.ap_server; + connection["ap_player"] = details.ap_player; + connection["ap_password"] = details.ap_password; + + output["connection_history"].push_back(connection); + } std::ofstream filewriter(filename_); filewriter << output; diff --git a/src/tracker_config.h b/src/tracker_config.h index b95194e..a1a6c1d 100644 --- a/src/tracker_config.h +++ b/src/tracker_config.h @@ -1,7 +1,19 @@ #ifndef TRACKER_CONFIG_H_36CDD648 #define TRACKER_CONFIG_H_36CDD648 +#include #include +#include + +struct ConnectionDetails { + std::string ap_server; + std::string ap_player; + std::string ap_password; + + bool operator!=(const ConnectionDetails& rhs) const { + return std::tie(ap_server, ap_player, ap_password) != std::tie(rhs.ap_server, rhs.ap_player, rhs.ap_password); + } +}; class TrackerConfig { public: @@ -11,13 +23,12 @@ class TrackerConfig { void Save(); - std::string ap_server; - std::string ap_player; - std::string ap_password; + ConnectionDetails connection_details; bool asked_to_check_for_updates = false; bool should_check_for_updates = false; bool hybrid_areas = false; bool show_hunt_panels = false; + std::deque connection_history; private: std::string filename_; diff --git a/src/tracker_frame.cpp b/src/tracker_frame.cpp index a8c1063..d64e0d3 100644 --- a/src/tracker_frame.cpp +++ b/src/tracker_frame.cpp @@ -115,9 +115,24 @@ void TrackerFrame::OnConnect(wxCommandEvent &event) { ConnectionDialog dlg; if (dlg.ShowModal() == wxID_OK) { - GetTrackerConfig().ap_server = dlg.GetServerValue(); - GetTrackerConfig().ap_player = dlg.GetPlayerValue(); - GetTrackerConfig().ap_password = dlg.GetPasswordValue(); + GetTrackerConfig().connection_details.ap_server = dlg.GetServerValue(); + GetTrackerConfig().connection_details.ap_player = dlg.GetPlayerValue(); + GetTrackerConfig().connection_details.ap_password = dlg.GetPasswordValue(); + + std::deque new_history; + new_history.push_back(GetTrackerConfig().connection_details); + + for (const ConnectionDetails& details : GetTrackerConfig().connection_history) { + if (details != GetTrackerConfig().connection_details) { + new_history.push_back(details); + } + } + + while (new_history.size() > 5) { + new_history.pop_back(); + } + + GetTrackerConfig().connection_history = std::move(new_history); GetTrackerConfig().Save(); AP_Connect(dlg.GetServerValue(), dlg.GetPlayerValue(), -- cgit 1.4.1