summaryrefslogtreecommitdiffstats
path: root/keygendialog.cpp
blob: 6789e091ed7a9f7b49b446493d7f54f0695b9967 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "keygendialog.h"
#include "ui_keygendialog.h"
#include "progressindicator.h"
#include <QDebug>
#include <QMessageBox>

KeygenDialog::KeygenDialog(Dialog *parent) :
    QDialog(parent),
    ui(new Ui::KeygenDialog)
{
    ui->setupUi(this);
    dialog = parent;
}

KeygenDialog::~KeygenDialog()
{
    delete ui;
}

void KeygenDialog::on_passphrase1_textChanged(const QString &arg1)
{
    if (ui->passphrase1->text() == ui->passphrase2->text()) {
        ui->buttonBox->setEnabled(true);
        replace("Passphrase", arg1);
        if (arg1 == "") {
            no_protection(true);
        } else {
            no_protection(false);
        }
    } else {
        ui->buttonBox->setEnabled(false);
    }
}

void KeygenDialog::on_passphrase2_textChanged(const QString &arg1)
{
    on_passphrase1_textChanged(arg1);
}

void KeygenDialog::on_checkBox_stateChanged(int arg1)
{
    if (arg1) {
        ui->plainTextEdit->setReadOnly(false);
        ui->plainTextEdit->setEnabled(true);
    } else {
        ui->plainTextEdit->setReadOnly(true);
        ui->plainTextEdit->setEnabled(false);
    }
}

void KeygenDialog::on_email_textChanged(const QString &arg1)
{
    replace("Name-Email", arg1);
}

void KeygenDialog::on_name_textChanged(const QString &arg1)
{
    replace("Name-Real", arg1);
}

/**
 * @brief KeygenDialog::replace
 * @param key
 * @param value
 */
void KeygenDialog::replace(QString key, QString value)
{
    QStringList clear;
    QString expert  = ui->plainTextEdit->toPlainText();
    QStringList lines = expert.split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
    foreach (QString line, lines) {
        line.replace(QRegExp(key+":.*"), key + ": " + value);
        if (key == "Passphrase") {
            line.replace("%no-protection", "Passphrase: " + value);
        }
        clear.append(line);
    }
    ui->plainTextEdit->setPlainText(clear.join("\n"));
}

/**
 * @brief KeygenDialog::no_protection
 * @param enable
 */\
void KeygenDialog::no_protection(bool enable)
{
    QStringList clear;
    QString expert  = ui->plainTextEdit->toPlainText();
    QStringList lines = expert.split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
    foreach (QString line, lines) {
        bool remove = false;
        if (!enable) {
            if (line.indexOf("%no-protection") == 0) {
                remove = true;
            }
        } else {
            if (line.indexOf("Passphrase") == 0) {
                line = "%no-protection";
            }
        }
        if (!remove) {
            clear.append(line);
        }
    }
    ui->plainTextEdit->setPlainText(clear.join("\n"));
}

/**
 * @brief KeygenDialog::done
 * @param r
 */
void KeygenDialog::done(int r)
{
    if(QDialog::Accepted == r)  // ok was pressed
    {
        ui->widget->setEnabled(false);
        ui->buttonBox->setEnabled(false);
        ui->checkBox->setEnabled(false);
        ui->plainTextEdit->setEnabled(false);

        QProgressIndicator* pi = new QProgressIndicator();
        pi->startAnimation();
        pi->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

        ui->frame->hide();
        ui->label->setText(QString("This operation can take some minutes.<br />") +
                           "We need to generate a lot of random bytes. It is a good idea to perform "
                           "some other action (type on the keyboard, move the mouse, utilize the "
                           "disks) during the prime generation; this gives the random number "
                           "generator a better chance to gain enough entropy.");

        this->layout()->addWidget(pi);

        this->show();
        dialog->genKey(ui->plainTextEdit->toPlainText(), this);
    }
    else    // cancel, close or exc was pressed
    {
        QDialog::done(r);
        return;
    }
}

void KeygenDialog::closeEvent(QCloseEvent *event) {
    // TODO save window size or somethign
    event->accept();
}