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 | |
15 | using namespace KContacts; |
16 | |
17 | class Q_DECL_HIDDEN Key::Private : public QSharedData |
18 | { |
19 | public: |
20 | Private() |
21 | : mId(KRandom::randomString(length: 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 | |
45 | Key::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 | |
53 | Key::Key(const Key &other) |
54 | : d(other.d) |
55 | { |
56 | } |
57 | |
58 | Key::~Key() |
59 | { |
60 | } |
61 | |
62 | bool 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 | |
93 | bool Key::operator!=(const Key &other) const |
94 | { |
95 | return !(*this == other); |
96 | } |
97 | |
98 | Key &Key::operator=(const Key &other) |
99 | { |
100 | if (this != &other) { |
101 | d = other.d; |
102 | } |
103 | |
104 | return *this; |
105 | } |
106 | |
107 | void Key::setId(const QString &id) |
108 | { |
109 | d->mId = id; |
110 | } |
111 | |
112 | QString Key::id() const |
113 | { |
114 | return d->mId; |
115 | } |
116 | |
117 | void Key::setBinaryData(const QByteArray &binary) |
118 | { |
119 | d->mBinaryData = binary; |
120 | d->mIsBinary = true; |
121 | } |
122 | |
123 | QByteArray Key::binaryData() const |
124 | { |
125 | return d->mBinaryData; |
126 | } |
127 | |
128 | void Key::setTextData(const QString &text) |
129 | { |
130 | d->mTextData = text; |
131 | d->mIsBinary = false; |
132 | } |
133 | |
134 | QString Key::textData() const |
135 | { |
136 | return d->mTextData; |
137 | } |
138 | |
139 | bool Key::isBinary() const |
140 | { |
141 | return d->mIsBinary; |
142 | } |
143 | |
144 | void Key::setType(Type type) |
145 | { |
146 | d->mType = type; |
147 | } |
148 | |
149 | void Key::setCustomTypeString(const QString &custom) |
150 | { |
151 | d->mCustomTypeString = custom; |
152 | } |
153 | |
154 | Key::Type Key::type() const |
155 | { |
156 | return d->mType; |
157 | } |
158 | |
159 | QString Key::customTypeString() const |
160 | { |
161 | return d->mCustomTypeString; |
162 | } |
163 | |
164 | QString Key::toString() const |
165 | { |
166 | QString str = QLatin1String("Key {\n"); |
167 | str += QStringLiteral(" Id: %1\n").arg(a: d->mId); |
168 | str += QStringLiteral(" Type: %1\n").arg(a: typeLabel(type: d->mType)); |
169 | if (d->mType == Custom) { |
170 | str += QStringLiteral(" CustomType: %1\n").arg(a: d->mCustomTypeString); |
171 | } |
172 | str += QStringLiteral(" IsBinary: %1\n").arg(a: d->mIsBinary ? QStringLiteral( "true") : QStringLiteral( "false")); |
173 | if (d->mIsBinary) { |
174 | str += QStringLiteral(" Binary: %1\n").arg(a: QString::fromLatin1(ba: d->mBinaryData.toBase64())); |
175 | } else { |
176 | str += QStringLiteral(" Text: %1\n").arg(a: d->mTextData); |
177 | } |
178 | str += QLatin1String("}\n"); |
179 | |
180 | return str; |
181 | } |
182 | |
183 | Key::TypeList Key::typeList() |
184 | { |
185 | static TypeList list; |
186 | |
187 | if (list.isEmpty()) { |
188 | list << X509 << PGP << Custom; |
189 | } |
190 | |
191 | return list; |
192 | } |
193 | |
194 | QString Key::typeLabel(Type type) |
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 |
213 | QDataStream &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 | |
219 | QDataStream &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 |
230 |