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
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
00020 Addressee::setFormattedName( name );
00021
00022 Addressee::setFamilyName( name );
00023
00024
00025 if ( custom( "KADDRESSBOOK", s_customFieldName ).isEmpty() )
00026 insertCustom( "KADDRESSBOOK", s_customFieldName, ";" );
00027 }
00028
00029
00030
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
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 );
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
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() );
00072 removeEntry( uid, email );
00073 QString str = custom( "KADDRESSBOOK", s_customFieldName );
00074
00075 str += ";" + uid + "," + email;
00076 insertCustom( "KADDRESSBOOK", s_customFieldName, str );
00077 }
00078
00079 void KPIM::DistributionList::removeEntry( const Addressee& addr, const QString& email )
00080 {
00081 removeEntry( addr.uid(), email );
00082
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() );
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;
00096 }
00097 str += ";" + thisUid + "," + thisEmail;
00098 }
00099 if ( str.isEmpty() )
00100 str = ";";
00101 insertCustom( "KADDRESSBOOK", s_customFieldName, str );
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
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
00149
00150
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() ) {
00160 a = lst.first();
00161 }
00162 }
00163
00164 if ( a.isEmpty() ) {
00165
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
00183 KABC::Addressee a = findByUidOrName( book, uid, email );
00184 if ( a.isEmpty() ) {
00185
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
00206 KABC::Addressee a = findByUidOrName( book, thisUid, thisEmail );
00207 if ( a.isEmpty() ) {
00208
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 }
This file is part of the documentation for libkdepim Library Version 3.3.2.