• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • 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 <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 // TODO: should use a kstaticdeleter?
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     // The uoid is 0 by default, so ignore this
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   // get all keys and convert them to our QHash.
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 // ------------------ Operators --------------------------//
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 // --------------------- Getters -----------------------------//
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   // add DQUOTE's if necessary:
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::signatureIsInlinedHtml() const
00301 {
00302   return mSignature.isInlinedHtml();
00303 }
00304 
00305 bool Identity::isDefault() const
00306 {
00307   return mIsDefault;
00308 }
00309 
00310 uint Identity::uoid() const
00311 {
00312   return property( QLatin1String( s_uoid ) ).toInt();
00313 }
00314 
00315 QString Identity::fullName() const
00316 {
00317   return property( QLatin1String( s_name ) ).toString();
00318 }
00319 
00320 QString Identity::organization() const
00321 {
00322   return property( QLatin1String( s_organization ) ).toString();
00323 }
00324 
00325 QByteArray Identity::pgpEncryptionKey() const
00326 {
00327   return property( QLatin1String( s_pgpe ) ).toByteArray();
00328 }
00329 
00330 QByteArray Identity::pgpSigningKey() const
00331 {
00332   return property( QLatin1String( s_pgps ) ).toByteArray();
00333 }
00334 
00335 QByteArray Identity::smimeEncryptionKey() const
00336 {
00337   return property( QLatin1String( s_smimee ) ).toByteArray();
00338 }
00339 
00340 QByteArray Identity::smimeSigningKey() const
00341 {
00342   return property( QLatin1String( s_smimes ) ).toByteArray();
00343 }
00344 
00345 QString Identity::preferredCryptoMessageFormat() const
00346 {
00347   return property( QLatin1String( s_prefcrypt ) ).toString();
00348 }
00349 
00350 QString Identity::emailAddr() const
00351 {
00352   return property( QLatin1String( s_email ) ).toString();
00353 }
00354 
00355 QString Identity::vCardFile() const
00356 {
00357   return property( QLatin1String( s_vcard ) ).toString();
00358 }
00359 
00360 QString Identity::replyToAddr() const
00361 {
00362   return property( QLatin1String( s_replyto ) ).toString();
00363 }
00364 
00365 QString Identity::bcc() const
00366 {
00367   return property( QLatin1String( s_bcc ) ).toString();
00368 }
00369 
00370 Signature &Identity::signature()
00371 {
00372   return mSignature;
00373 }
00374 
00375 bool Identity::isXFaceEnabled() const
00376 {
00377   return property( QLatin1String( s_xfaceenabled ) ).toBool();
00378 }
00379 
00380 QString Identity::xface() const
00381 {
00382   return property( QLatin1String( s_xface ) ).toString();
00383 }
00384 
00385 QString Identity::dictionary() const
00386 {
00387   return property( QLatin1String( s_dict ) ).toString();
00388 }
00389 
00390 QString Identity::templates() const
00391 {
00392   return property( QLatin1String( s_templates ) ).toString();
00393 }
00394 
00395 QString Identity::drafts() const
00396 {
00397   return property( QLatin1String( s_drafts ) ).toString();
00398 }
00399 
00400 QString Identity::fcc() const
00401 {
00402   return property( QLatin1String( s_fcc ) ).toString();
00403 }
00404 
00405 QString Identity::transport() const
00406 {
00407   return property( QLatin1String( s_transport ) ).toString();
00408 }
00409 
00410 bool Identity::signatureIsCommand() const
00411 {
00412   return mSignature.type() == Signature::FromCommand;
00413 }
00414 
00415 bool Identity::signatureIsPlainFile() const
00416 {
00417   return mSignature.type() == Signature::FromFile;
00418 }
00419 
00420 bool Identity::signatureIsInline() const
00421 {
00422   return mSignature.type() == Signature::Inlined;
00423 }
00424 
00425 bool Identity::useSignatureFile() const
00426 {
00427   return signatureIsPlainFile() || signatureIsCommand();
00428 }
00429 
00430 QString Identity::signatureInlineText() const
00431 {
00432   return mSignature.text();
00433 }
00434 
00435 QString Identity::signatureFile() const
00436 {
00437   return mSignature.url();
00438 }
00439 
00440 // --------------------- Setters -----------------------------//
00441 
00442 void Identity::setProperty( const QString &key, const QVariant &value )
00443 {
00444   if ( value.isNull() ||
00445        ( value.type() == QVariant::String && value.toString().isEmpty() ) ) {
00446     mPropertiesMap.remove( key );
00447   } else {
00448     mPropertiesMap.insert( key, value );
00449   }
00450 }
00451 
00452 void Identity::setUoid( uint aUoid )
00453 {
00454   setProperty( s_uoid, aUoid );
00455 }
00456 
00457 void Identity::setIdentityName( const QString &name )
00458 {
00459   setProperty( s_identity, name );
00460 }
00461 
00462 void Identity::setFullName( const QString &str )
00463 {
00464   setProperty( s_name, str );
00465 }
00466 
00467 void Identity::setOrganization( const QString &str )
00468 {
00469   setProperty( s_organization, str );
00470 }
00471 
00472 void Identity::setPGPSigningKey( const QByteArray &str )
00473 {
00474   setProperty( s_pgps, QString( str ) );
00475 }
00476 
00477 void Identity::setPGPEncryptionKey( const QByteArray &str )
00478 {
00479   setProperty( s_pgpe, QString( str ) );
00480 }
00481 
00482 void Identity::setSMIMESigningKey( const QByteArray &str )
00483 {
00484   setProperty( s_smimes, QString( str ) );
00485 }
00486 
00487 void Identity::setSMIMEEncryptionKey( const QByteArray &str )
00488 {
00489   setProperty( s_smimee, QString( str ) );
00490 }
00491 
00492 void Identity::setEmailAddr( const QString &str )
00493 {
00494   setProperty( s_email, str );
00495 }
00496 
00497 void Identity::setVCardFile( const QString &str )
00498 {
00499   setProperty( s_vcard, str );
00500 }
00501 
00502 void Identity::setReplyToAddr( const QString&str )
00503 {
00504   setProperty( s_replyto, str );
00505 }
00506 
00507 void Identity::setSignatureFile( const QString &str )
00508 {
00509   mSignature.setUrl( str, signatureIsCommand() );
00510 }
00511 
00512 void Identity::setSignatureInlineText( const QString &str )
00513 {
00514   mSignature.setText( str );
00515 }
00516 
00517 void Identity::setTransport( const QString &str )
00518 {
00519   setProperty( s_transport, str );
00520 }
00521 
00522 void Identity::setFcc( const QString &str )
00523 {
00524   setProperty( s_fcc, str );
00525 }
00526 
00527 void Identity::setDrafts( const QString &str )
00528 {
00529   setProperty( s_drafts, str );
00530 }
00531 
00532 void Identity::setTemplates( const QString &str )
00533 {
00534   setProperty( s_templates, str );
00535 }
00536 
00537 void Identity::setDictionary( const QString &str )
00538 {
00539   setProperty( s_dict, str );
00540 }
00541 
00542 void Identity::setBcc( const QString &str )
00543 {
00544   setProperty( s_bcc, str );
00545 }
00546 
00547 void Identity::setIsDefault( bool flag )
00548 {
00549   mIsDefault = flag;
00550 }
00551 
00552 void Identity::setPreferredCryptoMessageFormat( const QString &str )
00553 {
00554   setProperty( s_prefcrypt, str );
00555 }
00556 
00557 void Identity::setXFace( const QString &str )
00558 {
00559   QString strNew = str;
00560   strNew.remove( ' ' );
00561   strNew.remove( '\n' );
00562   strNew.remove( '\r' );
00563   setProperty( s_xface, strNew );
00564 }
00565 
00566 void Identity::setXFaceEnabled( const bool on )
00567 {
00568   setProperty( s_xfaceenabled, on );
00569 }
00570 
00571 void Identity::setSignature( const Signature &sig )
00572 {
00573   mSignature = sig;
00574 }

kpimidentities

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.6
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal