<commit_before>#include "QsLog.h"
#include <QMessageBox>
#include "DownloadRemoteParamsDialog.h"
#include "ui_DownloadRemoteParamsDialog.h"

DownloadRemoteParamsDialog::DownloadRemoteParamsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DownloadRemoteParamsDialog),
    m_networkReply(NULL)
{
    ui->setupUi(this);

    connect(this, SIGNAL(canceled()), this, SLOT(cancelDownload()));
    connect(ui->okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
    connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(closeButtonClicked()));
    connect(ui->refreshButton, SIGNAL(clicked()), this, SLOT(refreshParamList()));

    // [ToDo] For now just add a deafult item for Iris.
    QListWidgetItem *item = new QListWidgetItem("Iris.param", ui->listWidget);
    ui->listWidget->addItem(item);
    m_paramUrls.append("https://raw.github.com/diydrones/ardupilot/master/Tools/Frame_params/Iris.param?ref=master");

    // [TODO] stop hiding the refresh button
    ui->refreshButton->setVisible(false);

}

DownloadRemoteParamsDialog::~DownloadRemoteParamsDialog()
{
    delete ui;
}

void DownloadRemoteParamsDialog::setStatusText(QString text)
{
    QString message = tr("Status: %1").arg(text);
    ui->statusLabel->setText(message);
}

void DownloadRemoteParamsDialog::okButtonClicked()
{
    QLOG_DEBUG() << "okButtonClicked";

    m_url = m_paramUrls.at(ui->listWidget->currentRow());

    setStatusText(tr("Downloading %1").arg(m_url.toString()));

    downloadParamFile();

    startFileDownloadRequest(m_url);
}

void DownloadRemoteParamsDialog::closeButtonClicked()
{
    QLOG_DEBUG() << "closeButtonClicked";
    close();
}

void DownloadRemoteParamsDialog::refreshParamList()
{
    QLOG_DEBUG() << "refresh list of param files from server";
    setStatusText(tr("Refresh Param file list"));

    if (m_url.isEmpty())
        m_url = QUrl("https://api.github.com/repos/diydrones/ardupilot/contents/Tools/Frame_params");

    if (m_networkReply != NULL){
        delete m_networkReply;
        m_networkReply = NULL;
    }
    m_networkReply = m_networkAccessManager.get(QNetworkRequest(m_url));
    connect(m_networkReply, SIGNAL(finished()), this, SLOT(httpParamListFinished()));
    connect(m_networkReply, SIGNAL(downloadProgress(qint64,qint64)),
            this, SLOT(updateDataReadProgress(qint64,qint64)));

}

