From a19219e0ebeed5701fa8e0bee41d2529ffb7d966 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Fri, 6 Sep 2024 16:29:25 +0200 Subject: [PATCH] Use std::list for server history We don't need random access to the entries, so a list fits just as well. It also has better accessors we need. --- vncviewer/ServerDialog.cxx | 18 ++++++++++-------- vncviewer/ServerDialog.h | 4 ++-- vncviewer/parameters.cxx | 15 ++++++++++----- vncviewer/parameters.h | 6 +++--- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/vncviewer/ServerDialog.cxx b/vncviewer/ServerDialog.cxx index e401c519..c5db8e1a 100644 --- a/vncviewer/ServerDialog.cxx +++ b/vncviewer/ServerDialog.cxx @@ -133,14 +133,12 @@ void ServerDialog::run(const char* servername, char *newservername) dialog.show(); try { - size_t i; - dialog.loadServerHistory(); dialog.serverName->clear(); - for(i = 0; i < dialog.serverHistory.size(); ++i) + for (const string& entry : dialog.serverHistory) fltk_menu_add(dialog.serverName->menubutton(), - dialog.serverHistory[i].c_str(), 0, nullptr); + entry.c_str(), 0, nullptr); } catch (Exception& e) { vlog.error("%s", e.str()); fl_alert(_("Unable to load the server history:\n\n%s"), @@ -295,7 +293,7 @@ void ServerDialog::handleConnect(Fl_Widget* /*widget*/, void *data) } try { - vector::iterator elem = std::find(dialog->serverHistory.begin(), dialog->serverHistory.end(), servername); + list::iterator elem = std::find(dialog->serverHistory.begin(), dialog->serverHistory.end(), servername); // avoid duplicates in the history if(dialog->serverHistory.end() == elem) { dialog->serverHistory.insert(dialog->serverHistory.begin(), servername); @@ -314,7 +312,7 @@ void ServerDialog::loadServerHistory() serverHistory.clear(); #ifdef _WIN32 - loadHistoryFromRegKey(serverHistory); + serverHistory = loadHistoryFromRegKey(); return; #endif @@ -400,8 +398,12 @@ void ServerDialog::saveServerHistory() } // Save the last X elements to the config file. - for(size_t idx=0; idx < serverHistory.size() && idx <= SERVER_HISTORY_SIZE; idx++) - fprintf(f, "%s\n", serverHistory[idx].c_str()); + size_t count = 0; + for (const string& entry : serverHistory) { + if (++count > SERVER_HISTORY_SIZE) + break; + fprintf(f, "%s\n", entry.c_str()); + } fclose(f); } diff --git a/vncviewer/ServerDialog.h b/vncviewer/ServerDialog.h index a76a58cf..de330b59 100644 --- a/vncviewer/ServerDialog.h +++ b/vncviewer/ServerDialog.h @@ -21,7 +21,7 @@ #include #include -#include +#include class Fl_Widget; class Fl_Input_Choice; @@ -49,7 +49,7 @@ private: protected: Fl_Input_Choice *serverName; - std::vector serverHistory; + std::list serverHistory; std::string usedDir; }; diff --git a/vncviewer/parameters.cxx b/vncviewer/parameters.cxx index e40391a2..4bbf7a7f 100644 --- a/vncviewer/parameters.cxx +++ b/vncviewer/parameters.cxx @@ -418,7 +418,7 @@ static void removeValue(const char* _name, HKEY* hKey) { } } -void saveHistoryToRegKey(const vector& serverHistory) { +void saveHistoryToRegKey(const list& serverHistory) { HKEY hKey; LONG res = RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\TigerVNC\\vncviewer\\history", 0, nullptr, @@ -433,9 +433,11 @@ void saveHistoryToRegKey(const vector& serverHistory) { char indexString[3]; try { - while(index < serverHistory.size() && index <= SERVER_HISTORY_SIZE) { + for (const string& entry : serverHistory) { + if (index > SERVER_HISTORY_SIZE) + break; snprintf(indexString, 3, "%d", index); - setKeyString(indexString, serverHistory[index].c_str(), &hKey); + setKeyString(indexString, entry.c_str(), &hKey); index++; } } catch (Exception& e) { @@ -503,8 +505,9 @@ static void saveToReg(const char* servername) { throw rdr::SystemException(_("Failed to close registry key"), res); } -void loadHistoryFromRegKey(vector& serverHistory) { +list loadHistoryFromRegKey() { HKEY hKey; + list serverHistory; LONG res = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\TigerVNC\\vncviewer\\history", 0, @@ -512,7 +515,7 @@ void loadHistoryFromRegKey(vector& serverHistory) { if (res != ERROR_SUCCESS) { if (res == ERROR_FILE_NOT_FOUND) { // The key does not exist, defaults will be used. - return; + return serverHistory; } throw rdr::SystemException(_("Failed to open registry key"), res); @@ -543,6 +546,8 @@ void loadHistoryFromRegKey(vector& serverHistory) { res = RegCloseKey(hKey); if (res != ERROR_SUCCESS) throw rdr::SystemException(_("Failed to close registry key"), res); + + return serverHistory; } static void getParametersFromReg(VoidParameter* parameters[], diff --git a/vncviewer/parameters.h b/vncviewer/parameters.h index df7bc420..a25c932d 100644 --- a/vncviewer/parameters.h +++ b/vncviewer/parameters.h @@ -24,7 +24,7 @@ #include "MonitorIndicesParameter.h" #ifdef _WIN32 -#include +#include #include #endif @@ -84,8 +84,8 @@ void saveViewerParameters(const char *filename, const char *servername=nullptr); char* loadViewerParameters(const char *filename); #ifdef _WIN32 -void loadHistoryFromRegKey(std::vector& serverHistory); -void saveHistoryToRegKey(const std::vector& serverHistory); +std::list loadHistoryFromRegKey(); +void saveHistoryToRegKey(const std::list& serverHistory); #endif #endif -- 2.39.5