1 | /* |
2 | This file is part of libkabc. |
3 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. |
9 | |
10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "secrecy.h" |
22 | |
23 | #include <klocalizedstring.h> |
24 | |
25 | #include <QtCore/QDataStream> |
26 | #include <QtCore/QSharedData> |
27 | |
28 | using namespace KABC; |
29 | |
30 | class Secrecy::PrivateData : public QSharedData |
31 | { |
32 | public: |
33 | PrivateData() |
34 | : mType( Secrecy::Invalid ) |
35 | { |
36 | } |
37 | |
38 | PrivateData( const PrivateData &other ) |
39 | : QSharedData( other ) |
40 | { |
41 | mType = other.mType; |
42 | } |
43 | |
44 | Type mType; |
45 | }; |
46 | |
47 | Secrecy::Secrecy( Type type ) |
48 | : d( new PrivateData ) |
49 | { |
50 | d->mType = type; |
51 | } |
52 | |
53 | Secrecy::Secrecy( const Secrecy &other ) |
54 | : d( other.d ) |
55 | { |
56 | } |
57 | |
58 | Secrecy::~Secrecy() |
59 | { |
60 | } |
61 | |
62 | Secrecy &Secrecy::operator=( const Secrecy &other ) |
63 | { |
64 | if ( this != &other ) { |
65 | d = other.d; |
66 | } |
67 | |
68 | return *this; |
69 | } |
70 | |
71 | bool Secrecy::operator==( const Secrecy &other ) const |
72 | { |
73 | return d->mType == other.d->mType; |
74 | } |
75 | |
76 | bool Secrecy::operator!=( const Secrecy &other ) const |
77 | { |
78 | return !( *this == other ); |
79 | } |
80 | |
81 | bool Secrecy::isValid() const |
82 | { |
83 | return d->mType != Invalid; |
84 | } |
85 | |
86 | void Secrecy::setType( Type type ) |
87 | { |
88 | d->mType = type; |
89 | } |
90 | |
91 | Secrecy::Type Secrecy::type() const |
92 | { |
93 | return d->mType; |
94 | } |
95 | |
96 | Secrecy::TypeList Secrecy::typeList() |
97 | { |
98 | static TypeList list; |
99 | |
100 | if ( list.isEmpty() ) { |
101 | list << Public << Private << Confidential; |
102 | } |
103 | |
104 | return list; |
105 | } |
106 | |
107 | QString Secrecy::typeLabel( Type type ) |
108 | { |
109 | switch ( type ) { |
110 | case Public: |
111 | return i18nc( "access is for everyone" , "Public" ); |
112 | break; |
113 | case Private: |
114 | return i18nc( "access is by owner only" , "Private" ); |
115 | break; |
116 | case Confidential: |
117 | return i18nc( "access is by owner and a controlled group" , "Confidential" ); |
118 | break; |
119 | default: |
120 | return i18nc( "unknown secrecy type" , "Unknown type" ); |
121 | break; |
122 | } |
123 | } |
124 | |
125 | QString Secrecy::toString() const |
126 | { |
127 | QString str; |
128 | |
129 | str += QString::fromLatin1( "Secrecy {\n" ); |
130 | str += QString::fromLatin1( " Type: %1\n" ).arg( typeLabel( d->mType ) ); |
131 | str += QString::fromLatin1( "}\n" ); |
132 | |
133 | return str; |
134 | } |
135 | |
136 | QDataStream &KABC::operator<<( QDataStream &s, const Secrecy &secrecy ) |
137 | { |
138 | return s << (uint)secrecy.d->mType; |
139 | } |
140 | |
141 | QDataStream &KABC::operator>>( QDataStream &s, Secrecy &secrecy ) |
142 | { |
143 | uint type; |
144 | s >> type; |
145 | |
146 | switch ( type ) { |
147 | case 0: |
148 | secrecy.d->mType = Secrecy::Public; |
149 | break; |
150 | case 1: |
151 | secrecy.d->mType = Secrecy::Private; |
152 | break; |
153 | case 2: |
154 | secrecy.d->mType = Secrecy::Confidential; |
155 | break; |
156 | default: |
157 | secrecy.d->mType = Secrecy::Invalid; |
158 | break; |
159 | } |
160 | |
161 | return s; |
162 | } |
163 | |