void DownloadRemoteParamsDialog::httpParamListFinished()
{
    QLOG_DEBUG() << "DownloadRemoteParamsDialog::httpParamListFinished";

    QVariant redirectionTarget = m_networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    if (m_networkReply->error()) {
        QMessageBox::information(this, tr("HTTP"),
                                 tr("Failed to retrive list: %1.")
                                 .arg(m_networkReply->errorString()));

    } else if (!redirectionTarget.isNull()) {
        QUrl newUrl = m_url.resolved(redirectionTarget.toUrl());
        if (QMessageBox::question(this, tr("HTTP"),
                                  tr("Redirect to %1 ?").arg(newUrl.toString()),
                                  QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
            m_url = newUrl;
            m_networkReply->deleteLater();
            refreshParamList();
            return;
        }
    } else {
        // We have the JSON object from the server.
        m_paramListResponse = m_networkReply->readAll();
        QLOG_DEBUG() << "param list download response:" << m_paramListResponse;
    }


    // Clean up.
    disconnect(m_networkReply, SIGNAL(finished()), this, SLOT(httpParamListFinished()));
    disconnect(m_networkReply, SIGNAL(downloadProgress(qint64,qint64)),
            this, SLOT(updateDataReadProgress(qint64,qint64)));

    m_networkReply->deleteLater();
    m_networkReply = NULL;
}

void DownloadRemoteParamsDialog::startFileDownloadRequest(QUrl url)
{
    m_httpRequestAborted = false;
    if (m_networkReply != NULL){
        delete m_networkReply;
        m_networkReply = NULL;
    }
    m_networkReply = m_networkAccessManager.get(QNetworkRequest(url));
    connect(m_networkReply, SIGNAL(finished()), this, SLOT(httpFinished()));
    connect(m_networkReply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
    connect(m_networkReply, SIGNAL(downloadProgress(qint64,qint64)),
            this, SLOT(updateDataReadProgress(qint64,qint64)));


}

void DownloadRemoteParamsDialog::downloadParamFile()
{
    QString homeDir = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);

    QDir parameterDir = QDir(homeDir + "/apmplanner2/parameters");

    if (!parameterDir.exists())
        parameterDir.mkdir(parameterDir.filePath(""));

    QString fileName(parameterDir.filePath(
                          ui->listWidget->currentItem()->text()));

    if (fileName.isEmpty())
        return;

    if (QFile::exists(fileName)) {
        if (QMessageBox::question(this, tr("HTTP"),
              tr("There already exists a file called %1 in "
                 "the current directory. Overwrite?").arg(fileName),
              QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
                == QMessageBox::No)
            return;
        QFile::remove(fileName);
    }

    m_downloadedParamFile = new QFile(fileName);
    if (!m_downloadedParamFile->open(QIODevice::WriteOnly)) {<commit_msg>Use defines to setup directory to save parameters<commit_after>#include "QsLog.h"
#include <QMessageBox>
#include "DownloadRemoteParamsDialog.h"
#include "ui_DownloadRemoteParamsDialog.h"

DownloadRemoteParamsDialog::DownloadRemoteParamsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DownloadRemoteParamsDialog),
    m_networkReply(NULL)
{
    ui->setupUi(this);

    connect(this, SIGNAL(canceled()), this, SLOT(cancelDownload()));
    connect(ui->okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
    connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(closeButtonClicked()));
    connect(ui->refreshButton, SIGNAL(clicked()), this, SLOT(refreshParamList()));

    // [ToDo] For now just add a deafult item for Iris.
    QListWidgetItem *item = new QListWidgetItem("Iris.param", ui->listWidget);
    ui->listWidget->addItem(item);
    m_paramUrls.append("https://raw.github.com/diydrones/ardupilot/master/Tools/Frame_params/Iris.param?ref=master");

    // [TODO] stop hiding the refresh button
    ui->refreshButton->setVisible(false);

}

DownloadRemoteParamsDialog::~DownloadRemoteParamsDialog()
{
    delete ui;
}

void DownloadRemoteParamsDialog::setStatusText(QString text)
{
    QString message = tr("Status: %1").arg(text);
    ui->statusLabel->setText(message);
}

void DownloadRemoteParamsDialog::okButtonClicked()
{
    QLOG_DEBUG() << "okButtonClicked";

    m_url = m_paramUrls.at(ui->listWidget->currentRow());

    setStatusText(tr("Downloading %1").arg(m_url.toString()));

    downloadParamFile();

    startFileDownloadRequest(m_url);
}

void DownloadRemoteParamsDialog::closeButtonClicked()
{
    QLOG_DEBUG() << "closeButtonClicked";
    close();
}

void DownloadRemoteParamsDialog::refreshParamList()
{
    QLOG_DEBUG() << "refresh list of param files from server";
    setStatusText(tr("Refresh Param file list"));

    if (m_url.isEmpty())
        m_url = QUrl("https://api.github.com/repos/diydrones/ardupilot/contents/Tools/Frame_params");

    if (m_networkReply != NULL){
        delete m_networkReply;
        m_networkReply = NULL;
    }
    m_networkReply = m_networkAccessManager.get(QNetworkRequest(m_url));
    connect(m_networkReply, SIGNAL(finished()), this, SLOT(httpParamListFinished()));
    connect(m_networkReply, SIGNAL(downloadProgress(qint64,qint64)),
            this, SLOT(updateDataReadProgress(qint64,qint64)));

}

void DownloadRemoteParamsDialog::httpParamListFinished()
{
    QLOG_DEBUG() << "DownloadRemoteParamsDialog::httpParamListFinished";

    QVariant redirectionTarget = m_networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    if (m_networkReply->error()) {
        QMessageBox::information(this, tr("HTTP"),
                                 tr("Failed to retrive list: %1.")
                                 .arg(m_networkReply->errorString()));

    } else if (!redirectionTarget.isNull()) {
        QUrl newUrl = m_url.resolved(redirectionTarget.toUrl());
        if (QMessageBox::question(this, tr("HTTP"),
                                  tr("Redirect to %1 ?").arg(newUrl.toString()),
                                  QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
            m_url = newUrl;
            m_networkReply->deleteLater();
            refreshParamList();
            return;
        }
    } else {
        // We have the JSON object from the server.
        m_paramListResponse = m_networkReply->readAll();
        QLOG_DEBUG() << "param list download response:" << m_paramListResponse;
    }


    // Clean up.
    disconnect(m_networkReply, SIGNAL(finished()), this, SLOT(httpParamListFinished()));
    disconnect(m_networkReply, SIGNAL(downloadProgress(qint64,qint64)),
            this, SLOT(updateDataReadProgress(qint64,qint64)));

    m_networkReply->deleteLater();
    m_networkReply = NULL;
}

void DownloadRemoteParamsDialog::startFileDownloadRequest(QUrl url)
{
    m_httpRequestAborted = false;
    if (m_networkReply != NULL){
        delete m_networkReply;
        m_networkReply = NULL;
    }
    m_networkReply = m_networkAccessManager.get(QNetworkRequest(url));
    connect(m_networkReply, SIGNAL(finished()), this, SLOT(httpFinished()));
    connect(m_networkReply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
    connect(m_networkReply, SIGNAL(downloadProgress(qint64,qint64)),
            this, SLOT(updateDataReadProgress(qint64,qint64)));


}

void DownloadRemoteParamsDialog::downloadParamFile()
{
    QString homeDir = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);

    QDir parameterDir = QDir(homeDir + APP_DATA_DIRECTORY + PARAMETER_DIRECTORY);

    if (!parameterDir.exists())
        parameterDir.mkdir(parameterDir.filePath(""));

    QString fileName(parameterDir.filePath(
                          ui->listWidget->currentItem()->text()));

    if (fileName.isEmpty())
        return;

    if (QFile::exists(fileName)) {
        if (QMessageBox::question(this, tr("HTTP"),
              tr("There already exists a file called %1 in "
                 "the current directory. Overwrite?").arg(fileName),
              QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
                == QMessageBox::No)
            return;
        QFile::remove(fileName);
    }

    m_downloadedParamFile = new QFile(fileName);
    if (!m_downloadedParamFile->open(QIODevice::WriteOnly)) {
{'fraction_non_alphanumeric': 0.1322306326511541, 'fraction_numerical': 0.005513503410896178, 'mean_word_length': 3.7776785714285714, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 51, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 62, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6265724', 'n_tokens_mistral': 3243, 'n_tokens_neox': 3146, 'n_words': 518}