KContacts

key.cpp
1/*
2 This file is part of the KContacts framework.
3 SPDX-FileCopyrightText: 2002 Tobias Koenig <tokoe@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "key.h"
9
10#include <KLocalizedString>
11#include <krandom.h>
12
13#include <QSharedData>
14
15using namespace KContacts;
16
17class Q_DECL_HIDDEN Key::Private : public QSharedData
18{
19public:
20 Private()
21 : mId(KRandom::randomString(8))
22 {
23 }
24
25 Private(const Private &other)
26 : QSharedData(other)
27 {
28 mId = other.mId;
29 mBinaryData = other.mBinaryData;
30 mTextData = other.mTextData;
31 mCustomTypeString = other.mCustomTypeString;
32 mIsBinary = other.mIsBinary;
33 mType = other.mType;
34 }
35
36 QString mId;
37 QByteArray mBinaryData;
38 QString mTextData;
39 QString mCustomTypeString;
40
41 Type mType;
42 bool mIsBinary;
43};
44
45Key::Key(const QString &text, Type type)
46 : d(new Private)
47{
48 d->mTextData = text;
49 d->mIsBinary = false;
50 d->mType = type;
51}
52
53Key::Key(const Key &other)
54 : d(other.d)
55{
56}
57
59{
60}
61
62bool Key::operator==(const Key &other) const
63{
64 if (d->mId != other.d->mId) {
65 return false;
66 }
67
68 if (d->mType != other.d->mType) {
69 return false;
70 }
71
72 if (d->mIsBinary != other.d->mIsBinary) {
73 return false;
74 }
75
76 if (d->mIsBinary) {
77 if (d->mBinaryData != other.d->mBinaryData) {
78 return false;
79 }
80 } else {
81 if (d->mTextData != other.d->mTextData) {
82 return false;
83 }
84 }
85
86 if (d->mCustomTypeString != other.d->mCustomTypeString) {
87 return false;
88 }
89
90 return true;
91}
92
93bool Key::operator!=(const Key &other) const
94{
95 return !(*this == other);
96}
97
98Key &Key::operator=(const Key &other)
99{
100 if (this != &other) {
101 d = other.d;
102 }
103
104 return *this;
105}
106
107void Key::setId(const QString &id)
108{
109 d->mId = id;
110}
111
113{
114 return d->mId;
115}
116
118{
119 d->mBinaryData = binary;
120 d->mIsBinary = true;
121}
122
124{
125 return d->mBinaryData;
126}
127
128void Key::setTextData(const QString &text)
129{
130 d->mTextData = text;
131 d->mIsBinary = false;
132}
133
135{
136 return d->mTextData;
137}
138
139bool Key::isBinary() const
140{
141 return d->mIsBinary;
142}
143
145{
146 d->mType = type;
147}
148
150{
151 d->mCustomTypeString = custom;
152}
153
155{
156 return d->mType;
157}
158
160{
161 return d->mCustomTypeString;
162}
163
165{
166 QString str = QLatin1String("Key {\n");
167 str += QStringLiteral(" Id: %1\n").arg(d->mId);
168 str += QStringLiteral(" Type: %1\n").arg(typeLabel(d->mType));
169 if (d->mType == Custom) {
170 str += QStringLiteral(" CustomType: %1\n").arg(d->mCustomTypeString);
171 }
172 str += QStringLiteral(" IsBinary: %1\n").arg(d->mIsBinary ? QStringLiteral("true") : QStringLiteral("false"));
173 if (d->mIsBinary) {
174 str += QStringLiteral(" Binary: %1\n").arg(QString::fromLatin1(d->mBinaryData.toBase64()));
175 } else {
176 str += QStringLiteral(" Text: %1\n").arg(d->mTextData);
177 }
178 str += QLatin1String("}\n");
179
180 return str;
181}
182
184{
185 static TypeList list;
186
187 if (list.isEmpty()) {
188 list << X509 << PGP << Custom;
189 }
190
191 return list;
192}
193
195{
196 switch (type) {
197 case X509:
198 return i18nc("X.509 public key", "X509");
199 break;
200 case PGP:
201 return i18nc("Pretty Good Privacy key", "PGP");
202 break;
203 case Custom:
204 return i18nc("A custom key", "Custom");
205 break;
206 default:
207 return i18nc("another type of encryption key", "Unknown type");
208 break;
209 }
210}
211
212// clang-format off
213QDataStream &KContacts::operator<<(QDataStream &s, const Key &key)
214{
215 return s << key.d->mId << key.d->mType << key.d->mIsBinary << key.d->mBinaryData
216 << key.d->mTextData << key.d->mCustomTypeString;
217}
218
219QDataStream &KContacts::operator>>(QDataStream &s, Key &key)
220{
221 uint type;
222 s >> key.d->mId >> type >> key.d->mIsBinary >> key.d->mBinaryData >> key.d->mTextData
223 >> key.d->mCustomTypeString;
224
225 key.d->mType = Key::Type(type);
226
227 return s;
228}
229// clang-format on
A class to store an encryption key.
Definition key.h:22
static QString typeLabel(Type type)
Returns a translated label for a given key type.
Definition key.cpp:194
static TypeList typeList()
Returns a list of all available key types.
Definition key.cpp:183
bool operator!=(const Key &other) const
Not-equal operator.
Definition key.cpp:93
Type type() const
Returns the type, see Type.
Definition key.cpp:154
bool isBinary() const
Returns whether the key contains binary or text data.
Definition key.cpp:139
QString toString() const
Returns a string representation of the key.
Definition key.cpp:164
Type
Key types.
Definition key.h:35
@ Custom
Custom or IANA conform key.
Definition key.h:38
@ X509
X509 key.
Definition key.h:36
@ PGP
Pretty Good Privacy key.
Definition key.h:37
void setCustomTypeString(const QString &type)
Sets custom type string.
Definition key.cpp:149
QString id() const
Returns the unique identifier.
Definition key.cpp:112
QByteArray binaryData() const
Returns the binary data.
Definition key.cpp:123
QString customTypeString() const
Returns the custom type string.
Definition key.cpp:159
void setId(const QString &identifier)
Sets the unique identifier.
Definition key.cpp:107
~Key()
Destroys the key.
Definition key.cpp:58
void setBinaryData(const QByteArray &data)
Sets binary data.
Definition key.cpp:117
bool operator==(const Key &other) const
Equality operator.
Definition key.cpp:62
void setType(Type type)
Sets the type.
Definition key.cpp:144
Key(const QString &text=QString(), Type type=PGP)
Creates a new key.
Definition key.cpp:45
void setTextData(const QString &data)
Sets text data.
Definition key.cpp:128
QString textData() const
Returns the text data.
Definition key.cpp:134
Key & operator=(const Key &other)
Assignment operator.
Definition key.cpp:98
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KCOREADDONS_EXPORT QString randomString(int length)
bool isEmpty() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:39:36 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.