| 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() = default; |
| 59 | |
| 60 | bool Key::operator==(const Key &other) const |
| 61 | { |
| 62 | if (d->mId != other.d->mId) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | if (d->mType != other.d->mType) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if (d->mIsBinary != other.d->mIsBinary) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if (d->mIsBinary) { |
| 75 | if (d->mBinaryData != other.d->mBinaryData) { |
| 76 | return false; |
| 77 | } |
| 78 | } else { |
| 79 | if (d->mTextData != other.d->mTextData) { |
| 80 | return false; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (d->mCustomTypeString != other.d->mCustomTypeString) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | bool Key::operator!=(const Key &other) const |
| 92 | { |
| 93 | return !(*this == other); |
| 94 | } |
| 95 | |
| 96 | Key &Key::operator=(const Key &other) |
| 97 | { |
| 98 | if (this != &other) { |
| 99 | d = other.d; |
| 100 | } |
| 101 | |
| 102 | return *this; |
| 103 | } |
| 104 | |
| 105 | void Key::setId(const QString &id) |
| 106 | { |
| 107 | d->mId = id; |
| 108 | } |
| 109 | |
| 110 | QString Key::id() const |
| 111 | { |
| 112 | return d->mId; |
| 113 | } |
| 114 | |
| 115 | void Key::setBinaryData(const QByteArray &binary) |
| 116 | { |
| 117 | d->mBinaryData = binary; |
| 118 | d->mIsBinary = true; |
| 119 | } |
| 120 | |
| 121 | QByteArray Key::binaryData() const |
| 122 | { |
| 123 | return d->mBinaryData; |
| 124 | } |
| 125 | |
| 126 | void Key::setTextData(const QString &text) |
| 127 | { |
| 128 | d->mTextData = text; |
| 129 | d->mIsBinary = false; |
| 130 | } |
| 131 | |
| 132 | QString Key::textData() const |
| 133 | { |
| 134 | return d->mTextData; |
| 135 | } |
| 136 | |
| 137 | bool Key::isBinary() const |
| 138 | { |
| 139 | return d->mIsBinary; |
| 140 | } |
| 141 | |
| 142 | void Key::setType(Type type) |
| 143 | { |
| 144 | d->mType = type; |
| 145 | } |
| 146 | |
| 147 | void Key::setCustomTypeString(const QString &custom) |
| 148 | { |
| 149 | d->mCustomTypeString = custom; |
| 150 | } |
| 151 | |
| 152 | Key::Type Key::type() const |
| 153 | { |
| 154 | return d->mType; |
| 155 | } |
| 156 | |
| 157 | QString Key::customTypeString() const |
| 158 | { |
| 159 | return d->mCustomTypeString; |
| 160 | } |
| 161 | |
| 162 | QString Key::toString() const |
| 163 | { |
| 164 | QString str = QLatin1String("Key {\n"); |
| 165 | str += QStringLiteral(" Id: %1\n").arg(a: d->mId); |
| 166 | str += QStringLiteral(" Type: %1\n").arg(a: typeLabel(type: d->mType)); |
| 167 | if (d->mType == Custom) { |
| 168 | str += QStringLiteral(" CustomType: %1\n").arg(a: d->mCustomTypeString); |
| 169 | } |
| 170 | str += QStringLiteral(" IsBinary: %1\n").arg(a: d->mIsBinary ? QStringLiteral( "true") : QStringLiteral( "false")); |
| 171 | if (d->mIsBinary) { |
| 172 | str += QStringLiteral(" Binary: %1\n").arg(a: QString::fromLatin1(ba: d->mBinaryData.toBase64())); |
| 173 | } else { |
| 174 | str += QStringLiteral(" Text: %1\n").arg(a: d->mTextData); |
| 175 | } |
| 176 | str += QLatin1String("}\n"); |
| 177 | |
| 178 | return str; |
| 179 | } |
| 180 | |
| 181 | Key::TypeList Key::typeList() |
| 182 | { |
| 183 | static TypeList list; |
| 184 | |
| 185 | if (list.isEmpty()) { |
| 186 | list << X509 << PGP << Custom; |
| 187 | } |
| 188 | |
| 189 | return list; |
| 190 | } |
| 191 | |
| 192 | QString Key::typeLabel(Type type) |
| 193 | { |
| 194 | switch (type) { |
| 195 | case X509: |
| 196 | return i18nc("X.509 public key", "X509"); |
| 197 | break; |
| 198 | case PGP: |
| 199 | return i18nc("Pretty Good Privacy key", "PGP"); |
| 200 | break; |
| 201 | case Custom: |
| 202 | return i18nc("A custom key", "Custom"); |
| 203 | break; |
| 204 | default: |
| 205 | return i18nc("another type of encryption key", "Unknown type"); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // clang-format off |
| 211 | QDataStream &KContacts::operator<<(QDataStream &s, const Key &key) |
| 212 | { |
| 213 | return s << key.d->mId << key.d->mType << key.d->mIsBinary << key.d->mBinaryData |
| 214 | << key.d->mTextData << key.d->mCustomTypeString; |
| 215 | } |
| 216 | |
| 217 | QDataStream &KContacts::operator>>(QDataStream &s, Key &key) |
| 218 | { |
| 219 | uint type; |
| 220 | s >> key.d->mId >> type >> key.d->mIsBinary >> key.d->mBinaryData >> key.d->mTextData |
| 221 | >> key.d->mCustomTypeString; |
| 222 | |
| 223 | key.d->mType = Key::Type(type); |
| 224 | |
| 225 | return s; |
| 226 | } |
| 227 | // clang-format on |
| 228 |
