diff --git a/Source/Common/CommonUI/xToolsCheckBox.cpp b/Source/Common/CommonUI/xToolsCheckBox.cpp index f3a2cc39..c26e831f 100644 --- a/Source/Common/CommonUI/xToolsCheckBox.cpp +++ b/Source/Common/CommonUI/xToolsCheckBox.cpp @@ -18,25 +18,25 @@ xToolsCheckBox::xToolsCheckBox(QWidget* parent) void xToolsCheckBox::setGroupKey(const QString& group, const QString& key) { - mKey = group + "/" + key; + m_key = group + "/" + key; readFromSettingsFile(); } void xToolsCheckBox::readFromSettingsFile() { - if (mKey.isEmpty()) { + if (m_key.isEmpty()) { return; } - bool ret = xToolsSettings::instance()->value(mKey).toBool(); + bool ret = xToolsSettings::instance()->value(m_key).toBool(); setChecked(ret); } void xToolsCheckBox::writeToSettingsFile() { - if (mKey.isEmpty()) { + if (m_key.isEmpty()) { return; } - xToolsSettings::instance()->setValue(mKey, isChecked()); + xToolsSettings::instance()->setValue(m_key, isChecked()); } diff --git a/Source/Common/CommonUI/xToolsCheckBox.h b/Source/Common/CommonUI/xToolsCheckBox.h index 14860a35..ecf65aa8 100644 --- a/Source/Common/CommonUI/xToolsCheckBox.h +++ b/Source/Common/CommonUI/xToolsCheckBox.h @@ -18,7 +18,7 @@ public: void setGroupKey(const QString &group, const QString &key); private: - QString mKey; + QString m_key; private: void readFromSettingsFile(); diff --git a/Source/ToolBox/ToolBoxUI/xToolsToolBoxUi.cpp b/Source/ToolBox/ToolBoxUI/xToolsToolBoxUi.cpp index 0b091e98..9ab58766 100644 --- a/Source/ToolBox/ToolBoxUI/xToolsToolBoxUi.cpp +++ b/Source/ToolBox/ToolBoxUI/xToolsToolBoxUi.cpp @@ -36,7 +36,6 @@ #include "xToolsToolBoxUiOutputMenu.h" #include "xToolsToolFactory.h" #include "xToolsUdpTransmitterToolUi.h" -#include "xToolsApplication.h" #include "xToolsWebSocketTransmitterToolUi.h" #ifdef X_TOOLS_IMPORT_MODULE_BLUETOOTH @@ -68,7 +67,7 @@ xToolsToolBoxUi::~xToolsToolBoxUi() QList xToolsToolBoxUi::supportedCommunicationTools() { QList list; - list << xToolsToolFactory::SerialportTool + list << xToolsToolFactory::SerialPortTool #ifdef X_TOOLS_IMPORT_MODULE_BLUETOOTH << xToolsToolFactory::BleCentralTool #endif @@ -80,7 +79,7 @@ QList xToolsToolBoxUi::supportedCommunicationTools() QString xToolsToolBoxUi::communicationToolName(int type) { - if (type == xToolsToolFactory::SerialportTool) { + if (type == xToolsToolFactory::SerialPortTool) { return tr("SerialPort"); } else if (type == xToolsToolFactory::UdpClientTool) { return tr("UDP Client"); @@ -104,7 +103,7 @@ QString xToolsToolBoxUi::communicationToolName(int type) QIcon xToolsToolBoxUi::communicationToolIcon(int type) { QString fileName; - if (type == xToolsToolFactory::SerialportTool) { + if (type == xToolsToolFactory::SerialPortTool) { fileName = ":/Resources/Icons/IconSerialPort.svg"; } else if (type == xToolsToolFactory::UdpClientTool) { fileName = ":/Resources/Icons/IconUdpClient.svg"; @@ -154,7 +153,7 @@ void xToolsToolBoxUi::initialize(int type) xToolsCommunicationToolUi* xToolsToolBoxUi::communicationToolUi(int type) { xToolsCommunicationToolUi* w = nullptr; - if (type == xToolsToolFactory::SerialportTool) { + if (type == xToolsToolFactory::SerialPortTool) { w = new xToolsSerialPortToolUi(); } else if (type == xToolsToolFactory::UdpClientTool) { w = new xToolsSocketClientToolUi(); @@ -265,7 +264,7 @@ void xToolsToolBoxUi::output2ui(const QByteArray& bytes, const QString& flag, bo QString xToolsToolBoxUi::settingsGroup() { - if (m_communicationType == xToolsToolFactory::SerialportTool) { + if (m_communicationType == xToolsToolFactory::SerialPortTool) { return "SerialportToolBox"; } else if (m_communicationType == xToolsToolFactory::UdpClientTool) { return "UdpClientToolBox"; @@ -295,7 +294,7 @@ QByteArray xToolsToolBoxUi::calculateCrc(const QByteArray& bytes, bool fixedOrig int format = ui->comboBoxInputFormat->currentData().toInt(); QString input = ui->comboBoxInputText->currentText(); int esc = ctx.escapeCharacter; - + input = xToolsDataStructure::cookEscapeCharacter(esc, input); inputBytes = xToolsDataStructure::stringToByteArray(input, format); } diff --git a/Source/Tools/Tools/xToolsSocketClientTool.cpp b/Source/Tools/Tools/xToolsSocketClientTool.cpp index 8a7ef1c5..85412356 100644 --- a/Source/Tools/Tools/xToolsSocketClientTool.cpp +++ b/Source/Tools/Tools/xToolsSocketClientTool.cpp @@ -81,3 +81,36 @@ void xToolsSocketClientTool::setMessageType(int type) m_messageType = type; emit messageTypeChanged(); } + +bool xToolsSocketClientTool::authentication() +{ + return m_authentication; +} + +void xToolsSocketClientTool::setAuthentication(bool authentication) +{ + m_authentication = authentication; + emit authenticationChanged(); +} + +QString xToolsSocketClientTool::userName() +{ + return m_userName; +} + +void xToolsSocketClientTool::setUserName(const QString &userName) +{ + m_userName = userName; + emit userNameChanged(); +} + +QString xToolsSocketClientTool::password() +{ + return m_password; +} + +void xToolsSocketClientTool::setPassword(const QString &password) +{ + m_password = password; + emit passwordChanged(); +} diff --git a/Source/Tools/Tools/xToolsSocketClientTool.h b/Source/Tools/Tools/xToolsSocketClientTool.h index d256cbef..3253fee1 100644 --- a/Source/Tools/Tools/xToolsSocketClientTool.h +++ b/Source/Tools/Tools/xToolsSocketClientTool.h @@ -22,6 +22,9 @@ class xToolsSocketClientTool : public xToolsCommunicationTool Q_PROPERTY(QString bindingIpPort READ bindingIpPort NOTIFY bindingIpPortChanged) // Just for web socket client. Q_PROPERTY(int messageType READ messageType WRITE setMessageType NOTIFY messageTypeChanged) + Q_PROPERTY(bool authentication READ authentication WRITE setAuthentication NOTIFY authenticationChanged) + Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged) + Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged) // clang-format on public: explicit xToolsSocketClientTool(QObject *parent = nullptr); @@ -40,6 +43,12 @@ public: QString bindingIpPort(); int messageType(); void setMessageType(int type); + bool authentication(); + void setAuthentication(bool authentication); + QString userName(); + void setUserName(const QString &userName); + QString password(); + void setPassword(const QString &password); protected: QString m_clientIp; @@ -49,6 +58,10 @@ protected: int m_serverPort; QString m_bindingIpPort; qint8 m_messageType; + QString m_peerInfo; + QString m_userName; + QString m_password; + bool m_authentication; signals: void clientIpChanged(); @@ -58,4 +71,7 @@ signals: void serverPortChanged(); void bindingIpPortChanged(); void messageTypeChanged(); + void authenticationChanged(); + void userNameChanged(); + void passwordChanged(); }; diff --git a/Source/Tools/Tools/xToolsToolFactory.cpp b/Source/Tools/Tools/xToolsToolFactory.cpp index 6b1b55c8..8e2e39ce 100644 --- a/Source/Tools/Tools/xToolsToolFactory.cpp +++ b/Source/Tools/Tools/xToolsToolFactory.cpp @@ -50,7 +50,7 @@ xToolsBaseTool *xToolsToolFactory::createTool(int type) xToolsBaseTool *tool{nullptr}; if (AnalyzerTool == type) { tool = new xToolsAnalyzerTool(); - } else if (SerialportTool == type) { + } else if (SerialPortTool == type) { tool = new xToolsSerialPortTool(); } else if (EmitterTool == type) { tool = new xToolsEmitterTool(); @@ -119,7 +119,7 @@ QString xToolsToolFactory::toolName(int type) static QMap map; if (map.isEmpty()) { map.insert(AnalyzerTool, tr("Analyzer")); - map.insert(SerialportTool, tr("Serialport")); + map.insert(SerialPortTool, tr("Serialport")); map.insert(EmitterTool, tr("Emitter")); map.insert(MaskerTool, tr("Masker")); map.insert(ResponserTool, tr("Responser")); diff --git a/Source/Tools/Tools/xToolsToolFactory.h b/Source/Tools/Tools/xToolsToolFactory.h index 187f0b88..ca6cbb89 100644 --- a/Source/Tools/Tools/xToolsToolFactory.h +++ b/Source/Tools/Tools/xToolsToolFactory.h @@ -18,7 +18,7 @@ class xToolsToolFactory : public QObject public: enum ToolsType { AnalyzerTool, - SerialportTool, + SerialPortTool, EmitterTool, MaskerTool, ResponserTool, diff --git a/Source/Tools/Tools/xToolsWebSocketClientTool.cpp b/Source/Tools/Tools/xToolsWebSocketClientTool.cpp index 5e2fafb1..7340218f 100644 --- a/Source/Tools/Tools/xToolsWebSocketClientTool.cpp +++ b/Source/Tools/Tools/xToolsWebSocketClientTool.cpp @@ -58,10 +58,21 @@ bool xToolsWebSocketClientTool::initialize(QString &errStr) QString errStr = m_webSocket->errorString(); emit errorOccurred(errStr); }); - + QString address = "ws://" + m_serverIp + ":" + QString::number(m_serverPort); - qDebug() << "Server url: " + address; - m_webSocket->open(address); + qInfo() << "Server url: " + address; + if (m_authentication) { + QNetworkRequest request(address); + QString username = m_userName; + QString password = m_password; + QString concatenated = username + ":" + password; + QByteArray data = concatenated.toLocal8Bit().toBase64(); + QString headerData = "Basic " + data; + request.setRawHeader("Authorization", headerData.toLocal8Bit()); + m_webSocket->open(request); + } else { + m_webSocket->open(address); + } return true; } @@ -70,6 +81,7 @@ void xToolsWebSocketClientTool::writeBytes(const QByteArray &bytes) { qint64 ret = -1; QString hex; + if (m_messageType == 0) { hex = QString::fromLatin1(xToolsByteArrayToHex(bytes, ' ')); ret = m_webSocket->sendBinaryMessage(bytes); diff --git a/Source/Tools/Tools/xToolsWebSocketClientTool.h b/Source/Tools/Tools/xToolsWebSocketClientTool.h index 778dd808..a264494d 100644 --- a/Source/Tools/Tools/xToolsWebSocketClientTool.h +++ b/Source/Tools/Tools/xToolsWebSocketClientTool.h @@ -25,5 +25,4 @@ protected: private: QWebSocket *m_webSocket{Q_NULLPTR}; - QString m_peerInfo; }; diff --git a/Source/Tools/ToolsUI/xToolsSocketClientToolUi.cpp b/Source/Tools/ToolsUI/xToolsSocketClientToolUi.cpp index 2cf82e02..17f2f61e 100644 --- a/Source/Tools/ToolsUI/xToolsSocketClientToolUi.cpp +++ b/Source/Tools/ToolsUI/xToolsSocketClientToolUi.cpp @@ -18,6 +18,7 @@ xToolsSocketClientToolUi::xToolsSocketClientToolUi(QWidget *parent) , ui(new Ui::xToolsSocketClientToolUi) { ui->setupUi(this); + ui->widgetAuthentication->hide(); connect(ui->comboBoxClientAddress, static_cast(&QComboBox::activated), @@ -39,6 +40,19 @@ xToolsSocketClientToolUi::xToolsSocketClientToolUi(QWidget *parent) &QCheckBox::clicked, this, &xToolsSocketClientToolUi::onCheckBoxSpecifyIpAndPortClicked); + connect(ui->checkBoxAuthentication, + &QCheckBox::clicked, + this, + &xToolsSocketClientToolUi::onAuthenticationCheckBoxClicked); + connect(ui->lineEditUserName, + &QLineEdit::textChanged, + this, + &xToolsSocketClientToolUi::onUserNameLineEditTextChanged); + + connect(ui->lineEditPassword, + &QLineEdit::textChanged, + this, + &xToolsSocketClientToolUi::onPasswordLineEditTextChanged); } xToolsSocketClientToolUi::~xToolsSocketClientToolUi() @@ -53,7 +67,8 @@ void xToolsSocketClientToolUi::onIsWorkingChanged(bool isWorking) ui->checkBoxSpecifyIpAndPort->setEnabled(!isWorking); } -void xToolsSocketClientToolUi::onBaseToolUiInitialized(xToolsBaseTool *tool, const QString &settingsGroup) +void xToolsSocketClientToolUi::onBaseToolUiInitialized(xToolsBaseTool *tool, + const QString &settingsGroup) { if (!tool) { return; @@ -67,6 +82,8 @@ void xToolsSocketClientToolUi::onBaseToolUiInitialized(xToolsBaseTool *tool, con if (!tool->inherits("xToolsWebSocketClientTool")) { ui->labelMessageType->hide(); ui->comboBoxMessageType->hide(); + } else { + ui->widgetAuthentication->show(); } mTool = qobject_cast(tool); @@ -81,6 +98,9 @@ void xToolsSocketClientToolUi::onBaseToolUiInitialized(xToolsBaseTool *tool, con ui->comboBoxServerAddress->setGroupKey(settingsGroup, "serverAddress"); ui->spinBoxServerPort->setGroupKey(settingsGroup, "serverPort"); ui->comboBoxMessageType->setGroupKey(settingsGroup, "messageType"); + ui->checkBoxAuthentication->setGroupKey(settingsGroup, "authentication"); + ui->lineEditUserName->setGroupKey(settingsGroup, "username"); + ui->lineEditPassword->setGroupKey(settingsGroup, "password"); mTool->setClientIp(ui->comboBoxClientAddress->currentText().trimmed()); mTool->setClientPort(ui->spinBoxClientPort->value()); @@ -97,7 +117,7 @@ void xToolsSocketClientToolUi::onBaseToolUiInitialized(xToolsBaseTool *tool, con }); } -void xToolsSocketClientToolUi::onComboBoxClientAddressActived() +void xToolsSocketClientToolUi::onComboBoxClientAddressActivated() { if (mTool) { QString ip = ui->comboBoxClientAddress->currentText().trimmed(); @@ -134,3 +154,25 @@ void xToolsSocketClientToolUi::onCheckBoxSpecifyIpAndPortClicked() mTool->setSpecifyClientIpPort(checked); } } + +void xToolsSocketClientToolUi::onAuthenticationCheckBoxClicked() +{ + if (mTool) { + bool checked = ui->checkBoxAuthentication->isChecked(); + mTool->setAuthentication(checked); + } +} + +void xToolsSocketClientToolUi::onUserNameLineEditTextChanged(const QString &text) +{ + if (mTool) { + mTool->setUserName(text); + } +} + +void xToolsSocketClientToolUi::onPasswordLineEditTextChanged(const QString &text) +{ + if (mTool) { + mTool->setPassword(text); + } +} \ No newline at end of file diff --git a/Source/Tools/ToolsUI/xToolsSocketClientToolUi.h b/Source/Tools/ToolsUI/xToolsSocketClientToolUi.h index 6d9571f6..710dcccc 100644 --- a/Source/Tools/ToolsUI/xToolsSocketClientToolUi.h +++ b/Source/Tools/ToolsUI/xToolsSocketClientToolUi.h @@ -32,9 +32,13 @@ private: private: Ui::xToolsSocketClientToolUi *ui{nullptr}; - void onComboBoxClientAddressActived(); +public slots: + void onComboBoxClientAddressActivated(); void onSpinBoxClientPortValueChanged(int value); void onComboBoxServerAddressCurrentTextChanged(); void onSpinBoxServerPortValueChanged(int value); void onCheckBoxSpecifyIpAndPortClicked(); + void onAuthenticationCheckBoxClicked(); + void onUserNameLineEditTextChanged(const QString &text); + void onPasswordLineEditTextChanged(const QString &text); }; diff --git a/Source/Tools/ToolsUI/xToolsSocketClientToolUi.ui b/Source/Tools/ToolsUI/xToolsSocketClientToolUi.ui index d2e1ced5..c5573c8c 100644 --- a/Source/Tools/ToolsUI/xToolsSocketClientToolUi.ui +++ b/Source/Tools/ToolsUI/xToolsSocketClientToolUi.ui @@ -6,8 +6,8 @@ 0 0 - 165 - 217 + 141 + 247 @@ -26,17 +26,7 @@ 0 - - - - 65535 - - - 44444 - - - - + @@ -49,13 +39,6 @@ - - - - Qt::Horizontal - - - @@ -69,39 +52,6 @@ - - - - (closed) - - - - - - - - 0 - 0 - - - - Server IP - - - - - - - - 0 - 0 - - - - Server port - - - @@ -111,17 +61,7 @@ - Context - - - - - - - - 0 - 0 - + Binding info @@ -138,6 +78,19 @@ + + + + + 0 + 0 + + + + Server IP + + + @@ -151,10 +104,43 @@ + + + + 65535 + + + 44444 + + + + + + + + 0 + 0 + + + + + + + + + 0 + 0 + + + + Server port + + + - + 65535 @@ -167,6 +153,20 @@ + + + + (closed) + + + + + + + Qt::Horizontal + + + @@ -174,6 +174,65 @@ + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Authentication + + + + + + + + 0 + 0 + + + + + + + + User name + + + + + + + Password + + + + + + + + 0 + 0 + + + + + + + @@ -197,6 +256,11 @@ QComboBox
xToolsWebSocketMessageTypeComboBox.h
+ + xToolsLineEdit + QLineEdit +
xToolsLineEdit.h
+
diff --git a/Source/Tools/ToolsUI/xToolsToolUiFactory.cpp b/Source/Tools/ToolsUI/xToolsToolUiFactory.cpp index 238b98eb..844e00de 100644 --- a/Source/Tools/ToolsUI/xToolsToolUiFactory.cpp +++ b/Source/Tools/ToolsUI/xToolsToolUiFactory.cpp @@ -52,7 +52,7 @@ xToolsBaseToolUi *xToolsToolUiFactory::createToolUi(int type) switch (type) { case xToolsToolFactory::AnalyzerTool: return new xToolsAnalyzerToolUi(); - case xToolsToolFactory::SerialportTool: + case xToolsToolFactory::SerialPortTool: return new xToolsSerialPortToolUi(); case xToolsToolFactory::EmitterTool: return new xToolsEmitterToolUi();