kaddressbook

kabtools.cpp
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#include <tdeabc/addressbook.h>
25#include <tdeabc/vcardconverter.h>
26#include <tdeapplication.h>
27#include <kdebug.h>
28#include <ktempdir.h>
29
30#include <tqfile.h>
31
32#include "kabtools.h"
33
34static TQString uniqueFileName( const TDEABC::Addressee &addressee, TQStringList &existingFiles )
35{
36 TQString name;
37 TQString uniquePart;
38
39 uint number = 0;
40 do {
41 name = addressee.givenName() + "_" + addressee.familyName() + uniquePart + ".vcf";
42 name.replace( ' ', '_' );
43 name.replace( '/', '_' );
44
45 ++number;
46 uniquePart = TQString( "_%1" ).arg( number );
47 } while ( existingFiles.contains( name ) );
48
49 existingFiles.append( name );
50
51 return name;
52}
53
54void KABTools::mailVCards( const TQStringList &uids, TDEABC::AddressBook *ab )
55{
56 KURL::List urls;
57
58 KTempDir tempDir;
59 if ( tempDir.status() != 0 ) {
60 kdWarning() << strerror( tempDir.status() ) << endl;
61 return;
62 }
63
64 TQStringList existingFiles;
65 TQStringList::ConstIterator it( uids.begin() );
66 const TQStringList::ConstIterator endIt( uids.end() );
67 for ( ; it != endIt; ++it ) {
68 TDEABC::Addressee addressee = ab->findByUid( *it );
69
70 if ( addressee.isEmpty() )
71 continue;
72
73 TQString fileName = uniqueFileName( addressee, existingFiles );
74
75 TQString path = tempDir.name() + "/" + fileName;
76
77 TQFile file( path );
78
79 if ( file.open( IO_WriteOnly ) ) {
80 TDEABC::VCardConverter converter;
81 TDEABC::Addressee::List list;
82 list.append( addressee );
83#if defined(KABC_VCARD_ENCODING_FIX)
84 const TQCString vcard = converter.createVCardsRaw( list, TDEABC::VCardConverter::v3_0 );
85 file.writeBlock( vcard, vcard.length() );
86#else
87 TQString vcard = converter.createVCards( list, TDEABC::VCardConverter::v3_0 );
88 TQTextStream t( &file );
89 t.setEncoding( TQTextStream::UnicodeUTF8 );
90 t << vcard;
91#endif
92 file.close();
93
94 KURL url( path );
95 url.setFileEncoding( "UTF-8" );
96 urls.append( url );
97 }
98 }
99
100 tdeApp->invokeMailer( TQString(), TQString(), TQString(),
101 TQString(),
102 TQString(),
103 TQString(),
104 urls.toStringList() );
105}
106
107static void mergePictures( TDEABC::Picture &master, const TDEABC::Picture &slave )
108{
109 if ( master.isIntern() ) {
110 if ( master.data().isNull() ) {
111 if ( slave.isIntern() && !slave.data().isNull() )
112 master.setData( slave.data() );
113 else if ( !slave.isIntern() && !slave.url().isEmpty() )
114 master.setUrl( slave.url() );
115 }
116 } else {
117 if ( master.url().isEmpty() ) {
118 if ( slave.isIntern() && !slave.data().isNull() )
119 master.setData( slave.data() );
120 else if ( !slave.isIntern() && !slave.url().isEmpty() )
121 master.setUrl( slave.url() );
122 }
123 }
124}
125
126TDEABC::Addressee KABTools::mergeContacts( const TDEABC::Addressee::List &list )
127{
128 if ( list.count() == 0 ) //emtpy
129 return TDEABC::Addressee();
130 else if ( list.count() == 1 ) // nothing to merge
131 return list.first();
132
133 TDEABC::Addressee masterAddressee = list.first();
134
135 TDEABC::Addressee::List::ConstIterator contactIt( list.begin() );
136 const TDEABC::Addressee::List::ConstIterator contactEndIt( list.end() );
137 for ( ++contactIt; contactIt != contactEndIt; ++contactIt ) {
138 // ADR + LABEL
139 const TDEABC::Address::List addresses = (*contactIt).addresses();
140 TDEABC::Address::List masterAddresses = masterAddressee.addresses();
141 TDEABC::Address::List::ConstIterator addrIt( addresses.begin() );
142 const TDEABC::Address::List::ConstIterator addrEndIt( addresses.end() );
143 for ( ; addrIt != addrEndIt; ++addrIt ) {
144 if ( !masterAddresses.contains( *addrIt ) )
145 masterAddressee.insertAddress( *addrIt );
146 }
147
148 // BIRTHDAY
149 if ( masterAddressee.birthday().isNull() && !(*contactIt).birthday().isNull() )
150 masterAddressee.setBirthday( (*contactIt).birthday() );
151
152 // CATEGORIES
153 const TQStringList categories = (*contactIt).categories();
154 const TQStringList masterCategories = masterAddressee.categories();
155 TQStringList newCategories( masterCategories );
156 TQStringList::ConstIterator it( categories.begin() );
157 TQStringList::ConstIterator endIt( categories.end() );
158 for ( it = categories.begin(); it != endIt; ++it )
159 if ( !masterCategories.contains( *it ) )
160 newCategories.append( *it );
161 masterAddressee.setCategories( newCategories );
162
163 // CLASS
164 if ( !masterAddressee.secrecy().isValid() && (*contactIt).secrecy().isValid() )
165 masterAddressee.setSecrecy( (*contactIt).secrecy() );
166
167 // EMAIL
168 const TQStringList emails = (*contactIt).emails();
169 const TQStringList masterEmails = masterAddressee.emails();
170 endIt = emails.end();
171 for ( it = emails.begin(); it != endIt; ++it )
172 if ( !masterEmails.contains( *it ) )
173 masterAddressee.insertEmail( *it, false );
174
175 // FN
176 if ( masterAddressee.formattedName().isEmpty() && !(*contactIt).formattedName().isEmpty() )
177 masterAddressee.setFormattedName( (*contactIt).formattedName() );
178
179 // GEO
180 if ( !masterAddressee.geo().isValid() && (*contactIt).geo().isValid() )
181 masterAddressee.setGeo( (*contactIt).geo() );
182
183/*
184 // KEY
185*/
186 // LOGO
187 TDEABC::Picture logo = masterAddressee.logo();
188 mergePictures( logo, (*contactIt).logo() );
189 masterAddressee.setLogo( logo );
190
191 // MAILER
192 if ( masterAddressee.mailer().isEmpty() && !(*contactIt).mailer().isEmpty() )
193 masterAddressee.setMailer( (*contactIt).mailer() );
194
195 // N
196 if ( masterAddressee.assembledName().isEmpty() && !(*contactIt).assembledName().isEmpty() )
197 masterAddressee.setNameFromString( (*contactIt).assembledName() );
198
199 // NICKNAME
200 if ( masterAddressee.nickName().isEmpty() && !(*contactIt).nickName().isEmpty() )
201 masterAddressee.setNickName( (*contactIt).nickName() );
202
203 // NOTE
204 if ( masterAddressee.note().isEmpty() && !(*contactIt).note().isEmpty() )
205 masterAddressee.setNote( (*contactIt).note() );
206
207 // ORG
208 if ( masterAddressee.organization().isEmpty() && !(*contactIt).organization().isEmpty() )
209 masterAddressee.setOrganization( (*contactIt).organization() );
210
211 // PHOTO
212 TDEABC::Picture photo = masterAddressee.photo();
213 mergePictures( photo, (*contactIt).photo() );
214 masterAddressee.setPhoto( photo );
215
216 // PROID
217 if ( masterAddressee.productId().isEmpty() && !(*contactIt).productId().isEmpty() )
218 masterAddressee.setProductId( (*contactIt).productId() );
219
220 // REV
221 if ( masterAddressee.revision().isNull() && !(*contactIt).revision().isNull() )
222 masterAddressee.setRevision( (*contactIt).revision() );
223
224 // ROLE
225 if ( masterAddressee.role().isEmpty() && !(*contactIt).role().isEmpty() )
226 masterAddressee.setRole( (*contactIt).role() );
227
228 // SORT-STRING
229 if ( masterAddressee.sortString().isEmpty() && !(*contactIt).sortString().isEmpty() )
230 masterAddressee.setSortString( (*contactIt).sortString() );
231
232/*
233 // SOUND
234*/
235
236 // TEL
237 const TDEABC::PhoneNumber::List phones = (*contactIt).phoneNumbers();
238 const TDEABC::PhoneNumber::List masterPhones = masterAddressee.phoneNumbers();
239 TDEABC::PhoneNumber::List::ConstIterator phoneIt( phones.begin() );
240 const TDEABC::PhoneNumber::List::ConstIterator phoneEndIt( phones.end() );
241 for ( ; phoneIt != phoneEndIt; ++phoneIt )
242 if ( !masterPhones.contains( *phoneIt ) )
243 masterAddressee.insertPhoneNumber( *phoneIt );
244
245 // TITLE
246 if ( masterAddressee.title().isEmpty() && !(*contactIt).title().isEmpty() )
247 masterAddressee.setTitle( (*contactIt).title() );
248
249 // TZ
250 if ( !masterAddressee.timeZone().isValid() && (*contactIt).timeZone().isValid() )
251 masterAddressee.setTimeZone( (*contactIt).timeZone() );
252
253 // UID // ignore UID
254
255 // URL
256 if ( masterAddressee.url().isEmpty() && !(*contactIt).url().isEmpty() )
257 masterAddressee.setUrl( (*contactIt).url() );
258
259 // X-
260 const TQStringList customs = (*contactIt).customs();
261 const TQStringList masterCustoms = masterAddressee.customs();
262 TQStringList newCustoms( masterCustoms );
263 endIt = customs.end();
264 for ( it = customs.begin(); it != endIt; ++it )
265 if ( !masterCustoms.contains( *it ) )
266 newCustoms.append( *it );
267 masterAddressee.setCustoms( newCustoms );
268 }
269
270 return masterAddressee;
271}