1 #include "certificateselectiondialog.hpp"
2 #include "ui_certificateselectiondialog.h"
3
4 #include "certificatehelper.hpp"
5 #include "kristall.hpp"
6 #include "newidentitiydialog.hpp"
7
8 #include <random>
9 #include <QDebug>
10 #include <QItemSelectionModel>
11
CertificateSelectionDialog(QWidget * parent)12 CertificateSelectionDialog::CertificateSelectionDialog(QWidget *parent) :
13 QDialog(parent),
14 ui(new Ui::CertificateSelectionDialog)
15 {
16 ui->setupUi(this);
17
18 connect( // connect with "this" as context, so the connection will die when the window is destroyed
19 kristall::globals().localization.get(), &Localization::translationChanged,
20 this, [this]() { this->ui->retranslateUi(this); },
21 Qt::DirectConnection
22 );
23
24 this->ui->server_request->setVisible(false);
25
26 this->ui->certificates->setModel(&kristall::globals().identities);
27 this->ui->certificates->expandAll();
28
29 connect(this->ui->certificates->selectionModel(), &QItemSelectionModel::currentChanged, this, &CertificateSelectionDialog::on_currentChanged);
30 }
31
~CertificateSelectionDialog()32 CertificateSelectionDialog::~CertificateSelectionDialog()
33 {
34 delete ui;
35 }
36
setServerQuery(const QString & query)37 void CertificateSelectionDialog::setServerQuery(const QString &query)
38 {
39 this->ui->server_request->setText(query);
40 this->ui->server_request->setVisible(not query.isEmpty());
41 }
42
identity() const43 CryptoIdentity CertificateSelectionDialog::identity() const
44 {
45 return cryto_identity;
46 }
47
on_use_temp_cert_30m_clicked()48 void CertificateSelectionDialog::on_use_temp_cert_30m_clicked()
49 {
50 acceptTemporaryWithTimeout(QDateTime::currentDateTime().addSecs(1800 * 12));
51 }
52
on_use_temp_cert_1h_clicked()53 void CertificateSelectionDialog::on_use_temp_cert_1h_clicked()
54 {
55 acceptTemporaryWithTimeout(QDateTime::currentDateTime().addSecs(3600));
56 }
57
on_use_temp_cert_12h_clicked()58 void CertificateSelectionDialog::on_use_temp_cert_12h_clicked()
59 {
60 acceptTemporaryWithTimeout(QDateTime::currentDateTime().addSecs(3600 * 12));
61 }
62
on_use_temp_cert_24h_clicked()63 void CertificateSelectionDialog::on_use_temp_cert_24h_clicked()
64 {
65 acceptTemporaryWithTimeout(QDateTime::currentDateTime().addDays(1));
66 }
67
on_use_temp_cert_48h_clicked()68 void CertificateSelectionDialog::on_use_temp_cert_48h_clicked()
69 {
70 acceptTemporaryWithTimeout(QDateTime::currentDateTime().addDays(2));
71 }
72
acceptTemporaryWithTimeout(const QDateTime & timeout)73 void CertificateSelectionDialog::acceptTemporaryWithTimeout(const QDateTime &timeout)
74 {
75 std::default_random_engine rng;
76 rng.seed(QDateTime::currentDateTime().toMSecsSinceEpoch());
77
78 std::uniform_int_distribution<int> distr(0, 255);
79
80 char items[8];
81 for(auto & c : items) {
82 c = distr(rng);
83 }
84
85 this->cryto_identity = CertificateHelper::createNewIdentity(
86 QByteArray(items, sizeof items).toBase64(QByteArray::OmitTrailingEquals),
87 timeout);
88
89 this->accept();
90 }
91
on_currentChanged(const QModelIndex & current,const QModelIndex & previous)92 void CertificateSelectionDialog::on_currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
93 {
94 Q_UNUSED(current)
95 Q_UNUSED(previous)
96 auto id = kristall::globals().identities.getIdentity(current);
97
98 this->ui->use_selected_cert->setEnabled(id.isValid());
99 }
100
on_create_new_cert_clicked()101 void CertificateSelectionDialog::on_create_new_cert_clicked()
102 {
103 NewIdentitiyDialog dialog { this };
104
105 if(dialog.exec() != QDialog::Accepted)
106 return;
107
108 auto id = dialog.createIdentity();
109 if(not id.isValid())
110 return;
111 id.is_persistent = true;
112
113 kristall::globals().identities.addCertificate(
114 dialog.groupName(),
115 id);
116 }
117
on_use_selected_cert_clicked()118 void CertificateSelectionDialog::on_use_selected_cert_clicked()
119 {
120 auto sel = this->ui->certificates->selectionModel()->currentIndex();
121 this->cryto_identity = kristall::globals().identities.getIdentity(sel);
122 if(this->cryto_identity.isValid()) {
123 this->accept();
124 } else {
125 qDebug() << "Tried to use an invalid identity when the button should not be enabled. This is a bug!";
126 }
127 }
128
on_certificates_doubleClicked(const QModelIndex & index)129 void CertificateSelectionDialog::on_certificates_doubleClicked(const QModelIndex &index)
130 {
131 this->cryto_identity = kristall::globals().identities.getIdentity(index);
132 if(this->cryto_identity.isValid()) {
133 this->accept();
134 } else {
135 qDebug() << "Tried to use an invalid identity when the button should not be enabled. This is a bug!";
136 }
137 }
138