<commit_before>/*
** Copyright 2017 ViVoka
**
** Made by leroy_v
** Mail <<EMAIL>>
**
** vivoka.com
*/

#include "RequestBuilder.hpp"

// Qt includes -----------------------------------------------------------------
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSignalMapper>
#include <QMessageBox>
#include <QBuffer>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QDir>
#include <QSysInfo>
#include <QUrlQuery>
#include <QTimer>
#include <QCompleter>
#include <QFileSystemModel>
#include <QStringListModel>
#include <QList>
#include <QJsonDocument>
#include <QSet>

// C++ standard library includes -----------------------------------------------
#include <memory>

RequestBuilder::RequestBuilder(QWidget * parent) :
    QWidget(parent),
    _networkManager(new QNetworkAccessManager(this)),
    _currentRequest(nullptr),
    _urlCompletionModel(new QStringListModel())
{
    _ui.setupUi(this);

    // Add completer to content type line edit
    auto contentTypeCompleter = new QCompleter({"application/json",
                                                "application/x-www-form-urlencoded",
                                                "application/xml",
                                                "application/zip",
                                                "text/plain",
                                                "text/html"},
                                               _ui.leContentType);
    contentTypeCompleter->setCaseSensitivity(Qt::CaseInsensitive);
    _ui.leContentType->setCompleter(contentTypeCompleter);

    // Add completer to file path line edit
    auto filePathCompleterModel = new QFileSystemModel(_ui.leFilePath);
    filePathCompleterModel->setRootPath(QDir::rootPath());
    _ui.leFilePath->setCompleter(new QCompleter(filePathCompleterModel, _ui.leFilePath));

    // Add completer to the URL line edit with history for completion
    _urlCompletionModel->setParent(_ui.leUrl);
    _ui.leUrl->setCompleter(new QCompleter(_urlCompletionModel, _ui.leUrl));

    // Radio button content + file
    QObject::connect(_ui.rbFile, &QRadioButton::toggled, [this](bool checked)
    {
        _ui.leFilePath->setEnabled(checked);
        _ui.pbBrowseFile->setEnabled(checked);
        _ui.pteContent->setEnabled(!checked);
    });
    // Browse file button
    QObject::connect(_ui.pbBrowseFile, &QPushButton::clicked, [this]
    {
        static auto directoryPath = _ui.leFilePath->text().isEmpty() ? QDir::homePath()
                                         : QFileInfo(_ui.leFilePath->text()).absoluteFilePath();
        const auto filename = QFileDialog::getOpenFileName(this, "Choose file", directoryPath);
        if (filename.isEmpty())
            return ;
        directoryPath = QFileInfo(filename).absoluteFilePath();
        _ui.leFilePath->setText(filename);
    });

    // Push button GET, POST, PUT, Submit
    auto signalMapper = new QSignalMapper(this);
    signalMapper->setMapping(_ui.pbGet,  _ui.pbGet->text());
    signalMapper->setMapping(_ui.pbPost, _ui.pbPost->text());
    signalMapper->setMapping(_ui.pbPut,  _ui.pbPut->text());

    QObject::connect(_ui.pbGet,  &QPushButton::clicked, signalMapper,
                     static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map));
    QObject::connect(_ui.pbPost, &QPushButton::clicked, signalMapper,
                     static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map));
    QObject::connect(_ui.pbPut,  &QPushButton::clicked, signalMapper,
                     static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map));
    QObject::connect(signalMapper,
                     static_cast<void(QSignalMapper::*)(const QString &)>(&QSignalMapper::mapped),
                     this, &RequestBuilder::_submitRequest);
    QObject::connect(_ui.pbSubmit, &QPushButton::clicked, [this]
    { _submitRequest(_ui.cbMethod->currentText()); });

    // Header view add button
    QObject::connect(_ui.leNameHeaders, &QLineEdit::textChanged, [this](const QString & text)
    { _ui.pbAddHeaders->setEnabled(!text.isEmpty() && !_ui.leValueHeaders->text().isEmpty()); });
    QObject::connect(_ui.leValueHeaders, &QLineEdit::textChanged, [this](const QString & text)
    { _ui.pbAddHeaders->setEnabled(!text.isEmpty() && !_ui.leNameHeaders->text().isEmpty()); });
    QObject::connect(_ui.pbAddHeaders, &QPushButton::clicked, [this]
    {
        if (!_addEntryToTable(_ui.tableHeaders, _ui.leNameHeaders->text(), _ui.leValueHeaders->text()))
            return ;<commit_msg>Remove deprecated use of QSignalMapper by using lambdas instead<commit_after>/*
** Copyright 2017 ViVoka
**
** Made by leroy_v
** Mail <<EMAIL>>
**
** vivoka.com
*/

#include "RequestBuilder.hpp"

// Qt includes -----------------------------------------------------------------
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QMessageBox>
#include <QBuffer>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QDir>
#include <QSysInfo>
#include <QUrlQuery>
#include <QTimer>
#include <QCompleter>
#include <QFileSystemModel>
#include <QStringListModel>
#include <QList>
#include <QJsonDocument>
#include <QSet>

// C++ standard library includes -----------------------------------------------
#include <memory>

RequestBuilder::RequestBuilder(QWidget * parent) :
    QWidget(parent),
    _networkManager(new QNetworkAccessManager(this)),
    _currentRequest(nullptr),
    _urlCompletionModel(new QStringListModel())
{
    _ui.setupUi(this);

    // Add completer to content type line edit
    auto contentTypeCompleter = new QCompleter({"application/json",
                                                "application/x-www-form-urlencoded",
                                                "application/xml",
                                                "application/zip",
                                                "text/plain",
                                                "text/html"},
                                               _ui.leContentType);
    contentTypeCompleter->setCaseSensitivity(Qt::CaseInsensitive);
    _ui.leContentType->setCompleter(contentTypeCompleter);

    // Add completer to file path line edit
    auto filePathCompleterModel = new QFileSystemModel(_ui.leFilePath);
    filePathCompleterModel->setRootPath(QDir::rootPath());
    _ui.leFilePath->setCompleter(new QCompleter(filePathCompleterModel, _ui.leFilePath));

    // Add completer to the URL line edit with history for completion
    _urlCompletionModel->setParent(_ui.leUrl);
    _ui.leUrl->setCompleter(new QCompleter(_urlCompletionModel, _ui.leUrl));

    // Radio button content + file
    QObject::connect(_ui.rbFile, &QRadioButton::toggled, [this](bool checked)
    {
        _ui.leFilePath->setEnabled(checked);
        _ui.pbBrowseFile->setEnabled(checked);
        _ui.pteContent->setEnabled(!checked);
    });
    // Browse file button
    QObject::connect(_ui.pbBrowseFile, &QPushButton::clicked, [this]
    {
        static auto directoryPath = _ui.leFilePath->text().isEmpty() ? QDir::homePath()
                                         : QFileInfo(_ui.leFilePath->text()).absoluteFilePath();
        const auto filename = QFileDialog::getOpenFileName(this, "Choose file", directoryPath);
        if (filename.isEmpty())
            return ;
        directoryPath = QFileInfo(filename).absoluteFilePath();
        _ui.leFilePath->setText(filename);
    });

    // Push button GET, POST, PUT, Submit
    QObject::connect(_ui.pbGet, &QPushButton::clicked,
                     [this]{ _submitRequest(_ui.pbGet->text()); });
    QObject::connect(_ui.pbPost, &QPushButton::clicked,
                     [this]{ _submitRequest(_ui.pbPost->text()); });
    QObject::connect(_ui.pbPut,  &QPushButton::clicked,
                     [this]{ _submitRequest(_ui.pbPut->text()); });
    QObject::connect(_ui.pbSubmit, &QPushButton::clicked, [this]
    { _submitRequest(_ui.cbMethod->currentText()); });

    // Header view add button
    QObject::connect(_ui.leNameHeaders, &QLineEdit::textChanged, [this](const QString & text)
    { _ui.pbAddHeaders->setEnabled(!text.isEmpty() && !_ui.leValueHeaders->text().isEmpty()); });
    QObject::connect(_ui.leValueHeaders, &QLineEdit::textChanged, [this](const QString & text)
    { _ui.pbAddHeaders->setEnabled(!text.isEmpty() && !_ui.leNameHeaders->text().isEmpty()); });
    QObject::connect(_ui.pbAddHeaders, &QPushButton::clicked, [this]
    {
        if (!_addEntryToTable(_ui.tableHeaders, _ui.leNameHeaders->text(), _ui.leValueHeaders->text()))
            return ;
{'fraction_non_alphanumeric': 0.1573686657368666, 'fraction_numerical': 0.0009298000929800093, 'mean_word_length': 2.752725686873092, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6298783', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2402, 'n_words': 418}