• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.5 API Reference
  • KDE Home
  • Contact Us
 

kpimidentities

identity.cpp
00001 /*
00002     Copyright (c) 2002-2004 Marc Mutz <mutz@kde.org>
00003     Copyright (c) 2007 Tom Albers <tomalbers@kde.nl>
00004 
00005     This library is free software; you can redistribute it and/or modify it
00006     under the terms of the GNU Library General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or (at your
00008     option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful, but WITHOUT
00011     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013     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 the
00017     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018     02110-1301, USA.
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 #include <kpimutils/email.h>
00034 
00035 #include <QFileInfo>
00036 #include <QMimeData>
00037 #include <QByteArray>
00038 
00039 #include <sys/types.h>
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 #include <errno.h>
00043 #include <assert.h>
00044 
00045 using namespace KPIMIdentities;
00046 
00047 // TODO: should use a kstaticdeleter?
00048 static Identity *identityNull = 0;
00049 
00050 Identity::Identity( const QString &id, const QString &fullName,
00051                     const QString &emailAddr, const QString &organization,
00052                     const QString &replyToAddr )
00053   : mIsDefault( false )
00054 {
00055   setProperty( s_uoid, 0 );
00056   setProperty( s_identity, id );
00057   setProperty( s_name, fullName );
00058   setProperty( s_email, emailAddr );
00059   setProperty( s_organization, organization );
00060   setProperty( s_replyto, replyToAddr );
00061   setDictionary( Sonnet::defaultLanguageName() );
00062 }
00063 
00064 Identity::~Identity()
00065 {}
00066 
00067 const Identity &Identity::null()
00068 {
00069   if ( !identityNull ) {
00070     identityNull = new Identity;
00071   }
00072   return *identityNull;
00073 }
00074 
00075 bool Identity::isNull() const
00076 {
00077   bool empty = true;
00078   QHash<QString, QVariant>::const_iterator i = mPropertiesMap.constBegin();
00079   while ( i != mPropertiesMap.constEnd() ) {
00080 
00081     // Take into account that the dictionary for a null identity is not empty
00082     if ( i.key() == s_dict ) {
00083       ++i;
00084       continue;
00085     }
00086 
00087     // The uoid is 0 by default, so ignore this
00088     if ( !( i.key() == s_uoid && i.value().toUInt() == 0 ) ) {
00089       if ( !i.value().isNull() ||
00090           ( i.value().type() == QVariant::String && !i.value().toString().isEmpty() ) ) {
00091         empty = false;
00092       }
00093     }
00094     ++i;
00095   }
00096   return empty;
00097 }
00098 
00099 void Identity::readConfig( const KConfigGroup &config )
00100 {
00101   // get all keys and convert them to our QHash.
00102   QMap<QString,QString> entries = config.entryMap();
00103   QMap<QString,QString>::const_iterator i = entries.constBegin();
00104   QMap<QString,QString>::const_iterator end = entries.constEnd();
00105   while ( i != end ) {
00106     if ( i.key() == s_emailAliases ) {
00107       // HACK: Read s_emailAliases as a stringlist
00108       mPropertiesMap.insert( i.key(), config.readEntry( i.key(), QStringList() ) );
00109     } else {
00110       mPropertiesMap.insert( i.key(), config.readEntry( i.key() ) );
00111     }
00112     ++i;
00113   }
00114   mSignature.readConfig( config );
00115 }
00116 
00117 void Identity::writeConfig( KConfigGroup &config ) const
00118 {
00119   QHash<QString, QVariant>::const_iterator i = mPropertiesMap.constBegin();
00120   QHash<QString, QVariant>::const_iterator end = mPropertiesMap.constEnd();
00121   while ( i != end ) {
00122     config.writeEntry( i.key(), i.value() );
00123     kDebug( 5325 ) << "Store:" << i.key() << ":" << i.value();
00124     ++i;
00125   }
00126   mSignature.writeConfig( config );
00127 }
00128 
00129 bool Identity::mailingAllowed() const
00130 {
00131   return !property( s_email ).toString().isEmpty();
00132 }
00133 
00134 QString Identity::mimeDataType()
00135 {
00136   return "application/x-kmail-identity-drag";
00137 }
00138 
00139 bool Identity::canDecode( const QMimeData*md )
00140 {
00141   return md->hasFormat( mimeDataType() );
00142 }
00143 
00144 void Identity::populateMimeData( QMimeData*md )
00145 {
00146   QByteArray a;
00147   {
00148     QDataStream s( &a, QIODevice::WriteOnly );
00149     s << this;
00150   }
00151   md->setData( mimeDataType(), a );
00152 }
00153 
00154 Identity Identity::fromMimeData( const QMimeData*md )
00155 {
00156   Identity i;
00157   if ( canDecode( md ) ) {
00158     QByteArray ba = md->data( mimeDataType() );
00159     QDataStream s( &ba, QIODevice::ReadOnly );
00160     s >> i;
00161   }
00162   return i;
00163 }
00164 
00165 // ------------------ Operators --------------------------//
00166 
00167 QDataStream &KPIMIdentities::operator<<
00168 ( QDataStream &stream, const KPIMIdentities::Identity &i )
00169 {
00170   return stream << static_cast<quint32>( i.uoid() )
00171          << i.identityName()
00172          << i.fullName()
00173          << i.organization()
00174          << i.pgpSigningKey()
00175          << i.pgpEncryptionKey()
00176          << i.smimeSigningKey()
00177          << i.smimeEncryptionKey()
00178          << i.primaryEmailAddress()
00179          << i.emailAliases()
00180          << i.replyToAddr()
00181          << i.bcc()
00182          << i.vCardFile()
00183          << i.transport()
00184          << i.fcc()
00185          << i.drafts()
00186          << i.templates()
00187          << i.mPropertiesMap[s_signature]
00188          << i.dictionary()
00189          << i.xface()
00190          << i.preferredCryptoMessageFormat();
00191 }
00192 
00193 QDataStream &KPIMIdentities::operator>>
00194 ( QDataStream &stream, KPIMIdentities::Identity &i )
00195 {
00196   quint32 uoid;
00197   QString format;
00198   stream
00199   >> uoid
00200   >> i.mPropertiesMap[s_identity]
00201   >> i.mPropertiesMap[s_name]
00202   >> i.mPropertiesMap[s_organization]
00203   >> i.mPropertiesMap[s_pgps]
00204   >> i.mPropertiesMap[s_pgpe]
00205   >> i.mPropertiesMap[s_smimes]
00206   >> i.mPropertiesMap[s_smimee]
00207   >> i.mPropertiesMap[s_email]
00208   >> i.mPropertiesMap[s_emailAliases]
00209   >> i.mPropertiesMap[s_replyto]
00210   >> i.mPropertiesMap[s_bcc]
00211   >> i.mPropertiesMap[s_vcard]
00212   >> i.mPropertiesMap[s_transport]
00213   >> i.mPropertiesMap[s_fcc]
00214   >> i.mPropertiesMap[s_drafts]
00215   >> i.mPropertiesMap[s_templates]
00216   >> i.mPropertiesMap[s_signature]
00217   >> i.mPropertiesMap[s_dict]
00218   >> i.mPropertiesMap[s_xface]
00219   >> i.mPropertiesMap[s_prefcrypt];
00220   i.setProperty( s_uoid, uoid );
00221   return stream;
00222 }
00223 
00224 bool Identity::operator< ( const Identity &other ) const
00225 {
00226   if ( isDefault() ) {
00227     return true;
00228   }
00229   if ( other.isDefault() ) {
00230     return false;
00231   }
00232   return identityName() < other.identityName();
00233 }
00234 
00235 bool Identity::operator> ( const Identity &other ) const
00236 {
00237   if ( isDefault() ) {
00238     return false;
00239   }
00240   if ( other.isDefault() ) {
00241     return true;
00242   }
00243   return identityName() > other.identityName();
00244 }
00245 
00246 bool Identity::operator<= ( const Identity &other ) const
00247 {
00248   return !operator> ( other );
00249 }
00250 
00251 bool Identity::operator>= ( const Identity &other ) const
00252 {
00253   return !operator< ( other );
00254 }
00255 
00256 bool Identity::operator== ( const Identity &other ) const
00257 {
00258   return mPropertiesMap == other.mPropertiesMap &&
00259          mSignature == other.mSignature;
00260 }
00261 
00262 bool Identity::operator!= ( const Identity &other ) const
00263 {
00264   return !operator== ( other );
00265 }
00266 
00267 // --------------------- Getters -----------------------------//
00268 
00269 QVariant Identity::property( const QString &key ) const
00270 {
00271   return mPropertiesMap.value( key );
00272 }
00273 
00274 QString Identity::fullEmailAddr( void ) const
00275 {
00276   const QString name = mPropertiesMap.value( s_name ).toString();
00277   const QString mail = mPropertiesMap.value( s_email ).toString();
00278 
00279   if ( name.isEmpty() ) {
00280     return mail;
00281   }
00282 
00283   const QString specials( "()<>@,.;:[]" );
00284 
00285   QString result;
00286 
00287   // add DQUOTE's if necessary:
00288   bool needsQuotes=false;
00289   const int nameLength( name.length() );
00290   for ( int i=0; i < nameLength; i++ ) {
00291     if ( specials.contains( name[i] ) ) {
00292       needsQuotes = true;
00293     } else if ( name[i] == '\\' || name[i] == '"' ) {
00294       needsQuotes = true;
00295       result += '\\';
00296     }
00297     result += name[i];
00298   }
00299 
00300   if ( needsQuotes ) {
00301     result.insert( 0,'"' );
00302     result += '"';
00303   }
00304 
00305   result += " <" + mail + '>';
00306 
00307   return result;
00308 }
00309 
00310 QString Identity::identityName() const
00311 {
00312   return property( QLatin1String( s_identity ) ).toString();
00313 }
00314 
00315 QString Identity::signatureText( bool *ok ) const
00316 {
00317   return mSignature.withSeparator( ok );
00318 }
00319 
00320 bool Identity::signatureIsInlinedHtml() const
00321 {
00322   return mSignature.isInlinedHtml();
00323 }
00324 
00325 bool Identity::isDefault() const
00326 {
00327   return mIsDefault;
00328 }
00329 
00330 uint Identity::uoid() const
00331 {
00332   return property( QLatin1String( s_uoid ) ).toInt();
00333 }
00334 
00335 QString Identity::fullName() const
00336 {
00337   return property( QLatin1String( s_name ) ).toString();
00338 }
00339 
00340 QString Identity::organization() const
00341 {
00342   return property( QLatin1String( s_organization ) ).toString();
00343 }
00344 
00345 QByteArray Identity::pgpEncryptionKey() const
00346 {
00347   return property( QLatin1String( s_pgpe ) ).toByteArray();
00348 }
00349 
00350 QByteArray Identity::pgpSigningKey() const
00351 {
00352   return property( QLatin1String( s_pgps ) ).toByteArray();
00353 }
00354 
00355 QByteArray Identity::smimeEncryptionKey() const
00356 {
00357   return property( QLatin1String( s_smimee ) ).toByteArray();
00358 }
00359 
00360 QByteArray Identity::smimeSigningKey() const
00361 {
00362   return property( QLatin1String( s_smimes ) ).toByteArray();
00363 }
00364 
00365 QString Identity::preferredCryptoMessageFormat() const
00366 {
00367   return property( QLatin1String( s_prefcrypt ) ).toString();
00368 }
00369 
00370 QString Identity::emailAddr() const
00371 {
00372   return primaryEmailAddress();
00373 }
00374 
00375 QString Identity::primaryEmailAddress() const
00376 {
00377   return property( QLatin1String( s_email ) ).toString();
00378 }
00379 
00380 const QStringList Identity::emailAliases() const
00381 {
00382   return property( QLatin1String( s_emailAliases ) ).toStringList();
00383 }
00384 
00385 QString Identity::vCardFile() const
00386 {
00387   return property( QLatin1String( s_vcard ) ).toString();
00388 }
00389 
00390 QString Identity::replyToAddr() const
00391 {
00392   return property( QLatin1String( s_replyto ) ).toString();
00393 }
00394 
00395 QString Identity::bcc() const
00396 {
00397   return property( QLatin1String( s_bcc ) ).toString();
00398 }
00399 
00400 Signature &Identity::signature()
00401 {
00402   return mSignature;
00403 }
00404 
00405 bool Identity::isXFaceEnabled() const
00406 {
00407   return property( QLatin1String( s_xfaceenabled ) ).toBool();
00408 }
00409 
00410 QString Identity::xface() const
00411 {
00412   return property( QLatin1String( s_xface ) ).toString();
00413 }
00414 
00415 QString Identity::dictionary() const
00416 {
00417   return property( QLatin1String( s_dict ) ).toString();
00418 }
00419 
00420 QString Identity::templates() const
00421 {
00422   return property( QLatin1String( s_templates ) ).toString();
00423 }
00424 
00425 QString Identity::drafts() const
00426 {
00427   return property( QLatin1String( s_drafts ) ).toString();
00428 }
00429 
00430 QString Identity::fcc() const
00431 {
00432   return property( QLatin1String( s_fcc ) ).toString();
00433 }
00434 
00435 QString Identity::transport() const
00436 {
00437   return property( QLatin1String( s_transport ) ).toString();
00438 }
00439 
00440 bool Identity::signatureIsCommand() const
00441 {
00442   return mSignature.type() == Signature::FromCommand;
00443 }
00444 
00445 bool Identity::signatureIsPlainFile() const
00446 {
00447   return mSignature.type() == Signature::FromFile;
00448 }
00449 
00450 bool Identity::signatureIsInline() const
00451 {
00452   return mSignature.type() == Signature::Inlined;
00453 }
00454 
00455 bool Identity::useSignatureFile() const
00456 {
00457   return signatureIsPlainFile() || signatureIsCommand();
00458 }
00459 
00460 QString Identity::signatureInlineText() const
00461 {
00462   return mSignature.text();
00463 }
00464 
00465 QString Identity::signatureFile() const
00466 {
00467   return mSignature.url();
00468 }
00469 
00470 // --------------------- Setters -----------------------------//
00471 
00472 void Identity::setProperty( const QString &key, const QVariant &value )
00473 {
00474   if ( value.isNull() ||
00475        ( value.type() == QVariant::String && value.toString().isEmpty() ) ) {
00476     mPropertiesMap.remove( key );
00477   } else {
00478     mPropertiesMap.insert( key, value );
00479   }
00480 }
00481 
00482 void Identity::setUoid( uint aUoid )
00483 {
00484   setProperty( s_uoid, aUoid );
00485 }
00486 
00487 void Identity::setIdentityName( const QString &name )
00488 {
00489   setProperty( s_identity, name );
00490 }
00491 
00492 void Identity::setFullName( const QString &str )
00493 {
00494   setProperty( s_name, str );
00495 }
00496 
00497 void Identity::setOrganization( const QString &str )
00498 {
00499   setProperty( s_organization, str );
00500 }
00501 
00502 void Identity::setPGPSigningKey( const QByteArray &str )
00503 {
00504   setProperty( s_pgps, QString( str ) );
00505 }
00506 
00507 void Identity::setPGPEncryptionKey( const QByteArray &str )
00508 {
00509   setProperty( s_pgpe, QString( str ) );
00510 }
00511 
00512 void Identity::setSMIMESigningKey( const QByteArray &str )
00513 {
00514   setProperty( s_smimes, QString( str ) );
00515 }
00516 
00517 void Identity::setSMIMEEncryptionKey( const QByteArray &str )
00518 {
00519   setProperty( s_smimee, QString( str ) );
00520 }
00521 
00522 void Identity::setEmailAddr( const QString &str )
00523 {
00524   setPrimaryEmailAddress( str );
00525 }
00526 
00527 void Identity::setPrimaryEmailAddress( const QString & email )
00528 {
00529   setProperty( s_email, email );
00530 }
00531 
00532 void Identity::setEmailAliases( const QStringList & aliases )
00533 {
00534   setProperty( s_emailAliases, aliases );
00535 }
00536 
00537 void Identity::setVCardFile( const QString &str )
00538 {
00539   setProperty( s_vcard, str );
00540 }
00541 
00542 void Identity::setReplyToAddr( const QString&str )
00543 {
00544   setProperty( s_replyto, str );
00545 }
00546 
00547 void Identity::setSignatureFile( const QString &str )
00548 {
00549   mSignature.setUrl( str, signatureIsCommand() );
00550 }
00551 
00552 void Identity::setSignatureInlineText( const QString &str )
00553 {
00554   mSignature.setText( str );
00555 }
00556 
00557 void Identity::setTransport( const QString &str )
00558 {
00559   setProperty( s_transport, str );
00560 }
00561 
00562 void Identity::setFcc( const QString &str )
00563 {
00564   setProperty( s_fcc, str );
00565 }
00566 
00567 void Identity::setDrafts( const QString &str )
00568 {
00569   setProperty( s_drafts, str );
00570 }
00571 
00572 void Identity::setTemplates( const QString &str )
00573 {
00574   setProperty( s_templates, str );
00575 }
00576 
00577 void Identity::setDictionary( const QString &str )
00578 {
00579   setProperty( s_dict, str );
00580 }
00581 
00582 void Identity::setBcc( const QString &str )
00583 {
00584   setProperty( s_bcc, str );
00585 }
00586 
00587 void Identity::setIsDefault( bool flag )
00588 {
00589   mIsDefault = flag;
00590 }
00591 
00592 void Identity::setPreferredCryptoMessageFormat( const QString &str )
00593 {
00594   setProperty( s_prefcrypt, str );
00595 }
00596 
00597 void Identity::setXFace( const QString &str )
00598 {
00599   QString strNew = str;
00600   strNew.remove( ' ' );
00601   strNew.remove( '\n' );
00602   strNew.remove( '\r' );
00603   setProperty( s_xface, strNew );
00604 }
00605 
00606 void Identity::setXFaceEnabled( const bool on )
00607 {
00608   setProperty( s_xfaceenabled, on );
00609 }
00610 
00611 void Identity::setSignature( const Signature &sig )
00612 {
00613   mSignature = sig;
00614 }
00615 
00616 bool Identity::matchesEmailAddress( const QString & addr ) const
00617 {
00618   const QString addrSpec = KPIMUtils::extractEmailAddress( addr ).toLower();
00619   if ( addrSpec == primaryEmailAddress().toLower() )
00620     return true;
00621 
00622   foreach ( const QString &alias, emailAliases() ) {
00623     if ( alias.toLower() == addrSpec )
00624       return true;
00625   }
00626 
00627   return false;
00628 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Aug 27 2012 22:10:34 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kpimidentities

Skip menu "kpimidentities"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.8.5 API Reference

Skip menu "kdepimlibs-4.8.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal