Messagelib

dkimwidgetinfo.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "dkimwidgetinfo.h"
8#include "dkimmanager.h"
9#include "dkimutil.h"
10#include "messageviewer_dkimcheckerdebug.h"
11#include <KColorScheme>
12#include <KLocalizedString>
13
14#include <QHBoxLayout>
15#include <QLabel>
16
17using namespace MessageViewer;
18DKIMWidgetInfo::DKIMWidgetInfo(QWidget *parent)
19 : QWidget(parent)
20 , mLabel(new QLabel(this))
21{
22 auto mainLayout = new QHBoxLayout(this);
23 mainLayout->setObjectName(QLatin1StringView("mainLayout"));
24 mainLayout->setContentsMargins({});
25
26 mLabel->setAutoFillBackground(true);
27 mLabel->setObjectName(QLatin1StringView("label"));
28 mainLayout->addWidget(mLabel);
29 connect(DKIMManager::self(), &DKIMManager::result, this, &DKIMWidgetInfo::setResult);
30 connect(DKIMManager::self(), &DKIMManager::clearInfo, this, &DKIMWidgetInfo::clear);
31 initColors();
32}
33
34DKIMWidgetInfo::~DKIMWidgetInfo() = default;
35
36void DKIMWidgetInfo::updatePalette()
37{
38 initColors();
39 updateInfo();
40}
41
42void DKIMWidgetInfo::initColors()
43{
44 const KColorScheme colorScheme{QPalette::Active};
45 mWarningColor = colorScheme.background(KColorScheme::NeutralBackground).color();
46 mErrorColor = colorScheme.background(KColorScheme::NegativeBackground).color();
47 mOkColor = colorScheme.background(KColorScheme::PositiveBackground).color();
48 mDefaultColor = Qt::transparent; // colorScheme.background(KColorScheme::ActiveBackground).color();
49}
50
51MessageViewer::DKIMCheckSignatureJob::CheckSignatureResult DKIMWidgetInfo::result() const
52{
53 return mResult;
54}
55
56Akonadi::Item::Id DKIMWidgetInfo::currentItemId() const
57{
58 return mCurrentItemId;
59}
60
61void DKIMWidgetInfo::setCurrentItemId(Akonadi::Item::Id currentItemId)
62{
63 mCurrentItemId = currentItemId;
64}
65
66void DKIMWidgetInfo::setResult(const DKIMCheckSignatureJob::CheckSignatureResult &checkResult, Akonadi::Item::Id id)
67{
68 if (mCurrentItemId == id) {
69 if (mResult != checkResult) {
70 mResult = checkResult;
71 updateInfo();
72 }
73 }
74}
75
76void DKIMWidgetInfo::clear()
77{
78 mLabel->clear();
79 mLabel->setToolTip(QString());
80 QPalette pal = mLabel->palette();
81 pal.setColor(backgroundRole(), mDefaultColor);
82 mLabel->setPalette(pal);
83 mCurrentItemId = -1;
84 mResult = {};
85}
86
87void DKIMWidgetInfo::updateInfo()
88{
89 QPalette pal = mLabel->palette();
90 switch (mResult.status) {
91 case DKIMCheckSignatureJob::DKIMStatus::Unknown:
92 pal.setColor(backgroundRole(), mDefaultColor);
93 mLabel->setPalette(pal);
94 mLabel->setText(i18n("Unknown"));
95 break;
96 case DKIMCheckSignatureJob::DKIMStatus::Valid:
97 if (mResult.sdid.isEmpty()) {
98 qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "mResult.sdid is empty. It's a bug";
99 }
100 mLabel->setText(i18n("DKIM: valid (signed by %1)", mResult.sdid));
101 pal.setColor(backgroundRole(), (mResult.warning != DKIMCheckSignatureJob::DKIMWarning::Any) ? mWarningColor : mOkColor);
102 mLabel->setPalette(pal);
103 break;
104 case DKIMCheckSignatureJob::DKIMStatus::Invalid:
105 pal.setColor(backgroundRole(), mErrorColor);
106 mLabel->setPalette(pal);
107 mLabel->setText(i18n("DKIM: invalid"));
108 break;
109 case DKIMCheckSignatureJob::DKIMStatus::EmailNotSigned:
110 mLabel->setText(i18n("DKIM: Not signed"));
111 pal.setColor(backgroundRole(), mDefaultColor);
112 mLabel->setPalette(pal);
113 break;
114 case DKIMCheckSignatureJob::DKIMStatus::NeedToBeSigned:
115 mLabel->setText(i18n("DKIM: Should Be Signed by %1", mResult.sdid));
116 pal.setColor(backgroundRole(), mErrorColor);
117 mLabel->setPalette(pal);
118 break;
119 }
120 updateToolTip();
121}
122
123void DKIMWidgetInfo::updateToolTip()
124{
125 QString tooltip;
126 if (mResult.status == DKIMCheckSignatureJob::DKIMStatus::Invalid) {
127 switch (mResult.error) {
128 case DKIMCheckSignatureJob::DKIMError::Any:
129 break;
130 case DKIMCheckSignatureJob::DKIMError::CorruptedBodyHash:
131 tooltip = i18n("Body Hash was corrupted.");
132 break;
133 case DKIMCheckSignatureJob::DKIMError::DomainNotExist:
134 tooltip = i18n("The domain doesn't exist.");
135 break;
136 case DKIMCheckSignatureJob::DKIMError::MissingFrom:
137 tooltip = i18n("Missing header From.");
138 break;
139 case DKIMCheckSignatureJob::DKIMError::MissingSignature:
140 tooltip = i18n("Missing signature.");
141 break;
142 case DKIMCheckSignatureJob::DKIMError::InvalidQueryMethod:
143 tooltip = i18n("Invalid query method.");
144 break;
145 case DKIMCheckSignatureJob::DKIMError::InvalidHeaderCanonicalization:
146 tooltip = i18n("Invalid header canonicalization.");
147 break;
148 case DKIMCheckSignatureJob::DKIMError::InvalidBodyCanonicalization:
149 tooltip = i18n("Invalid body canonicalization.");
150 break;
151 case DKIMCheckSignatureJob::DKIMError::InvalidBodyHashAlgorithm:
152 tooltip = i18n("Unknown Body Hash Algorithm.");
153 break;
154 case DKIMCheckSignatureJob::DKIMError::InvalidSignAlgorithm:
155 tooltip = i18n("Signature algorithm is invalid.");
156 break;
157 case DKIMCheckSignatureJob::DKIMError::PublicKeyWasRevoked:
158 tooltip = i18n("The public key was revoked.");
159 break;
160 case DKIMCheckSignatureJob::DKIMError::SignatureTooLarge:
161 tooltip = i18n("Signature is too large.");
162 break;
163 case DKIMCheckSignatureJob::DKIMError::InsupportedHashAlgorithm:
164 tooltip = i18n("Hash Algorithm is unsupported.");
165 break;
166 case DKIMCheckSignatureJob::DKIMError::PublicKeyTooSmall:
167 tooltip = i18n("Public key is too small.");
168 break;
169 case DKIMCheckSignatureJob::DKIMError::ImpossibleToVerifySignature:
170 tooltip = i18n("Impossible to verify signature.");
171 break;
172 case DKIMCheckSignatureJob::DKIMError::DomainI:
173 tooltip = i18n("AUID must be in the same domain as SDID (s-flag set in key record).");
174 break;
175 case DKIMCheckSignatureJob::DKIMError::TestKeyMode:
176 tooltip = i18n("The signing domain is only testing DKIM.");
177 break;
178 case DKIMCheckSignatureJob::DKIMError::ImpossibleToDownloadKey:
179 tooltip = i18n("Impossible to download key.");
180 break;
181 case DKIMCheckSignatureJob::DKIMError::HashAlgorithmUnsafeSha1:
182 tooltip = i18n("Hash Algorithm unsafe (sha1)");
183 break;
184 case DKIMCheckSignatureJob::DKIMError::IDomainError:
185 tooltip = i18n("AUID is not in a subdomain of SDID");
186 break;
187 case DKIMCheckSignatureJob::DKIMError::PublicKeyConversionError:
188 tooltip = i18n("Problem during converting public key");
189 break;
190 }
191 }
192
193 switch (mResult.warning) {
194 case DKIMCheckSignatureJob::DKIMWarning::Any:
195 break;
196 case DKIMCheckSignatureJob::DKIMWarning::SignatureExpired:
197 tooltip += (tooltip.isEmpty() ? QChar() : QLatin1Char('\n')) + i18n("Signature expired");
198 break;
199 case DKIMCheckSignatureJob::DKIMWarning::SignatureCreatedInFuture:
200 tooltip += (tooltip.isEmpty() ? QChar() : QLatin1Char('\n')) + i18n("Signature created in the future");
201 break;
202 case DKIMCheckSignatureJob::DKIMWarning::SignatureTooSmall:
203 tooltip += (tooltip.isEmpty() ? QChar() : QLatin1Char('\n')) + i18n("Signature too small");
204 break;
205 case DKIMCheckSignatureJob::DKIMWarning::HashAlgorithmUnsafe:
206 tooltip += (tooltip.isEmpty() ? QChar() : QLatin1Char('\n')) + i18n("Hash Algorithm unsafe (sha1)");
207 break;
208 case DKIMCheckSignatureJob::DKIMWarning::PublicRsaKeyTooSmall:
209 tooltip += (tooltip.isEmpty() ? QChar() : QLatin1Char('\n')) + i18n("Public Key too small");
210 break;
211 }
212
213 if (mResult.status != DKIMCheckSignatureJob::DKIMStatus::Invalid) {
214 QStringList tooltipList;
215 for (const DKIMCheckSignatureJob::DKIMCheckSignatureAuthenticationResult &result : std::as_const(mResult.listSignatureAuthenticationResult)) {
216 switch (result.status) {
217 case DKIMCheckSignatureJob::DKIMStatus::Unknown:
218 break;
219 case DKIMCheckSignatureJob::DKIMStatus::Invalid:
220 switch (result.method) {
221 case DKIMCheckSignatureJob::AuthenticationMethod::Unknown: {
222 break;
223 }
224 case DKIMCheckSignatureJob::AuthenticationMethod::Spf:
225 case DKIMCheckSignatureJob::AuthenticationMethod::Dkim:
226 case DKIMCheckSignatureJob::AuthenticationMethod::Dmarc:
227 case DKIMCheckSignatureJob::AuthenticationMethod::Auth:
228 case DKIMCheckSignatureJob::AuthenticationMethod::Dkimatps: {
229 const QString str = i18nc("method name: info about it from parsing",
230 "%1: %2",
231 MessageViewer::DKIMUtil::convertAuthenticationMethodEnumToString(result.method),
232 result.infoResult);
233 if (!tooltipList.contains(str)) {
234 tooltipList.append(str);
235 }
236 break;
237 }
238 }
239 break;
240
241 case DKIMCheckSignatureJob::DKIMStatus::NeedToBeSigned:
242 break;
243 case DKIMCheckSignatureJob::DKIMStatus::EmailNotSigned:
244 switch (result.method) {
245 case DKIMCheckSignatureJob::AuthenticationMethod::Unknown: {
246 break;
247 }
248 case DKIMCheckSignatureJob::AuthenticationMethod::Spf:
249 case DKIMCheckSignatureJob::AuthenticationMethod::Dkim:
250 case DKIMCheckSignatureJob::AuthenticationMethod::Dmarc:
251 case DKIMCheckSignatureJob::AuthenticationMethod::Auth:
252 case DKIMCheckSignatureJob::AuthenticationMethod::Dkimatps: {
253 const QString str = i18n("%1: None", MessageViewer::DKIMUtil::convertAuthenticationMethodEnumToString(result.method));
254 if (!tooltipList.contains(str)) {
255 tooltipList.append(str);
256 }
257 break;
258 }
259 }
260 break;
261 case DKIMCheckSignatureJob::DKIMStatus::Valid:
262 switch (result.method) {
263 case DKIMCheckSignatureJob::AuthenticationMethod::Unknown: {
264 break;
265 }
266 case DKIMCheckSignatureJob::AuthenticationMethod::Dkim: {
267 const QString str =
268 i18n("%1: Valid (Signed by %2)", MessageViewer::DKIMUtil::convertAuthenticationMethodEnumToString(result.method), result.sdid);
269 if (!tooltipList.contains(str)) {
270 tooltipList.append(str);
271 }
272 break;
273 }
274 case DKIMCheckSignatureJob::AuthenticationMethod::Spf: {
275 const QString str = i18nc("method name: info about it from parsing",
276 "%1: %2",
277 MessageViewer::DKIMUtil::convertAuthenticationMethodEnumToString(result.method),
278 result.infoResult);
279 if (!tooltipList.contains(str)) {
280 tooltipList.append(str);
281 }
282
283 break;
284 }
285 case DKIMCheckSignatureJob::AuthenticationMethod::Auth:
286 case DKIMCheckSignatureJob::AuthenticationMethod::Dkimatps:
287 case DKIMCheckSignatureJob::AuthenticationMethod::Dmarc: {
288 const QString str = i18n("%1: Valid", MessageViewer::DKIMUtil::convertAuthenticationMethodEnumToString(result.method));
289 if (!tooltipList.contains(str)) {
290 tooltipList.append(str);
291 }
292
293 break;
294 }
295 }
296 break;
297 }
298 }
299 if (!tooltipList.isEmpty()) {
300 tooltip += (tooltip.isEmpty() ? QChar() : QLatin1Char('\n')) + tooltipList.join(QLatin1Char('\n'));
301 }
302 if (mResult.listSignatureAuthenticationResult.isEmpty()) {
303 tooltip += (tooltip.isEmpty() ? QChar() : QLatin1Char('\n')) + i18n("Not Signed");
304 }
305 }
306 qCDebug(MESSAGEVIEWER_DKIMCHECKER_LOG) << "mResult.authentication " << mResult.listSignatureAuthenticationResult;
307
308 mLabel->setToolTip(tooltip);
309}
310
311#include "moc_dkimwidgetinfo.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void clear()
void setText(const QString &)
void append(QList< T > &&value)
bool isEmpty() const const
void setColor(ColorGroup group, ColorRole role, const QColor &color)
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QString join(QChar separator) const const
transparent
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QPalette::ColorRole backgroundRole() const const
void setToolTip(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.