• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

kabc

  • sources
  • kde-4.12
  • kdepimlibs
  • kabc
key.cpp
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 
29 using namespace KABC;
30 
31 class 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 
59 Key::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 
67 Key::Key( const Key &other )
68  : d( other.d )
69 {
70 }
71 
72 Key::~Key()
73 {
74 }
75 
76 bool 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 
107 bool Key::operator!=( const Key &other ) const
108 {
109  return !( *this == other );
110 }
111 
112 Key &Key::operator=( const Key &other )
113 {
114  if ( this != &other ) {
115  d = other.d;
116  }
117 
118  return *this;
119 }
120 
121 void Key::setId( const QString &id )
122 {
123  d->mId = id;
124 }
125 
126 QString Key::id() const
127 {
128  return d->mId;
129 }
130 
131 void Key::setBinaryData( const QByteArray &binary )
132 {
133  d->mBinaryData = binary;
134  d->mIsBinary = true;
135 }
136 
137 QByteArray Key::binaryData() const
138 {
139  return d->mBinaryData;
140 }
141 
142 void Key::setTextData( const QString &text )
143 {
144  d->mTextData = text;
145  d->mIsBinary = false;
146 }
147 
148 QString Key::textData() const
149 {
150  return d->mTextData;
151 }
152 
153 bool Key::isBinary() const
154 {
155  return d->mIsBinary;
156 }
157 
158 void Key::setType( Type type )
159 {
160  d->mType = type;
161 }
162 
163 void Key::setCustomTypeString( const QString &custom )
164 {
165  d->mCustomTypeString = custom;
166 }
167 
168 Key::Type Key::type() const
169 {
170  return d->mType;
171 }
172 
173 QString Key::customTypeString() const
174 {
175  return d->mCustomTypeString;
176 }
177 
178 QString 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 
201 Key::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 
212 QString 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 
230 QDataStream &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 
236 QDataStream &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 }
KABC::Key::setId
void setId(const QString &identifier)
Sets the unique identifier.
Definition: key.cpp:121
KABC::Key::Key
Key(const QString &text=QString(), Type type=PGP)
Creates a new key.
Definition: key.cpp:59
KABC::Key::textData
QString textData() const
Returns the text data.
Definition: key.cpp:148
KABC::Key::PGP
Pretty Good Privacy key.
Definition: key.h:50
KABC::Key::setBinaryData
void setBinaryData(const QByteArray &data)
Sets binary data.
Definition: key.cpp:131
KABC::Key::setTextData
void setTextData(const QString &data)
Sets text data.
Definition: key.cpp:142
KABC::Key::typeList
static TypeList typeList()
Returns a list of all available key types.
Definition: key.cpp:201
KABC::Key::TypeList
QList< Type > TypeList
List of key types.
Definition: key.h:57
KABC::Key::binaryData
QByteArray binaryData() const
Returns the binary data.
Definition: key.cpp:137
KABC::Key
A class to store an encryption key.
Definition: key.h:34
KABC::Key::isBinary
bool isBinary() const
Returns whether the key contains binary or text data.
Definition: key.cpp:153
KABC::Key::X509
X509 key.
Definition: key.h:49
KABC::Key::Custom
Custom or IANA conform key.
Definition: key.h:51
KABC::Key::Type
Type
Key types.
Definition: key.h:48
KABC::Key::typeLabel
static QString typeLabel(Type type)
Returns a translated label for a given key type.
Definition: key.cpp:212
KABC::Key::toString
QString toString() const
Returns a string representation of the key.
Definition: key.cpp:178
KABC::Key::setCustomTypeString
void setCustomTypeString(const QString &type)
Sets custom type string.
Definition: key.cpp:163
KABC::Key::setType
void setType(Type type)
Sets the type.
Definition: key.cpp:158
KABC::Key::id
QString id() const
Returns the unique identifier.
Definition: key.cpp:126
KABC::Key::operator=
Key & operator=(const Key &other)
Assignment operator.
Definition: key.cpp:112
KABC::Key::customTypeString
QString customTypeString() const
Returns the custom type string.
Definition: key.cpp:173
KABC::Key::type
Type type() const
Returns the type, see Type.
Definition: key.cpp:168
KABC::Key::~Key
~Key()
Destroys the key.
Definition: key.cpp:72
KABC::Key::operator!=
bool operator!=(const Key &) const
Not-equal operator.
Definition: key.cpp:107
KABC::Key::operator==
bool operator==(const Key &) const
Equality operator.
Definition: key.cpp:76
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:01:05 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal