File indexing completed on 2024-09-15 03:37:17
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 "secrecy.h" 0009 0010 #include <KLocalizedString> 0011 0012 #include <QDataStream> 0013 #include <QSharedData> 0014 0015 using namespace KContacts; 0016 0017 class Q_DECL_HIDDEN Secrecy::PrivateData : public QSharedData 0018 { 0019 public: 0020 PrivateData() 0021 : mType(Secrecy::Invalid) 0022 { 0023 } 0024 0025 PrivateData(const PrivateData &other) 0026 : QSharedData(other) 0027 { 0028 mType = other.mType; 0029 } 0030 0031 Type mType; 0032 }; 0033 0034 Secrecy::Secrecy(Type type) 0035 : d(new PrivateData) 0036 { 0037 d->mType = type; 0038 } 0039 0040 Secrecy::Secrecy(const Secrecy &other) 0041 : d(other.d) 0042 { 0043 } 0044 0045 Secrecy::~Secrecy() 0046 { 0047 } 0048 0049 Secrecy &Secrecy::operator=(const Secrecy &other) 0050 { 0051 if (this != &other) { 0052 d = other.d; 0053 } 0054 0055 return *this; 0056 } 0057 0058 bool Secrecy::operator==(const Secrecy &other) const 0059 { 0060 return d->mType == other.d->mType; 0061 } 0062 0063 bool Secrecy::operator!=(const Secrecy &other) const 0064 { 0065 return !(*this == other); 0066 } 0067 0068 bool Secrecy::isValid() const 0069 { 0070 return d->mType != Invalid; 0071 } 0072 0073 void Secrecy::setType(Type type) 0074 { 0075 d->mType = type; 0076 } 0077 0078 Secrecy::Type Secrecy::type() const 0079 { 0080 return d->mType; 0081 } 0082 0083 Secrecy::TypeList Secrecy::typeList() 0084 { 0085 static TypeList list; 0086 0087 if (list.isEmpty()) { 0088 list << Public << Private << Confidential; 0089 } 0090 0091 return list; 0092 } 0093 0094 QString Secrecy::typeLabel(Type type) 0095 { 0096 switch (type) { 0097 case Public: 0098 return i18nc("access is for everyone", "Public"); 0099 break; 0100 case Private: 0101 return i18nc("access is by owner only", "Private"); 0102 break; 0103 case Confidential: 0104 return i18nc("access is by owner and a controlled group", "Confidential"); 0105 break; 0106 default: 0107 return i18nc("unknown secrecy type", "Unknown type"); 0108 break; 0109 } 0110 } 0111 0112 QString Secrecy::toString() const 0113 { 0114 QString str = QLatin1String("Secrecy {\n"); 0115 str += QStringLiteral(" Type: %1\n").arg(typeLabel(d->mType)); 0116 str += QLatin1String("}\n"); 0117 0118 return str; 0119 } 0120 0121 QDataStream &KContacts::operator<<(QDataStream &s, const Secrecy &secrecy) 0122 { 0123 return s << (uint)secrecy.d->mType; 0124 } 0125 0126 QDataStream &KContacts::operator>>(QDataStream &s, Secrecy &secrecy) 0127 { 0128 uint type; 0129 s >> type; 0130 0131 switch (type) { 0132 case 0: 0133 secrecy.d->mType = Secrecy::Public; 0134 break; 0135 case 1: 0136 secrecy.d->mType = Secrecy::Private; 0137 break; 0138 case 2: 0139 secrecy.d->mType = Secrecy::Confidential; 0140 break; 0141 default: 0142 secrecy.d->mType = Secrecy::Invalid; 0143 break; 0144 } 0145 0146 return s; 0147 }