kabtools.cpp
00001 /* 00002 This file is part of KAddressBook. 00003 Copyright (c) 2005 Tobias Koenig <tokoe@kde.org> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 00019 As a special exception, permission is given to link this program 00020 with any edition of TQt, and distribute the resulting executable, 00021 without including the source code for TQt in the source distribution. 00022 */ 00023 00024 #include <kabc/addressbook.h> 00025 #include <kabc/vcardconverter.h> 00026 #include <kapplication.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 KABC::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, KABC::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 KABC::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 KABC::VCardConverter converter; 00081 KABC::Addressee::List list; 00082 list.append( addressee ); 00083 #if defined(KABC_VCARD_ENCODING_FIX) 00084 const TQCString vcard = converter.createVCardsRaw( list, KABC::VCardConverter::v3_0 ); 00085 file.writeBlock( vcard, vcard.length() ); 00086 #else 00087 TQString vcard = converter.createVCards( list, KABC::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( KABC::Picture &master, const KABC::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 KABC::Addressee KABTools::mergeContacts( const KABC::Addressee::List &list ) 00127 { 00128 if ( list.count() == 0 ) //emtpy 00129 return KABC::Addressee(); 00130 else if ( list.count() == 1 ) // nothing to merge 00131 return list.first(); 00132 00133 KABC::Addressee masterAddressee = list.first(); 00134 00135 KABC::Addressee::List::ConstIterator contactIt( list.begin() ); 00136 const KABC::Addressee::List::ConstIterator contactEndIt( list.end() ); 00137 for ( ++contactIt; contactIt != contactEndIt; ++contactIt ) { 00138 // ADR + LABEL 00139 const KABC::Address::List addresses = (*contactIt).addresses(); 00140 KABC::Address::List masterAddresses = masterAddressee.addresses(); 00141 KABC::Address::List::ConstIterator addrIt( addresses.begin() ); 00142 const KABC::Address::List::ConstIterator addrEndIt( addresses.end() ); 00143 for ( ; addrIt != addrEndIt; ++addrIt ) { 00144 if ( !masterAddresses.contains( *addrIt ) ) 00145 masterAddressee.insertAddress( *addrIt ); 00146 } 00147 00148 // BIRTHDAY 00149 if ( masterAddressee.birthday().isNull() && !(*contactIt).birthday().isNull() ) 00150 masterAddressee.setBirthday( (*contactIt).birthday() ); 00151 00152 // CATEGORIES 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 // CLASS 00164 if ( !masterAddressee.secrecy().isValid() && (*contactIt).secrecy().isValid() ) 00165 masterAddressee.setSecrecy( (*contactIt).secrecy() ); 00166 00167 // EMAIL 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 // FN 00176 if ( masterAddressee.formattedName().isEmpty() && !(*contactIt).formattedName().isEmpty() ) 00177 masterAddressee.setFormattedName( (*contactIt).formattedName() ); 00178 00179 // GEO 00180 if ( !masterAddressee.geo().isValid() && (*contactIt).geo().isValid() ) 00181 masterAddressee.setGeo( (*contactIt).geo() ); 00182 00183 /* 00184 // KEY 00185 */ 00186 // LOGO 00187 KABC::Picture logo = masterAddressee.logo(); 00188 mergePictures( logo, (*contactIt).logo() ); 00189 masterAddressee.setLogo( logo ); 00190 00191 // MAILER 00192 if ( masterAddressee.mailer().isEmpty() && !(*contactIt).mailer().isEmpty() ) 00193 masterAddressee.setMailer( (*contactIt).mailer() ); 00194 00195 // N 00196 if ( masterAddressee.assembledName().isEmpty() && !(*contactIt).assembledName().isEmpty() ) 00197 masterAddressee.setNameFromString( (*contactIt).assembledName() ); 00198 00199 // NICKNAME 00200 if ( masterAddressee.nickName().isEmpty() && !(*contactIt).nickName().isEmpty() ) 00201 masterAddressee.setNickName( (*contactIt).nickName() ); 00202 00203 // NOTE 00204 if ( masterAddressee.note().isEmpty() && !(*contactIt).note().isEmpty() ) 00205 masterAddressee.setNote( (*contactIt).note() ); 00206 00207 // ORG 00208 if ( masterAddressee.organization().isEmpty() && !(*contactIt).organization().isEmpty() ) 00209 masterAddressee.setOrganization( (*contactIt).organization() ); 00210 00211 // PHOTO 00212 KABC::Picture photo = masterAddressee.photo(); 00213 mergePictures( photo, (*contactIt).photo() ); 00214 masterAddressee.setPhoto( photo ); 00215 00216 // PROID 00217 if ( masterAddressee.productId().isEmpty() && !(*contactIt).productId().isEmpty() ) 00218 masterAddressee.setProductId( (*contactIt).productId() ); 00219 00220 // REV 00221 if ( masterAddressee.revision().isNull() && !(*contactIt).revision().isNull() ) 00222 masterAddressee.setRevision( (*contactIt).revision() ); 00223 00224 // ROLE 00225 if ( masterAddressee.role().isEmpty() && !(*contactIt).role().isEmpty() ) 00226 masterAddressee.setRole( (*contactIt).role() ); 00227 00228 // SORT-STRING 00229 if ( masterAddressee.sortString().isEmpty() && !(*contactIt).sortString().isEmpty() ) 00230 masterAddressee.setSortString( (*contactIt).sortString() ); 00231 00232 /* 00233 // SOUND 00234 */ 00235 00236 // TEL 00237 const KABC::PhoneNumber::List phones = (*contactIt).phoneNumbers(); 00238 const KABC::PhoneNumber::List masterPhones = masterAddressee.phoneNumbers(); 00239 KABC::PhoneNumber::List::ConstIterator phoneIt( phones.begin() ); 00240 const KABC::PhoneNumber::List::ConstIterator phoneEndIt( phones.end() ); 00241 for ( ; phoneIt != phoneEndIt; ++phoneIt ) 00242 if ( !masterPhones.contains( *phoneIt ) ) 00243 masterAddressee.insertPhoneNumber( *phoneIt ); 00244 00245 // TITLE 00246 if ( masterAddressee.title().isEmpty() && !(*contactIt).title().isEmpty() ) 00247 masterAddressee.setTitle( (*contactIt).title() ); 00248 00249 // TZ 00250 if ( !masterAddressee.timeZone().isValid() && (*contactIt).timeZone().isValid() ) 00251 masterAddressee.setTimeZone( (*contactIt).timeZone() ); 00252 00253 // UID // ignore UID 00254 00255 // URL 00256 if ( masterAddressee.url().isEmpty() && !(*contactIt).url().isEmpty() ) 00257 masterAddressee.setUrl( (*contactIt).url() ); 00258 00259 // X- 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 }