<commit_before>#include "setupdialog.h"
#include "debug.h"
#include "utils.h"

#include <QDateTime>
#include <QDir>
#include <QFileDialog>
#include <QFileInfo>
#include <QHostInfo>
#include <QSettings>
#include <QStandardPaths>

SetupDialog::SetupDialog(QWidget *parent)
    : QDialog(parent),
      _loadingAnimation(":/icons/loading.gif"),
      _haveKey(false)
{
    _ui.setupUi(this);
    setWindowFlags((windowFlags() | Qt::CustomizeWindowHint) &
                   ~Qt::WindowMaximizeButtonHint);

    // These should be done before connecting objects to
    // validateAdvancedSetupPage() to avoid calling that function
    // unnecessarily.
    _tarsnapDir = Utils::findTarsnapClientInPath("", true);
    _ui.tarsnapPathLineEdit->setText(_tarsnapDir);
    _ui.machineNameLineEdit->setText(QHostInfo::localHostName());
    _ui.wizardStackedWidget->setCurrentWidget(_ui.welcomePage);

    _appDataDir = QStandardPaths::writableLocation(APPDATA);
    QDir keysDir(_appDataDir);
    if(!keysDir.exists())
        keysDir.mkpath(_appDataDir);
    _ui.appDataPathLineEdit->setText(_appDataDir);

    _tarsnapCacheDir =
        QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
    QDir cacheDir(_tarsnapCacheDir);
    if(!cacheDir.exists())
        cacheDir.mkpath(_tarsnapCacheDir);
    _ui.tarsnapCacheLineEdit->setText(_tarsnapCacheDir);

    _ui.loadingIconLabel->setMovie(&_loadingAnimation);
    _ui.errorLabel->hide();
    _ui.machineKeyLabel->hide();
    _ui.machineKeyCombo->hide();
    _ui.browseKeyButton->hide();
    _ui.advancedCLIWidget->hide();

    connect(_ui.welcomePageRadioButton, &QRadioButton::clicked, this,
            &SetupDialog::skipToPage);
    connect(_ui.restorePageRadioButton, &QRadioButton::clicked, this,
            &SetupDialog::skipToPage);
    connect(_ui.advancedPageRadioButton, &QRadioButton::clicked, this,
            &SetupDialog::skipToPage);
    connect(_ui.registerPageRadioButton, &QRadioButton::clicked, this,
            &SetupDialog::skipToPage);
    connect(_ui.donePageRadioButton, &QRadioButton::clicked, this,
            &SetupDialog::skipToPage);

    connect(_ui.wizardStackedWidget, &QStackedWidget::currentChanged, this,
            &SetupDialog::wizardPageChanged);

    // Welcome page
    connect(_ui.welcomePageSkipButton, &QPushButton::clicked,
            [&]() { commitSettings(true); });
    connect(_ui.welcomePageProceedButton, &QPushButton::clicked, this,
            &SetupDialog::setNextPage);

    // Advanced setup page
    connect(_ui.advancedCLIButton, &QPushButton::toggled,
            _ui.advancedCLIWidget, &QWidget::setVisible);
    connect(_ui.tarsnapPathBrowseButton, &QPushButton::clicked, this,
            &SetupDialog::showTarsnapPathBrowse);
    connect(_ui.tarsnapPathLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateAdvancedSetupPage);
    connect(_ui.tarsnapCacheBrowseButton, &QPushButton::clicked, this,
            &SetupDialog::showTarsnapCacheBrowse);
    connect(_ui.tarsnapCacheLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateAdvancedSetupPage);
    connect(_ui.appDataBrowseButton, &QPushButton::clicked, this,
            &SetupDialog::showAppDataBrowse);
    connect(_ui.appDataPathLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateAdvancedSetupPage);
    connect(_ui.advancedPageProceedButton, &QPushButton::clicked, this,
            &SetupDialog::setNextPage);

    // Restore page
    connect(_ui.restoreNoButton, &QPushButton::clicked, this,
            &SetupDialog::restoreNo);
    connect(_ui.restoreYesButton, &QPushButton::clicked, this,
            &SetupDialog::restoreYes);

    // Register page
    connect(_ui.tarsnapUserLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateRegisterPage);
    connect(_ui.tarsnapPasswordLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateRegisterPage);
    connect(_ui.machineNameLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateRegisterPage);
    connect(_ui.machineKeyCombo, &QComboBox::currentTextChanged, this,
            &SetupDialog::validateRegisterPage);
    connect(_ui.browseKeyButton, &QPushButton::clicked, this,
            &SetupDialog::registerHaveKeyBrowse);
    connect(_ui.registerMachineButton, &QPushButton::clicked, this,
            &SetupDialog::registerMachine);

    // Done page
    connect(_ui.doneButton, &QPushButton::clicked, this,
            &SetupDialog::commitSettings);
}

