00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "identity.h"
00022 #include "signature.h"
00023
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026 #include <kmessagebox.h>
00027 #include <kconfiggroup.h>
00028 #include <kurl.h>
00029 #include <kprocess.h>
00030 #include <kpimutils/kfileio.h>
00031
00032 #include <QFileInfo>
00033 #include <QMimeData>
00034 #include <QByteArray>
00035
00036 #include <sys/types.h>
00037 #include <stdlib.h>
00038 #include <stdio.h>
00039 #include <errno.h>
00040 #include <assert.h>
00041
00042 using namespace KPIMIdentities;
00043
00044
00045 static Identity *identityNull = 0;
00046
00047 Identity::Identity( const QString &id, const QString &fullName,
00048 const QString &emailAddr, const QString &organization,
00049 const QString &replyToAddr )
00050 : mIsDefault( false )
00051 {
00052 setProperty( s_uoid, 0 );
00053 setProperty( s_identity, id );
00054 setProperty( s_name, fullName );
00055 setProperty( s_email, emailAddr );
00056 setProperty( s_organization, organization );
00057 setProperty( s_replyto, replyToAddr );
00058 }
00059
00060 Identity::~Identity()
00061 {}
00062
00063 const Identity &Identity::null()
00064 {
00065 if ( !identityNull ) {
00066 identityNull = new Identity;
00067 }
00068 return *identityNull;
00069 }
00070
00071 bool Identity::isNull() const
00072 {
00073 bool empty = true;
00074 QHash<QString, QVariant>::const_iterator i = mPropertiesMap.constBegin();
00075 while ( i != mPropertiesMap.constEnd() ) {
00076
00077
00078 if ( !( i.key() == s_uoid && i.value().toUInt() == 0 ) ) {
00079 if ( !i.value().isNull() ||
00080 ( i.value().type() == QVariant::String && !i.value().toString().isEmpty() ) ) {
00081 empty = false;
00082 }
00083 }
00084 ++i;
00085 }
00086 return empty;
00087 }
00088
00089 void Identity::readConfig( const KConfigGroup &config )
00090 {
00091
00092 QMap<QString,QString> entries = config.entryMap();
00093 QMap<QString,QString>::const_iterator i = entries.constBegin();
00094 while ( i != entries.constEnd() ) {
00095 mPropertiesMap.insert( i.key(), i.value() );
00096 ++i;
00097 }
00098 mSignature.readConfig( config );
00099 }
00100
00101 void Identity::writeConfig( KConfigGroup &config ) const
00102 {
00103 QHash<QString, QVariant>::const_iterator i = mPropertiesMap.constBegin();
00104 while ( i != mPropertiesMap.constEnd() ) {
00105 config.writeEntry( i.key(), i.value() );
00106 kDebug( 5325 ) << "Store:" << i.key() << ":" << i.value();
00107 ++i;
00108 }
00109 mSignature.writeConfig( config );
00110 }
00111
00112 bool Identity::mailingAllowed() const
00113 {
00114 return !property( s_email ).toString().isEmpty();
00115 }
00116
00117 QString Identity::mimeDataType()
00118 {
00119 return "application/x-kmail-identity-drag";
00120 }
00121
00122 bool Identity::canDecode( const QMimeData*md )
00123 {
00124 return md->hasFormat( mimeDataType() );
00125 }
00126
00127 void Identity::populateMimeData( QMimeData*md )
00128 {
00129 QByteArray a;
00130 {
00131 QDataStream s( &a, QIODevice::WriteOnly );
00132 s << this;
00133 }
00134 md->setData( mimeDataType(), a );
00135 }
00136
00137 Identity Identity::fromMimeData( const QMimeData*md )
00138 {
00139 Identity i;
00140 if ( canDecode( md ) ) {
00141 QByteArray ba = md->data( mimeDataType() );
00142 QDataStream s( &ba, QIODevice::ReadOnly );
00143 s >> i;
00144 }
00145 return i;
00146 }
00147
00148
00149
00150 QDataStream &KPIMIdentities::operator<<
00151 ( QDataStream &stream, const KPIMIdentities::Identity &i )
00152 {
00153 return stream << static_cast<quint32>( i.uoid() )
00154 << i.identityName()
00155 << i.fullName()
00156 << i.organization()
00157 << i.pgpSigningKey()
00158 << i.pgpEncryptionKey()
00159 << i.smimeSigningKey()
00160 << i.smimeEncryptionKey()
00161 << i.emailAddr()
00162 << i.replyToAddr()
00163 << i.bcc()
00164 << i.vCardFile()
00165 << i.transport()
00166 << i.fcc()
00167 << i.drafts()
00168 << i.templates()
00169 << i.mPropertiesMap[s_signature]
00170 << i.dictionary()
00171 << i.xface()
00172 << i.preferredCryptoMessageFormat();
00173 }
00174
00175 QDataStream &KPIMIdentities::operator>>
00176 ( QDataStream &stream, KPIMIdentities::Identity &i )
00177 {
00178 quint32 uoid;
00179 QString format;
00180 stream
00181 >> uoid
00182 >> i.mPropertiesMap[s_identity]
00183 >> i.mPropertiesMap[s_name]
00184 >> i.mPropertiesMap[s_organization]
00185 >> i.mPropertiesMap[s_pgps]
00186 >> i.mPropertiesMap[s_pgpe]
00187 >> i.mPropertiesMap[s_smimes]
00188 >> i.mPropertiesMap[s_smimee]
00189 >> i.mPropertiesMap[s_email]
00190 >> i.mPropertiesMap[s_replyto]
00191 >> i.mPropertiesMap[s_bcc]
00192 >> i.mPropertiesMap[s_vcard]
00193 >> i.mPropertiesMap[s_transport]
00194 >> i.mPropertiesMap[s_fcc]
00195 >> i.mPropertiesMap[s_drafts]
00196 >> i.mPropertiesMap[s_templates]
00197 >> i.mPropertiesMap[s_signature]
00198 >> i.mPropertiesMap[s_dict]
00199 >> i.mPropertiesMap[s_xface]
00200 >> i.mPropertiesMap[s_prefcrypt];
00201 i.setProperty( s_uoid, uoid );
00202 return stream;
00203 }
00204
00205 bool Identity::operator< ( const Identity &other ) const
00206 {
00207 if ( isDefault() ) {
00208 return true;
00209 }
00210 if ( other.isDefault() ) {
00211 return false;
00212 }
00213 return identityName() < other.identityName();
00214 }
00215
00216 bool Identity::operator> ( const Identity &other ) const
00217 {
00218 if ( isDefault() ) {
00219 return false;
00220 }
00221 if ( other.isDefault() ) {
00222 return true;
00223 }
00224 return identityName() > other.identityName();
00225 }
00226
00227 bool Identity::operator<= ( const Identity &other ) const
00228 {
00229 return !operator> ( other );
00230 }
00231
00232 bool Identity::operator>= ( const Identity &other ) const
00233 {
00234 return !operator< ( other );
00235 }
00236
00237 bool Identity::operator== ( const Identity &other ) const
00238 {
00239 return mPropertiesMap == other.mPropertiesMap &&
00240 mSignature == other.mSignature;
00241 }
00242
00243 bool Identity::operator!= ( const Identity &other ) const
00244 {
00245 return !operator== ( other );
00246 }
00247
00248
00249
00250 QVariant Identity::property( const QString &key ) const
00251 {
00252 return mPropertiesMap.value( key );
00253 }
00254
00255 QString Identity::fullEmailAddr( void ) const
00256 {
00257 const QString name = mPropertiesMap.value( s_name ).toString();
00258 const QString mail = mPropertiesMap.value( s_email ).toString();
00259
00260 if ( name.isEmpty() ) {
00261 return mail;
00262 }
00263
00264 const QString specials( "()<>@,.;:[]" );
00265
00266 QString result;
00267
00268
00269 bool needsQuotes=false;
00270 for ( int i=0; i < name.length(); i++ ) {
00271 if ( specials.contains( name[i] ) ) {
00272 needsQuotes = true;
00273 } else if ( name[i] == '\\' || name[i] == '"' ) {
00274 needsQuotes = true;
00275 result += '\\';
00276 }
00277 result += name[i];
00278 }
00279
00280 if ( needsQuotes ) {
00281 result.insert( 0,'"' );
00282 result += '"';
00283 }
00284
00285 result += " <" + mail + '>';
00286
00287 return result;
00288 }
00289
00290 QString Identity::identityName() const
00291 {
00292 return property( QLatin1String( s_identity ) ).toString();
00293 }
00294
00295 QString Identity::signatureText( bool *ok ) const
00296 {
00297 return mSignature.withSeparator( ok );
00298 }
00299
00300 bool Identity::isDefault() const
00301 {
00302 return mIsDefault;
00303 }
00304
00305 uint Identity::uoid() const
00306 {
00307 return property( QLatin1String( s_uoid ) ).toInt();
00308 }
00309
00310 QString Identity::fullName() const
00311 {
00312 return property( QLatin1String( s_name ) ).toString();
00313 }
00314
00315 QString Identity::organization() const
00316 {
00317 return property( QLatin1String( s_organization ) ).toString();
00318 }
00319
00320 QByteArray Identity::pgpEncryptionKey() const
00321 {
00322 return property( QLatin1String( s_pgpe ) ).toByteArray();
00323 }
00324
00325 QByteArray Identity::pgpSigningKey() const
00326 {
00327 return property( QLatin1String( s_pgps ) ).toByteArray();
00328 }
00329
00330 QByteArray Identity::smimeEncryptionKey() const
00331 {
00332 return property( QLatin1String( s_smimee ) ).toByteArray();
00333 }
00334
00335 QByteArray Identity::smimeSigningKey() const
00336 {
00337 return property( QLatin1String( s_smimes ) ).toByteArray();
00338 }
00339
00340 QString Identity::preferredCryptoMessageFormat() const
00341 {
00342 return property( QLatin1String( s_prefcrypt ) ).toString();
00343 }
00344
00345 QString Identity::emailAddr() const
00346 {
00347 return property( QLatin1String( s_email ) ).toString();
00348 }
00349
00350 QString Identity::vCardFile() const
00351 {
00352 return property( QLatin1String( s_vcard ) ).toString();
00353 }
00354
00355 QString Identity::replyToAddr() const
00356 {
00357 return property( QLatin1String( s_replyto ) ).toString();
00358 }
00359
00360 QString Identity::bcc() const
00361 {
00362 return property( QLatin1String( s_bcc ) ).toString();
00363 }
00364
00365 Signature &Identity::signature()
00366 {
00367 return mSignature;
00368 }
00369
00370 bool Identity::isXFaceEnabled() const
00371 {
00372 return property( QLatin1String( s_xfaceenabled ) ).toBool();
00373 }
00374
00375 QString Identity::xface() const
00376 {
00377 return property( QLatin1String( s_xface ) ).toString();
00378 }
00379
00380 QString Identity::dictionary() const
00381 {
00382 return property( QLatin1String( s_dict ) ).toString();
00383 }
00384
00385 QString Identity::templates() const
00386 {
00387 return property( QLatin1String( s_templates ) ).toString();
00388 }
00389
00390 QString Identity::drafts() const
00391 {
00392 return property( QLatin1String( s_drafts ) ).toString();
00393 }
00394
00395 QString Identity::fcc() const
00396 {
00397 return property( QLatin1String( s_fcc ) ).toString();
00398 }
00399
00400 QString Identity::transport() const
00401 {
00402 return property( QLatin1String( s_transport ) ).toString();
00403 }
00404
00405 bool Identity::signatureIsCommand() const
00406 {
00407 return mSignature.type() == Signature::FromCommand;
00408 }
00409
00410 bool Identity::signatureIsPlainFile() const
00411 {
00412 return mSignature.type() == Signature::FromFile;
00413 }
00414
00415 bool Identity::signatureIsInline() const
00416 {
00417 return mSignature.type() == Signature::Inlined;
00418 }
00419
00420 bool Identity::useSignatureFile() const
00421 {
00422 return signatureIsPlainFile() || signatureIsCommand();
00423 }
00424
00425 QString Identity::signatureInlineText() const
00426 {
00427 return mSignature.text();
00428 }
00429
00430 QString Identity::signatureFile() const
00431 {
00432 return mSignature.url();
00433 }
00434
00435
00436
00437 void Identity::setProperty( const QString &key, const QVariant &value )
00438 {
00439 if ( value.isNull() ||
00440 ( value.type() == QVariant::String && value.toString().isEmpty() ) ) {
00441 mPropertiesMap.remove( key );
00442 } else {
00443 mPropertiesMap.insert( key, value );
00444 }
00445 }
00446
00447 void Identity::setUoid( uint aUoid )
00448 {
00449 setProperty( s_uoid, aUoid );
00450 }
00451
00452 void Identity::setIdentityName( const QString &name )
00453 {
00454 setProperty( s_identity, name );
00455 }
00456
00457 void Identity::setFullName( const QString &str )
00458 {
00459 setProperty( s_name, str );
00460 }
00461
00462 void Identity::setOrganization( const QString &str )
00463 {
00464 setProperty( s_organization, str );
00465 }
00466
00467 void Identity::setPGPSigningKey( const QByteArray &str )
00468 {
00469 setProperty( s_pgps, QString( str ) );
00470 }
00471
00472 void Identity::setPGPEncryptionKey( const QByteArray &str )
00473 {
00474 setProperty( s_pgpe, QString( str ) );
00475 }
00476
00477 void Identity::setSMIMESigningKey( const QByteArray &str )
00478 {
00479 setProperty( s_smimes, QString( str ) );
00480 }
00481
00482 void Identity::setSMIMEEncryptionKey( const QByteArray &str )
00483 {
00484 setProperty( s_smimee, QString( str ) );
00485 }
00486
00487 void Identity::setEmailAddr( const QString &str )
00488 {
00489 setProperty( s_email, str );
00490 }
00491
00492 void Identity::setVCardFile( const QString &str )
00493 {
00494 setProperty( s_vcard, str );
00495 }
00496
00497 void Identity::setReplyToAddr( const QString&str )
00498 {
00499 setProperty( s_replyto, str );
00500 }
00501
00502 void Identity::setSignatureFile( const QString &str )
00503 {
00504 mSignature.setUrl( str, signatureIsCommand() );
00505 }
00506
00507 void Identity::setSignatureInlineText( const QString &str )
00508 {
00509 mSignature.setText( str );
00510 }
00511
00512 void Identity::setTransport( const QString &str )
00513 {
00514 setProperty( s_transport, str );
00515 }
00516
00517 void Identity::setFcc( const QString &str )
00518 {
00519 setProperty( s_fcc, str );
00520 }
00521
00522 void Identity::setDrafts( const QString &str )
00523 {
00524 setProperty( s_drafts, str );
00525 }
00526
00527 void Identity::setTemplates( const QString &str )
00528 {
00529 setProperty( s_templates, str );
00530 }
00531
00532 void Identity::setDictionary( const QString &str )
00533 {
00534 setProperty( s_dict, str );
00535 }
00536
00537 void Identity::setBcc( const QString &str )
00538 {
00539 setProperty( s_bcc, str );
00540 }
00541
00542 void Identity::setIsDefault( bool flag )
00543 {
00544 mIsDefault = flag;
00545 }
00546
00547 void Identity::setPreferredCryptoMessageFormat( const QString &str )
00548 {
00549 setProperty( s_prefcrypt, str );
00550 }
00551
00552 void Identity::setXFace( const QString &str )
00553 {
00554
00555 QString strNew = str;
00556 strNew.remove( " " );
00557 strNew.remove( "\n" );
00558 strNew.remove( "\r" );
00559 setProperty( s_xface, strNew );
00560 }
00561
00562 void Identity::setXFaceEnabled( const bool on )
00563 {
00564 setProperty( s_xfaceenabled, on );
00565 }
00566
00567 void Identity::setSignature( const Signature &sig )
00568 {
00569 mSignature = sig;
00570 }