Libkleo

keyapprovaldialog.cpp
1/* -*- c++ -*-
2 keyapprovaldialog.h
3
4 This file is part of libkleopatra, the KDE keymanagement library
5 SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB
6
7 Based on kpgpui.h
8 SPDX-FileCopyrightText: 2001, 2002 the KPGP authors
9 See file libkdenetwork/AUTHORS.kpgp for details
10
11 SPDX-License-Identifier: GPL-2.0-or-later
12*/
13
14#include <config-libkleo.h>
15
16#include "keyapprovaldialog.h"
17
18#include "keyrequester.h"
19
20#include <KLocalizedString>
21#include <KSeparator>
22
23#include <QGpgME/Protocol>
24
25#include <QComboBox>
26#include <QDialogButtonBox>
27#include <QGridLayout>
28#include <QLabel>
29#include <QPushButton>
30#include <QScreen>
31#include <QScrollArea>
32#include <QStringList>
33#include <QVBoxLayout>
34
35#include <gpgme++/key.h>
36
37static Kleo::EncryptionPreference cb2pref(int i)
38{
39 switch (i) {
40 default:
41 case 0:
42 return Kleo::UnknownPreference;
43 case 1:
44 return Kleo::NeverEncrypt;
45 case 2:
46 return Kleo::AlwaysEncrypt;
47 case 3:
48 return Kleo::AlwaysEncryptIfPossible;
49 case 4:
50 return Kleo::AlwaysAskForEncryption;
51 case 5:
52 return Kleo::AskWheneverPossible;
53 }
54}
55
56static int pref2cb(Kleo::EncryptionPreference p)
57{
58 switch (p) {
59 default:
60 return 0;
61 case Kleo::NeverEncrypt:
62 return 1;
63 case Kleo::AlwaysEncrypt:
64 return 2;
65 case Kleo::AlwaysEncryptIfPossible:
66 return 3;
67 case Kleo::AlwaysAskForEncryption:
68 return 4;
69 case Kleo::AskWheneverPossible:
70 return 5;
71 }
72}
73
74static QStringList preferencesStrings()
75{
76 return QStringList() << xi18n("<placeholder>none</placeholder>") //
77 << i18n("Never Encrypt with This Key") //
78 << i18n("Always Encrypt with This Key") //
79 << i18n("Encrypt Whenever Encryption is Possible") //
80 << i18n("Always Ask") //
81 << i18n("Ask Whenever Encryption is Possible");
82}
83
84class Q_DECL_HIDDEN Kleo::KeyApprovalDialog::KeyApprovalDialogPrivate
85{
86public:
87 KeyApprovalDialogPrivate()
88 {
89 }
90
91 Kleo::KeyRequester *selfRequester = nullptr;
92 QStringList addresses;
93 std::vector<Kleo::KeyRequester *> requesters;
94 std::vector<QComboBox *> preferences;
95 bool prefsChanged = false;
96};
97
98Kleo::KeyApprovalDialog::KeyApprovalDialog(const std::vector<Item> &recipients, const std::vector<GpgME::Key> &sender, QWidget *parent)
99 : QDialog(parent)
100 , d(new KeyApprovalDialogPrivate())
101{
102 setWindowTitle(i18nc("@title:window", "Encryption Key Approval"));
103 auto mainLayout = new QVBoxLayout(this);
105 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
107 connect(buttonBox, &QDialogButtonBox::accepted, this, &KeyApprovalDialog::accept);
108 connect(buttonBox, &QDialogButtonBox::rejected, this, &KeyApprovalDialog::reject);
109 okButton->setDefault(true);
110 Q_ASSERT(!recipients.empty());
111
112 QFrame *page = new QFrame(this);
113 mainLayout->addWidget(page);
114 mainLayout->addWidget(buttonBox);
115 auto vlay = new QVBoxLayout(page);
116 vlay->setContentsMargins(0, 0, 0, 0);
117
118 vlay->addWidget(new QLabel(i18nc("@label:textbox", "The following keys will be used for encryption:"), page));
119
120 auto sv = new QScrollArea(page);
121 sv->setWidgetResizable(true);
122 vlay->addWidget(sv);
123
124 QWidget *view = new QWidget(sv->viewport());
125
126 auto glay = new QGridLayout(view);
127 glay->setColumnStretch(1, 1);
128 sv->setWidget(view);
129
130 int row = -1;
131
132 if (!sender.empty()) {
133 ++row;
134 glay->addWidget(new QLabel(i18nc("@label:textbox", "Your keys:"), view), row, 0);
135 d->selfRequester = new EncryptionKeyRequester(true, EncryptionKeyRequester::AllProtocols, view);
136 d->selfRequester->setKeys(sender);
137 glay->addWidget(d->selfRequester, row, 1);
138 ++row;
139 glay->addWidget(new KSeparator(Qt::Horizontal, view), row, 0, 1, 2);
140 }
141
142 const QStringList prefs = preferencesStrings();
143
144 for (auto it = recipients.begin(); it != recipients.end(); ++it) {
145 ++row;
146 glay->addWidget(new QLabel(i18nc("@label:textbox", "Recipient:"), view), row, 0);
147 glay->addWidget(new QLabel(it->address, view), row, 1);
148 d->addresses.push_back(it->address);
149
150 ++row;
151 glay->addWidget(new QLabel(i18nc("@label:textbox", "Encryption keys:"), view), row, 0);
152 KeyRequester *req = new EncryptionKeyRequester(true, EncryptionKeyRequester::AllProtocols, view);
153 req->setKeys(it->keys);
154 glay->addWidget(req, row, 1);
155 d->requesters.push_back(req);
156
157 ++row;
158 glay->addWidget(new QLabel(i18nc("@label:textbox", "Encryption preference:"), view), row, 0);
159 auto cb = new QComboBox(view);
160 cb->setEditable(false);
161 cb->addItems(prefs);
162 glay->addWidget(cb, row, 1);
163 cb->setCurrentIndex(pref2cb(it->pref));
164 connect(cb, qOverload<int>(&QComboBox::activated), this, &KeyApprovalDialog::slotPrefsChanged);
165 d->preferences.push_back(cb);
166 }
167
168 QSize size = sizeHint();
169
170 // don't make the dialog too large
171 const QSize desk = screen()->size();
172 resize(QSize(qMin(size.width(), 3 * desk.width() / 4), qMin(size.height(), 7 * desk.height() / 8)));
173}
174
175Kleo::KeyApprovalDialog::~KeyApprovalDialog() = default;
176
177std::vector<GpgME::Key> Kleo::KeyApprovalDialog::senderKeys() const
178{
179 return d->selfRequester ? d->selfRequester->keys() : std::vector<GpgME::Key>();
180}
181
182std::vector<Kleo::KeyApprovalDialog::Item> Kleo::KeyApprovalDialog::items() const
183{
184 Q_ASSERT(d->requesters.size() == static_cast<unsigned int>(d->addresses.size()));
185 Q_ASSERT(d->requesters.size() == d->preferences.size());
186
187 std::vector<Item> result;
188 result.reserve(d->requesters.size());
189 QStringList::const_iterator ait = d->addresses.constBegin();
190 auto rit = d->requesters.begin();
191 auto cit = d->preferences.begin();
192 while (ait != d->addresses.constEnd()) {
193 result.push_back(Item(*ait++, (*rit++)->keys(), cb2pref((*cit++)->currentIndex())));
194 }
195 return result;
196}
197
198bool Kleo::KeyApprovalDialog::preferencesChanged() const
199{
200 return d->prefsChanged;
201}
202
203void Kleo::KeyApprovalDialog::slotPrefsChanged()
204{
205 d->prefsChanged = true;
206}
207
208#include "moc_keyapprovaldialog.cpp"
Base class for SigningKeyRequester and EncryptionKeyRequester.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString xi18n(const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QAction * preferences(const QObject *recvr, const char *slot, QObject *parent)
void setShortcut(const QKeySequence &key)
void activated(int index)
QPushButton * button(StandardButton which) const const
void setDefault(bool)
int height() const const
int width() const const
Key_Return
Horizontal
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.