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