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