SetupDialog::~SetupDialog()
{
}

void SetupDialog::wizardPageChanged(int)
{
    if(_ui.wizardStackedWidget->currentWidget() == _ui.welcomePage)
    {
        _ui.welcomePageRadioButton->setChecked(true);
        _ui.titleLabel->setText(tr("Setup wizard"));
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.advancedPage)
    {
        _ui.advancedPageRadioButton->setChecked(true);
        _ui.titleLabel->setText(tr("Command-line utilities"));
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.restorePage)
    {
        _ui.restorePageRadioButton->setChecked(true);
        _ui.titleLabel->setText(tr("Machine key"));
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.registerPage)
    {
        _ui.registerPageRadioButton->setChecked(true);
        _ui.titleLabel->setText(tr("Register with server"));
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.donePage)
    {
        _ui.donePageRadioButton->setChecked(true);
        _ui.titleLabel->setText(tr("Setup complete!"));
    }
}

void SetupDialog::skipToPage()
{
    if(sender() == _ui.welcomePageRadioButton)
        _ui.wizardStackedWidget->setCurrentWidget(_ui.welcomePage);
    else if(sender() == _ui.restorePageRadioButton)
        _ui.wizardStackedWidget->setCurrentWidget(_ui.restorePage);
    else if(sender() == _ui.advancedPageRadioButton)
        _ui.wizardStackedWidget->setCurrentWidget(_ui.advancedPage);
    else if(sender() == _ui.registerPageRadioButton)
        _ui.wizardStackedWidget->setCurrentWidget(_ui.registerPage);
    else if(sender() == _ui.donePageRadioButton)
        _ui.wizardStackedWidget->setCurrentWidget(_ui.donePage);
}

void SetupDialog::setNextPage()
{
    if(_ui.wizardStackedWidget->currentWidget() == _ui.welcomePage)
    {
        _ui.wizardStackedWidget->setCurrentWidget(_ui.advancedPage);
        _ui.advancedPageRadioButton->setEnabled(true);
        bool advancedOk = validateAdvancedSetupPage();
        _ui.advancedCLIButton->setChecked(!advancedOk);
        if (advancedOk)
            _ui.advancedPageProceedButton->setFocus();
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.advancedPage)
    {
        _ui.wizardStackedWidget->setCurrentWidget(_ui.restorePage);
        _ui.restorePageRadioButton->setEnabled(true);
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.restorePage)
    {
        _ui.wizardStackedWidget->setCurrentWidget(_ui.registerPage);
        _ui.registerPageRadioButton->setEnabled(true);
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.registerPage)
    {
        _ui.wizardStackedWidget->setCurrentWidget(_ui.donePage);
        _ui.donePageRadioButton->setEnabled(true);
    }
}

void SetupDialog::showTarsnapPathBrowse()
{
    QString tarsnapPath =
        QFileDialog::getExistingDirectory(this, tr("Find Tarsnap client"), "");
    _ui.tarsnapPathLineEdit->setText(tarsnapPath);
}

void SetupDialog::showTarsnapCacheBrowse()
{
    QString tarsnapCacheDir =
        QFileDialog::getExistingDirectory(this, tr("Tarsnap cache location"),
                                          _tarsnapCacheDir);
    _ui.tarsnapCacheLineEdit->setText(tarsnapCacheDir);
}

void SetupDialog::showAppDataBrowse()
{
    QString appDataDir =
        QFileDialog::getExistingDirectory(this, tr("App data location"), "");
    _ui.appDataPathLineEdit->setText(appDataDir);
}

