Skip to content

Commit

Permalink
Use std::list for server history
Browse files Browse the repository at this point in the history
We don't need random access to the entries, so a list fits just as well.
It also has better accessors we need.
  • Loading branch information
CendioOssman committed Sep 6, 2024
1 parent 4236d0c commit a19219e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
18 changes: 10 additions & 8 deletions vncviewer/ServerDialog.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -295,7 +293,7 @@ void ServerDialog::handleConnect(Fl_Widget* /*widget*/, void *data)
}

try {
vector<string>::iterator elem = std::find(dialog->serverHistory.begin(), dialog->serverHistory.end(), servername);
list<string>::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);
Expand All @@ -314,7 +312,7 @@ void ServerDialog::loadServerHistory()
serverHistory.clear();

#ifdef _WIN32
loadHistoryFromRegKey(serverHistory);
serverHistory = loadHistoryFromRegKey();
return;
#endif

Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions vncviewer/ServerDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include <FL/Fl_Window.H>
#include <string>
#include <vector>
#include <list>

class Fl_Widget;
class Fl_Input_Choice;
Expand Down Expand Up @@ -49,7 +49,7 @@ class ServerDialog : public Fl_Window {

protected:
Fl_Input_Choice *serverName;
std::vector<std::string> serverHistory;
std::list<std::string> serverHistory;
std::string usedDir;
};

Expand Down
15 changes: 10 additions & 5 deletions vncviewer/parameters.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ static void removeValue(const char* _name, HKEY* hKey) {
}
}

void saveHistoryToRegKey(const vector<string>& serverHistory) {
void saveHistoryToRegKey(const list<string>& serverHistory) {
HKEY hKey;
LONG res = RegCreateKeyExW(HKEY_CURRENT_USER,
L"Software\\TigerVNC\\vncviewer\\history", 0, nullptr,
Expand All @@ -433,9 +433,11 @@ void saveHistoryToRegKey(const vector<string>& 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) {
Expand Down Expand Up @@ -503,16 +505,17 @@ static void saveToReg(const char* servername) {
throw rdr::SystemException(_("Failed to close registry key"), res);
}

void loadHistoryFromRegKey(vector<string>& serverHistory) {
list<string> loadHistoryFromRegKey() {
HKEY hKey;
list<string> serverHistory;

LONG res = RegOpenKeyExW(HKEY_CURRENT_USER,
L"Software\\TigerVNC\\vncviewer\\history", 0,
KEY_READ, &hKey);
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);
Expand Down Expand Up @@ -543,6 +546,8 @@ void loadHistoryFromRegKey(vector<string>& serverHistory) {
res = RegCloseKey(hKey);
if (res != ERROR_SUCCESS)
throw rdr::SystemException(_("Failed to close registry key"), res);

return serverHistory;
}

static void getParametersFromReg(VoidParameter* parameters[],
Expand Down
6 changes: 3 additions & 3 deletions vncviewer/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "MonitorIndicesParameter.h"

#ifdef _WIN32
#include <vector>
#include <list>
#include <string>
#endif

Expand Down Expand Up @@ -84,8 +84,8 @@ void saveViewerParameters(const char *filename, const char *servername=nullptr);
char* loadViewerParameters(const char *filename);

#ifdef _WIN32
void loadHistoryFromRegKey(std::vector<std::string>& serverHistory);
void saveHistoryToRegKey(const std::vector<std::string>& serverHistory);
std::list<std::string> loadHistoryFromRegKey();
void saveHistoryToRegKey(const std::list<std::string>& serverHistory);
#endif

#endif

0 comments on commit a19219e

Please sign in to comment.