24 #include <tdeabc/addressbook.h>
25 #include <tdeabc/vcardconverter.h>
26 #include <tdeapplication.h>
34 static TQString uniqueFileName(
const TDEABC::Addressee &addressee, TQStringList &existingFiles )
41 name = addressee.givenName() +
"_" + addressee.familyName() + uniquePart +
".vcf";
42 name.replace(
' ',
'_' );
43 name.replace(
'/',
'_' );
46 uniquePart = TQString(
"_%1" ).arg( number );
47 }
while ( existingFiles.contains( name ) );
49 existingFiles.append( name );
54 void KABTools::mailVCards(
const TQStringList &uids, TDEABC::AddressBook *ab )
59 if ( tempDir.status() != 0 ) {
60 kdWarning() << strerror( tempDir.status() ) << endl;
64 TQStringList existingFiles;
65 TQStringList::ConstIterator it( uids.begin() );
66 const TQStringList::ConstIterator endIt( uids.end() );
67 for ( ; it != endIt; ++it ) {
68 TDEABC::Addressee addressee = ab->findByUid( *it );
70 if ( addressee.isEmpty() )
73 TQString fileName = uniqueFileName( addressee, existingFiles );
75 TQString path = tempDir.name() +
"/" + fileName;
79 if ( file.open( IO_WriteOnly ) ) {
80 TDEABC::VCardConverter converter;
81 TDEABC::Addressee::List list;
82 list.append( addressee );
83 #if defined(KABC_VCARD_ENCODING_FIX)
84 const TQCString vcard = converter.createVCardsRaw( list, TDEABC::VCardConverter::v3_0 );
85 file.writeBlock( vcard, vcard.length() );
87 TQString vcard = converter.createVCards( list, TDEABC::VCardConverter::v3_0 );
88 TQTextStream t( &file );
89 t.setEncoding( TQTextStream::UnicodeUTF8 );
95 url.setFileEncoding(
"UTF-8" );
100 kapp->invokeMailer( TQString(), TQString(), TQString(),
104 urls.toStringList() );
107 static void mergePictures( TDEABC::Picture &master,
const TDEABC::Picture &slave )
109 if ( master.isIntern() ) {
110 if ( master.data().isNull() ) {
111 if ( slave.isIntern() && !slave.data().isNull() )
112 master.setData( slave.data() );
113 else if ( !slave.isIntern() && !slave.url().isEmpty() )
114 master.setUrl( slave.url() );
117 if ( master.url().isEmpty() ) {
118 if ( slave.isIntern() && !slave.data().isNull() )
119 master.setData( slave.data() );
120 else if ( !slave.isIntern() && !slave.url().isEmpty() )
121 master.setUrl( slave.url() );
126 TDEABC::Addressee KABTools::mergeContacts(
const TDEABC::Addressee::List &list )
128 if ( list.count() == 0 )
129 return TDEABC::Addressee();
130 else if ( list.count() == 1 )
133 TDEABC::Addressee masterAddressee = list.first();
135 TDEABC::Addressee::List::ConstIterator contactIt( list.begin() );
136 const TDEABC::Addressee::List::ConstIterator contactEndIt( list.end() );
137 for ( ++contactIt; contactIt != contactEndIt; ++contactIt ) {
139 const TDEABC::Address::List addresses = (*contactIt).addresses();
140 TDEABC::Address::List masterAddresses = masterAddressee.addresses();
141 TDEABC::Address::List::ConstIterator addrIt( addresses.begin() );
142 const TDEABC::Address::List::ConstIterator addrEndIt( addresses.end() );
143 for ( ; addrIt != addrEndIt; ++addrIt ) {
144 if ( !masterAddresses.contains( *addrIt ) )
145 masterAddressee.insertAddress( *addrIt );
149 if ( masterAddressee.birthday().isNull() && !(*contactIt).birthday().isNull() )
150 masterAddressee.setBirthday( (*contactIt).birthday() );
153 const TQStringList categories = (*contactIt).categories();
154 const TQStringList masterCategories = masterAddressee.categories();
155 TQStringList newCategories( masterCategories );
156 TQStringList::ConstIterator it( categories.begin() );
157 TQStringList::ConstIterator endIt( categories.end() );
158 for ( it = categories.begin(); it != endIt; ++it )
159 if ( !masterCategories.contains( *it ) )
160 newCategories.append( *it );
161 masterAddressee.setCategories( newCategories );
164 if ( !masterAddressee.secrecy().isValid() && (*contactIt).secrecy().isValid() )
165 masterAddressee.setSecrecy( (*contactIt).secrecy() );
168 const TQStringList emails = (*contactIt).emails();
169 const TQStringList masterEmails = masterAddressee.emails();
170 endIt = emails.end();
171 for ( it = emails.begin(); it != endIt; ++it )
172 if ( !masterEmails.contains( *it ) )
173 masterAddressee.insertEmail( *it,
false );
176 if ( masterAddressee.formattedName().isEmpty() && !(*contactIt).formattedName().isEmpty() )
177 masterAddressee.setFormattedName( (*contactIt).formattedName() );
180 if ( !masterAddressee.geo().isValid() && (*contactIt).geo().isValid() )
181 masterAddressee.setGeo( (*contactIt).geo() );
187 TDEABC::Picture logo = masterAddressee.logo();
188 mergePictures( logo, (*contactIt).logo() );
189 masterAddressee.setLogo( logo );
192 if ( masterAddressee.mailer().isEmpty() && !(*contactIt).mailer().isEmpty() )
193 masterAddressee.setMailer( (*contactIt).mailer() );
196 if ( masterAddressee.assembledName().isEmpty() && !(*contactIt).assembledName().isEmpty() )
197 masterAddressee.setNameFromString( (*contactIt).assembledName() );
200 if ( masterAddressee.nickName().isEmpty() && !(*contactIt).nickName().isEmpty() )
201 masterAddressee.setNickName( (*contactIt).nickName() );
204 if ( masterAddressee.note().isEmpty() && !(*contactIt).note().isEmpty() )
205 masterAddressee.setNote( (*contactIt).note() );
208 if ( masterAddressee.organization().isEmpty() && !(*contactIt).organization().isEmpty() )
209 masterAddressee.setOrganization( (*contactIt).organization() );
212 TDEABC::Picture photo = masterAddressee.photo();
213 mergePictures( photo, (*contactIt).photo() );
214 masterAddressee.setPhoto( photo );
217 if ( masterAddressee.productId().isEmpty() && !(*contactIt).productId().isEmpty() )
218 masterAddressee.setProductId( (*contactIt).productId() );
221 if ( masterAddressee.revision().isNull() && !(*contactIt).revision().isNull() )
222 masterAddressee.setRevision( (*contactIt).revision() );
225 if ( masterAddressee.role().isEmpty() && !(*contactIt).role().isEmpty() )
226 masterAddressee.setRole( (*contactIt).role() );
229 if ( masterAddressee.sortString().isEmpty() && !(*contactIt).sortString().isEmpty() )
230 masterAddressee.setSortString( (*contactIt).sortString() );
237 const TDEABC::PhoneNumber::List phones = (*contactIt).phoneNumbers();
238 const TDEABC::PhoneNumber::List masterPhones = masterAddressee.phoneNumbers();
239 TDEABC::PhoneNumber::List::ConstIterator phoneIt( phones.begin() );
240 const TDEABC::PhoneNumber::List::ConstIterator phoneEndIt( phones.end() );
241 for ( ; phoneIt != phoneEndIt; ++phoneIt )
242 if ( !masterPhones.contains( *phoneIt ) )
243 masterAddressee.insertPhoneNumber( *phoneIt );
246 if ( masterAddressee.title().isEmpty() && !(*contactIt).title().isEmpty() )
247 masterAddressee.setTitle( (*contactIt).title() );
250 if ( !masterAddressee.timeZone().isValid() && (*contactIt).timeZone().isValid() )
251 masterAddressee.setTimeZone( (*contactIt).timeZone() );
256 if ( masterAddressee.url().isEmpty() && !(*contactIt).url().isEmpty() )
257 masterAddressee.setUrl( (*contactIt).url() );
260 const TQStringList customs = (*contactIt).customs();
261 const TQStringList masterCustoms = masterAddressee.customs();
262 TQStringList newCustoms( masterCustoms );
263 endIt = customs.end();
264 for ( it = customs.begin(); it != endIt; ++it )
265 if ( !masterCustoms.contains( *it ) )
266 newCustoms.append( *it );
267 masterAddressee.setCustoms( newCustoms );
270 return masterAddressee;