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 "secrecy.h"
9
10#include <KLocalizedString>
11
12#include <QDataStream>
13#include <QSharedData>
14
15using namespace KContacts;
16
17class Q_DECL_HIDDEN Secrecy::PrivateData : public QSharedData
18{
19public:
20 PrivateData()
21 : mType(Secrecy::Invalid)
22 {
23 }
24
25 PrivateData(const PrivateData &other)
26 : QSharedData(other)
27 {
28 mType = other.mType;
29 }
30
31 Type mType;
32};
33
34Secrecy::Secrecy(Type type)
35 : d(new PrivateData)
36{
37 d->mType = type;
38}
39
40Secrecy::Secrecy(const Secrecy &other)
41 : d(other.d)
42{
43}
44
45Secrecy::~Secrecy() = default;
46
47Secrecy &Secrecy::operator=(const Secrecy &other)
48{
49 if (this != &other) {
50 d = other.d;
51 }
52
53 return *this;
54}
55
56bool Secrecy::operator==(const Secrecy &other) const
57{
58 return d->mType == other.d->mType;
59}
60
61bool Secrecy::operator!=(const Secrecy &other) const
62{
63 return !(*this == other);
64}
65
66bool Secrecy::isValid() const
67{
68 return d->mType != Invalid;
69}
70
71void Secrecy::setType(Type type)
72{
73 d->mType = type;
74}
75
76Secrecy::Type Secrecy::type() const
77{
78 return d->mType;
79}
80
81Secrecy::TypeList Secrecy::typeList()
82{
83 static TypeList list;
84
85 if (list.isEmpty()) {
86 list << Public << Private << Confidential;
87 }
88
89 return list;
90}
91
92QString Secrecy::typeLabel(Type type)
93{
94 switch (type) {
95 case Public:
96 return i18nc("access is for everyone", "Public");
97 break;
98 case Private:
99 return i18nc("access is by owner only", "Private");
100 break;
101 case Confidential:
102 return i18nc("access is by owner and a controlled group", "Confidential");
103 break;
104 default:
105 return i18nc("unknown secrecy type", "Unknown type");
106 break;
107 }
108}
109
110QString Secrecy::toString() const
111{
112 QString str = QLatin1String("Secrecy {\n");
113 str += QStringLiteral(" Type: %1\n").arg(a: typeLabel(type: d->mType));
114 str += QLatin1String("}\n");
115
116 return str;
117}
118
119QDataStream &KContacts::operator<<(QDataStream &s, const Secrecy &secrecy)
120{
121 return s << (uint)secrecy.d->mType;
122}
123
124QDataStream &KContacts::operator>>(QDataStream &s, Secrecy &secrecy)
125{
126 uint type;
127 s >> type;
128
129 switch (type) {
130 case 0:
131 secrecy.d->mType = Secrecy::Public;
132 break;
133 case 1:
134 secrecy.d->mType = Secrecy::Private;
135 break;
136 case 2:
137 secrecy.d->mType = Secrecy::Confidential;
138 break;
139 default:
140 secrecy.d->mType = Secrecy::Invalid;
141 break;
142 }
143
144 return s;
145}
146

source code of kcontacts/src/secrecy.cpp