00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <tdeabc/addressbook.h>
00025 #include <tdeabc/vcardconverter.h>
00026 #include <tdeapplication.h>
00027 #include <kdebug.h>
00028 #include <ktempdir.h>
00029
00030 #include <tqfile.h>
00031
00032 #include "kabtools.h"
00033
00034 static TQString uniqueFileName( const TDEABC::Addressee &addressee, TQStringList &existingFiles )
00035 {
00036 TQString name;
00037 TQString uniquePart;
00038
00039 uint number = 0;
00040 do {
00041 name = addressee.givenName() + "_" + addressee.familyName() + uniquePart + ".vcf";
00042 name.replace( ' ', '_' );
00043 name.replace( '/', '_' );
00044
00045 ++number;
00046 uniquePart = TQString( "_%1" ).arg( number );
00047 } while ( existingFiles.contains( name ) );
00048
00049 existingFiles.append( name );
00050
00051 return name;
00052 }
00053
00054 void KABTools::mailVCards( const TQStringList &uids, TDEABC::AddressBook *ab )
00055 {
00056 KURL::List urls;
00057
00058 KTempDir tempDir;
00059 if ( tempDir.status() != 0 ) {
00060 kdWarning() << strerror( tempDir.status() ) << endl;
00061 return;
00062 }
00063
00064 TQStringList existingFiles;
00065 TQStringList::ConstIterator it( uids.begin() );
00066 const TQStringList::ConstIterator endIt( uids.end() );
00067 for ( ; it != endIt; ++it ) {
00068 TDEABC::Addressee addressee = ab->findByUid( *it );
00069
00070 if ( addressee.isEmpty() )
00071 continue;
00072
00073 TQString fileName = uniqueFileName( addressee, existingFiles );
00074
00075 TQString path = tempDir.name() + "/" + fileName;
00076
00077 TQFile file( path );
00078
00079 if ( file.open( IO_WriteOnly ) ) {
00080 TDEABC::VCardConverter converter;
00081 TDEABC::Addressee::List list;
00082 list.append( addressee );
00083 #if defined(KABC_VCARD_ENCODING_FIX)
00084 const TQCString vcard = converter.createVCardsRaw( list, TDEABC::VCardConverter::v3_0 );
00085 file.writeBlock( vcard, vcard.length() );
00086 #else
00087 TQString vcard = converter.createVCards( list, TDEABC::VCardConverter::v3_0 );
00088 TQTextStream t( &file );
00089 t.setEncoding( TQTextStream::UnicodeUTF8 );
00090 t << vcard;
00091 #endif
00092 file.close();
00093
00094 KURL url( path );
00095 url.setFileEncoding( "UTF-8" );
00096 urls.append( url );
00097 }
00098 }
00099
00100 kapp->invokeMailer( TQString(), TQString(), TQString(),
00101 TQString(),
00102 TQString(),
00103 TQString(),
00104 urls.toStringList() );
00105 }
00106
00107 static void mergePictures( TDEABC::Picture &master, const TDEABC::Picture &slave )
00108 {
00109 if ( master.isIntern() ) {
00110 if ( master.data().isNull() ) {
00111 if ( slave.isIntern() && !slave.data().isNull() )
00112 master.setData( slave.data() );
00113 else if ( !slave.isIntern() && !slave.url().isEmpty() )
00114 master.setUrl( slave.url() );
00115 }
00116 } else {
00117 if ( master.url().isEmpty() ) {
00118 if ( slave.isIntern() && !slave.data().isNull() )
00119 master.setData( slave.data() );
00120 else if ( !slave.isIntern() && !slave.url().isEmpty() )
00121 master.setUrl( slave.url() );
00122 }
00123 }
00124 }
00125
00126 TDEABC::Addressee KABTools::mergeContacts( const TDEABC::Addressee::List &list )
00127 {
00128 if ( list.count() == 0 )
00129 return TDEABC::Addressee();
00130 else if ( list.count() == 1 )
00131 return list.first();
00132
00133 TDEABC::Addressee masterAddressee = list.first();
00134
00135 TDEABC::Addressee::List::ConstIterator contactIt( list.begin() );
00136 const TDEABC::Addressee::List::ConstIterator contactEndIt( list.end() );
00137 for ( ++contactIt; contactIt != contactEndIt; ++contactIt ) {
00138
00139 const TDEABC::Address::List addresses = (*contactIt).addresses();
00140 TDEABC::Address::List masterAddresses = masterAddressee.addresses();
00141 TDEABC::Address::List::ConstIterator addrIt( addresses.begin() );
00142 const TDEABC::Address::List::ConstIterator addrEndIt( addresses.end() );
00143 for ( ; addrIt != addrEndIt; ++addrIt ) {
00144 if ( !masterAddresses.contains( *addrIt ) )
00145 masterAddressee.insertAddress( *addrIt );
00146 }
00147
00148
00149 if ( masterAddressee.birthday().isNull() && !(*contactIt).birthday().isNull() )
00150 masterAddressee.setBirthday( (*contactIt).birthday() );
00151
00152
00153 const TQStringList categories = (*contactIt).categories();
00154 const TQStringList masterCategories = masterAddressee.categories();
00155 TQStringList newCategories( masterCategories );
00156 TQStringList::ConstIterator it( categories.begin() );
00157 TQStringList::ConstIterator endIt( categories.end() );
00158 for ( it = categories.begin(); it != endIt; ++it )
00159 if ( !masterCategories.contains( *it ) )
00160 newCategories.append( *it );
00161 masterAddressee.setCategories( newCategories );
00162
00163
00164 if ( !masterAddressee.secrecy().isValid() && (*contactIt).secrecy().isValid() )
00165 masterAddressee.setSecrecy( (*contactIt).secrecy() );
00166
00167
00168 const TQStringList emails = (*contactIt).emails();
00169 const TQStringList masterEmails = masterAddressee.emails();
00170 endIt = emails.end();
00171 for ( it = emails.begin(); it != endIt; ++it )
00172 if ( !masterEmails.contains( *it ) )
00173 masterAddressee.insertEmail( *it, false );
00174
00175
00176 if ( masterAddressee.formattedName().isEmpty() && !(*contactIt).formattedName().isEmpty() )
00177 masterAddressee.setFormattedName( (*contactIt).formattedName() );
00178
00179
00180 if ( !masterAddressee.geo().isValid() && (*contactIt).geo().isValid() )
00181 masterAddressee.setGeo( (*contactIt).geo() );
00182
00183
00184
00185
00186
00187 TDEABC::Picture logo = masterAddressee.logo();
00188 mergePictures( logo, (*contactIt).logo() );
00189 masterAddressee.setLogo( logo );
00190
00191
00192 if ( masterAddressee.mailer().isEmpty() && !(*contactIt).mailer().isEmpty() )
00193 masterAddressee.setMailer( (*contactIt).mailer() );
00194
00195
00196 if ( masterAddressee.assembledName().isEmpty() && !(*contactIt).assembledName().isEmpty() )
00197 masterAddressee.setNameFromString( (*contactIt).assembledName() );
00198
00199
00200 if ( masterAddressee.nickName().isEmpty() && !(*contactIt).nickName().isEmpty() )
00201 masterAddressee.setNickName( (*contactIt).nickName() );
00202
00203
00204 if ( masterAddressee.note().isEmpty() && !(*contactIt).note().isEmpty() )
00205 masterAddressee.setNote( (*contactIt).note() );
00206
00207
00208 if ( masterAddressee.organization().isEmpty() && !(*contactIt).organization().isEmpty() )
00209 masterAddressee.setOrganization( (*contactIt).organization() );
00210
00211
00212 TDEABC::Picture photo = masterAddressee.photo();
00213 mergePictures( photo, (*contactIt).photo() );
00214 masterAddressee.setPhoto( photo );
00215
00216
00217 if ( masterAddressee.productId().isEmpty() && !(*contactIt).productId().isEmpty() )
00218 masterAddressee.setProductId( (*contactIt).productId() );
00219
00220
00221 if ( masterAddressee.revision().isNull() && !(*contactIt).revision().isNull() )
00222 masterAddressee.setRevision( (*contactIt).revision() );
00223
00224
00225 if ( masterAddressee.role().isEmpty() && !(*contactIt).role().isEmpty() )
00226 masterAddressee.setRole( (*contactIt).role() );
00227
00228
00229 if ( masterAddressee.sortString().isEmpty() && !(*contactIt).sortString().isEmpty() )
00230 masterAddressee.setSortString( (*contactIt).sortString() );
00231
00232
00233
00234
00235
00236
00237 const TDEABC::PhoneNumber::List phones = (*contactIt).phoneNumbers();
00238 const TDEABC::PhoneNumber::List masterPhones = masterAddressee.phoneNumbers();
00239 TDEABC::PhoneNumber::List::ConstIterator phoneIt( phones.begin() );
00240 const TDEABC::PhoneNumber::List::ConstIterator phoneEndIt( phones.end() );
00241 for ( ; phoneIt != phoneEndIt; ++phoneIt )
00242 if ( !masterPhones.contains( *phoneIt ) )
00243 masterAddressee.insertPhoneNumber( *phoneIt );
00244
00245
00246 if ( masterAddressee.title().isEmpty() && !(*contactIt).title().isEmpty() )
00247 masterAddressee.setTitle( (*contactIt).title() );
00248
00249
00250 if ( !masterAddressee.timeZone().isValid() && (*contactIt).timeZone().isValid() )
00251 masterAddressee.setTimeZone( (*contactIt).timeZone() );
00252
00253
00254
00255
00256 if ( masterAddressee.url().isEmpty() && !(*contactIt).url().isEmpty() )
00257 masterAddressee.setUrl( (*contactIt).url() );
00258
00259
00260 const TQStringList customs = (*contactIt).customs();
00261 const TQStringList masterCustoms = masterAddressee.customs();
00262 TQStringList newCustoms( masterCustoms );
00263 endIt = customs.end();
00264 for ( it = customs.begin(); it != endIt; ++it )
00265 if ( !masterCustoms.contains( *it ) )
00266 newCustoms.append( *it );
00267 masterAddressee.setCustoms( newCustoms );
00268 }
00269
00270 return masterAddressee;
00271 }