• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

libkpgp

kpgpkey.cpp

Go to the documentation of this file.
00001 /*
00002     kpgpkey.cpp
00003 
00004     Copyright (C) 2001,2002 the KPGP authors
00005     See file AUTHORS.kpgp for details
00006 
00007     This file is part of KPGP, the KDE PGP/GnuPG support library.
00008 
00009     KPGP is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software Foundation,
00016     Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00017  */
00018 
00019 #include "kpgpkey.h"
00020 #include "kdebug.h"
00021 //Added by qt3to4:
00022 #include <QByteArray>
00023 
00024 namespace Kpgp {
00025 
00026 /* member functions of Kpgp::KeyIDList --------------------------------- */
00027 
00030 QStringList KeyIDList::toStringList() const
00031 {
00032   QStringList res;
00033   for( KeyIDList::ConstIterator it = begin(); it != end(); ++it ) {
00034     res << (*it).data();
00035   }
00036   return res;
00037 }
00038 
00041 KeyIDList KeyIDList::fromStringList( const QStringList& l )
00042 {
00043   KeyIDList res;
00044   for( QStringList::ConstIterator it = l.begin(); it != l.end(); ++it ) {
00045     res << (*it).toLocal8Bit();
00046   }
00047   return res;
00048 }
00049 
00050 /* member functions of Kpgp::UserID ------------------------------------ */
00051 
00052 UserID::UserID(const QString& str, const Validity validity,
00053                const bool revoked, const bool invalid)
00054 {
00055   mText = str;
00056   mValidity = validity;
00057   mRevoked = revoked;
00058   mInvalid = invalid;
00059 }
00060 
00061 
00062 /* member functions of Kpgp::Subkey ------------------------------------ */
00063 
00064 Subkey::Subkey(const KeyID& keyID, const bool secret)
00065 {
00066   mSecret = secret;
00067   mKeyID = keyID;
00068 
00069   mRevoked = false;
00070   mExpired = false;
00071   mDisabled = false;
00072   mInvalid = false;
00073   mCanEncrypt = false;
00074   mCanSign = false;
00075   mCanCertify = false;
00076   mKeyAlgo = 0;
00077   mKeyLen = 0;
00078   mFingerprint = 0;
00079   mTimestamp = 0;
00080   mExpiration = 0;
00081 }
00082 
00083 
00084 /* member functions of Kpgp::Key --------------------------------------- */
00085 
00086 Key::Key(const KeyID& keyid, const QString& uid, const bool secret) :
00087   mSubkeys(), mUserIDs()
00088 {
00089   mSecret = secret;
00090   if (!keyid.isEmpty())
00091     addSubkey(keyid, secret);
00092   if (!uid.isEmpty())
00093     addUserID(uid);
00094 
00095   mRevoked = false;
00096   mExpired = false;
00097   mDisabled = false;
00098   mInvalid = false;
00099   mCanEncrypt = false;
00100   mCanSign = false;
00101   mCanCertify = false;
00102 
00103   mEncryptPref = UnknownEncryptPref;
00104 }
00105 
00106 Key::~Key()
00107 {
00108   //kDebug( 5326 ) <<"Kpgp::Key: Deleting key" << primaryUserID();
00109   mUserIDs.setAutoDelete(true);
00110   mUserIDs.clear();
00111   mSubkeys.setAutoDelete(true);
00112   mSubkeys.clear();
00113 }
00114 
00115 void
00116 Key::clear()
00117 {
00118   mSecret = false;
00119   mRevoked = false;
00120   mExpired = false;
00121   mDisabled = false;
00122   mInvalid = false;
00123   mCanEncrypt = false;
00124   mCanSign = false;
00125   mCanCertify = false;
00126 
00127   mEncryptPref = UnknownEncryptPref;
00128 
00129   mSubkeys.setAutoDelete(true);
00130   mSubkeys.clear();
00131   mUserIDs.setAutoDelete(true);
00132   mUserIDs.clear();
00133 }
00134 
00135 Validity
00136 Key::keyTrust() const
00137 {
00138   Validity trust = KPGP_VALIDITY_UNKNOWN;
00139 
00140   for( UserIDListIterator it(mUserIDs); it.current(); ++it )
00141   {
00142     if( (*it)->validity() > trust )
00143       trust = (*it)->validity();
00144   }
00145   
00146   return trust;
00147 }
00148 
00149 Validity
00150 Key::keyTrust( const QString& uid ) const
00151 {
00152   Validity trust = KPGP_VALIDITY_UNKNOWN;
00153 
00154   if( uid.isEmpty() )
00155     return trust;
00156 
00157   for( UserIDListIterator it(mUserIDs); it.current(); ++it )
00158   {
00159     if( (*it)->text() == uid )
00160       trust = (*it)->validity();
00161   }
00162   
00163   return trust;
00164 }
00165 
00166 void
00167 Key::cloneKeyTrust( const Key* key )
00168 {
00169   if( !key )
00170     return;
00171 
00172   for( UserIDListIterator it(mUserIDs); it.current(); ++it )
00173   {
00174     (*it)->setValidity( key->keyTrust( (*it)->text() ) );
00175   }
00176 }
00177 
00178 bool
00179 Key::isValid() const
00180 {
00181   return ( !mRevoked && !mExpired && !mDisabled && !mInvalid );
00182 }
00183 
00184 
00185 bool
00186 Key::isValidEncryptionKey() const
00187 {
00188   return ( !mRevoked && !mExpired && !mDisabled && !mInvalid && mCanEncrypt );
00189 }
00190 
00191 
00192 bool
00193 Key::isValidSigningKey() const
00194 {
00195   return ( !mRevoked && !mExpired && !mDisabled && !mInvalid && mCanSign );
00196 }
00197 
00198 
00199 void Key::addUserID(const QString &uid, const Validity validity,
00200                     const bool revoked, const bool invalid)
00201 { 
00202   if (!uid.isEmpty()) {
00203     UserID *userID = new UserID(uid, validity, revoked, invalid);
00204     mUserIDs.append(userID);
00205   }
00206 }
00207 
00208 bool Key::matchesUserID(const QString& str, bool cs)
00209 {
00210   if (str.isEmpty() || mUserIDs.isEmpty())
00211     return false;
00212   
00213   for (UserIDListIterator it(mUserIDs); it.current(); ++it) {
00214     if (((*it)->text().indexOf(str, 0, cs?Qt::CaseSensitive:Qt::CaseInsensitive)) != -1)
00215       return true;
00216   }
00217 
00218   return false;
00219 }
00220 
00221 void Key::addSubkey(const KeyID& keyID, const bool secret)
00222 {
00223   if (!keyID.isEmpty()) {
00224     Subkey *key = new Subkey(keyID, secret);
00225     mSubkeys.append(key);
00226   }
00227 }
00228 
00229 Subkey *Key::getSubkey(const KeyID& keyID)
00230 {
00231   if (keyID.isEmpty() || mSubkeys.isEmpty())
00232     return 0;
00233   
00234   // is the given key ID a long (16 chars) or a short (8 chars) key ID?
00235   bool longKeyID = (keyID.length() == 16);
00236 
00237   for (SubkeyListIterator it(mSubkeys); it.current(); ++it) {
00238     if (longKeyID) {
00239       if ((*it)->longKeyID() == keyID)
00240         return (*it);
00241     }
00242     else {
00243       if ((*it)->keyID() == keyID)
00244         return (*it);
00245     }
00246   }
00247 
00248   return 0;
00249 }
00250 
00251 void Key::setFingerprint(const KeyID& keyID, const QByteArray &fpr)
00252 {
00253   Subkey *key;
00254   if ((key = getSubkey(keyID)) != 0) {
00255     key->setFingerprint(fpr);
00256   }
00257   else
00258     kDebug(5326) <<"Error: Can't set fingerprint. A subkey with key ID 0x"
00259                   << keyID << "doesn't exist.";
00260 }
00261 
00262 } // namespace Kpgp

libkpgp

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

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal