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