config: add ip:port part history save.

This commit is contained in:
2025-06-27 22:13:52 +08:00
parent 08855f3c60
commit b17f275758
58 changed files with 25578 additions and 30 deletions

80
Gui/GuiUtil/Config.cpp Normal file
View File

@@ -0,0 +1,80 @@
#include "Config.h"
#include <fstream>
FrelayConfig::FrelayConfig()
{
}
FrelayConfig::~FrelayConfig()
{
}
bool FrelayConfig::SaveIpPort(const QString& ipPort)
{
auto p = GlobalData::Ins()->GetConfigPath();
json j;
if (existFile(p)) {
std::ifstream ifs(p);
ifs >> j;
ifs.close();
}
CgConVec vec;
if (j.contains("connections")) {
vec = j["connections"].get<CgConVec>();
}
bool exist = false;
for (const auto& v : vec) {
if (v.ip == ipPort.toStdString()) {
exist = true;
break;
}
}
if (exist) {
return true;
}
vec.emplace(vec.begin(), CgConnection{ipPort.toStdString()});
if (vec.size() >= 10) {
vec.pop_back();
}
j["connections"] = vec;
std::ofstream ofs(p);
ofs << std::setw(4) << j << std::endl;
return true;
}
std::vector<QString> FrelayConfig::GetIpPort()
{
std::vector<QString> result;
auto p = GlobalData::Ins()->GetConfigPath();
json j;
if (existFile(p)) {
std::ifstream ifs(p);
ifs >> j;
ifs.close();
if (j.contains("connections")) {
auto vec = j["connections"].get<CgConVec>();
for (const auto& v : vec) {
result.push_back(QString::fromStdString(v.ip));
}
} else {
result.push_back("127.0.0.1:9009");
}
} else {
result.push_back("127.0.0.1:9009");
}
return result;
}
bool FrelayConfig::existFile(const std::string& path)
{
std::ifstream ifs(path);
return ifs.good();
}

31
Gui/GuiUtil/Config.h Normal file
View File

@@ -0,0 +1,31 @@
#ifndef CONFIG_H
#define CONFIG_H
#include <QString>
#include <Util.h>
#include <nlohmann/json.hpp>
#include <vector>
using json = nlohmann::json;
struct CgConnection {
std::string ip;
NLOHMANN_DEFINE_TYPE_INTRUSIVE(CgConnection, ip);
};
using CgConVec = std::vector<CgConnection>;
class FrelayConfig
{
public:
FrelayConfig();
~FrelayConfig();
public:
bool SaveIpPort(const QString& ipPort);
std::vector<QString> GetIpPort();
private:
bool existFile(const std::string& path);
};
#endif // CONFIG_H