kabc
key.cpp
00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "key.h" 00022 00023 #include <klocale.h> 00024 #include <krandom.h> 00025 00026 #include <QtCore/QDataStream> 00027 #include <QtCore/QSharedData> 00028 00029 using namespace KABC; 00030 00031 class Key::Private : public QSharedData 00032 { 00033 public: 00034 Private() 00035 : mId( KRandom::randomString( 8 ) ) 00036 { 00037 } 00038 00039 Private( const Private &other ) 00040 : QSharedData( other ) 00041 { 00042 mId = other.mId; 00043 mBinaryData = other.mBinaryData; 00044 mTextData = other.mTextData; 00045 mCustomTypeString = other.mCustomTypeString; 00046 mIsBinary = other.mIsBinary; 00047 mType = other.mType; 00048 } 00049 00050 QString mId; 00051 QByteArray mBinaryData; 00052 QString mTextData; 00053 QString mCustomTypeString; 00054 00055 bool mIsBinary; 00056 Type mType; 00057 }; 00058 00059 Key::Key( const QString &text, Type type ) 00060 : d( new Private ) 00061 { 00062 d->mTextData = text; 00063 d->mIsBinary = false; 00064 d->mType = type; 00065 } 00066 00067 Key::Key( const Key &other ) 00068 : d( other.d ) 00069 { 00070 } 00071 00072 Key::~Key() 00073 { 00074 } 00075 00076 bool Key::operator==( const Key &other ) const 00077 { 00078 if ( d->mId != other.d->mId ) { 00079 return false; 00080 } 00081 00082 if ( d->mType != other.d->mType ) { 00083 return false; 00084 } 00085 00086 if ( d->mIsBinary != other.d->mIsBinary ) { 00087 return false; 00088 } 00089 00090 if ( d->mIsBinary ) { 00091 if ( d->mBinaryData != other.d->mBinaryData ) { 00092 return false; 00093 } 00094 } else { 00095 if ( d->mTextData != other.d->mTextData ) { 00096 return false; 00097 } 00098 } 00099 00100 if ( d->mCustomTypeString != other.d->mCustomTypeString ) { 00101 return false; 00102 } 00103 00104 return true; 00105 } 00106 00107 bool Key::operator!=( const Key &other ) const 00108 { 00109 return !( *this == other ); 00110 } 00111 00112 Key &Key::operator=( const Key &other ) 00113 { 00114 if ( this != &other ) { 00115 d = other.d; 00116 } 00117 00118 return *this; 00119 } 00120 00121 void Key::setId( const QString &id ) 00122 { 00123 d->mId = id; 00124 } 00125 00126 QString Key::id() const 00127 { 00128 return d->mId; 00129 } 00130 00131 void Key::setBinaryData( const QByteArray &binary ) 00132 { 00133 d->mBinaryData = binary; 00134 d->mIsBinary = true; 00135 } 00136 00137 QByteArray Key::binaryData() const 00138 { 00139 return d->mBinaryData; 00140 } 00141 00142 void Key::setTextData( const QString &text ) 00143 { 00144 d->mTextData = text; 00145 d->mIsBinary = false; 00146 } 00147 00148 QString Key::textData() const 00149 { 00150 return d->mTextData; 00151 } 00152 00153 bool Key::isBinary() const 00154 { 00155 return d->mIsBinary; 00156 } 00157 00158 void Key::setType( Type type ) 00159 { 00160 d->mType = type; 00161 } 00162 00163 void Key::setCustomTypeString( const QString &custom ) 00164 { 00165 d->mCustomTypeString = custom; 00166 } 00167 00168 Key::Type Key::type() const 00169 { 00170 return d->mType; 00171 } 00172 00173 QString Key::customTypeString() const 00174 { 00175 return d->mCustomTypeString; 00176 } 00177 00178 QString Key::toString() const 00179 { 00180 QString str; 00181 00182 str += QLatin1String( "Key {\n" ); 00183 str += QString::fromLatin1( " Id: %1\n" ).arg( d->mId ); 00184 str += QString::fromLatin1( " Type: %1\n" ).arg( typeLabel( d->mType ) ); 00185 if ( d->mType == Custom ) { 00186 str += QString::fromLatin1( " CustomType: %1\n" ).arg( d->mCustomTypeString ); 00187 } 00188 str += QString::fromLatin1( " IsBinary: %1\n" ). 00189 arg( d->mIsBinary ? QLatin1String( "true" ) : QLatin1String( "false" ) ); 00190 if ( d->mIsBinary ) { 00191 str += QString::fromLatin1( " Binary: %1\n" ). 00192 arg( QString::fromLatin1( d->mBinaryData.toBase64() ) ); 00193 } else { 00194 str += QString::fromLatin1( " Text: %1\n" ).arg( d->mTextData ); 00195 } 00196 str += QLatin1String( "}\n" ); 00197 00198 return str; 00199 } 00200 00201 Key::TypeList Key::typeList() 00202 { 00203 static TypeList list; 00204 00205 if ( list.isEmpty() ) { 00206 list << X509 << PGP << Custom; 00207 } 00208 00209 return list; 00210 } 00211 00212 QString Key::typeLabel( Type type ) 00213 { 00214 switch ( type ) { 00215 case X509: 00216 return i18nc( "X.509 public key", "X509" ); 00217 break; 00218 case PGP: 00219 return i18nc( "Pretty Good Privacy key", "PGP" ); 00220 break; 00221 case Custom: 00222 return i18nc( "A custom key", "Custom" ); 00223 break; 00224 default: 00225 return i18nc( "another type of encryption key", "Unknown type" ); 00226 break; 00227 } 00228 } 00229 00230 QDataStream &KABC::operator<<( QDataStream &s, const Key &key ) 00231 { 00232 return s << key.d->mId << key.d->mType << key.d->mIsBinary << key.d->mBinaryData 00233 << key.d->mTextData << key.d->mCustomTypeString; 00234 } 00235 00236 QDataStream &KABC::operator>>( QDataStream &s, Key &key ) 00237 { 00238 uint type; 00239 s >> key.d->mId >> type >> key.d->mIsBinary >> key.d->mBinaryData >> key.d->mTextData 00240 >> key.d->mCustomTypeString; 00241 00242 key.d->mType = Key::Type( type ); 00243 00244 return s; 00245 }