bool SetupDialog::validateAdvancedSetupPage()
{
    bool result = false;

    setTarsnapVersion("");

    _tarsnapDir =
        Utils::findTarsnapClientInPath(_ui.tarsnapPathLineEdit->text(), true);
    _tarsnapCacheDir =
        Utils::validateTarsnapCache(_ui.tarsnapCacheLineEdit->text());
    QFileInfo appDataDir(_ui.appDataPathLineEdit->text());
    if(appDataDir.exists() && appDataDir.isDir() && appDataDir.isWritable())
        _appDataDir = _ui.appDataPathLineEdit->text();
    else
        _appDataDir.clear();

    if(_tarsnapDir.isEmpty() || _tarsnapCacheDir.isEmpty() ||
       _appDataDir.isEmpty())
        result = false;
    else
        result = true;

    if(result)
        emit getTarsnapVersion(_tarsnapDir);

    _ui.advancedPageProceedButton->setEnabled(result);

    return result;
}

void SetupDialog::restoreNo()
{
    _haveKey = false;
    _ui.machineKeyLabel->hide();
    _ui.machineKeyCombo->hide();
    _ui.browseKeyButton->hide();
    _ui.tarsnapUserLabel->show();
    _ui.tarsnapUserLineEdit->show();
    _ui.tarsnapPasswordLabel->show();
    _ui.tarsnapPasswordLineEdit->show();
    _ui.registerPageInfoLabel->setText(
        tr("Please use your Tarsnap account "
           "credentials to register this machine with the service "
           "and create a local key for this machine."));
    _ui.registerPageInfoLabelAside->setText(
        tr("Don't have an account? Register on "
        "<a href=\"http://tarsnap.com\">tarsnap.com</a>, "
        "then come back."));
    _ui.errorLabel->clear();
    setNextPage();
}<commit_msg>setup: only clear version string if validation failed<commit_after>#include "setupdialog.h"
#include "debug.h"
#include "utils.h"

#include <QDateTime>
#include <QDir>
#include <QFileDialog>
#include <QFileInfo>
#include <QHostInfo>
#include <QSettings>
#include <QStandardPaths>

