<commit_before>#include "jobwidget.h"
#include "debug.h"
#include "restoredialog.h"
#include "ui_jobwidget.h"
#include "utils.h"

#include <QMenu>

JobWidget::JobWidget(QWidget *parent)
    : QWidget(parent), _ui(new Ui::JobWidget), _saveEnabled(false)
{
    _ui->setupUi(this);
    _ui->archiveListWidget->setAttribute(Qt::WA_MacShowFocusRect, false);

    connect(_ui->jobNameLineEdit, &QLineEdit::textChanged, [&]() {
        if(_job->objectKey().isEmpty())
            emit enableSave(canSaveNew());
    });
    connect(_ui->jobTreeWidget, &FilePickerWidget::selectionChanged, [&]() {
        if(_job->objectKey().isEmpty())
            emit enableSave(canSaveNew());
        else
            save();
    });
//    connect(_ui->jobTreeWidget, &FilePickerWidget::focusLost,
//            [&](){
//                    if(!_job->objectKey().isEmpty())
//                        save();
//            });

    connect(_ui->includeScheduledCheckBox, &QCheckBox::toggled, this,
            &JobWidget::save);
    connect(_ui->preservePathsCheckBox, &QCheckBox::toggled, this,
            &JobWidget::save);
    connect(_ui->traverseMountCheckBox, &QCheckBox::toggled, this,
            &JobWidget::save);
    connect(_ui->followSymLinksCheckBox, &QCheckBox::toggled, this,
            &JobWidget::save);
    connect(_ui->skipNoDumpCheckBox, &QCheckBox::toggled, this, &JobWidget::save);
    connect(_ui->skipFilesSizeSpinBox, &QSpinBox::editingFinished, this,
            &JobWidget::save);
    connect(_ui->skipFilesCheckBox, &QCheckBox::toggled, this, &JobWidget::save);
    connect(_ui->skipFilesLineEdit, &QLineEdit::editingFinished, this,
            &JobWidget::save);
    connect(_ui->hideButton, &QPushButton::clicked, this, &JobWidget::collapse);
    connect(_ui->restoreLatestArchiveButton, &QPushButton::clicked, this,
            &JobWidget::restoreLatestArchive);
    connect(_ui->archiveListWidget, &ArchiveListWidget::inspectArchive, this,
            &JobWidget::inspectJobArchive);
    connect(_ui->archiveListWidget, &ArchiveListWidget::restoreArchive, this,
            &JobWidget::restoreJobArchive);
    connect(_ui->archiveListWidget, &ArchiveListWidget::deleteArchives, this,
            &JobWidget::deleteJobArchives);
    connect(_ui->skipFilesDefaultsButton, &QPushButton::clicked, [&]() {
        QSettings settings;
        _ui->skipFilesLineEdit->setText(
            settings.value("app/skip_system_files", DEFAULT_SKIP_FILES).toString());
    });
    connect(_ui->archiveListWidget,
            &ArchiveListWidget::customContextMenuRequested, this,
            &JobWidget::showArchiveListMenu);
    connect(_ui->actionDelete, &QAction::triggered, _ui->archiveListWidget,
            &ArchiveListWidget::removeSelectedItems);
    connect(_ui->actionRestore, &QAction::triggered, _ui->archiveListWidget,
            &ArchiveListWidget::restoreSelectedItem);
    connect(_ui->actionInspect, &QAction::triggered, _ui->archiveListWidget,
            &ArchiveListWidget::inspectSelectedItem);
}

JobWidget::~JobWidget()
{
    delete _ui;
}
JobPtr JobWidget::job() const
{
    return _job;
}

