config: add ip:port part history save.
This commit is contained in:
@@ -25,6 +25,7 @@ Form/Transform.h Form/Transform.cpp Form/Transform.ui
|
||||
../Res/frelay.qrc ../Res/ico.rc
|
||||
Control/cusTableWidget.cpp Control/cusTableWidget.h
|
||||
Control/cpTableWidget.h Control/cpTableWidget.cpp
|
||||
GuiUtil/Config.h GuiUtil/Config.cpp
|
||||
)
|
||||
|
||||
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <InfoMsg.h>
|
||||
#include <InfoPack.hpp>
|
||||
#include <QRegularExpression>
|
||||
#include <future>
|
||||
|
||||
#include "GuiUtil/Public.h"
|
||||
@@ -74,12 +75,24 @@ void Connecter::HandleClients(const InfoClientVec& clients)
|
||||
}
|
||||
}
|
||||
|
||||
void Connecter::SetConfigPtr(std::shared_ptr<FrelayConfig> config)
|
||||
{
|
||||
config_ = config;
|
||||
auto ipPorts = config_->GetIpPort();
|
||||
for (const auto& ipPort : ipPorts) {
|
||||
ui->comboBox->addItem(ipPort);
|
||||
}
|
||||
if (ui->comboBox->count() > 0) {
|
||||
ui->comboBox->setCurrentIndex(0);
|
||||
}
|
||||
}
|
||||
|
||||
void Connecter::Connect()
|
||||
{
|
||||
auto ip = ui->edIP->text().trimmed();
|
||||
auto port = ui->edPort->text().trimmed();
|
||||
if (ip.isEmpty() || port.isEmpty()) {
|
||||
FTCommon::msg(this, tr("IP or Port is empty."));
|
||||
QString ip;
|
||||
QString port;
|
||||
if (!parseIpPort(ui->comboBox->currentText(), ip, port)) {
|
||||
FTCommon::msg(this, QString(tr("IP or port is invalid.")));
|
||||
return;
|
||||
}
|
||||
emit sigDoConnect(ip, port.toInt());
|
||||
@@ -87,6 +100,7 @@ void Connecter::Connect()
|
||||
|
||||
void Connecter::setState(ConnectState cs)
|
||||
{
|
||||
config_->SaveIpPort(ui->comboBox->currentText());
|
||||
switch (cs) {
|
||||
case CS_CONNECTING:
|
||||
ui->btnConnect->setEnabled(false);
|
||||
@@ -145,10 +159,7 @@ std::string Connecter::getCurClient()
|
||||
void Connecter::InitControl()
|
||||
{
|
||||
ui->btnDisconnect->setEnabled(false);
|
||||
ui->edIP->setText("127.0.0.1");
|
||||
ui->edPort->setText("9009");
|
||||
// ui->edIP->setMinimumWidth(120);
|
||||
// ui->edPort->setFixedWidth(60);
|
||||
ui->comboBox->setEditable(true);
|
||||
|
||||
connect(ui->btnConnect, &QPushButton::clicked, this, &Connecter::Connect);
|
||||
connect(ui->btnRefresh, &QPushButton::clicked, this, &Connecter::RefreshClient);
|
||||
@@ -181,3 +192,31 @@ void Connecter::InitControl()
|
||||
|
||||
setMaximumWidth(300);
|
||||
}
|
||||
|
||||
bool Connecter::parseIpPort(const QString& ipPort, QString& outIp, QString& outPort)
|
||||
{
|
||||
QRegularExpression regex("^\\s*"
|
||||
"("
|
||||
"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
|
||||
"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
|
||||
"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
|
||||
"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
|
||||
")"
|
||||
"\\s*:\\s*"
|
||||
"("
|
||||
"\\d{1,5}"
|
||||
")"
|
||||
"\\s*$");
|
||||
|
||||
QRegularExpressionMatch match = regex.match(ipPort);
|
||||
if (!match.hasMatch()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outIp = match.captured(1);
|
||||
outPort = match.captured(2);
|
||||
|
||||
bool portOk;
|
||||
int portNum = outPort.toInt(&portOk);
|
||||
return portOk && portNum > 0 && portNum <= 65535;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
#include <QStandardItemModel>
|
||||
#include <QWidget>
|
||||
|
||||
#include "GuiUtil/Config.h"
|
||||
#include "GuiUtil/Public.h"
|
||||
|
||||
namespace Ui {
|
||||
class Connecter;
|
||||
}
|
||||
@@ -29,6 +32,7 @@ public:
|
||||
void RunWorker(ClientCore* clientCore);
|
||||
void SetRemoteCall(const std::function<void(const QString& id)>& call);
|
||||
void HandleClients(const InfoClientVec& clients);
|
||||
void SetConfigPtr(std::shared_ptr<FrelayConfig> config);
|
||||
|
||||
signals:
|
||||
void sendConnect(ConnectState cs);
|
||||
@@ -44,6 +48,7 @@ private:
|
||||
void Disconnect();
|
||||
void ShowContextMenu(const QPoint& pos);
|
||||
std::string getCurClient();
|
||||
bool parseIpPort(const QString& ipPort, QString& outIp, QString& outPort);
|
||||
|
||||
private:
|
||||
Ui::Connecter* ui;
|
||||
@@ -56,6 +61,7 @@ private:
|
||||
SocketWorker* sockWorker_{};
|
||||
HeatBeat* heatBeat_{};
|
||||
QStandardItemModel* model_;
|
||||
std::shared_ptr<FrelayConfig> config_;
|
||||
};
|
||||
|
||||
#endif // CONNECTCONTROL_H
|
||||
|
||||
@@ -19,12 +19,19 @@
|
||||
<item>
|
||||
<widget class="QLabel" name="lbIP">
|
||||
<property name="text">
|
||||
<string>Server IP:</string>
|
||||
<string>Server:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="edIP"/>
|
||||
<widget class="QComboBox" name="comboBox">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
@@ -50,23 +57,6 @@
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Port:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="edPort">
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>60</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
|
||||
80
Gui/GuiUtil/Config.cpp
Normal file
80
Gui/GuiUtil/Config.cpp
Normal 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
31
Gui/GuiUtil/Config.h
Normal 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
|
||||
@@ -12,7 +12,13 @@ static LogPrint* logPrint = nullptr;
|
||||
|
||||
frelayGUI::frelayGUI(QWidget* parent) : QMainWindow(parent), ui(new Ui::frelayGUI)
|
||||
{
|
||||
config_ = std::make_shared<FrelayConfig>();
|
||||
|
||||
ui->setupUi(this);
|
||||
|
||||
QString configRoot = Util::GetCurConfigPath("frelay");
|
||||
GlobalData::Ins()->SetConfigPath(configRoot.toStdString() + "/frelayConfig.json");
|
||||
|
||||
InitControl();
|
||||
ControlSignal();
|
||||
ControlLayout();
|
||||
@@ -47,6 +53,7 @@ void frelayGUI::InitControl()
|
||||
connecter_ = new Connecter(this);
|
||||
connecter_->RunWorker(clientCore_);
|
||||
connecter_->SetRemoteCall([this](const QString& id) { clientCore_->SetRemoteID(id); });
|
||||
connecter_->SetConfigPtr(config_);
|
||||
|
||||
localFile_ = new FileManager(this);
|
||||
remoteFile_ = new FileManager(this);
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
#include <QMainWindow>
|
||||
#include <QTabWidget>
|
||||
|
||||
#include "Control/CompareControl.h"
|
||||
#include "Control/ConnectControl.h"
|
||||
#include "Control/FileControl.h"
|
||||
#include "Control/CompareControl.h"
|
||||
#include "Form/Transform.h"
|
||||
#include "GuiUtil/Config.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui {
|
||||
@@ -48,5 +49,6 @@ private:
|
||||
ClientCore* clientCore_;
|
||||
TransForm* transform_;
|
||||
Compare* compare_;
|
||||
std::shared_ptr<FrelayConfig> config_;
|
||||
};
|
||||
#endif // FRELAYGUI_H
|
||||
|
||||
Reference in New Issue
Block a user