File: keyinfodialog.cpp

package info (click to toggle)
kgpg 4:18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 12,168 kB
  • sloc: cpp: 18,911; xml: 159; makefile: 8; sh: 4
file content (408 lines) | stat: -rw-r--r-- 13,066 bytes parent folder | download | duplicates (2)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Copyright (C) 2002 Jean-Baptiste Mardelle <bj@altern.org>
 * Copyright (C) 2007 Jimmy Gilles <jimmygilles@gmail.com>
 * Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2016,2017 Rolf Eike Beer <kde@opensource.sf-tec.de>
 * Copyright (C) 2011 Philip Greggory Lee <rocketman768@gmail.com>
 */

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "keyinfodialog.h"

#include "kgpgchangekey.h"
#include <kgpgsettings.h>
#include "selectexpirydate.h"
#include "core/convert.h"
#include "core/images.h"
#include "core/kgpgkey.h"
#include "model/kgpgitemmodel.h"
#include "model/kgpgitemnode.h"
#include "transactions/kgpgchangepass.h"

#include <KConfigGroup>
#include <KLocalizedString>
#include <KMessageBox>

#include <QApplication>
#include <QCheckBox>
#include <QDesktopServices>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QImage>
#include <QLocale>
#include <QPixmap>
#include <QPushButton>
#include <QVBoxLayout>

using namespace KgpgCore;

KgpgTrustLabel::KgpgTrustLabel(QWidget *parent)
	: QWidget(parent),
	m_text_w(new QLabel(this)),
	m_color_w(new QLabel(this))
{
    m_text_w->setTextInteractionFlags(Qt::TextSelectableByMouse);

    m_color_w->setLineWidth(1);
    m_color_w->setFrameShape(QFrame::Box);
    m_color_w->setAutoFillBackground(true);
    m_color_w->setMinimumWidth(64);

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->setSpacing(10);
    layout->setMargin(2);
    layout->addWidget(m_text_w);
    layout->addWidget(m_color_w);

    change();
}

void KgpgTrustLabel::setText(const QString &text)
{
    m_text = text;
    change();
}

void KgpgTrustLabel::setColor(const QColor &color)
{
    m_color = color;
    change();
}

QString KgpgTrustLabel::text() const
{
    return m_text;
}

QColor KgpgTrustLabel::color() const
{
    return m_color;
}

void KgpgTrustLabel::change()
{
    m_text_w->setText(m_text);

    QPalette palette = m_color_w->palette();
    palette.setColor(m_color_w->backgroundRole(), m_color);
    m_color_w->setPalette(palette);
}

KgpgKeyInfo::KgpgKeyInfo(KGpgKeyNode *node, KGpgItemModel *model, QWidget *parent)
	: QDialog(parent),
	keychange(new KGpgChangeKey(node, this)),
	m_node(node),
	m_model(model),
	m_trust(new KgpgTrustLabel(this)),
	m_keywaschanged(false),
	m_closewhendone(false)
{
	Q_ASSERT(m_model != nullptr);
	Q_ASSERT(m_node != nullptr);

    setupUi(this);
    m_owtrust->addItem(Convert::toString(GPGME_VALIDITY_UNDEFINED));
    m_owtrust->addItem(Convert::toString(GPGME_VALIDITY_NEVER));
    m_owtrust->addItem(Convert::toString(GPGME_VALIDITY_MARGINAL));
    m_owtrust->addItem(Convert::toString(GPGME_VALIDITY_FULL));
    m_owtrust->addItem(Convert::toString(GPGME_VALIDITY_ULTIMATE));

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    QWidget *mainWidget = new QWidget(this);
    setLayout(mainLayout);
    mainLayout->addWidget(mainWidget);
    buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::Apply);
    QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
    okButton->setDefault(true);
    okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &KgpgKeyInfo::okButtonClicked);
    connect(buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &KgpgKeyInfo::applyButtonClicked);
    connect(buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &KgpgKeyInfo::cancelButtonClicked);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &KgpgKeyInfo::reject);
    okButton->setDefault(true);
    buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);

    m_email->setUnderline(false);
    int trustRow;
    formLayout_keyproperties->getWidgetPosition(tl_trust, &trustRow, nullptr);
    formLayout_keyproperties->setWidget(trustRow, QFormLayout::FieldRole, m_trust);

    // Hide some widgets if this is not a secret node.
    if ( ! m_node->isSecret() ) {
        m_expirationbtn->hide();
        m_password->hide();
    }

    mainLayout->addWidget(page);
    mainLayout->addWidget(buttonBox);

    connect(m_owtrust, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &KgpgKeyInfo::slotChangeTrust);
    connect(m_photoid, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated), this, &KgpgKeyInfo::slotLoadPhoto);
    connect(m_email, static_cast<void (KUrlLabel::*)(const QString &)>(&KUrlLabel::leftClickedUrl), this, &KgpgKeyInfo::slotOpenUrl);
    connect(keychange, &KGpgChangeKey::done, this, &KgpgKeyInfo::slotApplied);
    connect(m_disable, &QCheckBox::toggled, this, &KgpgKeyInfo::slotDisableKey);
    connect(m_expirationbtn, &QPushButton::clicked, this, &KgpgKeyInfo::slotChangeDate);
    connect(m_password, &QPushButton::clicked, this, &KgpgKeyInfo::slotChangePass);

    displayKey();
    adjustSize();
    gr_fingerprint->setMinimumHeight(gr_fingerprint->height());
}

KgpgKeyInfo::~KgpgKeyInfo()
{
	if (keychange) {
		// make sure anything that happens as result of the selfdestruct does not call
		// out into a method of this object again, as it is under destruction and that
		// can cause crashes (see bug 373910). Since ~QObject has not been run yet the
		// connections are not yet removed automatically.
		disconnect(keychange, &KGpgChangeKey::done, this, &KgpgKeyInfo::slotApplied);
		keychange->selfdestruct(false);
	}
}

void KgpgKeyInfo::reloadNode()
{
	const QString kid(m_node->getId());

	// this will delete m_node
	m_model->refreshKey(m_node);

	m_node = m_model->getRootNode()->findKey(kid);
	if (m_node != nullptr) {
		displayKey();
	} else {
		KMessageBox::error(this, i18n("<qt>The requested key is not present in the keyring anymore.<br />Perhaps it was deleted by another application</qt>"), i18n("Key not found"));
		m_keywaschanged = false;
		close();
	}
}

void KgpgKeyInfo::displayKey()
{
    const QString name = m_node->getName();
    setWindowTitle(name);
    m_name->setText(QLatin1String( "<qt><b>" ) + name + QLatin1String( "</b></qt>" ));

    const QString email = m_node->getEmail();
    if (email.isEmpty()) {
        m_email->setText(i18nc("no email address", "none"));
        m_email->setUrl(QString());
        m_email->setEnabled(false);
    } else {
        m_email->setText(QLatin1String( "<qt><b>&lt;" ) + email + QLatin1String( "&gt;</b></qt>" ));
        m_email->setUrl(QLatin1String( "mailto:" ) + name + QLatin1Char( '<' ) + email + QLatin1Char( '>' ));
    }

    const KgpgKey *key = m_node->getKey();
    m_caps->setText(Convert::toString(key->keytype()));

    QString trust;
    QColor trustcolor;

    if (key->valid()) {
        QModelIndex idx = m_model->nodeIndex(m_node, KEYCOLUMN_TRUST);
        trust = m_model->data(idx, Qt::AccessibleTextRole).toString();
        trustcolor = m_model->data(idx, Qt::BackgroundColorRole).value<QColor>();
    } else {
        trust = Convert::toString(TRUST_DISABLED);
        trustcolor = KGpgSettings::colorBad();
    }

    m_id->setText(m_node->getId().right(16));
    m_algorithm->setText(Convert::toString(key->algorithm()) + QLatin1String( " / " ) + Convert::toString(key->encryptionAlgorithm()));
    m_algorithm->setWhatsThis(i18n("<qt>The left part is the algorithm used by the <b>signature</b> key. The right part is the algorithm used by the <b>encryption</b> key.</qt>"));
    m_creation->setText(QLocale().toString(m_node->getCreation().date(), QLocale::ShortFormat));
    if (m_node->getExpiration().isNull())
        m_expiration->setText(i18nc("Unlimited key lifetime", "Unlimited"));
    else
        m_expiration->setText(QLocale().toString(m_node->getExpiration().date(), QLocale::ShortFormat));
    m_trust->setText(trust);
    m_trust->setColor(trustcolor);
    m_length->setText(m_node->getSize());
    m_length->setWhatsThis(i18n("<qt>The left part is the size of the <b>signature</b> key. The right part is the size of the <b>encryption</b> key.</qt>"));
    m_fingerprint->setText(m_node->getBeautifiedFingerprint());

    const QString comment = m_node->getComment();
    if (comment.isEmpty()) {
        m_comment->setText(i18nc("no key comment", "<em>none</em>"));
        m_comment->setTextFormat(Qt::RichText);
    } else {
        m_comment->setText(comment);
        m_comment->setTextFormat(Qt::PlainText);
    }

	switch (key->ownerTrust()) {
	case GPGME_VALIDITY_NEVER:
		m_owtrust->setCurrentIndex(1);
		break;
	case GPGME_VALIDITY_MARGINAL:
		m_owtrust->setCurrentIndex(2);
		break;
	case GPGME_VALIDITY_FULL:
		m_owtrust->setCurrentIndex(3);
		break;
	case GPGME_VALIDITY_ULTIMATE:
		m_owtrust->setCurrentIndex(4);
		break;
	case GPGME_VALIDITY_UNDEFINED:
	default:
		m_owtrust->setCurrentIndex(0);
		break;
	}

    if (!key->valid())
        m_disable->setChecked(true);

    connect(m_node, &KGpgKeyNode::expanded, this, &KgpgKeyInfo::slotKeyExpanded);
    m_node->expand();
    m_photoid->clear();
}

void KgpgKeyInfo::slotOpenUrl(const QString &url) const
{
    QDesktopServices::openUrl(QUrl(url));
}

void KgpgKeyInfo::slotLoadPhoto(const QString &uid)
{
	int i = uid.toInt();
	QPixmap pixmap = m_node->getUid(i)->toUatNode()->getPixmap();
	QImage img = pixmap.toImage();
	pixmap = QPixmap::fromImage(img.scaled(m_photo->width(), m_photo->height(), Qt::KeepAspectRatio));
	m_photo->setPixmap(pixmap);
}

void KgpgKeyInfo::slotChangeDate()
{
	QPointer<SelectExpiryDate> dialog = new SelectExpiryDate(this, m_node->getExpiration());
	if (dialog->exec() == QDialog::Accepted) {
		keychange->setExpiration(dialog->date());
		buttonBox->button(QDialogButtonBox::Apply)->setEnabled(keychange->wasChanged());
	}
	delete dialog;
}

void KgpgKeyInfo::slotDisableKey(const bool ison)
{
	keychange->setDisable(ison);
	buttonBox->button(QDialogButtonBox::Apply)->setEnabled(keychange->wasChanged());
}

void KgpgKeyInfo::slotChangePass()
{
	KGpgChangePass *cp = new KGpgChangePass(this, m_node->getId());

	connect(cp, &KGpgChangePass::done, this, &KgpgKeyInfo::slotInfoPasswordChanged);

	cp->start();
	QApplication::setOverrideCursor(QCursor(Qt::BusyCursor));
}

void KgpgKeyInfo::slotInfoPasswordChanged(int result)
{
	sender()->deleteLater();

	QApplication::restoreOverrideCursor();

	switch (result) {
	case KGpgTransaction::TS_OK:
		// when pinentry is used it is indistinguishable if everything went fine
		// or the user cancelled the password entries, so simply show nothing
		break;
	case KGpgTransaction::TS_BAD_PASSPHRASE:
		KMessageBox::error(this, i18n("Bad old passphrase, the passphrase for the key was not changed"), i18n("Could not change passphrase"));
		break;
	case KGpgTransaction::TS_USER_ABORTED:
		break;
	default:
		KMessageBox::error(this, i18n("KGpg was unable to change the passphrase."));
	}
}

void KgpgKeyInfo::slotChangeTrust(const int newtrust)
{
	keychange->setOwTrust(static_cast<gpgme_validity_t>(newtrust + 1));
	buttonBox->button(QDialogButtonBox::Apply)->setEnabled(keychange->wasChanged());
}

void KgpgKeyInfo::setControlEnable(const bool b)
{
    m_owtrust->setEnabled(b);
    m_disable->setEnabled(b);
    buttonBox->button(QDialogButtonBox::Apply)->setEnabled(b && keychange->wasChanged());

    if (m_expirationbtn)
        m_expirationbtn->setEnabled(b);
    if (m_password)
        m_password->setEnabled(b);

    if (b)
        QApplication::restoreOverrideCursor();
    else
        QApplication::setOverrideCursor(QCursor(Qt::BusyCursor));
}

void KgpgKeyInfo::okButtonClicked()
{
	m_closewhendone = true;
	applyButtonClicked();
}

void KgpgKeyInfo::applyButtonClicked()
{
	setControlEnable(false);
	keychange->apply();
}

void KgpgKeyInfo::cancelButtonClicked()
{
	if (m_keywaschanged && m_node)
		emit keyNeedsRefresh(m_node);
	reject();
}

void KgpgKeyInfo::slotApplied(int result)
{
	if (result) {
		KMessageBox::error(this, i18n("Changing key properties failed."), i18n("Key properties"));
	} else {
		m_keywaschanged = true;
		if (m_node)
			emit keyNeedsRefresh(m_node);
		reloadNode();
	}
	setControlEnable(true);

	if (m_closewhendone)
		accept();
}

void KgpgKeyInfo::slotKeyExpanded()
{
	// the counting starts at 1 and that is the primary uid which can't be a photo id
	int i = 2;
	const KGpgSignableNode *uat;

	while ((uat = m_node->getUid(i++)) != nullptr) {
		if (uat->getType() != KgpgCore::ITYPE_UAT)
			continue;

		m_photoid->addItem(uat->getId());
	}

	bool hasphoto = (m_photoid->count() > 0);

	m_photoid->setVisible(hasphoto);
	m_photoid->setEnabled(hasphoto);
	if (hasphoto)
		slotLoadPhoto(m_photoid->currentText());
}