File indexing completed on 2024-05-26 05:24:21

0001 /*
0002     dn.h
0003 
0004     This file is part of libkleopatra, the KDE keymanagement library
0005     SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB
0006     SPDX-FileCopyrightText: 2021 g10 Code GmbH
0007     SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #pragma once
0013 
0014 #include "kleo_export.h"
0015 
0016 #include <QList>
0017 #include <QString>
0018 #include <QStringList>
0019 
0020 namespace Kleo
0021 {
0022 
0023 /**
0024    @short DN parser and reorderer
0025 */
0026 class KLEO_EXPORT DN
0027 {
0028 public:
0029     class Attribute;
0030     using AttributeList = QList<Attribute>;
0031     using const_iterator = AttributeList::const_iterator;
0032 
0033     DN();
0034     explicit DN(const QString &dn);
0035     explicit DN(const char *utf8DN);
0036     DN(const DN &other);
0037     ~DN();
0038 
0039     const DN &operator=(const DN &other);
0040 
0041     static QStringList attributeOrder();
0042     static void setAttributeOrder(const QStringList &order);
0043 
0044     static QStringList defaultAttributeOrder();
0045 
0046     static QStringList attributeNames();
0047     static QString attributeNameToLabel(const QString &name);
0048 
0049     /** @return the value in rfc-2253-escaped form */
0050     static QString escape(const QString &value);
0051 
0052     /** @return the DN in a reordered form, according to the settings in
0053         the [DN] group of the application's config file */
0054     QString prettyDN() const;
0055     /** @return the DN in the original form */
0056     QString dn() const;
0057     /**
0058        \overload
0059        Uses \a sep as separator (default: ,)
0060     */
0061     QString dn(const QString &sep) const;
0062 
0063     QString operator[](const QString &attr) const;
0064 
0065     void append(const Attribute &attr);
0066 
0067     const_iterator begin() const;
0068     const_iterator end() const;
0069 
0070 private:
0071     void detach();
0072 
0073 private:
0074     class Private;
0075     Private *d;
0076 };
0077 
0078 class KLEO_EXPORT DN::Attribute
0079 {
0080 public:
0081     using List = DN::AttributeList;
0082 
0083     explicit Attribute(const QString &name = QString(), const QString &value = QString())
0084         : mName(name.toUpper())
0085         , mValue(value)
0086     {
0087     }
0088     Attribute(const Attribute &other)
0089         : mName(other.name())
0090         , mValue(other.value())
0091     {
0092     }
0093 
0094     const Attribute &operator=(const Attribute &other)
0095     {
0096         if (this != &other) {
0097             mName = other.name();
0098             mValue = other.value();
0099         }
0100         return *this;
0101     }
0102 
0103     const QString &name() const
0104     {
0105         return mName;
0106     }
0107     const QString &value() const
0108     {
0109         return mValue;
0110     }
0111 
0112     void setValue(const QString &value)
0113     {
0114         mValue = value;
0115     }
0116 
0117 private:
0118     QString mName;
0119     QString mValue;
0120 };
0121 
0122 }