one entry for korganizer in khelpcenters navigation tree is enough
[kdepim.git] / libkpgp / kpgpkey.cpp
bloba5f8ef2fd68ac6b6b92a08dbec7394c77e900b00
1 /*
2 kpgpkey.cpp
4 Copyright (C) 2001,2002 the KPGP authors
5 See file AUTHORS.kpgp for details
7 This file is part of KPGP, the KDE PGP/GnuPG support library.
9 KPGP is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "kpgpkey.h"
20 #include "kdebug.h"
21 //Added by qt3to4:
22 #include <QByteArray>
24 namespace Kpgp {
26 /* member functions of Kpgp::KeyIDList --------------------------------- */
28 /** Converts from a KeyIDList to a QStringList.
30 QStringList KeyIDList::toStringList() const
32 QStringList res;
33 for( KeyIDList::ConstIterator it = begin(); it != end(); ++it ) {
34 res << (*it).data();
36 return res;
39 /** Converts from a QStringList to a KeyIDList.
41 KeyIDList KeyIDList::fromStringList( const QStringList& l )
43 KeyIDList res;
44 for( QStringList::ConstIterator it = l.begin(); it != l.end(); ++it ) {
45 res << (*it).toLocal8Bit();
47 return res;
50 /* member functions of Kpgp::UserID ------------------------------------ */
52 UserID::UserID(const QString& str, const Validity validity,
53 const bool revoked, const bool invalid)
55 mText = str;
56 mValidity = validity;
57 mRevoked = revoked;
58 mInvalid = invalid;
62 /* member functions of Kpgp::Subkey ------------------------------------ */
64 Subkey::Subkey(const KeyID& keyID, const bool secret)
66 mSecret = secret;
67 mKeyID = keyID;
69 mRevoked = false;
70 mExpired = false;
71 mDisabled = false;
72 mInvalid = false;
73 mCanEncrypt = false;
74 mCanSign = false;
75 mCanCertify = false;
76 mKeyAlgo = 0;
77 mKeyLen = 0;
78 mFingerprint = 0;
79 mTimestamp = 0;
80 mExpiration = 0;
84 /* member functions of Kpgp::Key --------------------------------------- */
86 Key::Key(const KeyID& keyid, const QString& uid, const bool secret) :
87 mSubkeys(), mUserIDs()
89 mSecret = secret;
90 if (!keyid.isEmpty())
91 addSubkey(keyid, secret);
92 if (!uid.isEmpty())
93 addUserID(uid);
95 mRevoked = false;
96 mExpired = false;
97 mDisabled = false;
98 mInvalid = false;
99 mCanEncrypt = false;
100 mCanSign = false;
101 mCanCertify = false;
103 mEncryptPref = UnknownEncryptPref;
106 Key::~Key()
108 //kDebug( 5326 ) <<"Kpgp::Key: Deleting key" << primaryUserID();
109 qDeleteAll( mUserIDs );
110 mUserIDs.clear();
111 qDeleteAll( mSubkeys );
112 mSubkeys.clear();
115 void
116 Key::clear()
118 mSecret = false;
119 mRevoked = false;
120 mExpired = false;
121 mDisabled = false;
122 mInvalid = false;
123 mCanEncrypt = false;
124 mCanSign = false;
125 mCanCertify = false;
127 mEncryptPref = UnknownEncryptPref;
129 qDeleteAll( mSubkeys );
130 mSubkeys.clear();
131 qDeleteAll( mUserIDs );
132 mUserIDs.clear();
135 Validity
136 Key::keyTrust() const
138 Validity trust = KPGP_VALIDITY_UNKNOWN;
140 foreach ( UserID* userId, mUserIDs )
142 if( userId->validity() > trust )
143 trust = userId->validity();
146 return trust;
149 Validity
150 Key::keyTrust( const QString& uid ) const
152 Validity trust = KPGP_VALIDITY_UNKNOWN;
154 if( uid.isEmpty() )
155 return trust;
157 foreach ( UserID* userId, mUserIDs )
159 if( userId->text() == uid )
160 trust = userId->validity();
163 return trust;
166 void
167 Key::cloneKeyTrust( const Key* key )
169 if( !key )
170 return;
172 foreach ( UserID* userId, mUserIDs )
174 userId->setValidity( key->keyTrust( userId->text() ) );
178 bool
179 Key::isValid() const
181 return ( !mRevoked && !mExpired && !mDisabled && !mInvalid );
185 bool
186 Key::isValidEncryptionKey() const
188 return ( !mRevoked && !mExpired && !mDisabled && !mInvalid && mCanEncrypt );
192 bool
193 Key::isValidSigningKey() const
195 return ( !mRevoked && !mExpired && !mDisabled && !mInvalid && mCanSign );
199 void Key::addUserID(const QString &uid, const Validity validity,
200 const bool revoked, const bool invalid)
202 if (!uid.isEmpty()) {
203 UserID *userID = new UserID(uid, validity, revoked, invalid);
204 mUserIDs.append(userID);
208 bool Key::matchesUserID(const QString& str, bool cs)
210 if (str.isEmpty() || mUserIDs.isEmpty())
211 return false;
213 foreach ( UserID *userId, mUserIDs ) {
214 if ((userId->text().indexOf(str, 0, cs?Qt::CaseSensitive:Qt::CaseInsensitive)) != -1)
215 return true;
218 return false;
221 void Key::addSubkey(const KeyID& keyID, const bool secret)
223 if (!keyID.isEmpty()) {
224 Subkey *key = new Subkey(keyID, secret);
225 mSubkeys.append(key);
229 Subkey *Key::getSubkey(const KeyID& keyID)
231 if (keyID.isEmpty() || mSubkeys.isEmpty())
232 return 0;
234 // is the given key ID a long (16 chars) or a short (8 chars) key ID?
235 bool longKeyID = (keyID.length() == 16);
237 foreach ( Subkey* subKey, mSubkeys ) {
238 if (longKeyID) {
239 if (subKey->longKeyID() == keyID)
240 return subKey;
242 else {
243 if (subKey->keyID() == keyID)
244 return subKey;
248 return 0;
251 void Key::setFingerprint(const KeyID& keyID, const QByteArray &fpr)
253 Subkey *key;
254 if ((key = getSubkey(keyID)) != 0) {
255 key->setFingerprint(fpr);
257 else
258 kDebug(5326) <<"Error: Can't set fingerprint. A subkey with key ID 0x"
259 << keyID << "doesn't exist.";
262 } // namespace Kpgp