<commit_before>#include "courserasubmission.h"
#include "ui_courserasubmission.h"
#include "mainwindow.h"

#include <QCheckBox>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QSettings>

#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrlQuery>
#include <QCryptographicHash>

CourseraSubmission::CourseraSubmission(MainWindow* mw0, CourseraProject& cp) :
    QDialog(mw0), _cur_phase(S_NONE), project(cp), mw(mw0),
    ui(new Ui::CourseraSubmission)
{
    ui->setupUi(this);
    QVBoxLayout* modelLayout = new QVBoxLayout;
    ui->modelBox->setLayout(modelLayout);
    QVBoxLayout* problemLayout = new QVBoxLayout;
    ui->problemBox->setLayout(problemLayout);

    for (int i=0; i<project.models.size(); i++) {
        const CourseraItem& item = project.models.at(i);
        QCheckBox* cb = new QCheckBox(item.name);
        cb->setChecked(false);
        modelLayout->addWidget(cb);
    }
    for (int i=0; i<project.problems.size(); i++) {
        const CourseraItem& item = project.problems.at(i);
        QCheckBox* cb = new QCheckBox(item.name);
        cb->setChecked(true);
        problemLayout->addWidget(cb);
    }
    if (project.models.empty())
        modelLayout->addWidget(new QLabel("none"));
    if (project.problems.empty())
        problemLayout->addWidget(new QLabel("none"));
    _output_stream.setString(&_submission);

    QSettings settings;
    settings.beginGroup("coursera");
    ui->storePassword->setChecked(settings.value("storeLogin",false).toBool());
    ui->login->setText(settings.value("login").toString());
    ui->password->setText(settings.value("password").toString());
    settings.endGroup();
}

CourseraSubmission::~CourseraSubmission()
{
    QSettings settings;
    settings.beginGroup("coursera");
    bool storeLogin = ui->storePassword->isChecked();
    settings.setValue("storeLogin", storeLogin);
    if (storeLogin) {
        settings.setValue("login",ui->login->text());
        settings.setValue("password",ui->password->text());
    } else {
        settings.setValue("login","");
        settings.setValue("password","");
    }
    settings.endGroup();
    delete ui;
}

QByteArray CourseraSubmission::challenge_response(QString passwd, QString challenge)
{
    QByteArray hash = QCryptographicHash::hash(QString(challenge+passwd).toLocal8Bit(), QCryptographicHash::Sha1);
    return hash.toHex();
}

void CourseraSubmission::disableUI()
{
    ui->loginGroup->setEnabled(false);
    ui->modelBox->setEnabled(false);
    ui->problemBox->setEnabled(false);
    ui->runButton->setText("Cancel");
}

void CourseraSubmission::enableUI()
{
    ui->loginGroup->setEnabled(true);
    ui->modelBox->setEnabled(true);
    ui->problemBox->setEnabled(true);
    ui->runButton->setText("Run and submit");
}

void CourseraSubmission::cancelOperation()
{
    switch (_cur_phase) {
    case S_NONE:
        return;
    case S_WAIT_CHALLENGE:
        disconnect(reply, SIGNAL(finished()), this, SLOT(rcv_challenge()));
        break;
    case S_WAIT_SOLVE:
        disconnect(mw, SIGNAL(finished()), this, SLOT(solver_finished()));
        mw->on_actionStop_triggered();
        break;
    case S_WAIT_SUBMIT:
        disconnect(reply, SIGNAL(finished()), this, SLOT(rcv_solution_reply()));
        break;
    }
    ui->textBrowser->insertPlainText("Cancelled.\n");
    _cur_phase = S_NONE;
    enableUI();
}

void CourseraSubmission::reject()
{
    cancelOperation();
    QDialog::reject();
}

void CourseraSubmission::on_checkLoginButton_clicked()
{
    _current_model = -2;
    get_challenge();
}

