libkdepim Library API Documentation

distributionlist.cpp

00001 #include "distributionlist.h"
00002 #include <kabc/addressbook.h>
00003 
00004 static const char* s_customFieldName = "DistributionList";
00005 
00006 KPIM::DistributionList::DistributionList()
00007  : KABC::Addressee()
00008 {
00009   // can't insert the custom entry here, we need to remain a null addressee
00010 }
00011 
00012 KPIM::DistributionList::DistributionList( const KABC::Addressee& addr )
00013  : KABC::Addressee( addr )
00014 {
00015 }
00016 
00017 void KPIM::DistributionList::setName( const QString &name )
00018 {
00019   // We can't use Addressee::setName, the name isn't saved/loaded in the vcard (fixed in 3.4)
00020   Addressee::setFormattedName( name );
00021   // Also set family name, just in case this entry appears in the normal contacts list (e.g. old kaddressbook)
00022   Addressee::setFamilyName( name );
00023   // We're not an empty addressee anymore
00024   // Set the custom field to non-empty, so that isDistributionList works
00025   if ( custom( "KADDRESSBOOK", s_customFieldName ).isEmpty() )
00026     insertCustom( "KADDRESSBOOK", s_customFieldName, ";" );
00027 }
00028 
00029 // Helper function, to parse the contents of the custom field
00030 // Returns a list of { uid, email }
00031 typedef QValueList<QPair<QString, QString> > ParseList;
00032 static ParseList parseCustom( const QString& str )
00033 {
00034   ParseList res;
00035   const QStringList lst = QStringList::split( ';', str );
00036   for( QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it ) {
00037     if ( (*it).isEmpty() )
00038       continue;
00039     // parse "uid,email"
00040     QStringList helpList = QStringList::split( ',', (*it) );
00041     Q_ASSERT( !helpList.isEmpty() );
00042     if ( helpList.isEmpty() )
00043       continue;
00044     const QString uid = helpList.first();
00045     QString email;
00046     Q_ASSERT( helpList.count() < 3 ); // 1 or 2 items, but not more
00047     if ( helpList.count() == 2 )
00048       email = helpList.last();
00049     res.append( qMakePair( uid, email ) );
00050   }
00051   return res;
00052 }
00053 
00054 QString cleanupFormattedName( const QString & _formattedName )
00055 {
00056   QString formattedName =_formattedName;
00057   formattedName.replace(',', ' ' );
00058   formattedName.replace(';', ' ' );
00059   return formattedName;
00060 }
00061 
00062 void KPIM::DistributionList::insertEntry( const Addressee& addr, const QString& email )
00063 {
00064   // insertEntry will removeEntry(uid), but not with formattedName
00065   removeEntry( cleanupFormattedName( addr.formattedName() ), email );
00066   insertEntry( addr.uid(), email );
00067 }
00068 
00069 void KPIM::DistributionList::insertEntry( const QString& uid, const QString& email )
00070 {
00071   Q_ASSERT( !email.isEmpty() || email.isNull() ); // hopefully never called with "", would lead to confusion
00072   removeEntry( uid, email ); // avoid duplicates
00073   QString str = custom( "KADDRESSBOOK", s_customFieldName );
00074   // Assumption: UIDs don't contain ; nor ,
00075   str += ";" + uid + "," + email;
00076   insertCustom( "KADDRESSBOOK", s_customFieldName, str ); // replace old value
00077 }
00078 
00079 void KPIM::DistributionList::removeEntry( const Addressee& addr, const QString& email )
00080 {
00081   removeEntry( addr.uid(), email );
00082   // Also remove entries with the full name as uid (for the kolab thing)
00083   removeEntry( cleanupFormattedName( addr.formattedName() ), email );
00084 }
00085 
00086 void KPIM::DistributionList::removeEntry( const QString& uid, const QString& email )
00087 {
00088   Q_ASSERT( !email.isEmpty() || email.isNull() ); // hopefully never called with "", would lead to confusion
00089   ParseList parseList = parseCustom( custom( "KADDRESSBOOK", s_customFieldName ) );
00090   QString str;
00091   for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) {
00092     const QString thisUid = (*it).first;
00093     const QString thisEmail = (*it).second;
00094     if ( thisUid == uid && thisEmail == email ) {
00095       continue; // remove that one
00096     }
00097     str += ";" + thisUid + "," + thisEmail;
00098   }
00099   if ( str.isEmpty() )
00100     str = ";"; // keep something, for isDistributionList to work
00101   insertCustom( "KADDRESSBOOK", s_customFieldName, str ); // replace old value
00102 }
00103 
00104 bool KPIM::DistributionList::isDistributionList( const KABC::Addressee& addr )
00105 {
00106   const QString str = addr.custom( "KADDRESSBOOK", s_customFieldName );
00107   return !str.isEmpty();
00108 }
00109 
00110 // ###### KDE4: add findByFormattedName to KABC::AddressBook
00111 static KABC::Addressee::List findByFormattedName( KABC::AddressBook* book,
00112                                             const QString& name,
00113                                             bool caseSensitive = true )
00114 {
00115   KABC::Addressee::List res;
00116   KABC::AddressBook::Iterator abIt;
00117   for ( abIt = book->begin(); abIt != book->end(); ++abIt )
00118   {
00119     if ( caseSensitive && (*abIt).formattedName() == name )
00120       res.append( *abIt );
00121     if ( !caseSensitive && (*abIt).formattedName().lower() == name.lower() )
00122       res.append( *abIt );
00123   }
00124   return res;
00125 }
00126 
00127 KPIM::DistributionList KPIM::DistributionList::findByName( KABC::AddressBook* book,
00128                                                            const QString& name,
00129                                                            bool caseSensitive )
00130 {
00131   KABC::AddressBook::Iterator abIt;
00132   for ( abIt = book->begin(); abIt != book->end(); ++abIt )
00133   {
00134     if ( isDistributionList( *abIt ) ) {
00135       if ( caseSensitive && (*abIt).formattedName() == name )
00136         return *abIt;
00137       if ( !caseSensitive && (*abIt).formattedName().lower() == name.lower() )
00138         return *abIt;
00139     }
00140   }
00141   return DistributionList();
00142 }
00143 
00144 static KABC::Addressee findByUidOrName( KABC::AddressBook* book, const QString& uidOrName, const QString& email )
00145 {
00146   KABC::Addressee a = book->findByUid( uidOrName );
00147   if ( a.isEmpty() ) {
00148     // UID not found, maybe it is a name instead.
00149     // If we have an email, let's use that for the lookup.
00150     // [This is used by e.g. the Kolab resource]
00151     if ( !email.isEmpty() ) {
00152       KABC::Addressee::List lst = book->findByEmail( email );
00153       KABC::Addressee::List::ConstIterator listit = lst.begin();
00154       for ( ; listit != lst.end(); ++listit )
00155         if ( (*listit).formattedName() == uidOrName ) {
00156           a = *listit;
00157           break;
00158         }
00159       if ( !lst.isEmpty() && a.isEmpty() ) { // found that email, but no match on the fullname
00160         a = lst.first(); // probably the last name changed
00161       }
00162     }
00163     // If we don't have an email, or if we didn't find any match for it, look up by full name
00164     if ( a.isEmpty() ) {
00165       // (But this has to be done here, since when loading we might not have the entries yet)
00166       KABC::Addressee::List lst = findByFormattedName( book, uidOrName );
00167       if ( !lst.isEmpty() )
00168         a = lst.first();
00169     }
00170   }
00171   return a;
00172 }
00173 
00174 KPIM::DistributionList::Entry::List KPIM::DistributionList::entries( KABC::AddressBook* book ) const
00175 {
00176   Entry::List res;
00177   const QString str = custom( "KADDRESSBOOK", s_customFieldName );
00178   ParseList parseList = parseCustom( str );
00179   for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) {
00180     const QString uid = (*it).first;
00181     const QString email = (*it).second;
00182     // look up contact
00183     KABC::Addressee a = findByUidOrName( book, uid, email );
00184     if ( a.isEmpty() ) {
00185       // ## The old DistributionListManager had a "missing entries" list...
00186       kdWarning() << "Addressee not found: " << uid << " email: " << email << endl;
00187     } else {
00188       Entry e( a, email );
00189       res.append( e );
00190     }
00191   }
00192   return res;
00193 }
00194 
00195 QStringList KPIM::DistributionList::emails( KABC::AddressBook* book ) const
00196 {
00197   QStringList emails;
00198 
00199   const QString str = custom( "KADDRESSBOOK", s_customFieldName );
00200   ParseList parseList = parseCustom( str );
00201   for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) {
00202     const QString thisUid = (*it).first;
00203     const QString thisEmail = (*it).second;
00204 
00205     // look up contact
00206     KABC::Addressee a = findByUidOrName( book, thisUid, thisEmail );
00207     if ( a.isEmpty() ) {
00208       // ## The old DistributionListManager had a "missing entries" list...
00209       continue;
00210     }
00211 
00212     QString email = thisEmail.isEmpty() ? a.fullEmail() :
00213                     a.fullEmail( thisEmail );
00214     if ( !email.isEmpty() ) {
00215       emails.append( email );
00216     }
00217   }
00218 
00219   return emails;
00220 }
00221 
00222 QValueList<KPIM::DistributionList>
00223  KPIM::DistributionList::allDistributionLists( KABC::AddressBook* book )
00224 {
00225   QValueList<KPIM::DistributionList> lst;
00226   KABC::AddressBook::Iterator abIt;
00227   for ( abIt = book->begin(); abIt != book->end(); ++abIt )
00228   {
00229     if ( isDistributionList( *abIt ) ) {
00230       lst.append( KPIM::DistributionList( *abIt ) );
00231     }
00232   }
00233   return lst;
00234 }
KDE Logo
This file is part of the documentation for libkdepim Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Aug 2 09:53:35 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003