libtdepim

distributionlist.cpp
1#include "distributionlist.h"
2#include <tdeabc/addressbook.h>
3
4static const char* s_customFieldName = "DistributionList";
5
7 : TDEABC::Addressee()
8{
9 // can't insert the custom entry here, we need to remain a null addressee
10}
11
12KPIM::DistributionList::DistributionList( const TDEABC::Addressee& addr )
13 : TDEABC::Addressee( addr )
14{
15}
16
17void KPIM::DistributionList::setName( const TQString &name )
18{
19 // We can't use Addressee::setName, the name isn't saved/loaded in the vcard (fixed in 3.4)
20 Addressee::setFormattedName( name );
21 // Also set family name, just in case this entry appears in the normal contacts list (e.g. old kaddressbook)
22 Addressee::setFamilyName( name );
23 // We're not an empty addressee anymore
24 // Set the custom field to non-empty, so that isDistributionList works
25 if ( custom( "KADDRESSBOOK", s_customFieldName ).isEmpty() )
26 insertCustom( "KADDRESSBOOK", s_customFieldName, ";" );
27}
28
29// Helper function, to parse the contents of the custom field
30// Returns a list of { uid, email }
31typedef TQValueList<TQPair<TQString, TQString> > ParseList;
32static ParseList parseCustom( const TQString& str )
33{
34 ParseList res;
35 const TQStringList lst = TQStringList::split( ';', str );
36 for( TQStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it ) {
37 if ( (*it).isEmpty() )
38 continue;
39 // parse "uid,email"
40 TQStringList helpList = TQStringList::split( ',', (*it), true );
41 Q_ASSERT( !helpList.isEmpty() );
42 if ( helpList.isEmpty() )
43 continue;
44 Q_ASSERT( helpList.count() < 3 ); // 1 or 2 items, but not more
45 const TQString uid = helpList.first();
46 const TQString email = helpList.last();
47 res.append( qMakePair( uid, email ) );
48 }
49 return res;
50}
51
52void KPIM::DistributionList::insertEntry( const Addressee& addr, const TQString& email )
53{
54 // insertEntry will removeEntry(uid), but not with formattedName
55 removeEntry( addr.formattedName(), email );
56 insertEntry( addr.uid(), email );
57}
58
59void KPIM::DistributionList::insertEntry( const TQString& uid, const TQString& email )
60{
61 Q_ASSERT( !email.isEmpty() || email.isNull() ); // hopefully never called with "", would lead to confusion
62 removeEntry( uid, email ); // avoid duplicates
63 TQString str = custom( "KADDRESSBOOK", s_customFieldName );
64 // Assumption: UIDs don't contain ; nor ,
65 str += ";" + uid + "," + email;
66 insertCustom( "KADDRESSBOOK", s_customFieldName, str ); // replace old value
67}
68
69void KPIM::DistributionList::removeEntry( const Addressee& addr, const TQString& email )
70{
71 removeEntry( addr.uid(), email );
72 // Also remove entries with the full name as uid (for the kolab thing)
73 removeEntry( addr.formattedName(), email );
74}
75
76void KPIM::DistributionList::removeEntry( const TQString& uid, const TQString& email )
77{
78 Q_ASSERT( !email.isEmpty() || email.isNull() ); // hopefully never called with "", would lead to confusion
79 ParseList parseList = parseCustom( custom( "KADDRESSBOOK", s_customFieldName ) );
80 TQString str;
81 for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) {
82 const TQString thisUid = (*it).first;
83 const TQString thisEmail = (*it).second;
84 if ( thisUid == uid && thisEmail == email ) {
85 continue; // remove that one
86 }
87 str += ";" + thisUid + "," + thisEmail;
88 }
89 if ( str.isEmpty() )
90 str = ";"; // keep something, for isDistributionList to work
91 insertCustom( "KADDRESSBOOK", s_customFieldName, str ); // replace old value
92}
93
94bool KPIM::DistributionList::isDistributionList( const TDEABC::Addressee& addr )
95{
96 const TQString str = addr.custom( "KADDRESSBOOK", s_customFieldName );
97 return !str.isEmpty();
98}
99
100// ###### KDE4: add findByFormattedName to TDEABC::AddressBook
101static TDEABC::Addressee::List findByFormattedName( TDEABC::AddressBook* book,
102 const TQString& name,
103 bool caseSensitive = true )
104{
105 TDEABC::Addressee::List res;
106 TDEABC::AddressBook::Iterator abIt;
107 for ( abIt = book->begin(); abIt != book->end(); ++abIt )
108 {
109 if ( caseSensitive && (*abIt).formattedName() == name )
110 res.append( *abIt );
111 if ( !caseSensitive && (*abIt).formattedName().lower() == name.lower() )
112 res.append( *abIt );
113 }
114 return res;
115}
116
117KPIM::DistributionList KPIM::DistributionList::findByName( TDEABC::AddressBook* book,
118 const TQString& name,
119 bool caseSensitive )
120{
121 TDEABC::AddressBook::Iterator abIt;
122 for ( abIt = book->begin(); abIt != book->end(); ++abIt )
123 {
124 if ( isDistributionList( *abIt ) ) {
125 if ( caseSensitive && (*abIt).formattedName() == name )
126 return *abIt;
127 if ( !caseSensitive && (*abIt).formattedName().lower() == name.lower() )
128 return *abIt;
129 }
130 }
131 return DistributionList();
132}
133
134static TDEABC::Addressee findByUidOrName( TDEABC::AddressBook* book, const TQString& uidOrName, const TQString& email )
135{
136 TDEABC::Addressee a = book->findByUid( uidOrName );
137 if ( a.isEmpty() ) {
138 // UID not found, maybe it is a name instead.
139 // If we have an email, let's use that for the lookup.
140 // [This is used by e.g. the Kolab resource]
141 if ( !email.isEmpty() ) {
142 TDEABC::Addressee::List lst = book->findByEmail( email );
143 TDEABC::Addressee::List::ConstIterator listit = lst.begin();
144 for ( ; listit != lst.end(); ++listit )
145 if ( (*listit).formattedName() == uidOrName ) {
146 a = *listit;
147 break;
148 }
149 if ( !lst.isEmpty() && a.isEmpty() ) { // found that email, but no match on the fullname
150 a = lst.first(); // probably the last name changed
151 }
152 }
153 // If we don't have an email, or if we didn't find any match for it, look up by full name
154 if ( a.isEmpty() ) {
155 // (But this has to be done here, since when loading we might not have the entries yet)
156 TDEABC::Addressee::List lst = findByFormattedName( book, uidOrName );
157 if ( !lst.isEmpty() )
158 a = lst.first();
159 }
160 }
161 return a;
162}
163
164KPIM::DistributionList::Entry::List KPIM::DistributionList::entries( TDEABC::AddressBook* book ) const
165{
166 Entry::List res;
167 const TQString str = custom( "KADDRESSBOOK", s_customFieldName );
168 const ParseList parseList = parseCustom( str );
169 for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) {
170 const TQString uid = (*it).first;
171 const TQString email = (*it).second;
172 // look up contact
173 TDEABC::Addressee a = findByUidOrName( book, uid, email );
174 if ( a.isEmpty() ) {
175 // ## The old DistributionListManager had a "missing entries" list...
176 kdWarning() << "Addressee not found: " << uid << endl;
177 } else {
178 res.append( Entry( a, email ) );
179 }
180 }
181 return res;
182}
183
184TQStringList KPIM::DistributionList::emails( TDEABC::AddressBook* book ) const
185{
186 TQStringList emails;
187
188 const TQString str = custom( "KADDRESSBOOK", s_customFieldName );
189 ParseList parseList = parseCustom( str );
190 for( ParseList::ConstIterator it = parseList.begin(); it != parseList.end(); ++it ) {
191 const TQString thisUid = (*it).first;
192 const TQString thisEmail = (*it).second;
193
194 // look up contact
195 TDEABC::Addressee a = findByUidOrName( book, thisUid, thisEmail );
196 if ( a.isEmpty() ) {
197 // ## The old DistributionListManager had a "missing entries" list...
198 continue;
199 }
200
201 const TQString email = thisEmail.isEmpty() ? a.fullEmail() :
202 a.fullEmail( thisEmail );
203 if ( !email.isEmpty() ) {
204 emails.append( email );
205 }
206 }
207
208 return emails;
209}
210
211TQValueList<KPIM::DistributionList>
212 KPIM::DistributionList::allDistributionLists( TDEABC::AddressBook* book )
213{
214 TQValueList<KPIM::DistributionList> lst;
215 TDEABC::AddressBook::Iterator abIt;
216 for ( abIt = book->begin(); abIt != book->end(); ++abIt )
217 {
218 if ( isDistributionList( *abIt ) ) {
219 lst.append( KPIM::DistributionList( *abIt ) );
220 }
221 }
222 return lst;
223}
Distribution list of email addresses.
void insertEntry(const Addressee &, const TQString &email=TQString())
Insert an entry into this distribution list.
void setName(const TQString &name)
HACK: reimplemented from Addressee, but it's NOT virtual there.
void removeEntry(const Addressee &, const TQString &email=TQString())
Remove an entry from this distribution list.
DistributionList()
Create a distribution list.
Entry::List entries(TDEABC::AddressBook *book) const
Return list of entries belonging to this distribution list.
TQStringList emails(TDEABC::AddressBook *book) const
Return list of email addresses, which belong to this distributon list.
Distribution List Entry.