SetupDialog::SetupDialog(QWidget *parent)
    : QDialog(parent),
      _loadingAnimation(":/icons/loading.gif"),
      _haveKey(false)
{
    _ui.setupUi(this);
    setWindowFlags((windowFlags() | Qt::CustomizeWindowHint) &
                   ~Qt::WindowMaximizeButtonHint);

    // These should be done before connecting objects to
    // validateAdvancedSetupPage() to avoid calling that function
    // unnecessarily.
    _tarsnapDir = Utils::findTarsnapClientInPath("", true);
    _ui.tarsnapPathLineEdit->setText(_tarsnapDir);
    _ui.machineNameLineEdit->setText(QHostInfo::localHostName());
    _ui.wizardStackedWidget->setCurrentWidget(_ui.welcomePage);

    _appDataDir = QStandardPaths::writableLocation(APPDATA);
    QDir keysDir(_appDataDir);
    if(!keysDir.exists())
        keysDir.mkpath(_appDataDir);
    _ui.appDataPathLineEdit->setText(_appDataDir);

    _tarsnapCacheDir =
        QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
    QDir cacheDir(_tarsnapCacheDir);
    if(!cacheDir.exists())
        cacheDir.mkpath(_tarsnapCacheDir);
    _ui.tarsnapCacheLineEdit->setText(_tarsnapCacheDir);

    _ui.loadingIconLabel->setMovie(&_loadingAnimation);
    _ui.errorLabel->hide();
    _ui.machineKeyLabel->hide();
    _ui.machineKeyCombo->hide();
    _ui.browseKeyButton->hide();
    _ui.advancedCLIWidget->hide();

    connect(_ui.welcomePageRadioButton, &QRadioButton::clicked, this,
            &SetupDialog::skipToPage);
    connect(_ui.restorePageRadioButton, &QRadioButton::clicked, this,
            &SetupDialog::skipToPage);
    connect(_ui.advancedPageRadioButton, &QRadioButton::clicked, this,
            &SetupDialog::skipToPage);
    connect(_ui.registerPageRadioButton, &QRadioButton::clicked, this,
            &SetupDialog::skipToPage);
    connect(_ui.donePageRadioButton, &QRadioButton::clicked, this,
            &SetupDialog::skipToPage);

    connect(_ui.wizardStackedWidget, &QStackedWidget::currentChanged, this,
            &SetupDialog::wizardPageChanged);

    // Welcome page
    connect(_ui.welcomePageSkipButton, &QPushButton::clicked,
            [&]() { commitSettings(true); });
    connect(_ui.welcomePageProceedButton, &QPushButton::clicked, this,
            &SetupDialog::setNextPage);

    // Advanced setup page
    connect(_ui.advancedCLIButton, &QPushButton::toggled,
            _ui.advancedCLIWidget, &QWidget::setVisible);
    connect(_ui.tarsnapPathBrowseButton, &QPushButton::clicked, this,
            &SetupDialog::showTarsnapPathBrowse);
    connect(_ui.tarsnapPathLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateAdvancedSetupPage);
    connect(_ui.tarsnapCacheBrowseButton, &QPushButton::clicked, this,
            &SetupDialog::showTarsnapCacheBrowse);
    connect(_ui.tarsnapCacheLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateAdvancedSetupPage);
    connect(_ui.appDataBrowseButton, &QPushButton::clicked, this,
            &SetupDialog::showAppDataBrowse);
    connect(_ui.appDataPathLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateAdvancedSetupPage);
    connect(_ui.advancedPageProceedButton, &QPushButton::clicked, this,
            &SetupDialog::setNextPage);

    // Restore page
    connect(_ui.restoreNoButton, &QPushButton::clicked, this,
            &SetupDialog::restoreNo);
    connect(_ui.restoreYesButton, &QPushButton::clicked, this,
            &SetupDialog::restoreYes);

    // Register page
    connect(_ui.tarsnapUserLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateRegisterPage);
    connect(_ui.tarsnapPasswordLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateRegisterPage);
    connect(_ui.machineNameLineEdit, &QLineEdit::textChanged, this,
            &SetupDialog::validateRegisterPage);
    connect(_ui.machineKeyCombo, &QComboBox::currentTextChanged, this,
            &SetupDialog::validateRegisterPage);
    connect(_ui.browseKeyButton, &QPushButton::clicked, this,
            &SetupDialog::registerHaveKeyBrowse);
    connect(_ui.registerMachineButton, &QPushButton::clicked, this,
            &SetupDialog::registerMachine);

    // Done page
    connect(_ui.doneButton, &QPushButton::clicked, this,
            &SetupDialog::commitSettings);
}

SetupDialog::~SetupDialog()
{
}

void SetupDialog::wizardPageChanged(int)
{
    if(_ui.wizardStackedWidget->currentWidget() == _ui.welcomePage)
    {
        _ui.welcomePageRadioButton->setChecked(true);
        _ui.titleLabel->setText(tr("Setup wizard"));
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.advancedPage)
    {
        _ui.advancedPageRadioButton->setChecked(true);
        _ui.titleLabel->setText(tr("Command-line utilities"));
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.restorePage)
    {
        _ui.restorePageRadioButton->setChecked(true);
        _ui.titleLabel->setText(tr("Machine key"));
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.registerPage)
    {
        _ui.registerPageRadioButton->setChecked(true);
        _ui.titleLabel->setText(tr("Register with server"));
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.donePage)
    {
        _ui.donePageRadioButton->setChecked(true);
        _ui.titleLabel->setText(tr("Setup complete!"));
    }
}

void SetupDialog::skipToPage()
{
    if(sender() == _ui.welcomePageRadioButton)
        _ui.wizardStackedWidget->setCurrentWidget(_ui.welcomePage);
    else if(sender() == _ui.restorePageRadioButton)
        _ui.wizardStackedWidget->setCurrentWidget(_ui.restorePage);
    else if(sender() == _ui.advancedPageRadioButton)
        _ui.wizardStackedWidget->setCurrentWidget(_ui.advancedPage);
    else if(sender() == _ui.registerPageRadioButton)
        _ui.wizardStackedWidget->setCurrentWidget(_ui.registerPage);
    else if(sender() == _ui.donePageRadioButton)
        _ui.wizardStackedWidget->setCurrentWidget(_ui.donePage);
}

void SetupDialog::setNextPage()
{
    if(_ui.wizardStackedWidget->currentWidget() == _ui.welcomePage)
    {
        _ui.wizardStackedWidget->setCurrentWidget(_ui.advancedPage);
        _ui.advancedPageRadioButton->setEnabled(true);
        bool advancedOk = validateAdvancedSetupPage();
        _ui.advancedCLIButton->setChecked(!advancedOk);
        if (advancedOk)
            _ui.advancedPageProceedButton->setFocus();
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.advancedPage)
    {
        _ui.wizardStackedWidget->setCurrentWidget(_ui.restorePage);
        _ui.restorePageRadioButton->setEnabled(true);
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.restorePage)
    {
        _ui.wizardStackedWidget->setCurrentWidget(_ui.registerPage);
        _ui.registerPageRadioButton->setEnabled(true);
    }
    else if(_ui.wizardStackedWidget->currentWidget() == _ui.registerPage)
    {
        _ui.wizardStackedWidget->setCurrentWidget(_ui.donePage);
        _ui.donePageRadioButton->setEnabled(true);
    }
}

void SetupDialog::showTarsnapPathBrowse()
{
    QString tarsnapPath =
        QFileDialog::getExistingDirectory(this, tr("Find Tarsnap client"), "");
    _ui.tarsnapPathLineEdit->setText(tarsnapPath);
}

void SetupDialog::showTarsnapCacheBrowse()
{
    QString tarsnapCacheDir =
        QFileDialog::getExistingDirectory(this, tr("Tarsnap cache location"),
                                          _tarsnapCacheDir);
    _ui.tarsnapCacheLineEdit->setText(tarsnapCacheDir);
}

void SetupDialog::showAppDataBrowse()
{
    QString appDataDir =
        QFileDialog::getExistingDirectory(this, tr("App data location"), "");
    _ui.appDataPathLineEdit->setText(appDataDir);
}

bool SetupDialog::validateAdvancedSetupPage()
{
    bool result = false;


    _tarsnapDir =
        Utils::findTarsnapClientInPath(_ui.tarsnapPathLineEdit->text(), true);
    _tarsnapCacheDir =
        Utils::validateTarsnapCache(_ui.tarsnapCacheLineEdit->text());
    QFileInfo appDataDir(_ui.appDataPathLineEdit->text());
    if(appDataDir.exists() && appDataDir.isDir() && appDataDir.isWritable())
        _appDataDir = _ui.appDataPathLineEdit->text();
    else
        _appDataDir.clear();

    if(_tarsnapDir.isEmpty() || _tarsnapCacheDir.isEmpty() ||
       _appDataDir.isEmpty())
        result = false;
    else
        result = true;

    if(result)
        emit getTarsnapVersion(_tarsnapDir);
    else
        setTarsnapVersion("");

    _ui.advancedPageProceedButton->setEnabled(result);

    return result;
}

void SetupDialog::restoreNo()
{
    _haveKey = false;
    _ui.machineKeyLabel->hide();
    _ui.machineKeyCombo->hide();
    _ui.browseKeyButton->hide();
    _ui.tarsnapUserLabel->show();
    _ui.tarsnapUserLineEdit->show();
    _ui.tarsnapPasswordLabel->show();
    _ui.tarsnapPasswordLineEdit->show();
    _ui.registerPageInfoLabel->setText(
        tr("Please use your Tarsnap account "
           "credentials to register this machine with the service "
           "and create a local key for this machine."));
    _ui.registerPageInfoLabelAside->setText(
        tr("Don't have an account? Register on "
        "<a href=\"http://tarsnap.com\">tarsnap.com</a>, "
        "then come back."));
    _ui.errorLabel->clear();
    setNextPage();
}
{'fraction_non_alphanumeric': 0.12477035326229595, 'fraction_numerical': 0.0, 'mean_word_length': 3.911575148234081, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 147, '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': '896536', 'n_tokens_mistral': 5986, 'n_tokens_neox': 5810, 'n_words': 750}