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