PreferencesDialog::PreferencesDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::PreferencesDialog)
{
    m_ui->setupUi(this);

    // Key Settings
    m_ui->ngIDLineEdit->setValidator(new HexValidator(this));
    m_ui->ngKeyIDLineEdit->setValidator(new HexValidator(this));
    m_ui->ngSigPt1LineEdit->setValidator(new HexValidator(this));
    m_ui->ngSigPt2LineEdit->setValidator(new HexValidator(this));
    m_ui->ngPrivLineEdit->setValidator(new HexValidator(this));
    m_ui->macAddrLineEdit->setValidator(new HexValidator(this));
    connect(m_ui->ngSigPt1LineEdit, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
    connect(m_ui->ngSigPt2LineEdit, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
    connect(m_ui->loadKeysBtn, SIGNAL(clicked()), this, SLOT(onLoadKeys()));

    // Region Settings
    SettingsManager* settings = SettingsManager::instance();

    switch(settings->defaultRegion())
    {
        case SettingsManager::NTSCU: m_ui->ntscURB->setChecked(true); break;
        case SettingsManager::NTSCJ: m_ui->ntscJRB->setChecked(true); break;
        case SettingsManager::PAL:   m_ui->palRB  ->setChecked(true); break;
    }

    m_ui->ntscUNameLE->setText(settings->defaultPlayerNameForRegion(SettingsManager::NTSCU));
    m_ui->ntscJNameLE->setText(settings->defaultPlayerNameForRegion(SettingsManager::NTSCJ));
    m_ui->palNameLE  ->setText(settings->defaultPlayerNameForRegion(SettingsManager::PAL  ));
}
void SettingsDialog::loadSettings()
{
    // Region Settings
    SettingsManager* settings = SettingsManager::instance();
    ui->settingsTab->setCurrentIndex(0);
    switch(settings->defaultRegion())
    {
    case Region::NTSCU: ui->ntscURB->setChecked(true); break;
    case Region::NTSCK: ui->ntscKRB->setChecked(true); break;
    case Region::NTSCJ: ui->ntscJRB->setChecked(true); break;
    case Region::PAL:   ui->palRB  ->setChecked(true); break;
    default: break;
    }

    ui->ntscUNameLE      ->setText(settings->defaultPlayerNameForRegion(Region::NTSCU));
    ui->ntscUNameLE      ->setModified(false);
    ui->ntscKNameLE      ->setText(settings->defaultPlayerNameForRegion(Region::NTSCK));
    ui->ntscKNameLE      ->setModified(false);
    ui->ntscJNameLE      ->setText(settings->defaultPlayerNameForRegion(Region::NTSCJ));
    ui->ntscJNameLE      ->setModified(false);
    ui->palNameLE        ->setText(settings->defaultPlayerNameForRegion(Region::PAL  ));
    ui->palNameLE        ->setModified(false);
    ui->updateUrlLineEdit->setText(settings->updateUrl());
    ui->updateUrlLineEdit->setModified(false);
    ui->checkOnStart     ->setChecked(settings->updateCheckOnStart());
}