void CourseraSubmission::get_challenge()
{
    QString email = ui->login->text();
    if (email.isEmpty()) {
        QMessageBox::warning(this, "MiniZinc IDE",
                             "Enter an email address for Coursera login!");
        _current_model = -2;
        return;
    }
    if (ui->password->text().isEmpty()) {
        QMessageBox::warning(this, "MiniZinc IDE",
                             "Enter a password for Coursera login!");
        _current_model = -2;
        return;
    }
    QUrl url("https://class.coursera.org/" + project.course + "/assignment/challenge");
    QUrlQuery q;
    q.addQueryItem("email_address", email);
    q.addQueryItem("assignment_part_sid", "6vp6Er9J-dev");
    q.addQueryItem("response_encoding","delim");<commit_msg>Add dialog that asks whether to abort a Coursera submission<commit_after>#include "courserasubmission.h"
#include "ui_courserasubmission.h"
#include "mainwindow.h"

#include <QCheckBox>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QSettings>

#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrlQuery>
#include <QCryptographicHash>

CourseraSubmission::CourseraSubmission(MainWindow* mw0, CourseraProject& cp) :
    QDialog(mw0), _cur_phase(S_NONE), project(cp), mw(mw0),
    ui(new Ui::CourseraSubmission)
{
    ui->setupUi(this);
    QVBoxLayout* modelLayout = new QVBoxLayout;
    ui->modelBox->setLayout(modelLayout);
    QVBoxLayout* problemLayout = new QVBoxLayout;
    ui->problemBox->setLayout(problemLayout);

    for (int i=0; i<project.models.size(); i++) {
        const CourseraItem& item = project.models.at(i);
        QCheckBox* cb = new QCheckBox(item.name);
        cb->setChecked(false);
        modelLayout->addWidget(cb);
    }
    for (int i=0; i<project.problems.size(); i++) {
        const CourseraItem& item = project.problems.at(i);
        QCheckBox* cb = new QCheckBox(item.name);
        cb->setChecked(true);
        problemLayout->addWidget(cb);
    }
    if (project.models.empty())
        modelLayout->addWidget(new QLabel("none"));
    if (project.problems.empty())
        problemLayout->addWidget(new QLabel("none"));
    _output_stream.setString(&_submission);

    QSettings settings;
    settings.beginGroup("coursera");
    ui->storePassword->setChecked(settings.value("storeLogin",false).toBool());
    ui->login->setText(settings.value("login").toString());
    ui->password->setText(settings.value("password").toString());
    settings.endGroup();
}

CourseraSubmission::~CourseraSubmission()
{
    QSettings settings;
    settings.beginGroup("coursera");
    bool storeLogin = ui->storePassword->isChecked();
    settings.setValue("storeLogin", storeLogin);
    if (storeLogin) {
        settings.setValue("login",ui->login->text());
        settings.setValue("password",ui->password->text());
    } else {
        settings.setValue("login","");
        settings.setValue("password","");
    }
    settings.endGroup();
    delete ui;
}

QByteArray CourseraSubmission::challenge_response(QString passwd, QString challenge)
{
    QByteArray hash = QCryptographicHash::hash(QString(challenge+passwd).toLocal8Bit(), QCryptographicHash::Sha1);
    return hash.toHex();
}

void CourseraSubmission::disableUI()
{
    ui->loginGroup->setEnabled(false);
    ui->modelBox->setEnabled(false);
    ui->problemBox->setEnabled(false);
    ui->runButton->setText("Abort");
}

void CourseraSubmission::enableUI()
{
    ui->loginGroup->setEnabled(true);
    ui->modelBox->setEnabled(true);
    ui->problemBox->setEnabled(true);
    ui->runButton->setText("Run and submit");
}

void CourseraSubmission::cancelOperation()
{
    switch (_cur_phase) {
    case S_NONE:
        return;
    case S_WAIT_CHALLENGE:
        disconnect(reply, SIGNAL(finished()), this, SLOT(rcv_challenge()));
        break;
    case S_WAIT_SOLVE:
        disconnect(mw, SIGNAL(finished()), this, SLOT(solver_finished()));
        mw->on_actionStop_triggered();
        break;
    case S_WAIT_SUBMIT:
        disconnect(reply, SIGNAL(finished()), this, SLOT(rcv_solution_reply()));
        break;
    }
    ui->textBrowser->insertPlainText("Aborted.\n");
    _cur_phase = S_NONE;
    enableUI();
}

void CourseraSubmission::reject()
{
    if (_cur_phase != S_NONE &&
            QMessageBox::warning(this, "MiniZinc IDE",
                                 "Do you want to close this window and abort the Coursera submission?",
                                 QMessageBox::Close| QMessageBox::Cancel,
                                 QMessageBox::Cancel) == QMessageBox::Cancel) {
        return;
    }
    cancelOperation();
    QDialog::reject();
}

void CourseraSubmission::on_checkLoginButton_clicked()
{
    _current_model = -2;
    get_challenge();
}

void CourseraSubmission::get_challenge()
{
    QString email = ui->login->text();
    if (email.isEmpty()) {
        QMessageBox::warning(this, "MiniZinc IDE",
                             "Enter an email address for Coursera login!");
        _current_model = -2;
        return;
    }
    if (ui->password->text().isEmpty()) {
        QMessageBox::warning(this, "MiniZinc IDE",
                             "Enter a password for Coursera login!");
        _current_model = -2;
        return;
    }
    QUrl url("https://class.coursera.org/" + project.course + "/assignment/challenge");
    QUrlQuery q;
    q.addQueryItem("email_address", email);
    q.addQueryItem("assignment_part_sid", "6vp6Er9J-dev");
    q.addQueryItem("response_encoding","delim");
{'fraction_non_alphanumeric': 0.14800528983910072, 'fraction_numerical': 0.0028653295128939827, 'mean_word_length': 3.5763993948562782, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 111, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2471351', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2872, 'n_words': 471}