• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kaddressbook

kabtools.cpp

Go to the documentation of this file.
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 <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); //TODO: Should this be left on disk?
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 ) //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 }

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal