Libkleo

dnattributes.cpp
1/*
2 This file is part of libkleopatra, the KDE keymanagement library
3 SPDX-FileCopyrightText: 2021 g10 Code GmbH
4 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
5
6 SPDX-License-Identifier: GPL-2.0-or-later
7*/
8
9#include "dnattributes.h"
10
11#include <libkleo_debug.h>
12
13#include <KLazyLocalizedString>
14
15#include <QMap>
16
17namespace
18{
19static const QStringList defaultOrder = {
20 QStringLiteral("CN"),
21 QStringLiteral("L"),
22 QStringLiteral("_X_"),
23 QStringLiteral("OU"),
24 QStringLiteral("O"),
25 QStringLiteral("C"),
26};
27
28class DNAttributeOrderStore
29{
30 DNAttributeOrderStore()
31 : mAttributeOrder{defaultOrder}
32 {
33 }
34
35public:
36 static DNAttributeOrderStore *instance()
37 {
38 static DNAttributeOrderStore *self = new DNAttributeOrderStore();
39 return self;
40 }
41
42 const QStringList &attributeOrder() const
43 {
44 return mAttributeOrder.empty() ? defaultOrder : mAttributeOrder;
45 }
46
47 void setAttributeOrder(const QStringList &order)
48 {
49 mAttributeOrder = order;
50 }
51
52private:
53 QStringList mAttributeOrder;
54};
55}
56
57namespace
58{
59static const QMap<QString, KLazyLocalizedString> attributeNamesAndLabels = {
60 // clang-format off
61 {QStringLiteral("CN"), kli18n("Common name") },
62 {QStringLiteral("SN"), kli18n("Surname") },
63 {QStringLiteral("GN"), kli18n("Given name") },
64 {QStringLiteral("L"), kli18n("Location") },
65 {QStringLiteral("T"), kli18n("Title") },
66 {QStringLiteral("OU"), kli18n("Organizational unit")},
67 {QStringLiteral("O"), kli18n("Organization") },
68 {QStringLiteral("PC"), kli18n("Postal code") },
69 {QStringLiteral("C"), kli18n("Country code") },
70 {QStringLiteral("SP"), kli18n("State or province") },
71 {QStringLiteral("DC"), kli18n("Domain component") },
72 {QStringLiteral("BC"), kli18n("Business category") },
73 {QStringLiteral("EMAIL"), kli18n("Email address") },
74 {QStringLiteral("MAIL"), kli18n("Mail address") },
75 {QStringLiteral("MOBILE"), kli18n("Mobile phone number")},
76 {QStringLiteral("TEL"), kli18n("Telephone number") },
77 {QStringLiteral("FAX"), kli18n("Fax number") },
78 {QStringLiteral("STREET"), kli18n("Street address") },
79 {QStringLiteral("UID"), kli18n("Unique ID") },
80 // clang-format on
81};
82}
83
84// static
85QStringList Kleo::DNAttributes::order()
86{
87 return DNAttributeOrderStore::instance()->attributeOrder();
88}
89
90// static
91void Kleo::DNAttributes::setOrder(const QStringList &order)
92{
93 DNAttributeOrderStore::instance()->setAttributeOrder(order);
94}
95
96// static
97QStringList Kleo::DNAttributes::defaultOrder()
98{
99 return ::defaultOrder;
100}
101
102QStringList Kleo::DNAttributes::names()
103{
104 return attributeNamesAndLabels.keys();
105}
106
107QString Kleo::DNAttributes::nameToLabel(const QString &name)
108{
109 const QString key{name.trimmed().toUpper()};
110 if (DNAttributes::names().contains(key)) {
111 return attributeNamesAndLabels.value(key).toString();
112 }
113 qCWarning(LIBKLEO_LOG) << "Attribute " << key << " doesn't exit. Bug ?";
114 return {};
115}
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 4 2025 12:04:00 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.