KContacts

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

KDE's Doxygen guidelines are available online.