summaryrefslogtreecommitdiffabout
path: root/kabc/addressbook.cpp
Unidiff
Diffstat (limited to 'kabc/addressbook.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addressbook.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
index 16927e2..d037d2f 100644
--- a/kabc/addressbook.cpp
+++ b/kabc/addressbook.cpp
@@ -1,1032 +1,1076 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28/*US 28/*US
29 29
30#include <qfile.h> 30#include <qfile.h>
31#include <qregexp.h> 31#include <qregexp.h>
32#include <qtimer.h> 32#include <qtimer.h>
33 33
34#include <kapplication.h> 34#include <kapplication.h>
35#include <kinstance.h> 35#include <kinstance.h>
36#include <kstandarddirs.h> 36#include <kstandarddirs.h>
37 37
38#include "errorhandler.h" 38#include "errorhandler.h"
39*/ 39*/
40#include <qptrlist.h> 40#include <qptrlist.h>
41#include <qtextstream.h> 41#include <qtextstream.h>
42#include <qfile.h> 42#include <qfile.h>
43#include <qregexp.h> 43#include <qregexp.h>
44 44
45#include <kglobal.h> 45#include <kglobal.h>
46#include <klocale.h> 46#include <klocale.h>
47#include <kmessagebox.h> 47#include <kmessagebox.h>
48#include <kdebug.h> 48#include <kdebug.h>
49#include <libkcal/syncdefines.h> 49#include <libkcal/syncdefines.h>
50#include <libkdepim/phoneaccess.h>
50#include "addressbook.h" 51#include "addressbook.h"
51#include "resource.h" 52#include "resource.h"
52#include "vcardconverter.h" 53#include "vcardconverter.h"
53#include "vcardparser/vcardtool.h" 54#include "vcardparser/vcardtool.h"
54 55
55//US #include "addressbook.moc" 56//US #include "addressbook.moc"
56 57
57using namespace KABC; 58using namespace KABC;
58 59
59struct AddressBook::AddressBookData 60struct AddressBook::AddressBookData
60{ 61{
61 Addressee::List mAddressees; 62 Addressee::List mAddressees;
62 Addressee::List mRemovedAddressees; 63 Addressee::List mRemovedAddressees;
63 Field::List mAllFields; 64 Field::List mAllFields;
64 KConfig *mConfig; 65 KConfig *mConfig;
65 KRES::Manager<Resource> *mManager; 66 KRES::Manager<Resource> *mManager;
66//US ErrorHandler *mErrorHandler; 67//US ErrorHandler *mErrorHandler;
67}; 68};
68 69
69struct AddressBook::Iterator::IteratorData 70struct AddressBook::Iterator::IteratorData
70{ 71{
71 Addressee::List::Iterator mIt; 72 Addressee::List::Iterator mIt;
72}; 73};
73 74
74struct AddressBook::ConstIterator::ConstIteratorData 75struct AddressBook::ConstIterator::ConstIteratorData
75{ 76{
76 Addressee::List::ConstIterator mIt; 77 Addressee::List::ConstIterator mIt;
77}; 78};
78 79
79AddressBook::Iterator::Iterator() 80AddressBook::Iterator::Iterator()
80{ 81{
81 d = new IteratorData; 82 d = new IteratorData;
82} 83}
83 84
84AddressBook::Iterator::Iterator( const AddressBook::Iterator &i ) 85AddressBook::Iterator::Iterator( const AddressBook::Iterator &i )
85{ 86{
86 d = new IteratorData; 87 d = new IteratorData;
87 d->mIt = i.d->mIt; 88 d->mIt = i.d->mIt;
88} 89}
89 90
90AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i ) 91AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i )
91{ 92{
92 if( this == &i ) return *this; // guard against self assignment 93 if( this == &i ) return *this; // guard against self assignment
93 delete d; // delete the old data the Iterator was completely constructed before 94 delete d; // delete the old data the Iterator was completely constructed before
94 d = new IteratorData; 95 d = new IteratorData;
95 d->mIt = i.d->mIt; 96 d->mIt = i.d->mIt;
96 return *this; 97 return *this;
97} 98}
98 99
99AddressBook::Iterator::~Iterator() 100AddressBook::Iterator::~Iterator()
100{ 101{
101 delete d; 102 delete d;
102} 103}
103 104
104const Addressee &AddressBook::Iterator::operator*() const 105const Addressee &AddressBook::Iterator::operator*() const
105{ 106{
106 return *(d->mIt); 107 return *(d->mIt);
107} 108}
108 109
109Addressee &AddressBook::Iterator::operator*() 110Addressee &AddressBook::Iterator::operator*()
110{ 111{
111 return *(d->mIt); 112 return *(d->mIt);
112} 113}
113 114
114Addressee *AddressBook::Iterator::operator->() 115Addressee *AddressBook::Iterator::operator->()
115{ 116{
116 return &(*(d->mIt)); 117 return &(*(d->mIt));
117} 118}
118 119
119AddressBook::Iterator &AddressBook::Iterator::operator++() 120AddressBook::Iterator &AddressBook::Iterator::operator++()
120{ 121{
121 (d->mIt)++; 122 (d->mIt)++;
122 return *this; 123 return *this;
123} 124}
124 125
125AddressBook::Iterator &AddressBook::Iterator::operator++(int) 126AddressBook::Iterator &AddressBook::Iterator::operator++(int)
126{ 127{
127 (d->mIt)++; 128 (d->mIt)++;
128 return *this; 129 return *this;
129} 130}
130 131
131AddressBook::Iterator &AddressBook::Iterator::operator--() 132AddressBook::Iterator &AddressBook::Iterator::operator--()
132{ 133{
133 (d->mIt)--; 134 (d->mIt)--;
134 return *this; 135 return *this;
135} 136}
136 137
137AddressBook::Iterator &AddressBook::Iterator::operator--(int) 138AddressBook::Iterator &AddressBook::Iterator::operator--(int)
138{ 139{
139 (d->mIt)--; 140 (d->mIt)--;
140 return *this; 141 return *this;
141} 142}
142 143
143bool AddressBook::Iterator::operator==( const Iterator &it ) 144bool AddressBook::Iterator::operator==( const Iterator &it )
144{ 145{
145 return ( d->mIt == it.d->mIt ); 146 return ( d->mIt == it.d->mIt );
146} 147}
147 148
148bool AddressBook::Iterator::operator!=( const Iterator &it ) 149bool AddressBook::Iterator::operator!=( const Iterator &it )
149{ 150{
150 return ( d->mIt != it.d->mIt ); 151 return ( d->mIt != it.d->mIt );
151} 152}
152 153
153 154
154AddressBook::ConstIterator::ConstIterator() 155AddressBook::ConstIterator::ConstIterator()
155{ 156{
156 d = new ConstIteratorData; 157 d = new ConstIteratorData;
157} 158}
158 159
159AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i ) 160AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i )
160{ 161{
161 d = new ConstIteratorData; 162 d = new ConstIteratorData;
162 d->mIt = i.d->mIt; 163 d->mIt = i.d->mIt;
163} 164}
164 165
165AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i ) 166AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i )
166{ 167{
167 if( this == &i ) return *this; // guard for self assignment 168 if( this == &i ) return *this; // guard for self assignment
168 delete d; // delete the old data because the Iterator was really constructed before 169 delete d; // delete the old data because the Iterator was really constructed before
169 d = new ConstIteratorData; 170 d = new ConstIteratorData;
170 d->mIt = i.d->mIt; 171 d->mIt = i.d->mIt;
171 return *this; 172 return *this;
172} 173}
173 174
174AddressBook::ConstIterator::~ConstIterator() 175AddressBook::ConstIterator::~ConstIterator()
175{ 176{
176 delete d; 177 delete d;
177} 178}
178 179
179const Addressee &AddressBook::ConstIterator::operator*() const 180const Addressee &AddressBook::ConstIterator::operator*() const
180{ 181{
181 return *(d->mIt); 182 return *(d->mIt);
182} 183}
183 184
184const Addressee* AddressBook::ConstIterator::operator->() const 185const Addressee* AddressBook::ConstIterator::operator->() const
185{ 186{
186 return &(*(d->mIt)); 187 return &(*(d->mIt));
187} 188}
188 189
189AddressBook::ConstIterator &AddressBook::ConstIterator::operator++() 190AddressBook::ConstIterator &AddressBook::ConstIterator::operator++()
190{ 191{
191 (d->mIt)++; 192 (d->mIt)++;
192 return *this; 193 return *this;
193} 194}
194 195
195AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int) 196AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int)
196{ 197{
197 (d->mIt)++; 198 (d->mIt)++;
198 return *this; 199 return *this;
199} 200}
200 201
201AddressBook::ConstIterator &AddressBook::ConstIterator::operator--() 202AddressBook::ConstIterator &AddressBook::ConstIterator::operator--()
202{ 203{
203 (d->mIt)--; 204 (d->mIt)--;
204 return *this; 205 return *this;
205} 206}
206 207
207AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int) 208AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int)
208{ 209{
209 (d->mIt)--; 210 (d->mIt)--;
210 return *this; 211 return *this;
211} 212}
212 213
213bool AddressBook::ConstIterator::operator==( const ConstIterator &it ) 214bool AddressBook::ConstIterator::operator==( const ConstIterator &it )
214{ 215{
215 return ( d->mIt == it.d->mIt ); 216 return ( d->mIt == it.d->mIt );
216} 217}
217 218
218bool AddressBook::ConstIterator::operator!=( const ConstIterator &it ) 219bool AddressBook::ConstIterator::operator!=( const ConstIterator &it )
219{ 220{
220 return ( d->mIt != it.d->mIt ); 221 return ( d->mIt != it.d->mIt );
221} 222}
222 223
223 224
224AddressBook::AddressBook() 225AddressBook::AddressBook()
225{ 226{
226 init(0, "contact"); 227 init(0, "contact");
227} 228}
228 229
229AddressBook::AddressBook( const QString &config ) 230AddressBook::AddressBook( const QString &config )
230{ 231{
231 init(config, "contact"); 232 init(config, "contact");
232} 233}
233 234
234AddressBook::AddressBook( const QString &config, const QString &family ) 235AddressBook::AddressBook( const QString &config, const QString &family )
235{ 236{
236 init(config, family); 237 init(config, family);
237 238
238} 239}
239 240
240// the default family is "contact" 241// the default family is "contact"
241void AddressBook::init(const QString &config, const QString &family ) 242void AddressBook::init(const QString &config, const QString &family )
242{ 243{
243 blockLSEchange = false; 244 blockLSEchange = false;
244 d = new AddressBookData; 245 d = new AddressBookData;
245 QString fami = family; 246 QString fami = family;
246 if (config != 0) { 247 if (config != 0) {
247 if ( family == "syncContact" ) { 248 if ( family == "syncContact" ) {
248 qDebug("creating sync config "); 249 qDebug("creating sync config ");
249 fami = "contact"; 250 fami = "contact";
250 KConfig* con = new KConfig( locateLocal("config", "syncContactrc") ); 251 KConfig* con = new KConfig( locateLocal("config", "syncContactrc") );
251 con->setGroup( "General" ); 252 con->setGroup( "General" );
252 con->writeEntry( "ResourceKeys", QString("sync") ); 253 con->writeEntry( "ResourceKeys", QString("sync") );
253 con->writeEntry( "Standard", QString("sync") ); 254 con->writeEntry( "Standard", QString("sync") );
254 con->setGroup( "Resource_sync" ); 255 con->setGroup( "Resource_sync" );
255 con->writeEntry( "FileName", config ); 256 con->writeEntry( "FileName", config );
256 con->writeEntry( "FileFormat", QString("vcard") ); 257 con->writeEntry( "FileFormat", QString("vcard") );
257 con->writeEntry( "ResourceIdentifier", QString("sync") ); 258 con->writeEntry( "ResourceIdentifier", QString("sync") );
258 con->writeEntry( "ResourceName", QString("sync_res") ); 259 con->writeEntry( "ResourceName", QString("sync_res") );
259 if ( config.right(4) == ".xml" ) 260 if ( config.right(4) == ".xml" )
260 con->writeEntry( "ResourceType", QString("qtopia") ); 261 con->writeEntry( "ResourceType", QString("qtopia") );
261 else if ( config == "sharp" ) { 262 else if ( config == "sharp" ) {
262 con->writeEntry( "ResourceType", QString("sharp") ); 263 con->writeEntry( "ResourceType", QString("sharp") );
263 } else { 264 } else {
264 con->writeEntry( "ResourceType", QString("file") ); 265 con->writeEntry( "ResourceType", QString("file") );
265 } 266 }
266 //con->sync(); 267 //con->sync();
267 d->mConfig = con; 268 d->mConfig = con;
268 } 269 }
269 else 270 else
270 d->mConfig = new KConfig( locateLocal("config", config) ); 271 d->mConfig = new KConfig( locateLocal("config", config) );
271// qDebug("AddressBook::init 1 config=%s",config.latin1() ); 272// qDebug("AddressBook::init 1 config=%s",config.latin1() );
272 } 273 }
273 else { 274 else {
274 d->mConfig = 0; 275 d->mConfig = 0;
275// qDebug("AddressBook::init 1 config=0"); 276// qDebug("AddressBook::init 1 config=0");
276 } 277 }
277 278
278//US d->mErrorHandler = 0; 279//US d->mErrorHandler = 0;
279 d->mManager = new KRES::Manager<Resource>( fami, false ); 280 d->mManager = new KRES::Manager<Resource>( fami, false );
280 d->mManager->readConfig( d->mConfig ); 281 d->mManager->readConfig( d->mConfig );
281 if ( family == "syncContact" ) { 282 if ( family == "syncContact" ) {
282 KRES::Manager<Resource> *manager = d->mManager; 283 KRES::Manager<Resource> *manager = d->mManager;
283 KRES::Manager<Resource>::ActiveIterator it; 284 KRES::Manager<Resource>::ActiveIterator it;
284 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 285 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
285 (*it)->setAddressBook( this ); 286 (*it)->setAddressBook( this );
286 if ( !(*it)->open() ) 287 if ( !(*it)->open() )
287 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) ); 288 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) );
288 } 289 }
289 Resource *res = standardResource(); 290 Resource *res = standardResource();
290 if ( !res ) { 291 if ( !res ) {
291 qDebug("ERROR: no standard resource"); 292 qDebug("ERROR: no standard resource");
292 res = manager->createResource( "file" ); 293 res = manager->createResource( "file" );
293 if ( res ) 294 if ( res )
294 { 295 {
295 addResource( res ); 296 addResource( res );
296 } 297 }
297 else 298 else
298 qDebug(" No resource available!!!"); 299 qDebug(" No resource available!!!");
299 } 300 }
300 setStandardResource( res ); 301 setStandardResource( res );
301 manager->writeConfig(); 302 manager->writeConfig();
302 } 303 }
303 addCustomField( i18n( "Department" ), KABC::Field::Organization, 304 addCustomField( i18n( "Department" ), KABC::Field::Organization,
304 "X-Department", "KADDRESSBOOK" ); 305 "X-Department", "KADDRESSBOOK" );
305 addCustomField( i18n( "Profession" ), KABC::Field::Organization, 306 addCustomField( i18n( "Profession" ), KABC::Field::Organization,
306 "X-Profession", "KADDRESSBOOK" ); 307 "X-Profession", "KADDRESSBOOK" );
307 addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization, 308 addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization,
308 "X-AssistantsName", "KADDRESSBOOK" ); 309 "X-AssistantsName", "KADDRESSBOOK" );
309 addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization, 310 addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization,
310 "X-ManagersName", "KADDRESSBOOK" ); 311 "X-ManagersName", "KADDRESSBOOK" );
311 addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal, 312 addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal,
312 "X-SpousesName", "KADDRESSBOOK" ); 313 "X-SpousesName", "KADDRESSBOOK" );
313 addCustomField( i18n( "Office" ), KABC::Field::Personal, 314 addCustomField( i18n( "Office" ), KABC::Field::Personal,
314 "X-Office", "KADDRESSBOOK" ); 315 "X-Office", "KADDRESSBOOK" );
315 addCustomField( i18n( "IM Address" ), KABC::Field::Personal, 316 addCustomField( i18n( "IM Address" ), KABC::Field::Personal,
316 "X-IMAddress", "KADDRESSBOOK" ); 317 "X-IMAddress", "KADDRESSBOOK" );
317 addCustomField( i18n( "Anniversary" ), KABC::Field::Personal, 318 addCustomField( i18n( "Anniversary" ), KABC::Field::Personal,
318 "X-Anniversary", "KADDRESSBOOK" ); 319 "X-Anniversary", "KADDRESSBOOK" );
319 320
320 //US added this field to become compatible with Opie/qtopia addressbook 321 //US added this field to become compatible with Opie/qtopia addressbook
321 // values can be "female" or "male" or "". An empty field represents undefined. 322 // values can be "female" or "male" or "". An empty field represents undefined.
322 addCustomField( i18n( "Gender" ), KABC::Field::Personal, 323 addCustomField( i18n( "Gender" ), KABC::Field::Personal,
323 "X-Gender", "KADDRESSBOOK" ); 324 "X-Gender", "KADDRESSBOOK" );
324 addCustomField( i18n( "Children" ), KABC::Field::Personal, 325 addCustomField( i18n( "Children" ), KABC::Field::Personal,
325 "X-Children", "KADDRESSBOOK" ); 326 "X-Children", "KADDRESSBOOK" );
326 addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal, 327 addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal,
327 "X-FreeBusyUrl", "KADDRESSBOOK" ); 328 "X-FreeBusyUrl", "KADDRESSBOOK" );
328 addCustomField( i18n( "ExternalID" ), KABC::Field::Personal, 329 addCustomField( i18n( "ExternalID" ), KABC::Field::Personal,
329 "X-ExternalID", "KADDRESSBOOK" ); 330 "X-ExternalID", "KADDRESSBOOK" );
330} 331}
331 332
332AddressBook::~AddressBook() 333AddressBook::~AddressBook()
333{ 334{
334 delete d->mConfig; d->mConfig = 0; 335 delete d->mConfig; d->mConfig = 0;
335 delete d->mManager; d->mManager = 0; 336 delete d->mManager; d->mManager = 0;
336//US delete d->mErrorHandler; d->mErrorHandler = 0; 337//US delete d->mErrorHandler; d->mErrorHandler = 0;
337 delete d; d = 0; 338 delete d; d = 0;
338} 339}
339 340
340bool AddressBook::load() 341bool AddressBook::load()
341{ 342{
342 343
343 344
344 clear(); 345 clear();
345 346
346 KRES::Manager<Resource>::ActiveIterator it; 347 KRES::Manager<Resource>::ActiveIterator it;
347 bool ok = true; 348 bool ok = true;
348 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 349 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
349 if ( !(*it)->load() ) { 350 if ( !(*it)->load() ) {
350 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); 351 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) );
351 ok = false; 352 ok = false;
352 } 353 }
353 354
354 // mark all addressees as unchanged 355 // mark all addressees as unchanged
355 Addressee::List::Iterator addrIt; 356 Addressee::List::Iterator addrIt;
356 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) { 357 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) {
357 (*addrIt).setChanged( false ); 358 (*addrIt).setChanged( false );
358 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" ); 359 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" );
359 if ( !id.isEmpty() ) { 360 if ( !id.isEmpty() ) {
360 //qDebug("setId aa %s ", id.latin1()); 361 //qDebug("setId aa %s ", id.latin1());
361 (*addrIt).setIDStr(id ); 362 (*addrIt).setIDStr(id );
362 } 363 }
363 } 364 }
364 blockLSEchange = true; 365 blockLSEchange = true;
365 return ok; 366 return ok;
366} 367}
367 368
368bool AddressBook::save( Ticket *ticket ) 369bool AddressBook::save( Ticket *ticket )
369{ 370{
370 kdDebug(5700) << "AddressBook::save()"<< endl; 371 kdDebug(5700) << "AddressBook::save()"<< endl;
371 372
372 if ( ticket->resource() ) { 373 if ( ticket->resource() ) {
373 deleteRemovedAddressees(); 374 deleteRemovedAddressees();
374 return ticket->resource()->save( ticket ); 375 return ticket->resource()->save( ticket );
375 } 376 }
376 377
377 return false; 378 return false;
378} 379}
379void AddressBook::export2File( QString fileName ) 380void AddressBook::export2File( QString fileName )
380{ 381{
381 382
382 QFile outFile( fileName ); 383 QFile outFile( fileName );
383 if ( !outFile.open( IO_WriteOnly ) ) { 384 if ( !outFile.open( IO_WriteOnly ) ) {
384 QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" ); 385 QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" );
385 KMessageBox::error( 0, text.arg( fileName ) ); 386 KMessageBox::error( 0, text.arg( fileName ) );
386 return ; 387 return ;
387 } 388 }
388 QTextStream t( &outFile ); 389 QTextStream t( &outFile );
389 t.setEncoding( QTextStream::UnicodeUTF8 ); 390 t.setEncoding( QTextStream::UnicodeUTF8 );
390 Iterator it; 391 Iterator it;
391 KABC::VCardConverter::Version version; 392 KABC::VCardConverter::Version version;
392 version = KABC::VCardConverter::v3_0; 393 version = KABC::VCardConverter::v3_0;
393 for ( it = begin(); it != end(); ++it ) { 394 for ( it = begin(); it != end(); ++it ) {
394 if ( !(*it).IDStr().isEmpty() ) { 395 if ( !(*it).IDStr().isEmpty() ) {
395 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() ); 396 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() );
396 } 397 }
397 KABC::VCardConverter converter; 398 KABC::VCardConverter converter;
398 QString vcard; 399 QString vcard;
399 //Resource *resource() const; 400 //Resource *resource() const;
400 converter.addresseeToVCard( *it, vcard, version ); 401 converter.addresseeToVCard( *it, vcard, version );
401 t << vcard << "\r\n"; 402 t << vcard << "\r\n";
402 } 403 }
403 t << "\r\n\r\n"; 404 t << "\r\n\r\n";
404 outFile.close(); 405 outFile.close();
405} 406}
406void AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld ) 407void AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld )
407{ 408{
408 409
409 if ( removeOld ) 410 if ( removeOld )
410 setUntagged(); 411 setUntagged();
411 KABC::Addressee::List list; 412 KABC::Addressee::List list;
412 QFile file( fileName ); 413 QFile file( fileName );
413 file.open( IO_ReadOnly ); 414 file.open( IO_ReadOnly );
414 QByteArray rawData = file.readAll(); 415 QByteArray rawData = file.readAll();
415 file.close(); 416 file.close();
416 QString data; 417 QString data;
417 if ( replaceLabel ) { 418 if ( replaceLabel ) {
418 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); 419 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 );
419 data.replace ( QRegExp("LABEL") , "ADR" ); 420 data.replace ( QRegExp("LABEL") , "ADR" );
420 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); 421 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" );
421 } else 422 } else
422 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 423 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
423 KABC::VCardTool tool; 424 KABC::VCardTool tool;
424 list = tool.parseVCards( data ); 425 list = tool.parseVCards( data );
425 KABC::Addressee::List::Iterator it; 426 KABC::Addressee::List::Iterator it;
426 for ( it = list.begin(); it != list.end(); ++it ) { 427 for ( it = list.begin(); it != list.end(); ++it ) {
427 (*it).setResource( 0 ); 428 (*it).setResource( 0 );
428 if ( replaceLabel ) 429 if ( replaceLabel )
429 (*it).removeVoice(); 430 (*it).removeVoice();
430 if ( removeOld ) 431 if ( removeOld )
431 (*it).setTagged( true ); 432 (*it).setTagged( true );
432 insertAddressee( (*it), false, true ); 433 insertAddressee( (*it), false, true );
433 } 434 }
434 if ( removeOld ) 435 if ( removeOld )
435 removeUntagged(); 436 removeUntagged();
436} 437}
437void AddressBook::setUntagged() 438void AddressBook::setUntagged()
438{ 439{
439 Iterator ait; 440 Iterator ait;
440 for ( ait = begin(); ait != end(); ++ait ) { 441 for ( ait = begin(); ait != end(); ++ait ) {
441 (*ait).setTagged( false ); 442 (*ait).setTagged( false );
442 } 443 }
443} 444}
444void AddressBook::removeUntagged() 445void AddressBook::removeUntagged()
445{ 446{
446 Iterator ait; 447 Iterator ait;
447 bool todelete = false; 448 bool todelete = false;
448 Iterator todel; 449 Iterator todel;
449 for ( ait = begin(); ait != end(); ++ait ) { 450 for ( ait = begin(); ait != end(); ++ait ) {
450 if ( todelete ) 451 if ( todelete )
451 removeAddressee( todel ); 452 removeAddressee( todel );
452 if (!(*ait).tagged()) { 453 if (!(*ait).tagged()) {
453 todelete = true; 454 todelete = true;
454 todel = ait; 455 todel = ait;
455 } else 456 } else
456 todelete = false; 457 todelete = false;
457 } 458 }
458 if ( todelete ) 459 if ( todelete )
459 removeAddressee( todel ); 460 removeAddressee( todel );
460 deleteRemovedAddressees(); 461 deleteRemovedAddressees();
461} 462}
463void AddressBook::smplifyAddressees()
464{
465 Iterator ait;
466 for ( ait = begin(); ait != end(); ++ait ) {
467 (*ait).simplifyEmails();
468 (*ait).simplifyPhoneNumbers();
469 (*ait).simplifyPhoneNumberTypes();
470 (*ait).simplifyAddresses();
471 }
472}
473void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync )
474{
475 Iterator ait;
476 for ( ait = begin(); ait != end(); ++ait ) {
477 QString id = (*ait).IDStr();
478 (*ait).setIDStr( ":");
479 (*ait).setExternalUID( id );
480 (*ait).setOriginalExternalUID( id );
481 if ( isPreSync )
482 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL );
483 else
484 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
485 }
486}
487bool AddressBook::saveABphone( QString fileName )
488{
489 smplifyAddressees();
490 qDebug("saveABphone:: saving AB... ");
491 if ( ! saveAB() )
492 return false;
493 qDebug("saveABphone:: writing to phone... ");
494 if ( !PhoneAccess::writeToPhone( fileName) ) {
495 return false;
496 }
497 qDebug("saveABphone:: re-reading from phone... ");
498 if ( !PhoneAccess::readFromPhone( fileName) ) {
499 return false;
500 }
501 qDebug("reloading phone book... ");
502 if ( !load() )
503 return false;
504 return true;
505}
462bool AddressBook::saveAB() 506bool AddressBook::saveAB()
463{ 507{
464 bool ok = true; 508 bool ok = true;
465 509
466 deleteRemovedAddressees(); 510 deleteRemovedAddressees();
467 Iterator ait; 511 Iterator ait;
468 for ( ait = begin(); ait != end(); ++ait ) { 512 for ( ait = begin(); ait != end(); ++ait ) {
469 if ( !(*ait).IDStr().isEmpty() ) { 513 if ( !(*ait).IDStr().isEmpty() ) {
470 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); 514 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() );
471 } 515 }
472 } 516 }
473 KRES::Manager<Resource>::ActiveIterator it; 517 KRES::Manager<Resource>::ActiveIterator it;
474 KRES::Manager<Resource> *manager = d->mManager; 518 KRES::Manager<Resource> *manager = d->mManager;
475 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 519 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
476 if ( !(*it)->readOnly() && (*it)->isOpen() ) { 520 if ( !(*it)->readOnly() && (*it)->isOpen() ) {
477 Ticket *ticket = requestSaveTicket( *it ); 521 Ticket *ticket = requestSaveTicket( *it );
478// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); 522// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() );
479 if ( !ticket ) { 523 if ( !ticket ) {
480 error( i18n( "Unable to save to resource '%1'. It is locked." ) 524 error( i18n( "Unable to save to resource '%1'. It is locked." )
481 .arg( (*it)->resourceName() ) ); 525 .arg( (*it)->resourceName() ) );
482 return false; 526 return false;
483 } 527 }
484 528
485 //if ( !save( ticket ) ) 529 //if ( !save( ticket ) )
486 if ( ticket->resource() ) { 530 if ( ticket->resource() ) {
487 if ( ! ticket->resource()->save( ticket ) ) 531 if ( ! ticket->resource()->save( ticket ) )
488 ok = false; 532 ok = false;
489 } else 533 } else
490 ok = false; 534 ok = false;
491 535
492 } 536 }
493 } 537 }
494 return ok; 538 return ok;
495} 539}
496 540
497AddressBook::Iterator AddressBook::begin() 541AddressBook::Iterator AddressBook::begin()
498{ 542{
499 Iterator it = Iterator(); 543 Iterator it = Iterator();
500 it.d->mIt = d->mAddressees.begin(); 544 it.d->mIt = d->mAddressees.begin();
501 return it; 545 return it;
502} 546}
503 547
504AddressBook::ConstIterator AddressBook::begin() const 548AddressBook::ConstIterator AddressBook::begin() const
505{ 549{
506 ConstIterator it = ConstIterator(); 550 ConstIterator it = ConstIterator();
507 it.d->mIt = d->mAddressees.begin(); 551 it.d->mIt = d->mAddressees.begin();
508 return it; 552 return it;
509} 553}
510 554
511AddressBook::Iterator AddressBook::end() 555AddressBook::Iterator AddressBook::end()
512{ 556{
513 Iterator it = Iterator(); 557 Iterator it = Iterator();
514 it.d->mIt = d->mAddressees.end(); 558 it.d->mIt = d->mAddressees.end();
515 return it; 559 return it;
516} 560}
517 561
518AddressBook::ConstIterator AddressBook::end() const 562AddressBook::ConstIterator AddressBook::end() const
519{ 563{
520 ConstIterator it = ConstIterator(); 564 ConstIterator it = ConstIterator();
521 it.d->mIt = d->mAddressees.end(); 565 it.d->mIt = d->mAddressees.end();
522 return it; 566 return it;
523} 567}
524 568
525void AddressBook::clear() 569void AddressBook::clear()
526{ 570{
527 d->mAddressees.clear(); 571 d->mAddressees.clear();
528} 572}
529 573
530Ticket *AddressBook::requestSaveTicket( Resource *resource ) 574Ticket *AddressBook::requestSaveTicket( Resource *resource )
531{ 575{
532 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; 576 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl;
533 577
534 if ( !resource ) 578 if ( !resource )
535 { 579 {
536 qDebug("AddressBook::requestSaveTicket no resource" ); 580 qDebug("AddressBook::requestSaveTicket no resource" );
537 resource = standardResource(); 581 resource = standardResource();
538 } 582 }
539 583
540 KRES::Manager<Resource>::ActiveIterator it; 584 KRES::Manager<Resource>::ActiveIterator it;
541 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 585 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
542 if ( (*it) == resource ) { 586 if ( (*it) == resource ) {
543 if ( (*it)->readOnly() || !(*it)->isOpen() ) 587 if ( (*it)->readOnly() || !(*it)->isOpen() )
544 return 0; 588 return 0;
545 else 589 else
546 return (*it)->requestSaveTicket(); 590 return (*it)->requestSaveTicket();
547 } 591 }
548 } 592 }
549 593
550 return 0; 594 return 0;
551} 595}
552 596
553void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource ) 597void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource )
554{ 598{
555 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { 599 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) {
556 //qDebug("block insert "); 600 //qDebug("block insert ");
557 return; 601 return;
558 } 602 }
559 //qDebug("inserting.... %s ",a.uid().latin1() ); 603 //qDebug("inserting.... %s ",a.uid().latin1() );
560 bool found = false; 604 bool found = false;
561 Addressee::List::Iterator it; 605 Addressee::List::Iterator it;
562 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { 606 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) {
563 if ( a.uid() == (*it).uid() ) { 607 if ( a.uid() == (*it).uid() ) {
564 608
565 bool changed = false; 609 bool changed = false;
566 Addressee addr = a; 610 Addressee addr = a;
567 if ( addr != (*it) ) 611 if ( addr != (*it) )
568 changed = true; 612 changed = true;
569 613
570 if ( takeResource ) { 614 if ( takeResource ) {
571 Resource * res = (*it).resource(); 615 Resource * res = (*it).resource();
572 (*it) = a; 616 (*it) = a;
573 (*it).setResource( res ); 617 (*it).setResource( res );
574 } else { 618 } else {
575 (*it) = a; 619 (*it) = a;
576 if ( (*it).resource() == 0 ) 620 if ( (*it).resource() == 0 )
577 (*it).setResource( standardResource() ); 621 (*it).setResource( standardResource() );
578 } 622 }
579 if ( changed ) { 623 if ( changed ) {
580 if ( setRev ) { 624 if ( setRev ) {
581 625
582 // get rid of micro seconds 626 // get rid of micro seconds
583 QDateTime dt = QDateTime::currentDateTime(); 627 QDateTime dt = QDateTime::currentDateTime();
584 QTime t = dt.time(); 628 QTime t = dt.time();
585 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) ); 629 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) );
586 (*it).setRevision( dt ); 630 (*it).setRevision( dt );
587 } 631 }
588 (*it).setChanged( true ); 632 (*it).setChanged( true );
589 } 633 }
590 634
591 found = true; 635 found = true;
592 } else { 636 } else {
593 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 637 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
594 QString name = (*it).uid().mid( 19 ); 638 QString name = (*it).uid().mid( 19 );
595 Addressee b = a; 639 Addressee b = a;
596 QString id = b.getID( name ); 640 QString id = b.getID( name );
597 if ( ! id.isEmpty() ) { 641 if ( ! id.isEmpty() ) {
598 QString des = (*it).note(); 642 QString des = (*it).note();
599 int startN; 643 int startN;
600 if( (startN = des.find( id ) ) >= 0 ) { 644 if( (startN = des.find( id ) ) >= 0 ) {
601 int endN = des.find( ",", startN+1 ); 645 int endN = des.find( ",", startN+1 );
602 des = des.left( startN ) + des.mid( endN+1 ); 646 des = des.left( startN ) + des.mid( endN+1 );
603 (*it).setNote( des ); 647 (*it).setNote( des );
604 } 648 }
605 } 649 }
606 } 650 }
607 } 651 }
608 } 652 }
609 if ( found ) 653 if ( found )
610 return; 654 return;
611 d->mAddressees.append( a ); 655 d->mAddressees.append( a );
612 Addressee& addr = d->mAddressees.last(); 656 Addressee& addr = d->mAddressees.last();
613 if ( addr.resource() == 0 ) 657 if ( addr.resource() == 0 )
614 addr.setResource( standardResource() ); 658 addr.setResource( standardResource() );
615 659
616 addr.setChanged( true ); 660 addr.setChanged( true );
617} 661}
618 662
619void AddressBook::removeAddressee( const Addressee &a ) 663void AddressBook::removeAddressee( const Addressee &a )
620{ 664{
621 Iterator it; 665 Iterator it;
622 Iterator it2; 666 Iterator it2;
623 bool found = false; 667 bool found = false;
624 for ( it = begin(); it != end(); ++it ) { 668 for ( it = begin(); it != end(); ++it ) {
625 if ( a.uid() == (*it).uid() ) { 669 if ( a.uid() == (*it).uid() ) {
626 found = true; 670 found = true;
627 it2 = it; 671 it2 = it;
628 } else { 672 } else {
629 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 673 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
630 QString name = (*it).uid().mid( 19 ); 674 QString name = (*it).uid().mid( 19 );
631 Addressee b = a; 675 Addressee b = a;
632 QString id = b.getID( name ); 676 QString id = b.getID( name );
633 if ( ! id.isEmpty() ) { 677 if ( ! id.isEmpty() ) {
634 QString des = (*it).note(); 678 QString des = (*it).note();
635 if( des.find( id ) < 0 ) { 679 if( des.find( id ) < 0 ) {
636 des += id + ","; 680 des += id + ",";
637 (*it).setNote( des ); 681 (*it).setNote( des );
638 } 682 }
639 } 683 }
640 } 684 }
641 685
642 } 686 }
643 } 687 }
644 688
645 if ( found ) 689 if ( found )
646 removeAddressee( it2 ); 690 removeAddressee( it2 );
647 691
648} 692}
649 693
650void AddressBook::removeSyncAddressees( bool removeDeleted ) 694void AddressBook::removeSyncAddressees( bool removeDeleted )
651{ 695{
652 Iterator it = begin(); 696 Iterator it = begin();
653 Iterator it2 ; 697 Iterator it2 ;
654 QDateTime dt ( QDate( 2004,1,1) ); 698 QDateTime dt ( QDate( 2004,1,1) );
655 while ( it != end() ) { 699 while ( it != end() ) {
656 (*it).setRevision( dt ); 700 (*it).setRevision( dt );
657 (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" ); 701 (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" );
658 (*it).setIDStr(""); 702 (*it).setIDStr("");
659 if ( ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE && removeDeleted )|| (*it).uid().left( 19 ) == QString("last-syncAddressee-")) { 703 if ( ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE && removeDeleted )|| (*it).uid().left( 19 ) == QString("last-syncAddressee-")) {
660 it2 = it; 704 it2 = it;
661 //qDebug("removing %s ",(*it).uid().latin1() ); 705 //qDebug("removing %s ",(*it).uid().latin1() );
662 ++it; 706 ++it;
663 removeAddressee( it2 ); 707 removeAddressee( it2 );
664 } else { 708 } else {
665 //qDebug("skipping %s ",(*it).uid().latin1() ); 709 //qDebug("skipping %s ",(*it).uid().latin1() );
666 ++it; 710 ++it;
667 } 711 }
668 } 712 }
669 deleteRemovedAddressees(); 713 deleteRemovedAddressees();
670} 714}
671 715
672void AddressBook::removeAddressee( const Iterator &it ) 716void AddressBook::removeAddressee( const Iterator &it )
673{ 717{
674 d->mRemovedAddressees.append( (*it) ); 718 d->mRemovedAddressees.append( (*it) );
675 d->mAddressees.remove( it.d->mIt ); 719 d->mAddressees.remove( it.d->mIt );
676} 720}
677 721
678AddressBook::Iterator AddressBook::find( const Addressee &a ) 722AddressBook::Iterator AddressBook::find( const Addressee &a )
679{ 723{
680 Iterator it; 724 Iterator it;
681 for ( it = begin(); it != end(); ++it ) { 725 for ( it = begin(); it != end(); ++it ) {
682 if ( a.uid() == (*it).uid() ) { 726 if ( a.uid() == (*it).uid() ) {
683 return it; 727 return it;
684 } 728 }
685 } 729 }
686 return end(); 730 return end();
687} 731}
688 732
689Addressee AddressBook::findByUid( const QString &uid ) 733Addressee AddressBook::findByUid( const QString &uid )
690{ 734{
691 Iterator it; 735 Iterator it;
692 for ( it = begin(); it != end(); ++it ) { 736 for ( it = begin(); it != end(); ++it ) {
693 if ( uid == (*it).uid() ) { 737 if ( uid == (*it).uid() ) {
694 return *it; 738 return *it;
695 } 739 }
696 } 740 }
697 return Addressee(); 741 return Addressee();
698} 742}
699void AddressBook::preExternSync( AddressBook* aBook, const QString& csd ) 743void AddressBook::preExternSync( AddressBook* aBook, const QString& csd )
700{ 744{
701 //qDebug("AddressBook::preExternSync "); 745 //qDebug("AddressBook::preExternSync ");
702 AddressBook::Iterator it; 746 AddressBook::Iterator it;
703 for ( it = begin(); it != end(); ++it ) { 747 for ( it = begin(); it != end(); ++it ) {
704 (*it).setID( csd, (*it).externalUID() ); 748 (*it).setID( csd, (*it).externalUID() );
705 (*it).computeCsum( csd ); 749 (*it).computeCsum( csd );
706 } 750 }
707 mergeAB( aBook ,csd ); 751 mergeAB( aBook ,csd );
708} 752}
709void AddressBook::postExternSync( AddressBook* aBook , const QString& csd) 753void AddressBook::postExternSync( AddressBook* aBook , const QString& csd)
710{ 754{
711 //qDebug("AddressBook::postExternSync "); 755 //qDebug("AddressBook::postExternSync ");
712 AddressBook::Iterator it; 756 AddressBook::Iterator it;
713 for ( it = begin(); it != end(); ++it ) { 757 for ( it = begin(); it != end(); ++it ) {
714 // qDebug("check uid %s ", (*it).uid().latin1() ); 758 // qDebug("check uid %s ", (*it).uid().latin1() );
715 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID || 759 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID ||
716 (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_CSUM ) { 760 (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_CSUM ) {
717 Addressee ad = aBook->findByUid( ( (*it).uid() )); 761 Addressee ad = aBook->findByUid( ( (*it).uid() ));
718 if ( ad.isEmpty() ) { 762 if ( ad.isEmpty() ) {
719 qDebug("postExternSync:ERROR addressee is empty: %s ", (*it).uid().latin1()); 763 qDebug("postExternSync:ERROR addressee is empty: %s ", (*it).uid().latin1());
720 } else { 764 } else {
721 (*it).computeCsum( csd ); 765 (*it).computeCsum( csd );
722 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID ) 766 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID )
723 ad.setID( csd, (*it).externalUID() ); 767 ad.setID( csd, (*it).externalUID() );
724 ad.setCsum( csd, (*it).getCsum( csd ) ); 768 ad.setCsum( csd, (*it).getCsum( csd ) );
725 aBook->insertAddressee( ad ); 769 aBook->insertAddressee( ad );
726 } 770 }
727 } 771 }
728 } 772 }
729} 773}
730 774
731bool AddressBook::containsExternalUid( const QString& uid ) 775bool AddressBook::containsExternalUid( const QString& uid )
732{ 776{
733 Iterator it; 777 Iterator it;
734 for ( it = begin(); it != end(); ++it ) { 778 for ( it = begin(); it != end(); ++it ) {
735 if ( uid == (*it).externalUID( ) ) 779 if ( uid == (*it).externalUID( ) )
736 return true; 780 return true;
737 } 781 }
738 return false; 782 return false;
739} 783}
740Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile ) 784Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile )
741{ 785{
742 Iterator it; 786 Iterator it;
743 for ( it = begin(); it != end(); ++it ) { 787 for ( it = begin(); it != end(); ++it ) {
744 if ( uid == (*it).getID( profile ) ) 788 if ( uid == (*it).getID( profile ) )
745 return (*it); 789 return (*it);
746 } 790 }
747 return Addressee(); 791 return Addressee();
748} 792}
749void AddressBook::mergeAB( AddressBook *aBook, const QString& profile ) 793void AddressBook::mergeAB( AddressBook *aBook, const QString& profile )
750{ 794{
751 Iterator it; 795 Iterator it;
752 Addressee ad; 796 Addressee ad;
753 for ( it = begin(); it != end(); ++it ) { 797 for ( it = begin(); it != end(); ++it ) {
754 ad = aBook->findByExternUid( (*it).externalUID(), profile ); 798 ad = aBook->findByExternUid( (*it).externalUID(), profile );
755 if ( !ad.isEmpty() ) { 799 if ( !ad.isEmpty() ) {
756 (*it).mergeContact( ad ); 800 (*it).mergeContact( ad );
757 } 801 }
758 } 802 }
759#if 0 803#if 0
760 // test only 804 // test only
761 for ( it = begin(); it != end(); ++it ) { 805 for ( it = begin(); it != end(); ++it ) {
762 806
763 qDebug("uid %s ", (*it).uid().latin1()); 807 qDebug("uid %s ", (*it).uid().latin1());
764 } 808 }
765#endif 809#endif
766} 810}
767 811
768#if 0 812#if 0
769Addressee::List AddressBook::getExternLastSyncAddressees() 813Addressee::List AddressBook::getExternLastSyncAddressees()
770{ 814{
771 Addressee::List results; 815 Addressee::List results;
772 816
773 Iterator it; 817 Iterator it;
774 for ( it = begin(); it != end(); ++it ) { 818 for ( it = begin(); it != end(); ++it ) {
775 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) { 819 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) {
776 if ( (*it).familyName().left(4) == "!E: " ) 820 if ( (*it).familyName().left(4) == "!E: " )
777 results.append( *it ); 821 results.append( *it );
778 } 822 }
779 } 823 }
780 824
781 return results; 825 return results;
782} 826}
783#endif 827#endif
784void AddressBook::resetTempSyncStat() 828void AddressBook::resetTempSyncStat()
785{ 829{
786 Iterator it; 830 Iterator it;
787 for ( it = begin(); it != end(); ++it ) { 831 for ( it = begin(); it != end(); ++it ) {
788 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL ); 832 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL );
789 } 833 }
790 834
791} 835}
792 836
793QStringList AddressBook:: uidList() 837QStringList AddressBook:: uidList()
794{ 838{
795 QStringList results; 839 QStringList results;
796 Iterator it; 840 Iterator it;
797 for ( it = begin(); it != end(); ++it ) { 841 for ( it = begin(); it != end(); ++it ) {
798 results.append( (*it).uid() ); 842 results.append( (*it).uid() );
799 } 843 }
800 return results; 844 return results;
801} 845}
802 846
803 847
804Addressee::List AddressBook::allAddressees() 848Addressee::List AddressBook::allAddressees()
805{ 849{
806 return d->mAddressees; 850 return d->mAddressees;
807 851
808} 852}
809 853
810Addressee::List AddressBook::findByName( const QString &name ) 854Addressee::List AddressBook::findByName( const QString &name )
811{ 855{
812 Addressee::List results; 856 Addressee::List results;
813 857
814 Iterator it; 858 Iterator it;
815 for ( it = begin(); it != end(); ++it ) { 859 for ( it = begin(); it != end(); ++it ) {
816 if ( name == (*it).realName() ) { 860 if ( name == (*it).realName() ) {
817 results.append( *it ); 861 results.append( *it );
818 } 862 }
819 } 863 }
820 864
821 return results; 865 return results;
822} 866}
823 867
824Addressee::List AddressBook::findByEmail( const QString &email ) 868Addressee::List AddressBook::findByEmail( const QString &email )
825{ 869{
826 Addressee::List results; 870 Addressee::List results;
827 QStringList mailList; 871 QStringList mailList;
828 872
829 Iterator it; 873 Iterator it;
830 for ( it = begin(); it != end(); ++it ) { 874 for ( it = begin(); it != end(); ++it ) {
831 mailList = (*it).emails(); 875 mailList = (*it).emails();
832 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) { 876 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) {
833 if ( email == (*ite) ) { 877 if ( email == (*ite) ) {
834 results.append( *it ); 878 results.append( *it );
835 } 879 }
836 } 880 }
837 } 881 }
838 882
839 return results; 883 return results;
840} 884}
841 885
842Addressee::List AddressBook::findByCategory( const QString &category ) 886Addressee::List AddressBook::findByCategory( const QString &category )
843{ 887{
844 Addressee::List results; 888 Addressee::List results;
845 889
846 Iterator it; 890 Iterator it;
847 for ( it = begin(); it != end(); ++it ) { 891 for ( it = begin(); it != end(); ++it ) {
848 if ( (*it).hasCategory( category) ) { 892 if ( (*it).hasCategory( category) ) {
849 results.append( *it ); 893 results.append( *it );
850 } 894 }
851 } 895 }
852 896
853 return results; 897 return results;
854} 898}
855 899
856void AddressBook::dump() const 900void AddressBook::dump() const
857{ 901{
858 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl; 902 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl;
859 903
860 ConstIterator it; 904 ConstIterator it;
861 for( it = begin(); it != end(); ++it ) { 905 for( it = begin(); it != end(); ++it ) {
862 (*it).dump(); 906 (*it).dump();
863 } 907 }
864 908
865 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl; 909 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl;
866} 910}
867 911
868QString AddressBook::identifier() 912QString AddressBook::identifier()
869{ 913{
870 QStringList identifier; 914 QStringList identifier;
871 915
872 916
873 KRES::Manager<Resource>::ActiveIterator it; 917 KRES::Manager<Resource>::ActiveIterator it;
874 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 918 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
875 if ( !(*it)->identifier().isEmpty() ) 919 if ( !(*it)->identifier().isEmpty() )
876 identifier.append( (*it)->identifier() ); 920 identifier.append( (*it)->identifier() );
877 } 921 }
878 922
879 return identifier.join( ":" ); 923 return identifier.join( ":" );
880} 924}
881 925
882Field::List AddressBook::fields( int category ) 926Field::List AddressBook::fields( int category )
883{ 927{
884 if ( d->mAllFields.isEmpty() ) { 928 if ( d->mAllFields.isEmpty() ) {
885 d->mAllFields = Field::allFields(); 929 d->mAllFields = Field::allFields();
886 } 930 }
887 931
888 if ( category == Field::All ) return d->mAllFields; 932 if ( category == Field::All ) return d->mAllFields;
889 933
890 Field::List result; 934 Field::List result;
891 Field::List::ConstIterator it; 935 Field::List::ConstIterator it;
892 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) { 936 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) {
893 if ( (*it)->category() & category ) result.append( *it ); 937 if ( (*it)->category() & category ) result.append( *it );
894 } 938 }
895 939
896 return result; 940 return result;
897} 941}
898 942
899bool AddressBook::addCustomField( const QString &label, int category, 943bool AddressBook::addCustomField( const QString &label, int category,
900 const QString &key, const QString &app ) 944 const QString &key, const QString &app )
901{ 945{
902 if ( d->mAllFields.isEmpty() ) { 946 if ( d->mAllFields.isEmpty() ) {
903 d->mAllFields = Field::allFields(); 947 d->mAllFields = Field::allFields();
904 } 948 }
905//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app; 949//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app;
906 QString a = app.isNull() ? KGlobal::getAppName() : app; 950 QString a = app.isNull() ? KGlobal::getAppName() : app;
907 951
908 QString k = key.isNull() ? label : key; 952 QString k = key.isNull() ? label : key;
909 953
910 Field *field = Field::createCustomField( label, category, k, a ); 954 Field *field = Field::createCustomField( label, category, k, a );
911 955
912 if ( !field ) return false; 956 if ( !field ) return false;
913 957
914 d->mAllFields.append( field ); 958 d->mAllFields.append( field );
915 959
916 return true; 960 return true;
917} 961}
918 962
919QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab ) 963QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab )
920{ 964{
921 if (!ab.d) return s; 965 if (!ab.d) return s;
922 966
923 return s << ab.d->mAddressees; 967 return s << ab.d->mAddressees;
924} 968}
925 969
926QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab ) 970QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab )
927{ 971{
928 if (!ab.d) return s; 972 if (!ab.d) return s;
929 973
930 s >> ab.d->mAddressees; 974 s >> ab.d->mAddressees;
931 975
932 return s; 976 return s;
933} 977}
934 978
935bool AddressBook::addResource( Resource *resource ) 979bool AddressBook::addResource( Resource *resource )
936{ 980{
937 if ( !resource->open() ) { 981 if ( !resource->open() ) {
938 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl; 982 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl;
939 return false; 983 return false;
940 } 984 }
941 985
942 resource->setAddressBook( this ); 986 resource->setAddressBook( this );
943 987
944 d->mManager->add( resource ); 988 d->mManager->add( resource );
945 return true; 989 return true;
946} 990}
947 991
948bool AddressBook::removeResource( Resource *resource ) 992bool AddressBook::removeResource( Resource *resource )
949{ 993{
950 resource->close(); 994 resource->close();
951 995
952 if ( resource == standardResource() ) 996 if ( resource == standardResource() )
953 d->mManager->setStandardResource( 0 ); 997 d->mManager->setStandardResource( 0 );
954 998
955 resource->setAddressBook( 0 ); 999 resource->setAddressBook( 0 );
956 1000
957 d->mManager->remove( resource ); 1001 d->mManager->remove( resource );
958 return true; 1002 return true;
959} 1003}
960 1004
961QPtrList<Resource> AddressBook::resources() 1005QPtrList<Resource> AddressBook::resources()
962{ 1006{
963 QPtrList<Resource> list; 1007 QPtrList<Resource> list;
964 1008
965// qDebug("AddressBook::resources() 1"); 1009// qDebug("AddressBook::resources() 1");
966 1010
967 KRES::Manager<Resource>::ActiveIterator it; 1011 KRES::Manager<Resource>::ActiveIterator it;
968 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 1012 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
969 list.append( *it ); 1013 list.append( *it );
970 1014
971 return list; 1015 return list;
972} 1016}
973 1017
974/*US 1018/*US
975void AddressBook::setErrorHandler( ErrorHandler *handler ) 1019void AddressBook::setErrorHandler( ErrorHandler *handler )
976{ 1020{
977 delete d->mErrorHandler; 1021 delete d->mErrorHandler;
978 d->mErrorHandler = handler; 1022 d->mErrorHandler = handler;
979} 1023}
980*/ 1024*/
981 1025
982void AddressBook::error( const QString& msg ) 1026void AddressBook::error( const QString& msg )
983{ 1027{
984/*US 1028/*US
985 if ( !d->mErrorHandler ) // create default error handler 1029 if ( !d->mErrorHandler ) // create default error handler
986 d->mErrorHandler = new ConsoleErrorHandler; 1030 d->mErrorHandler = new ConsoleErrorHandler;
987 1031
988 if ( d->mErrorHandler ) 1032 if ( d->mErrorHandler )
989 d->mErrorHandler->error( msg ); 1033 d->mErrorHandler->error( msg );
990 else 1034 else
991 kdError(5700) << "no error handler defined" << endl; 1035 kdError(5700) << "no error handler defined" << endl;
992*/ 1036*/
993 kdDebug(5700) << "msg" << endl; 1037 kdDebug(5700) << "msg" << endl;
994 qDebug(msg); 1038 qDebug(msg);
995} 1039}
996 1040
997void AddressBook::deleteRemovedAddressees() 1041void AddressBook::deleteRemovedAddressees()
998{ 1042{
999 Addressee::List::Iterator it; 1043 Addressee::List::Iterator it;
1000 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) { 1044 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) {
1001 Resource *resource = (*it).resource(); 1045 Resource *resource = (*it).resource();
1002 if ( resource && !resource->readOnly() && resource->isOpen() ) 1046 if ( resource && !resource->readOnly() && resource->isOpen() )
1003 resource->removeAddressee( *it ); 1047 resource->removeAddressee( *it );
1004 } 1048 }
1005 1049
1006 d->mRemovedAddressees.clear(); 1050 d->mRemovedAddressees.clear();
1007} 1051}
1008 1052
1009void AddressBook::setStandardResource( Resource *resource ) 1053void AddressBook::setStandardResource( Resource *resource )
1010{ 1054{
1011// qDebug("AddressBook::setStandardResource 1"); 1055// qDebug("AddressBook::setStandardResource 1");
1012 d->mManager->setStandardResource( resource ); 1056 d->mManager->setStandardResource( resource );
1013} 1057}
1014 1058
1015Resource *AddressBook::standardResource() 1059Resource *AddressBook::standardResource()
1016{ 1060{
1017 return d->mManager->standardResource(); 1061 return d->mManager->standardResource();
1018} 1062}
1019 1063
1020KRES::Manager<Resource> *AddressBook::resourceManager() 1064KRES::Manager<Resource> *AddressBook::resourceManager()
1021{ 1065{
1022 return d->mManager; 1066 return d->mManager;
1023} 1067}
1024 1068
1025void AddressBook::cleanUp() 1069void AddressBook::cleanUp()
1026{ 1070{
1027 KRES::Manager<Resource>::ActiveIterator it; 1071 KRES::Manager<Resource>::ActiveIterator it;
1028 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 1072 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
1029 if ( !(*it)->readOnly() && (*it)->isOpen() ) 1073 if ( !(*it)->readOnly() && (*it)->isOpen() )
1030 (*it)->cleanUp(); 1074 (*it)->cleanUp();
1031 } 1075 }
1032} 1076}