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 "key.h"
22
23#include <klocalizedstring.h>
24#include <krandom.h>
25
26#include <QtCore/QDataStream>
27#include <QtCore/QSharedData>
28
29using namespace KABC;
30
31class Key::Private : public QSharedData
32{
33 public:
34 Private()
35 : mId( KRandom::randomString( 8 ) )
36 {
37 }
38
39 Private( const Private &other )
40 : QSharedData( other )
41 {
42 mId = other.mId;
43 mBinaryData = other.mBinaryData;
44 mTextData = other.mTextData;
45 mCustomTypeString = other.mCustomTypeString;
46 mIsBinary = other.mIsBinary;
47 mType = other.mType;
48 }
49
50 QString mId;
51 QByteArray mBinaryData;
52 QString mTextData;
53 QString mCustomTypeString;
54
55 bool mIsBinary;
56 Type mType;
57};
58
59Key::Key( const QString &text, Type type )
60 : d( new Private )
61{
62 d->mTextData = text;
63 d->mIsBinary = false;
64 d->mType = type;
65}
66
67Key::Key( const Key &other )
68 : d( other.d )
69{
70}
71
72Key::~Key()
73{
74}
75
76bool Key::operator==( const Key &other ) const
77{
78 if ( d->mId != other.d->mId ) {
79 return false;
80 }
81
82 if ( d->mType != other.d->mType ) {
83 return false;
84 }
85
86 if ( d->mIsBinary != other.d->mIsBinary ) {
87 return false;
88 }
89
90 if ( d->mIsBinary ) {
91 if ( d->mBinaryData != other.d->mBinaryData ) {
92 return false;
93 }
94 } else {
95 if ( d->mTextData != other.d->mTextData ) {
96 return false;
97 }
98 }
99
100 if ( d->mCustomTypeString != other.d->mCustomTypeString ) {
101 return false;
102 }
103
104 return true;
105}
106
107bool Key::operator!=( const Key &other ) const
108{
109 return !( *this == other );
110}
111
112Key &Key::operator=( const Key &other )
113{
114 if ( this != &other ) {
115 d = other.d;
116 }
117
118 return *this;
119}
120
121void Key::setId( const QString &id )
122{
123 d->mId = id;
124}
125
126QString Key::id() const
127{
128 return d->mId;
129}
130
131void Key::setBinaryData( const QByteArray &binary )
132{
133 d->mBinaryData = binary;
134 d->mIsBinary = true;
135}
136
137QByteArray Key::binaryData() const
138{
139 return d->mBinaryData;
140}
141
142void Key::setTextData( const QString &text )
143{
144 d->mTextData = text;
145 d->mIsBinary = false;
146}
147
148QString Key::textData() const
149{
150 return d->mTextData;
151}
152
153bool Key::isBinary() const
154{
155 return d->mIsBinary;
156}
157
158void Key::setType( Type type )
159{
160 d->mType = type;
161}
162
163void Key::setCustomTypeString( const QString &custom )
164{
165 d->mCustomTypeString = custom;
166}
167
168Key::Type Key::type() const
169{
170 return d->mType;
171}
172
173QString Key::customTypeString() const
174{
175 return d->mCustomTypeString;
176}
177
178QString Key::toString() const
179{
180 QString str;
181
182 str += QLatin1String( "Key {\n" );
183 str += QString::fromLatin1( " Id: %1\n" ).arg( d->mId );
184 str += QString::fromLatin1( " Type: %1\n" ).arg( typeLabel( d->mType ) );
185 if ( d->mType == Custom ) {
186 str += QString::fromLatin1( " CustomType: %1\n" ).arg( d->mCustomTypeString );
187 }
188 str += QString::fromLatin1( " IsBinary: %1\n" ).
189 arg( d->mIsBinary ? QLatin1String( "true" ) : QLatin1String( "false" ) );
190 if ( d->mIsBinary ) {
191 str += QString::fromLatin1( " Binary: %1\n" ).
192 arg( QString::fromLatin1( d->mBinaryData.toBase64() ) );
193 } else {
194 str += QString::fromLatin1( " Text: %1\n" ).arg( d->mTextData );
195 }
196 str += QLatin1String( "}\n" );
197
198 return str;
199}
200
201Key::TypeList Key::typeList()
202{
203 static TypeList list;
204
205 if ( list.isEmpty() ) {
206 list << X509 << PGP << Custom;
207 }
208
209 return list;
210}
211
212QString Key::typeLabel( Type type )
213{
214 switch ( type ) {
215 case X509:
216 return i18nc( "X.509 public key", "X509" );
217 break;
218 case PGP:
219 return i18nc( "Pretty Good Privacy key", "PGP" );
220 break;
221 case Custom:
222 return i18nc( "A custom key", "Custom" );
223 break;
224 default:
225 return i18nc( "another type of encryption key", "Unknown type" );
226 break;
227 }
228}
229
230QDataStream &KABC::operator<<( QDataStream &s, const Key &key )
231{
232 return s << key.d->mId << key.d->mType << key.d->mIsBinary << key.d->mBinaryData
233 << key.d->mTextData << key.d->mCustomTypeString;
234}
235
236QDataStream &KABC::operator>>( QDataStream &s, Key &key )
237{
238 uint type;
239 s >> key.d->mId >> type >> key.d->mIsBinary >> key.d->mBinaryData >> key.d->mTextData
240 >> key.d->mCustomTypeString;
241
242 key.d->mType = Key::Type( type );
243
244 return s;
245}
246