File indexing completed on 2025-01-19 03:39:52
0001 /* 0002 This file is part of the KContacts framework. 0003 SPDX-FileCopyrightText: 2002 Tobias Koenig <tokoe@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "key.h" 0009 0010 #include <KLocalizedString> 0011 #include <krandom.h> 0012 0013 #include <QSharedData> 0014 0015 using namespace KContacts; 0016 0017 class Q_DECL_HIDDEN Key::Private : public QSharedData 0018 { 0019 public: 0020 Private() 0021 : mId(KRandom::randomString(8)) 0022 { 0023 } 0024 0025 Private(const Private &other) 0026 : QSharedData(other) 0027 { 0028 mId = other.mId; 0029 mBinaryData = other.mBinaryData; 0030 mTextData = other.mTextData; 0031 mCustomTypeString = other.mCustomTypeString; 0032 mIsBinary = other.mIsBinary; 0033 mType = other.mType; 0034 } 0035 0036 QString mId; 0037 QByteArray mBinaryData; 0038 QString mTextData; 0039 QString mCustomTypeString; 0040 0041 Type mType; 0042 bool mIsBinary; 0043 }; 0044 0045 Key::Key(const QString &text, Type type) 0046 : d(new Private) 0047 { 0048 d->mTextData = text; 0049 d->mIsBinary = false; 0050 d->mType = type; 0051 } 0052 0053 Key::Key(const Key &other) 0054 : d(other.d) 0055 { 0056 } 0057 0058 Key::~Key() 0059 { 0060 } 0061 0062 bool Key::operator==(const Key &other) const 0063 { 0064 if (d->mId != other.d->mId) { 0065 return false; 0066 } 0067 0068 if (d->mType != other.d->mType) { 0069 return false; 0070 } 0071 0072 if (d->mIsBinary != other.d->mIsBinary) { 0073 return false; 0074 } 0075 0076 if (d->mIsBinary) { 0077 if (d->mBinaryData != other.d->mBinaryData) { 0078 return false; 0079 } 0080 } else { 0081 if (d->mTextData != other.d->mTextData) { 0082 return false; 0083 } 0084 } 0085 0086 if (d->mCustomTypeString != other.d->mCustomTypeString) { 0087 return false; 0088 } 0089 0090 return true; 0091 } 0092 0093 bool Key::operator!=(const Key &other) const 0094 { 0095 return !(*this == other); 0096 } 0097 0098 Key &Key::operator=(const Key &other) 0099 { 0100 if (this != &other) { 0101 d = other.d; 0102 } 0103 0104 return *this; 0105 } 0106 0107 void Key::setId(const QString &id) 0108 { 0109 d->mId = id; 0110 } 0111 0112 QString Key::id() const 0113 { 0114 return d->mId; 0115 } 0116 0117 void Key::setBinaryData(const QByteArray &binary) 0118 { 0119 d->mBinaryData = binary; 0120 d->mIsBinary = true; 0121 } 0122 0123 QByteArray Key::binaryData() const 0124 { 0125 return d->mBinaryData; 0126 } 0127 0128 void Key::setTextData(const QString &text) 0129 { 0130 d->mTextData = text; 0131 d->mIsBinary = false; 0132 } 0133 0134 QString Key::textData() const 0135 { 0136 return d->mTextData; 0137 } 0138 0139 bool Key::isBinary() const 0140 { 0141 return d->mIsBinary; 0142 } 0143 0144 void Key::setType(Type type) 0145 { 0146 d->mType = type; 0147 } 0148 0149 void Key::setCustomTypeString(const QString &custom) 0150 { 0151 d->mCustomTypeString = custom; 0152 } 0153 0154 Key::Type Key::type() const 0155 { 0156 return d->mType; 0157 } 0158 0159 QString Key::customTypeString() const 0160 { 0161 return d->mCustomTypeString; 0162 } 0163 0164 QString Key::toString() const 0165 { 0166 QString str = QLatin1String("Key {\n"); 0167 str += QStringLiteral(" Id: %1\n").arg(d->mId); 0168 str += QStringLiteral(" Type: %1\n").arg(typeLabel(d->mType)); 0169 if (d->mType == Custom) { 0170 str += QStringLiteral(" CustomType: %1\n").arg(d->mCustomTypeString); 0171 } 0172 str += QStringLiteral(" IsBinary: %1\n").arg(d->mIsBinary ? QStringLiteral("true") : QStringLiteral("false")); 0173 if (d->mIsBinary) { 0174 str += QStringLiteral(" Binary: %1\n").arg(QString::fromLatin1(d->mBinaryData.toBase64())); 0175 } else { 0176 str += QStringLiteral(" Text: %1\n").arg(d->mTextData); 0177 } 0178 str += QLatin1String("}\n"); 0179 0180 return str; 0181 } 0182 0183 Key::TypeList Key::typeList() 0184 { 0185 static TypeList list; 0186 0187 if (list.isEmpty()) { 0188 list << X509 << PGP << Custom; 0189 } 0190 0191 return list; 0192 } 0193 0194 QString Key::typeLabel(Type type) 0195 { 0196 switch (type) { 0197 case X509: 0198 return i18nc("X.509 public key", "X509"); 0199 break; 0200 case PGP: 0201 return i18nc("Pretty Good Privacy key", "PGP"); 0202 break; 0203 case Custom: 0204 return i18nc("A custom key", "Custom"); 0205 break; 0206 default: 0207 return i18nc("another type of encryption key", "Unknown type"); 0208 break; 0209 } 0210 } 0211 0212 // clang-format off 0213 QDataStream &KContacts::operator<<(QDataStream &s, const Key &key) 0214 { 0215 return s << key.d->mId << key.d->mType << key.d->mIsBinary << key.d->mBinaryData 0216 << key.d->mTextData << key.d->mCustomTypeString; 0217 } 0218 0219 QDataStream &KContacts::operator>>(QDataStream &s, Key &key) 0220 { 0221 uint type; 0222 s >> key.d->mId >> type >> key.d->mIsBinary >> key.d->mBinaryData >> key.d->mTextData 0223 >> key.d->mCustomTypeString; 0224 0225 key.d->mType = Key::Type(type); 0226 0227 return s; 0228 } 0229 // clang-format on