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