#include "main_window.h" #include "LtsRestClient/lts_rest_client.h" #include "ui_main_window.h" #include #include #include #include #include #include #include #include #include // Documentation on how to use it can be found at https://confluence-classified.rh-int.de/spaces/LEG/pages/207919552/HOWTO+RestClientUi+Tool MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // Connect core HTTP actions connect(ui->m_getBtn, &QPushButton::clicked, this, &MainWindow::onGetClicked); connect(ui->m_postBtn, &QPushButton::clicked, this, &MainWindow::onPostClicked); connect(ui->m_putBtn, &QPushButton::clicked, this, &MainWindow::onPutClicked); connect(ui->m_deleteBtn, &QPushButton::clicked, this, &MainWindow::onDeleteClicked); // Connect logging output actions connect(ui->m_clearResponseBtn, &QPushButton::clicked, this, &MainWindow::onClearResponseClicked); connect(ui->m_exportResponseBtn, &QPushButton::clicked, this, &MainWindow::onExportResponseClicked); loadRequestTemplates(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::onClearResponseClicked() { ui->m_responseOutput->clear(); } void MainWindow::onExportResponseClicked() { const QString content = ui->m_responseOutput->toPlainText().trimmed(); if (content.isEmpty()) { QMessageBox::warning(this, QStringLiteral("Export Failed"), QStringLiteral("There is no response data available to export.")); return; } const QString fileName = QFileDialog::getSaveFileName(this, QStringLiteral("Save Response as JSON"), QDir::homePath() + QStringLiteral("/response.json"), QStringLiteral("JSON Files (*.json);;All Files (*)")); if (fileName.isEmpty()) return; QJsonParseError parseError; const QJsonDocument jsonDoc = QJsonDocument::fromJson(content.toUtf8(), &parseError); QByteArray dataToWrite; if (parseError.error == QJsonParseError::NoError) dataToWrite = jsonDoc.toJson(QJsonDocument::Indented); else dataToWrite = content.toUtf8(); QFile file(fileName); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { file.write(dataToWrite); file.close(); QMessageBox::information(this, QStringLiteral("Success"), QStringLiteral("File saved successfully.")); } else QMessageBox::critical(this, QStringLiteral("Error"), QStringLiteral("Could not write to file:\n") + file.errorString()); } void MainWindow::highlightMethodButton(const QString &method) { // Reset stylesheets for all HTTP method buttons back to default ui->m_getBtn->setStyleSheet(QString()); ui->m_postBtn->setStyleSheet(QString()); ui->m_putBtn->setStyleSheet(QString()); ui->m_deleteBtn->setStyleSheet(QString()); // Modern green border highlight style string const QString highlightStyle = QStringLiteral("border: 2px solid #2ecc71; font-weight: bold; background-color: #e8f8f5;"); // Apply style based on the uppercase method string const QString upperMethod = method.toUpper().trimmed(); if (upperMethod == QStringLiteral("GET")) ui->m_getBtn->setStyleSheet(highlightStyle); else if (upperMethod == QStringLiteral("POST")) ui->m_postBtn->setStyleSheet(highlightStyle); else if (upperMethod == QStringLiteral("PUT")) ui->m_putBtn->setStyleSheet(highlightStyle); else if (upperMethod == QStringLiteral("DELETE") || upperMethod == QStringLiteral("DEL")) ui->m_deleteBtn->setStyleSheet(highlightStyle); } void MainWindow::loadRequestTemplates() { const QString templatePath = QCoreApplication::applicationDirPath() + QStringLiteral("/templates"); QDir directory(templatePath); if (!directory.exists()) directory.mkpath(QStringLiteral(".")); QStringList filters; filters << QStringLiteral("*.json"); directory.setNameFilters(filters); const QFileInfoList fileList = directory.entryInfoList(QDir::Files); int insertIndex = 0; for (const QFileInfo &fileInfo : fileList) { const QString absolutePath = fileInfo.absoluteFilePath(); QFile file(absolutePath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { continue; } const QByteArray fileContent = file.readAll(); file.close(); QJsonParseError parseError; const QJsonDocument metaDoc = QJsonDocument::fromJson(fileContent, &parseError); if (parseError.error != QJsonParseError::NoError || !metaDoc.isObject()) continue; const QJsonObject metaObj = metaDoc.object(); QString displayName = metaObj.value(QStringLiteral("name")).toString(); if (displayName.isEmpty()) displayName = fileInfo.fileName(); const QString targetEndpoint = metaObj.value(QStringLiteral("endpoint")).toString(); const QString targetMethod = metaObj.value(QStringLiteral("method")).toString().toUpper().trimmed(); QPushButton *templateBtn = new QPushButton(displayName, this); connect(templateBtn, &QPushButton::clicked, this, [this, metaObj, targetEndpoint, targetMethod]() { // 1. Fill values into fields ui->m_endpointInput->setText(targetEndpoint); ui->m_jsonInput->clear(); if (metaObj.contains(QStringLiteral("payload"))) { const QJsonValue payloadValue = metaObj.value(QStringLiteral("payload")); if (payloadValue.isObject()) { const QJsonDocument doc(payloadValue.toObject()); ui->m_jsonInput->setPlainText(QString::fromUtf8(doc.toJson(QJsonDocument::Indented))); } else if (payloadValue.isArray()) { const QJsonDocument doc(payloadValue.toArray()); ui->m_jsonInput->setPlainText(QString::fromUtf8(doc.toJson(QJsonDocument::Indented))); } else { ui->m_jsonInput->setPlainText(payloadValue.toVariant().toString()); } } // 2. Visually focus the matching HTTP button highlightMethodButton(targetMethod); // 3. NEW: Optional Auto-Submit trigger chain execution if (ui->m_autoSubmitCheck->isChecked()) { if (targetMethod == QStringLiteral("GET")) ui->m_getBtn->animateClick(); // animateClick provides nice visual feedback else if (targetMethod == QStringLiteral("POST")) ui->m_postBtn->animateClick(); else if (targetMethod == QStringLiteral("PUT")) ui->m_putBtn->animateClick(); else if (targetMethod == QStringLiteral("DELETE") || targetMethod == QStringLiteral("DEL")) ui->m_deleteBtn->animateClick(); } }); ui->templateButtonContainer->insertWidget(insertIndex++, templateBtn); } } LtsCommon::LtsRestClient *MainWindow::createClient() { return new LtsCommon::LtsRestClient(ui->m_urlInput->text(), this); } void MainWindow::showSuccess(const QJsonDocument &json) { ui->m_responseOutput->setTextColor(QColor(Qt::darkGreen)); ui->m_responseOutput->setText(QString::fromLatin1(json.toJson(QJsonDocument::Indented))); } void MainWindow::showError(const QString &error) { ui->m_responseOutput->setTextColor(QColor(Qt::red)); ui->m_responseOutput->setText(QStringLiteral("Error: ") + error); } void MainWindow::onGetClicked() { ui->m_responseOutput->clear(); LtsCommon::LtsRestClient *client = createClient(); client->get( ui->m_endpointInput->text(), [this](const QJsonDocument &json) { showSuccess(json); }, [this](const QString &err) { showError(err); }); } void MainWindow::onPostClicked() { ui->m_responseOutput->clear(); QJsonParseError parseError; QJsonDocument jsonDoc = QJsonDocument::fromJson(ui->m_jsonInput->toPlainText().toUtf8(), &parseError); if (parseError.error != QJsonParseError::NoError) { showError(QStringLiteral("Invalid JSON input syntax: ") + parseError.errorString()); return; } LtsCommon::LtsRestClient *client = createClient(); client->post( ui->m_endpointInput->text(), jsonDoc, [this](const QJsonDocument &json) { showSuccess(json); }, [this](const QString &err) { showError(err); }); } void MainWindow::onPutClicked() { ui->m_responseOutput->clear(); QJsonParseError parseError; QJsonDocument jsonDoc = QJsonDocument::fromJson(ui->m_jsonInput->toPlainText().toUtf8(), &parseError); if (parseError.error != QJsonParseError::NoError) { showError(QStringLiteral("Invalid JSON input syntax: ") + parseError.errorString()); return; } LtsCommon::LtsRestClient *client = createClient(); client->put( ui->m_endpointInput->text(), jsonDoc, [this](const QJsonDocument &json) { showSuccess(json); }, [this](const QString &err) { showError(err); }); } void MainWindow::onDeleteClicked() { ui->m_responseOutput->clear(); LtsCommon::LtsRestClient *client = createClient(); client->del( ui->m_endpointInput->text(), [this](const QJsonDocument &json) { showSuccess(json); }, [this](const QString &err) { showError(err); }); }