kaddressbook

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 Qt, and distribute the resulting executable,
00021     without including the source code for Qt 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 <qfile.h>
00031 
00032 #include "kabtools.h"
00033 
00034 static QString uniqueFileName( const KABC::Addressee &addressee, QStringList &existingFiles )
00035 {
00036   QString name;
00037   QString 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 = QString( "_%1" ).arg( number );
00047   } while ( existingFiles.contains( name ) );
00048 
00049   existingFiles.append( name );
00050 
00051   return name;
00052 }
00053 
00054 void KABTools::mailVCards( const QStringList &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   QStringList existingFiles;
00065   QStringList::ConstIterator it( uids.begin() );
00066   const QStringList::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     QString fileName = uniqueFileName( addressee, existingFiles );
00074 
00075     QString path = tempDir.name() + "/" + fileName;
00076 
00077     QFile file( path );
00078 
00079     if ( file.open( IO_WriteOnly ) ) {
00080       KABC::VCardConverter converter;
00081       KABC::Addressee::List list;
00082       list.append( addressee );
00083       QString vcard = converter.createVCards( list, KABC::VCardConverter::v3_0 );
00084 
00085       QTextStream t( &file );
00086       t.setEncoding( QTextStream::UnicodeUTF8 );
00087       t << vcard;
00088 
00089       file.close();
00090 
00091       KURL url( path );
00092       url.setFileEncoding( "UTF-8" );
00093       urls.append( url );
00094     }
00095   }
00096 
00097   kapp->invokeMailer( QString::null, QString::null, QString::null,
00098                       QString::null,
00099                       QString::null,
00100                       QString::null,
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 ) //emtpy
00126     return KABC::Addressee();
00127   else if ( list.count() == 1 ) // nothing to merge
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     // ADR + LABEL
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     // BIRTHDAY
00146     if ( masterAddressee.birthday().isNull() && !(*contactIt).birthday().isNull() )
00147       masterAddressee.setBirthday( (*contactIt).birthday() );
00148 
00149     // CATEGORIES
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     // CLASS
00161     if ( !masterAddressee.secrecy().isValid() && (*contactIt).secrecy().isValid() )
00162       masterAddressee.setSecrecy( (*contactIt).secrecy() );
00163 
00164     // EMAIL
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     // FN
00173     if ( masterAddressee.formattedName().isEmpty() && !(*contactIt).formattedName().isEmpty() )
00174       masterAddressee.setFormattedName( (*contactIt).formattedName() );
00175 
00176     // GEO
00177     if ( !masterAddressee.geo().isValid() && (*contactIt).geo().isValid() )
00178       masterAddressee.setGeo( (*contactIt).geo() );
00179 
00180 /*
00181   // KEY
00182 */
00183     // LOGO
00184     KABC::Picture logo = masterAddressee.logo();
00185     mergePictures( logo, (*contactIt).logo() );
00186     masterAddressee.setLogo( logo );
00187 
00188     // MAILER
00189     if ( masterAddressee.mailer().isEmpty() && !(*contactIt).mailer().isEmpty() )
00190       masterAddressee.setMailer( (*contactIt).mailer() );
00191 
00192     // N
00193     if ( masterAddressee.assembledName().isEmpty() && !(*contactIt).assembledName().isEmpty() )
00194       masterAddressee.setNameFromString( (*contactIt).assembledName() );
00195 
00196     // NICKNAME
00197     if ( masterAddressee.nickName().isEmpty() && !(*contactIt).nickName().isEmpty() )
00198       masterAddressee.setNickName( (*contactIt).nickName() );
00199 
00200     // NOTE
00201     if ( masterAddressee.note().isEmpty() && !(*contactIt).note().isEmpty() )
00202       masterAddressee.setNote( (*contactIt).note() );
00203 
00204     // ORG
00205     if ( masterAddressee.organization().isEmpty() && !(*contactIt).organization().isEmpty() )
00206       masterAddressee.setOrganization( (*contactIt).organization() );
00207 
00208     // PHOTO
00209     KABC::Picture photo = masterAddressee.photo();
00210     mergePictures( photo, (*contactIt).photo() );
00211     masterAddressee.setPhoto( photo );
00212 
00213     // PROID
00214     if ( masterAddressee.productId().isEmpty() && !(*contactIt).productId().isEmpty() )
00215       masterAddressee.setProductId( (*contactIt).productId() );
00216 
00217     // REV
00218     if ( masterAddressee.revision().isNull() && !(*contactIt).revision().isNull() )
00219       masterAddressee.setRevision( (*contactIt).revision() );
00220 
00221     // ROLE
00222     if ( masterAddressee.role().isEmpty() && !(*contactIt).role().isEmpty() )
00223       masterAddressee.setRole( (*contactIt).role() );
00224 
00225     // SORT-STRING
00226     if ( masterAddressee.sortString().isEmpty() && !(*contactIt).sortString().isEmpty() )
00227       masterAddressee.setSortString( (*contactIt).sortString() );
00228 
00229 /*
00230   // SOUND
00231 */
00232 
00233     // TEL
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     // TITLE
00243     if ( masterAddressee.title().isEmpty() && !(*contactIt).title().isEmpty() )
00244       masterAddressee.setTitle( (*contactIt).title() );
00245 
00246     // TZ
00247     if ( !masterAddressee.timeZone().isValid() && (*contactIt).timeZone().isValid() )
00248       masterAddressee.setTimeZone( (*contactIt).timeZone() );
00249 
00250     // UID // ignore UID
00251 
00252     // URL
00253     if ( masterAddressee.url().isEmpty() && !(*contactIt).url().isEmpty() )
00254       masterAddressee.setUrl( (*contactIt).url() );
00255 
00256     // X-
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 }
KDE Home | KDE Accessibility Home | Description of Access Keys