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 void KPIM::DistributionList::insertEntry( const Addressee& addr, const QString& email )
00055 {
00056
00057 removeEntry( addr.formattedName(), email );
00058 insertEntry( addr.uid(), email );
00059 }
00060
00061 void KPIM::DistributionList::insertEntry( const QString& uid, const QString& email )
00062 {
00063 Q_ASSERT( !email.isEmpty() || email.isNull() );
00064 removeEntry( uid, email );
00065 QString str = custom( "KADDRESSBOOK", s_customFieldName );
00066
00067 str += ";" + uid + "," + email;
00068 insertCustom( "KADDRESSBOOK", s_customFieldName, str );
00069 }
00070
00071 void KPIM::DistributionList::removeEntry( const Addressee& addr, const QString& email )
00072 {
00073 removeEntry( addr.uid(), email );
00074
00075 removeEntry( addr.formattedName(), email );
00076 }
00077
00078 void KPIM::DistributionList::removeEntry( const QString& uid, const QString& email )
00079 {
00080 Q_ASSERT( !email.isEmpty() || email.isNull() );
00081 ParseList parseList = parseCustom( custom( "KADDRESSBOOK", s_customFieldName ) );
00082 QString str;
00083 for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) {
00084 const QString thisUid = (*it).first;
00085 const QString thisEmail = (*it).second;
00086 if ( thisUid == uid && thisEmail == email ) {
00087 continue;
00088 }
00089 str += ";" + thisUid + "," + thisEmail;
00090 }
00091 if ( str.isEmpty() )
00092 str = ";";
00093 insertCustom( "KADDRESSBOOK", s_customFieldName, str );
00094 }
00095
00096 bool KPIM::DistributionList::isDistributionList( const KABC::Addressee& addr )
00097 {
00098 const QString str = addr.custom( "KADDRESSBOOK", s_customFieldName );
00099 return !str.isEmpty();
00100 }
00101
00102
00103 static KABC::Addressee::List findByFormattedName( KABC::AddressBook* book,
00104 const QString& name,
00105 bool caseSensitive = true )
00106 {
00107 KABC::Addressee::List res;
00108 KABC::AddressBook::Iterator abIt;
00109 for ( abIt = book->begin(); abIt != book->end(); ++abIt )
00110 {
00111 if ( caseSensitive && (*abIt).formattedName() == name )
00112 res.append( *abIt );
00113 if ( !caseSensitive && (*abIt).formattedName().lower() == name.lower() )
00114 res.append( *abIt );
00115 }
00116 return res;
00117 }
00118
00119 KPIM::DistributionList KPIM::DistributionList::findByName( KABC::AddressBook* book,
00120 const QString& name,
00121 bool caseSensitive )
00122 {
00123 KABC::AddressBook::Iterator abIt;
00124 for ( abIt = book->begin(); abIt != book->end(); ++abIt )
00125 {
00126 if ( isDistributionList( *abIt ) ) {
00127 if ( caseSensitive && (*abIt).formattedName() == name )
00128 return *abIt;
00129 if ( !caseSensitive && (*abIt).formattedName().lower() == name.lower() )
00130 return *abIt;
00131 }
00132 }
00133 return DistributionList();
00134 }
00135
00136 static KABC::Addressee findByUidOrName( KABC::AddressBook* book, const QString& uidOrName, const QString& email )
00137 {
00138 KABC::Addressee a = book->findByUid( uidOrName );
00139 if ( a.isEmpty() ) {
00140
00141
00142
00143 if ( !email.isEmpty() ) {
00144 KABC::Addressee::List lst = book->findByEmail( email );
00145 KABC::Addressee::List::ConstIterator listit = lst.begin();
00146 for ( ; listit != lst.end(); ++listit )
00147 if ( (*listit).formattedName() == uidOrName ) {
00148 a = *listit;
00149 break;
00150 }
00151 if ( !lst.isEmpty() && a.isEmpty() ) {
00152 a = lst.first();
00153 }
00154 }
00155
00156 if ( a.isEmpty() ) {
00157
00158 KABC::Addressee::List lst = findByFormattedName( book, uidOrName );
00159 if ( !lst.isEmpty() )
00160 a = lst.first();
00161 }
00162 }
00163 return a;
00164 }
00165
00166 KPIM::DistributionList::Entry::List KPIM::DistributionList::entries( KABC::AddressBook* book ) const
00167 {
00168 Entry::List res;
00169 const QString str = custom( "KADDRESSBOOK", s_customFieldName );
00170 const ParseList parseList = parseCustom( str );
00171 for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) {
00172 const QString uid = (*it).first;
00173 const QString email = (*it).second;
00174
00175 KABC::Addressee a = findByUidOrName( book, uid, email );
00176 if ( a.isEmpty() ) {
00177
00178 kdWarning() << "Addressee not found: " << uid << endl;
00179 } else {
00180 res.append( Entry( a, email ) );
00181 }
00182 }
00183 return res;
00184 }
00185
00186 QStringList KPIM::DistributionList::emails( KABC::AddressBook* book ) const
00187 {
00188 QStringList emails;
00189
00190 const QString str = custom( "KADDRESSBOOK", s_customFieldName );
00191 ParseList parseList = parseCustom( str );
00192 for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) {
00193 const QString thisUid = (*it).first;
00194 const QString thisEmail = (*it).second;
00195
00196
00197 KABC::Addressee a = findByUidOrName( book, thisUid, thisEmail );
00198 if ( a.isEmpty() ) {
00199
00200 continue;
00201 }
00202
00203 const QString email = thisEmail.isEmpty() ? a.fullEmail() :
00204 a.fullEmail( thisEmail );
00205 if ( !email.isEmpty() ) {
00206 emails.append( email );
00207 }
00208 }
00209
00210 return emails;
00211 }
00212
00213 QValueList<KPIM::DistributionList>
00214 KPIM::DistributionList::allDistributionLists( KABC::AddressBook* book )
00215 {
00216 QValueList<KPIM::DistributionList> lst;
00217 KABC::AddressBook::Iterator abIt;
00218 for ( abIt = book->begin(); abIt != book->end(); ++abIt )
00219 {
00220 if ( isDistributionList( *abIt ) ) {
00221 lst.append( KPIM::DistributionList( *abIt ) );
00222 }
00223 }
00224 return lst;
00225 }