void JobWidget::setJob(const JobPtr &job)
{
    _job = job;

    // Creating a new job?
    if(_job->objectKey().isEmpty())
    {
        _ui->tabWidget->setTabEnabled(_ui->tabWidget->indexOf(_ui->archiveListTab),
                                      false);
        _ui->restoreLatestArchiveButton->hide();
        _ui->jobNameLabel->hide();
        _ui->jobNameLineEdit->show();
        _ui->tabWidget->setCurrentWidget(_ui->jobTreeTab);
        _ui->jobNameLineEdit->setFocus();
    }
    else
    {
        _saveEnabled = false;
        _ui->tabWidget->setTabEnabled(_ui->tabWidget->indexOf(_ui->archiveListTab),
                                      true);
        _ui->restoreLatestArchiveButton->show();
        _ui->jobNameLabel->show();
        _ui->jobNameLineEdit->hide();
        _ui->tabWidget->setCurrentWidget(_ui->jobTreeTab);
        connect(_job.data(), &Job::changed, this, &JobWidget::updateDetails);
        _saveEnabled = true;
    }
    updateDetails();
}

void JobWidget::save()
{
    if(_saveEnabled && !_job->objectKey().isEmpty())
    {
        DEBUG << "SAVE JOB";
        _job->setUrls(_ui->jobTreeWidget->getSelectedUrls());
        _job->setOptionScheduledEnabled(
            _ui->includeScheduledCheckBox->isChecked());
        _job->setOptionPreservePaths(_ui->preservePathsCheckBox->isChecked());
        _job->setOptionTraverseMount(_ui->traverseMountCheckBox->isChecked());
        _job->setOptionFollowSymLinks(_ui->followSymLinksCheckBox->isChecked());
        _job->setOptionSkipNoDump(_ui->skipNoDumpCheckBox->isChecked());
        _job->setOptionSkipFilesSize(_ui->skipFilesSizeSpinBox->value());
        _job->setOptionSkipFiles(_ui->skipFilesCheckBox->isChecked());
        _job->setOptionSkipFilesPatterns(_ui->skipFilesLineEdit->text());
        _job->save();
    }
}

void JobWidget::saveNew()<commit_msg>Resolve issues when setting a new Job for JobWidget.<commit_after>#include "jobwidget.h"
#include "debug.h"
#include "restoredialog.h"
#include "ui_jobwidget.h"
#include "utils.h"

#include <QMenu>

JobWidget::JobWidget(QWidget *parent)
    : QWidget(parent), _ui(new Ui::JobWidget), _saveEnabled(false)
{
    _ui->setupUi(this);
    _ui->archiveListWidget->setAttribute(Qt::WA_MacShowFocusRect, false);

    connect(_ui->jobNameLineEdit, &QLineEdit::textChanged, [&]() {
        if(_job->objectKey().isEmpty())
            emit enableSave(canSaveNew());
    });
    connect(_ui->jobTreeWidget, &FilePickerWidget::selectionChanged, [&]() {
        if(_job->objectKey().isEmpty())
            emit enableSave(canSaveNew());
        else
            save();
    });
//    connect(_ui->jobTreeWidget, &FilePickerWidget::focusLost,
//            [&](){
//                    if(!_job->objectKey().isEmpty())
//                        save();
//            });

    connect(_ui->includeScheduledCheckBox, &QCheckBox::toggled, this,
            &JobWidget::save);
    connect(_ui->preservePathsCheckBox, &QCheckBox::toggled, this,
            &JobWidget::save);
    connect(_ui->traverseMountCheckBox, &QCheckBox::toggled, this,
            &JobWidget::save);
    connect(_ui->followSymLinksCheckBox, &QCheckBox::toggled, this,
            &JobWidget::save);
    connect(_ui->skipNoDumpCheckBox, &QCheckBox::toggled, this, &JobWidget::save);
    connect(_ui->skipFilesSizeSpinBox, &QSpinBox::editingFinished, this,
            &JobWidget::save);
    connect(_ui->skipFilesCheckBox, &QCheckBox::toggled, this, &JobWidget::save);
    connect(_ui->skipFilesLineEdit, &QLineEdit::editingFinished, this,
            &JobWidget::save);
    connect(_ui->hideButton, &QPushButton::clicked, this, &JobWidget::collapse);
    connect(_ui->restoreLatestArchiveButton, &QPushButton::clicked, this,
            &JobWidget::restoreLatestArchive);
    connect(_ui->archiveListWidget, &ArchiveListWidget::inspectArchive, this,
            &JobWidget::inspectJobArchive);
    connect(_ui->archiveListWidget, &ArchiveListWidget::restoreArchive, this,
            &JobWidget::restoreJobArchive);
    connect(_ui->archiveListWidget, &ArchiveListWidget::deleteArchives, this,
            &JobWidget::deleteJobArchives);
    connect(_ui->skipFilesDefaultsButton, &QPushButton::clicked, [&]() {
        QSettings settings;
        _ui->skipFilesLineEdit->setText(
            settings.value("app/skip_system_files", DEFAULT_SKIP_FILES).toString());
    });
    connect(_ui->archiveListWidget,
            &ArchiveListWidget::customContextMenuRequested, this,
            &JobWidget::showArchiveListMenu);
    connect(_ui->actionDelete, &QAction::triggered, _ui->archiveListWidget,
            &ArchiveListWidget::removeSelectedItems);
    connect(_ui->actionRestore, &QAction::triggered, _ui->archiveListWidget,
            &ArchiveListWidget::restoreSelectedItem);
    connect(_ui->actionInspect, &QAction::triggered, _ui->archiveListWidget,
            &ArchiveListWidget::inspectSelectedItem);
}

JobWidget::~JobWidget()
{
    delete _ui;
}
JobPtr JobWidget::job() const
{
    return _job;
}

void JobWidget::setJob(const JobPtr &job)
{
    _job = job;

    _saveEnabled = false;
    // Creating a new job?
    if(_job->objectKey().isEmpty())
    {
        _ui->tabWidget->setTabEnabled(_ui->tabWidget->indexOf(_ui->archiveListTab),
                                      false);
        _ui->restoreLatestArchiveButton->hide();
        _ui->jobNameLabel->hide();
        _ui->jobNameLineEdit->show();
        _ui->jobNameLineEdit->setFocus();
    }
    else
    {
        _ui->tabWidget->setTabEnabled(_ui->tabWidget->indexOf(_ui->archiveListTab),
                                      true);
        _ui->restoreLatestArchiveButton->show();
        _ui->jobNameLabel->show();
        _ui->jobNameLineEdit->hide();
        connect(_job.data(), &Job::changed, this, &JobWidget::updateDetails);
    }
    _ui->tabWidget->setCurrentWidget(_ui->jobTreeTab);
    updateDetails();
    _saveEnabled = true;
}

void JobWidget::save()
{
    if(_saveEnabled && !_job->objectKey().isEmpty())
    {
        DEBUG << "SAVE JOB";
        _job->setUrls(_ui->jobTreeWidget->getSelectedUrls());
        _job->setOptionScheduledEnabled(
            _ui->includeScheduledCheckBox->isChecked());
        _job->setOptionPreservePaths(_ui->preservePathsCheckBox->isChecked());
        _job->setOptionTraverseMount(_ui->traverseMountCheckBox->isChecked());
        _job->setOptionFollowSymLinks(_ui->followSymLinksCheckBox->isChecked());
        _job->setOptionSkipNoDump(_ui->skipNoDumpCheckBox->isChecked());
        _job->setOptionSkipFilesSize(_ui->skipFilesSizeSpinBox->value());
        _job->setOptionSkipFiles(_ui->skipFilesCheckBox->isChecked());
        _job->setOptionSkipFilesPatterns(_ui->skipFilesLineEdit->text());
        _job->save();
    }
}

void JobWidget::saveNew()
{'fraction_non_alphanumeric': 0.14802100161550888, 'fraction_numerical': 0.0, 'mean_word_length': 3.4417040358744395, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 186, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5493246', 'n_tokens_mistral': 2988, 'n_tokens_neox': 2928, 'n_words': 354}