aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/key_generate_dialog.cpp
blob: 1e9790e966e558371a2c128d9f18e1bc2da6c1e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "key_generate_dialog.h"

#include <QFile>
#include <QPushButton>
#include <QUiLoader>
#include <QVBoxLayout>

#include "options.h"

KeyGenerateDialog::KeyGenerateDialog(QWidget *parent)
    : QDialog(parent)
{
  QUiLoader l_loader(this);
  QFile l_uiFile(Options::getInstance().getUIAsset("key_generate_dialog.ui"));
  if (!l_uiFile.open(QFile::ReadOnly))
  {
    qCritical() << "Unable to open file " << l_uiFile.fileName();
    return;
  }
  ui_widget = l_loader.load(&l_uiFile, this);
  auto l_layout = new QVBoxLayout(this);
  l_layout->addWidget(ui_widget);
  setWindowTitle("Generate key");

  FROM_UI(QLineEdit, key_name);
  FROM_UI(QLineEdit, key_password);
  ui_key_password->setEchoMode(QLineEdit::Password);
  connect(ui_key_password, &QLineEdit::textChanged, this, &KeyGenerateDialog::validate);
  FROM_UI(QLineEdit, key_password_confirm);
  ui_key_password_confirm->setEchoMode(QLineEdit::Password);
  connect(ui_key_password_confirm, &QLineEdit::textChanged, this, &KeyGenerateDialog::validate);
  FROM_UI(QDialogButtonBox, key_gen_buttons);
  connect(ui_key_gen_buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
  connect(ui_key_gen_buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);

  validate();
}

// Empty passwords should be also checked.
void KeyGenerateDialog::validate()
{
  ui_key_gen_buttons->button(QDialogButtonBox::Ok)->setEnabled(!ui_key_password->text().isEmpty() && (ui_key_password->text() == ui_key_password_confirm->text()));
}

QStringView KeyGenerateDialog::key_name()
{
  return ui_key_name->text();
}

QByteArray KeyGenerateDialog::key_password()
{
  return ui_key_password->text().toUtf8();
}