summaryrefslogtreecommitdiffabout
path: root/kabc/addressbook.cpp
Unidiff
Diffstat (limited to 'kabc/addressbook.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addressbook.cpp133
1 files changed, 97 insertions, 36 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
index 16e1653..ec9f893 100644
--- a/kabc/addressbook.cpp
+++ b/kabc/addressbook.cpp
@@ -1,788 +1,849 @@
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 41
42#include <kglobal.h> 42#include <kglobal.h>
43#include <klocale.h> 43#include <klocale.h>
44#include <kdebug.h> 44#include <kdebug.h>
45#include <libkcal/syncdefines.h> 45#include <libkcal/syncdefines.h>
46#include "addressbook.h" 46#include "addressbook.h"
47#include "resource.h" 47#include "resource.h"
48 48
49//US #include "addressbook.moc" 49//US #include "addressbook.moc"
50 50
51using namespace KABC; 51using namespace KABC;
52 52
53struct AddressBook::AddressBookData 53struct AddressBook::AddressBookData
54{ 54{
55 Addressee::List mAddressees; 55 Addressee::List mAddressees;
56 Addressee::List mRemovedAddressees; 56 Addressee::List mRemovedAddressees;
57 Field::List mAllFields; 57 Field::List mAllFields;
58 KConfig *mConfig; 58 KConfig *mConfig;
59 KRES::Manager<Resource> *mManager; 59 KRES::Manager<Resource> *mManager;
60//US ErrorHandler *mErrorHandler; 60//US ErrorHandler *mErrorHandler;
61}; 61};
62 62
63struct AddressBook::Iterator::IteratorData 63struct AddressBook::Iterator::IteratorData
64{ 64{
65 Addressee::List::Iterator mIt; 65 Addressee::List::Iterator mIt;
66}; 66};
67 67
68struct AddressBook::ConstIterator::ConstIteratorData 68struct AddressBook::ConstIterator::ConstIteratorData
69{ 69{
70 Addressee::List::ConstIterator mIt; 70 Addressee::List::ConstIterator mIt;
71}; 71};
72 72
73AddressBook::Iterator::Iterator() 73AddressBook::Iterator::Iterator()
74{ 74{
75 d = new IteratorData; 75 d = new IteratorData;
76} 76}
77 77
78AddressBook::Iterator::Iterator( const AddressBook::Iterator &i ) 78AddressBook::Iterator::Iterator( const AddressBook::Iterator &i )
79{ 79{
80 d = new IteratorData; 80 d = new IteratorData;
81 d->mIt = i.d->mIt; 81 d->mIt = i.d->mIt;
82} 82}
83 83
84AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i ) 84AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i )
85{ 85{
86 if( this == &i ) return *this; // guard against self assignment 86 if( this == &i ) return *this; // guard against self assignment
87 delete d; // delete the old data the Iterator was completely constructed before 87 delete d; // delete the old data the Iterator was completely constructed before
88 d = new IteratorData; 88 d = new IteratorData;
89 d->mIt = i.d->mIt; 89 d->mIt = i.d->mIt;
90 return *this; 90 return *this;
91} 91}
92 92
93AddressBook::Iterator::~Iterator() 93AddressBook::Iterator::~Iterator()
94{ 94{
95 delete d; 95 delete d;
96} 96}
97 97
98const Addressee &AddressBook::Iterator::operator*() const 98const Addressee &AddressBook::Iterator::operator*() const
99{ 99{
100 return *(d->mIt); 100 return *(d->mIt);
101} 101}
102 102
103Addressee &AddressBook::Iterator::operator*() 103Addressee &AddressBook::Iterator::operator*()
104{ 104{
105 return *(d->mIt); 105 return *(d->mIt);
106} 106}
107 107
108Addressee *AddressBook::Iterator::operator->() 108Addressee *AddressBook::Iterator::operator->()
109{ 109{
110 return &(*(d->mIt)); 110 return &(*(d->mIt));
111} 111}
112 112
113AddressBook::Iterator &AddressBook::Iterator::operator++() 113AddressBook::Iterator &AddressBook::Iterator::operator++()
114{ 114{
115 (d->mIt)++; 115 (d->mIt)++;
116 return *this; 116 return *this;
117} 117}
118 118
119AddressBook::Iterator &AddressBook::Iterator::operator++(int) 119AddressBook::Iterator &AddressBook::Iterator::operator++(int)
120{ 120{
121 (d->mIt)++; 121 (d->mIt)++;
122 return *this; 122 return *this;
123} 123}
124 124
125AddressBook::Iterator &AddressBook::Iterator::operator--() 125AddressBook::Iterator &AddressBook::Iterator::operator--()
126{ 126{
127 (d->mIt)--; 127 (d->mIt)--;
128 return *this; 128 return *this;
129} 129}
130 130
131AddressBook::Iterator &AddressBook::Iterator::operator--(int) 131AddressBook::Iterator &AddressBook::Iterator::operator--(int)
132{ 132{
133 (d->mIt)--; 133 (d->mIt)--;
134 return *this; 134 return *this;
135} 135}
136 136
137bool AddressBook::Iterator::operator==( const Iterator &it ) 137bool AddressBook::Iterator::operator==( const Iterator &it )
138{ 138{
139 return ( d->mIt == it.d->mIt ); 139 return ( d->mIt == it.d->mIt );
140} 140}
141 141
142bool AddressBook::Iterator::operator!=( const Iterator &it ) 142bool AddressBook::Iterator::operator!=( const Iterator &it )
143{ 143{
144 return ( d->mIt != it.d->mIt ); 144 return ( d->mIt != it.d->mIt );
145} 145}
146 146
147 147
148AddressBook::ConstIterator::ConstIterator() 148AddressBook::ConstIterator::ConstIterator()
149{ 149{
150 d = new ConstIteratorData; 150 d = new ConstIteratorData;
151} 151}
152 152
153AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i ) 153AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i )
154{ 154{
155 d = new ConstIteratorData; 155 d = new ConstIteratorData;
156 d->mIt = i.d->mIt; 156 d->mIt = i.d->mIt;
157} 157}
158 158
159AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i ) 159AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i )
160{ 160{
161 if( this == &i ) return *this; // guard for self assignment 161 if( this == &i ) return *this; // guard for self assignment
162 delete d; // delete the old data because the Iterator was really constructed before 162 delete d; // delete the old data because the Iterator was really constructed before
163 d = new ConstIteratorData; 163 d = new ConstIteratorData;
164 d->mIt = i.d->mIt; 164 d->mIt = i.d->mIt;
165 return *this; 165 return *this;
166} 166}
167 167
168AddressBook::ConstIterator::~ConstIterator() 168AddressBook::ConstIterator::~ConstIterator()
169{ 169{
170 delete d; 170 delete d;
171} 171}
172 172
173const Addressee &AddressBook::ConstIterator::operator*() const 173const Addressee &AddressBook::ConstIterator::operator*() const
174{ 174{
175 return *(d->mIt); 175 return *(d->mIt);
176} 176}
177 177
178const Addressee* AddressBook::ConstIterator::operator->() const 178const Addressee* AddressBook::ConstIterator::operator->() const
179{ 179{
180 return &(*(d->mIt)); 180 return &(*(d->mIt));
181} 181}
182 182
183AddressBook::ConstIterator &AddressBook::ConstIterator::operator++() 183AddressBook::ConstIterator &AddressBook::ConstIterator::operator++()
184{ 184{
185 (d->mIt)++; 185 (d->mIt)++;
186 return *this; 186 return *this;
187} 187}
188 188
189AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int) 189AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int)
190{ 190{
191 (d->mIt)++; 191 (d->mIt)++;
192 return *this; 192 return *this;
193} 193}
194 194
195AddressBook::ConstIterator &AddressBook::ConstIterator::operator--() 195AddressBook::ConstIterator &AddressBook::ConstIterator::operator--()
196{ 196{
197 (d->mIt)--; 197 (d->mIt)--;
198 return *this; 198 return *this;
199} 199}
200 200
201AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int) 201AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int)
202{ 202{
203 (d->mIt)--; 203 (d->mIt)--;
204 return *this; 204 return *this;
205} 205}
206 206
207bool AddressBook::ConstIterator::operator==( const ConstIterator &it ) 207bool AddressBook::ConstIterator::operator==( const ConstIterator &it )
208{ 208{
209 return ( d->mIt == it.d->mIt ); 209 return ( d->mIt == it.d->mIt );
210} 210}
211 211
212bool AddressBook::ConstIterator::operator!=( const ConstIterator &it ) 212bool AddressBook::ConstIterator::operator!=( const ConstIterator &it )
213{ 213{
214 return ( d->mIt != it.d->mIt ); 214 return ( d->mIt != it.d->mIt );
215} 215}
216 216
217 217
218AddressBook::AddressBook() 218AddressBook::AddressBook()
219{ 219{
220 init(0, "contact"); 220 init(0, "contact");
221} 221}
222 222
223AddressBook::AddressBook( const QString &config ) 223AddressBook::AddressBook( const QString &config )
224{ 224{
225 init(config, "contact"); 225 init(config, "contact");
226} 226}
227 227
228AddressBook::AddressBook( const QString &config, const QString &family ) 228AddressBook::AddressBook( const QString &config, const QString &family )
229{ 229{
230 init(config, family); 230 init(config, family);
231 231
232} 232}
233 233
234// the default family is "contact" 234// the default family is "contact"
235void AddressBook::init(const QString &config, const QString &family ) 235void AddressBook::init(const QString &config, const QString &family )
236{ 236{
237 blockLSEchange = false;
237 d = new AddressBookData; 238 d = new AddressBookData;
238 QString fami = family; 239 QString fami = family;
239 qDebug("new ab "); 240 qDebug("new ab ");
240 if (config != 0) { 241 if (config != 0) {
241 qDebug("config != 0 "); 242 qDebug("config != 0 ");
242 if ( family == "syncContact" ) { 243 if ( family == "syncContact" ) {
243 qDebug("creating sync config "); 244 qDebug("creating sync config ");
244 fami = "contact"; 245 fami = "contact";
245 KConfig* con = new KConfig( locateLocal("config", "syncContactrc") ); 246 KConfig* con = new KConfig( locateLocal("config", "syncContactrc") );
246 con->setGroup( "General" ); 247 con->setGroup( "General" );
247 con->writeEntry( "ResourceKeys", QString("sync") ); 248 con->writeEntry( "ResourceKeys", QString("sync") );
248 con->writeEntry( "Standard", QString("sync") ); 249 con->writeEntry( "Standard", QString("sync") );
249 con->setGroup( "Resource_sync" ); 250 con->setGroup( "Resource_sync" );
250 con->writeEntry( "FileFormat", QString("vcard") ); 251 con->writeEntry( "FileFormat", QString("vcard") );
251 con->writeEntry( "FileName", config ); 252 con->writeEntry( "FileName", config );
252 con->writeEntry( "ResourceIdentifier", QString("sync") ); 253 con->writeEntry( "ResourceIdentifier", QString("sync") );
253 con->writeEntry( "ResourceName", QString("sync_res") ); 254 con->writeEntry( "ResourceName", QString("sync_res") );
254 con->writeEntry( "ResourceType", QString("file") ); 255 con->writeEntry( "ResourceType", QString("file") );
255 //con->sync(); 256 //con->sync();
256 d->mConfig = con; 257 d->mConfig = con;
257 } 258 }
258 else 259 else
259 d->mConfig = new KConfig( locateLocal("config", config) ); 260 d->mConfig = new KConfig( locateLocal("config", config) );
260// qDebug("AddressBook::init 1 config=%s",config.latin1() ); 261// qDebug("AddressBook::init 1 config=%s",config.latin1() );
261 } 262 }
262 else { 263 else {
263 d->mConfig = 0; 264 d->mConfig = 0;
264// qDebug("AddressBook::init 1 config=0"); 265// qDebug("AddressBook::init 1 config=0");
265 } 266 }
266 267
267//US d->mErrorHandler = 0; 268//US d->mErrorHandler = 0;
268 d->mManager = new KRES::Manager<Resource>( fami, false ); 269 d->mManager = new KRES::Manager<Resource>( fami, false );
269 d->mManager->readConfig( d->mConfig ); 270 d->mManager->readConfig( d->mConfig );
270 if ( family == "syncContact" ) { 271 if ( family == "syncContact" ) {
271 KRES::Manager<Resource> *manager = d->mManager; 272 KRES::Manager<Resource> *manager = d->mManager;
272 KRES::Manager<Resource>::ActiveIterator it; 273 KRES::Manager<Resource>::ActiveIterator it;
273 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 274 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
274 (*it)->setAddressBook( this ); 275 (*it)->setAddressBook( this );
275 if ( !(*it)->open() ) 276 if ( !(*it)->open() )
276 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) ); 277 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) );
277 } 278 }
278 Resource *res = standardResource(); 279 Resource *res = standardResource();
279 if ( !res ) { 280 if ( !res ) {
280 qDebug("ERROR: no standard resource"); 281 qDebug("ERROR: no standard resource");
281 res = manager->createResource( "file" ); 282 res = manager->createResource( "file" );
282 if ( res ) 283 if ( res )
283 { 284 {
284 addResource( res ); 285 addResource( res );
285 } 286 }
286 else 287 else
287 qDebug(" No resource available!!!"); 288 qDebug(" No resource available!!!");
288 } 289 }
289 setStandardResource( res ); 290 setStandardResource( res );
290 manager->writeConfig(); 291 manager->writeConfig();
291 } 292 }
292 addCustomField( i18n( "Department" ), KABC::Field::Organization, 293 addCustomField( i18n( "Department" ), KABC::Field::Organization,
293 "X-Department", "KADDRESSBOOK" ); 294 "X-Department", "KADDRESSBOOK" );
294 addCustomField( i18n( "Profession" ), KABC::Field::Organization, 295 addCustomField( i18n( "Profession" ), KABC::Field::Organization,
295 "X-Profession", "KADDRESSBOOK" ); 296 "X-Profession", "KADDRESSBOOK" );
296 addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization, 297 addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization,
297 "X-AssistantsName", "KADDRESSBOOK" ); 298 "X-AssistantsName", "KADDRESSBOOK" );
298 addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization, 299 addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization,
299 "X-ManagersName", "KADDRESSBOOK" ); 300 "X-ManagersName", "KADDRESSBOOK" );
300 addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal, 301 addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal,
301 "X-SpousesName", "KADDRESSBOOK" ); 302 "X-SpousesName", "KADDRESSBOOK" );
302 addCustomField( i18n( "Office" ), KABC::Field::Personal, 303 addCustomField( i18n( "Office" ), KABC::Field::Personal,
303 "X-Office", "KADDRESSBOOK" ); 304 "X-Office", "KADDRESSBOOK" );
304 addCustomField( i18n( "IM Address" ), KABC::Field::Personal, 305 addCustomField( i18n( "IM Address" ), KABC::Field::Personal,
305 "X-IMAddress", "KADDRESSBOOK" ); 306 "X-IMAddress", "KADDRESSBOOK" );
306 addCustomField( i18n( "Anniversary" ), KABC::Field::Personal, 307 addCustomField( i18n( "Anniversary" ), KABC::Field::Personal,
307 "X-Anniversary", "KADDRESSBOOK" ); 308 "X-Anniversary", "KADDRESSBOOK" );
308 309
309 //US added this field to become compatible with Opie/qtopia addressbook 310 //US added this field to become compatible with Opie/qtopia addressbook
310 // values can be "female" or "male" or "". An empty field represents undefined. 311 // values can be "female" or "male" or "". An empty field represents undefined.
311 addCustomField( i18n( "Gender" ), KABC::Field::Personal, 312 addCustomField( i18n( "Gender" ), KABC::Field::Personal,
312 "X-Gender", "KADDRESSBOOK" ); 313 "X-Gender", "KADDRESSBOOK" );
313 addCustomField( i18n( "Children" ), KABC::Field::Personal, 314 addCustomField( i18n( "Children" ), KABC::Field::Personal,
314 "X-Children", "KADDRESSBOOK" ); 315 "X-Children", "KADDRESSBOOK" );
315 addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal, 316 addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal,
316 "X-FreeBusyUrl", "KADDRESSBOOK" ); 317 "X-FreeBusyUrl", "KADDRESSBOOK" );
317 addCustomField( i18n( "ExternalID" ), KABC::Field::Personal, 318 addCustomField( i18n( "ExternalID" ), KABC::Field::Personal,
318 "X-ExternalID", "KADDRESSBOOK" ); 319 "X-ExternalID", "KADDRESSBOOK" );
319} 320}
320 321
321AddressBook::~AddressBook() 322AddressBook::~AddressBook()
322{ 323{
323 delete d->mConfig; d->mConfig = 0; 324 delete d->mConfig; d->mConfig = 0;
324 delete d->mManager; d->mManager = 0; 325 delete d->mManager; d->mManager = 0;
325//US delete d->mErrorHandler; d->mErrorHandler = 0; 326//US delete d->mErrorHandler; d->mErrorHandler = 0;
326 delete d; d = 0; 327 delete d; d = 0;
327} 328}
328 329
329bool AddressBook::load() 330bool AddressBook::load()
330{ 331{
331 332
332 333
333 clear(); 334 clear();
334 335
335 KRES::Manager<Resource>::ActiveIterator it; 336 KRES::Manager<Resource>::ActiveIterator it;
336 bool ok = true; 337 bool ok = true;
337 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 338 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
338 if ( !(*it)->load() ) { 339 if ( !(*it)->load() ) {
339 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); 340 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) );
340 ok = false; 341 ok = false;
341 } 342 }
342 343
343 // mark all addressees as unchanged 344 // mark all addressees as unchanged
344 Addressee::List::Iterator addrIt; 345 Addressee::List::Iterator addrIt;
345 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) 346 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt )
346 (*addrIt).setChanged( false ); 347 (*addrIt).setChanged( false );
347 348
349 blockLSEchange = true;
348 return ok; 350 return ok;
349} 351}
350 352
351bool AddressBook::save( Ticket *ticket ) 353bool AddressBook::save( Ticket *ticket )
352{ 354{
353 kdDebug(5700) << "AddressBook::save()"<< endl; 355 kdDebug(5700) << "AddressBook::save()"<< endl;
354 356
355 if ( ticket->resource() ) { 357 if ( ticket->resource() ) {
356 deleteRemovedAddressees(); 358 deleteRemovedAddressees();
357 return ticket->resource()->save( ticket ); 359 return ticket->resource()->save( ticket );
358 } 360 }
359 361
360 return false; 362 return false;
361} 363}
362bool AddressBook::saveAB() 364bool AddressBook::saveAB()
363{ 365{
364 bool ok = true; 366 bool ok = true;
365 367
366 deleteRemovedAddressees(); 368 deleteRemovedAddressees();
367 369
368 KRES::Manager<Resource>::ActiveIterator it; 370 KRES::Manager<Resource>::ActiveIterator it;
369 KRES::Manager<Resource> *manager = d->mManager; 371 KRES::Manager<Resource> *manager = d->mManager;
370 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 372 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
371 if ( !(*it)->readOnly() && (*it)->isOpen() ) { 373 if ( !(*it)->readOnly() && (*it)->isOpen() ) {
372 Ticket *ticket = requestSaveTicket( *it ); 374 Ticket *ticket = requestSaveTicket( *it );
373// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); 375// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() );
374 if ( !ticket ) { 376 if ( !ticket ) {
375 error( i18n( "Unable to save to resource '%1'. It is locked." ) 377 error( i18n( "Unable to save to resource '%1'. It is locked." )
376 .arg( (*it)->resourceName() ) ); 378 .arg( (*it)->resourceName() ) );
377 return false; 379 return false;
378 } 380 }
379 381
380 //if ( !save( ticket ) ) 382 //if ( !save( ticket ) )
381 if ( ticket->resource() ) { 383 if ( ticket->resource() ) {
382 if ( ! ticket->resource()->save( ticket ) ) 384 if ( ! ticket->resource()->save( ticket ) )
383 ok = false; 385 ok = false;
384 } else 386 } else
385 ok = false; 387 ok = false;
386 388
387 } 389 }
388 } 390 }
389 return ok; 391 return ok;
390} 392}
391 393
392AddressBook::Iterator AddressBook::begin() 394AddressBook::Iterator AddressBook::begin()
393{ 395{
394 Iterator it = Iterator(); 396 Iterator it = Iterator();
395 it.d->mIt = d->mAddressees.begin(); 397 it.d->mIt = d->mAddressees.begin();
396 return it; 398 return it;
397} 399}
398 400
399AddressBook::ConstIterator AddressBook::begin() const 401AddressBook::ConstIterator AddressBook::begin() const
400{ 402{
401 ConstIterator it = ConstIterator(); 403 ConstIterator it = ConstIterator();
402 it.d->mIt = d->mAddressees.begin(); 404 it.d->mIt = d->mAddressees.begin();
403 return it; 405 return it;
404} 406}
405 407
406AddressBook::Iterator AddressBook::end() 408AddressBook::Iterator AddressBook::end()
407{ 409{
408 Iterator it = Iterator(); 410 Iterator it = Iterator();
409 it.d->mIt = d->mAddressees.end(); 411 it.d->mIt = d->mAddressees.end();
410 return it; 412 return it;
411} 413}
412 414
413AddressBook::ConstIterator AddressBook::end() const 415AddressBook::ConstIterator AddressBook::end() const
414{ 416{
415 ConstIterator it = ConstIterator(); 417 ConstIterator it = ConstIterator();
416 it.d->mIt = d->mAddressees.end(); 418 it.d->mIt = d->mAddressees.end();
417 return it; 419 return it;
418} 420}
419 421
420void AddressBook::clear() 422void AddressBook::clear()
421{ 423{
422 d->mAddressees.clear(); 424 d->mAddressees.clear();
423} 425}
424 426
425Ticket *AddressBook::requestSaveTicket( Resource *resource ) 427Ticket *AddressBook::requestSaveTicket( Resource *resource )
426{ 428{
427 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; 429 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl;
428 430
429 if ( !resource ) 431 if ( !resource )
430 { 432 {
431 qDebug("AddressBook::requestSaveTicket no resource" ); 433 qDebug("AddressBook::requestSaveTicket no resource" );
432 resource = standardResource(); 434 resource = standardResource();
433 } 435 }
434 436
435 KRES::Manager<Resource>::ActiveIterator it; 437 KRES::Manager<Resource>::ActiveIterator it;
436 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 438 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
437 if ( (*it) == resource ) { 439 if ( (*it) == resource ) {
438 if ( (*it)->readOnly() || !(*it)->isOpen() ) 440 if ( (*it)->readOnly() || !(*it)->isOpen() )
439 return 0; 441 return 0;
440 else 442 else
441 return (*it)->requestSaveTicket(); 443 return (*it)->requestSaveTicket();
442 } 444 }
443 } 445 }
444 446
445 return 0; 447 return 0;
446} 448}
447 449
448void AddressBook::insertAddressee( const Addressee &a, bool setRev ) 450void AddressBook::insertAddressee( const Addressee &a, bool setRev )
449{ 451{
450 Addressee::List::Iterator it; 452 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) {
451 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { 453 return;
452 if ( a.uid() == (*it).uid() ) { 454 }
453 if ( setRev && (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 455 bool found = false;
454 return; 456 Addressee::List::Iterator it;
455 } 457 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) {
456 bool changed = false; 458 if ( a.uid() == (*it).uid() ) {
457 Addressee addr = a; 459
458 if ( addr != (*it) ) 460 bool changed = false;
459 changed = true; 461 Addressee addr = a;
460 462 if ( addr != (*it) )
461 (*it) = a; 463 changed = true;
462 if ( (*it).resource() == 0 ) 464
463 (*it).setResource( standardResource() ); 465 (*it) = a;
464 466 if ( (*it).resource() == 0 )
465 if ( changed ) { 467 (*it).setResource( standardResource() );
466 if ( setRev ) { 468
469 if ( changed ) {
470 if ( setRev ) {
467 471
468 // get rid of micro seconds 472 // get rid of micro seconds
469 QDateTime dt = QDateTime::currentDateTime(); 473 QDateTime dt = QDateTime::currentDateTime();
470 QTime t = dt.time(); 474 QTime t = dt.time();
471 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) ); 475 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) );
472 (*it).setRevision( dt ); 476 (*it).setRevision( dt );
473 } 477 }
474 (*it).setChanged( true ); 478 (*it).setChanged( true );
475 } 479 }
476 480
477 return; 481 found = true;
482 } else {
483 if ( (*it).uid() == QString("last-syncAddressee-") ) {
484 QString name = (*it).uid().mid( 19 );
485 Addressee b = a;
486 QString id = b.getID( name );
487 if ( ! id.isEmpty() ) {
488 QString des = (*it).note();
489 int startN;
490 if( (startN = des.find( id ) ) >= 0 ) {
491 int endN = des.find( ",", startN+1 );
492 des = des.left( startN ) + des.mid( endN+1 );
493 (*it).setNote( des );
494 }
495 }
496 }
497 }
478 } 498 }
479 } 499 if ( found )
480 d->mAddressees.append( a ); 500 return;
481 Addressee& addr = d->mAddressees.last(); 501 d->mAddressees.append( a );
482 if ( addr.resource() == 0 ) 502 Addressee& addr = d->mAddressees.last();
483 addr.setResource( standardResource() ); 503 if ( addr.resource() == 0 )
504 addr.setResource( standardResource() );
484 505
485 addr.setChanged( true ); 506 addr.setChanged( true );
486} 507}
487 508
488void AddressBook::removeAddressee( const Addressee &a ) 509void AddressBook::removeAddressee( const Addressee &a )
489{ 510{
490 Iterator it; 511 Iterator it;
512 Iterator it2;
513 bool found = false;
491 for ( it = begin(); it != end(); ++it ) { 514 for ( it = begin(); it != end(); ++it ) {
492 if ( a.uid() == (*it).uid() ) { 515 if ( a.uid() == (*it).uid() ) {
493 removeAddressee( it ); 516 found = true;
494 return; 517 it2 = it;
518 } else {
519 if ( (*it).uid() == QString("last-syncAddressee-") ) {
520 QString name = (*it).uid().mid( 19 );
521 Addressee b = a;
522 QString id = b.getID( name );
523 if ( ! id.isEmpty() ) {
524 QString des = (*it).note();
525 if( des.find( id ) < 0 ) {
526 des += id + ",";
527 (*it).setNote( des );
528 }
529 }
530 }
531
495 } 532 }
496 } 533 }
534
535 if ( found )
536 removeAddressee( it2 );
537
538}
539
540void AddressBook::removeDeletedAddressees()
541{
542 deleteRemovedAddressees();
543 Iterator it = begin();
544 Iterator it2 ;
545 QDateTime dt ( QDate( 2004,1,1) );
546 while ( it != end() ) {
547 (*it).setRevision( dt );
548 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE ) {
549 it2 = it;
550 ++it;
551 removeAddressee( it2 );
552 } else
553 ++it;
554 }
555 deleteRemovedAddressees();
497} 556}
498 557
499void AddressBook::removeAddressee( const Iterator &it ) 558void AddressBook::removeAddressee( const Iterator &it )
500{ 559{
501 d->mRemovedAddressees.append( (*it) ); 560 d->mRemovedAddressees.append( (*it) );
502 d->mAddressees.remove( it.d->mIt ); 561 d->mAddressees.remove( it.d->mIt );
503} 562}
504 563
505AddressBook::Iterator AddressBook::find( const Addressee &a ) 564AddressBook::Iterator AddressBook::find( const Addressee &a )
506{ 565{
507 Iterator it; 566 Iterator it;
508 for ( it = begin(); it != end(); ++it ) { 567 for ( it = begin(); it != end(); ++it ) {
509 if ( a.uid() == (*it).uid() ) { 568 if ( a.uid() == (*it).uid() ) {
510 return it; 569 return it;
511 } 570 }
512 } 571 }
513 return end(); 572 return end();
514} 573}
515 574
516Addressee AddressBook::findByUid( const QString &uid ) 575Addressee AddressBook::findByUid( const QString &uid )
517{ 576{
518 Iterator it; 577 Iterator it;
519 for ( it = begin(); it != end(); ++it ) { 578 for ( it = begin(); it != end(); ++it ) {
520 if ( uid == (*it).uid() ) { 579 if ( uid == (*it).uid() ) {
521 return *it; 580 return *it;
522 } 581 }
523 } 582 }
524 return Addressee(); 583 return Addressee();
525} 584}
585#if 0
526Addressee::List AddressBook::getExternLastSyncAddressees() 586Addressee::List AddressBook::getExternLastSyncAddressees()
527{ 587{
528 Addressee::List results; 588 Addressee::List results;
529 589
530 Iterator it; 590 Iterator it;
531 for ( it = begin(); it != end(); ++it ) { 591 for ( it = begin(); it != end(); ++it ) {
532 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) { 592 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) {
533 if ( (*it).familyName().left(3) == "E: " ) 593 if ( (*it).familyName().left(4) == "!E: " )
534 results.append( *it ); 594 results.append( *it );
535 } 595 }
536 } 596 }
537 597
538 return results; 598 return results;
539} 599}
600#endif
540void AddressBook::resetTempSyncStat() 601void AddressBook::resetTempSyncStat()
541{ 602{
542 Iterator it; 603 Iterator it;
543 for ( it = begin(); it != end(); ++it ) { 604 for ( it = begin(); it != end(); ++it ) {
544 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL ); 605 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL );
545 } 606 }
546 607
547} 608}
548 609
549QStringList AddressBook:: uidList() 610QStringList AddressBook:: uidList()
550{ 611{
551 QStringList results; 612 QStringList results;
552 Iterator it; 613 Iterator it;
553 for ( it = begin(); it != end(); ++it ) { 614 for ( it = begin(); it != end(); ++it ) {
554 results.append( (*it).uid() ); 615 results.append( (*it).uid() );
555 } 616 }
556 return results; 617 return results;
557} 618}
558 619
559 620
560Addressee::List AddressBook::allAddressees() 621Addressee::List AddressBook::allAddressees()
561{ 622{
562 return d->mAddressees; 623 return d->mAddressees;
563 624
564} 625}
565 626
566Addressee::List AddressBook::findByName( const QString &name ) 627Addressee::List AddressBook::findByName( const QString &name )
567{ 628{
568 Addressee::List results; 629 Addressee::List results;
569 630
570 Iterator it; 631 Iterator it;
571 for ( it = begin(); it != end(); ++it ) { 632 for ( it = begin(); it != end(); ++it ) {
572 if ( name == (*it).realName() ) { 633 if ( name == (*it).realName() ) {
573 results.append( *it ); 634 results.append( *it );
574 } 635 }
575 } 636 }
576 637
577 return results; 638 return results;
578} 639}
579 640
580Addressee::List AddressBook::findByEmail( const QString &email ) 641Addressee::List AddressBook::findByEmail( const QString &email )
581{ 642{
582 Addressee::List results; 643 Addressee::List results;
583 QStringList mailList; 644 QStringList mailList;
584 645
585 Iterator it; 646 Iterator it;
586 for ( it = begin(); it != end(); ++it ) { 647 for ( it = begin(); it != end(); ++it ) {
587 mailList = (*it).emails(); 648 mailList = (*it).emails();
588 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) { 649 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) {
589 if ( email == (*ite) ) { 650 if ( email == (*ite) ) {
590 results.append( *it ); 651 results.append( *it );
591 } 652 }
592 } 653 }
593 } 654 }
594 655
595 return results; 656 return results;
596} 657}
597 658
598Addressee::List AddressBook::findByCategory( const QString &category ) 659Addressee::List AddressBook::findByCategory( const QString &category )
599{ 660{
600 Addressee::List results; 661 Addressee::List results;
601 662
602 Iterator it; 663 Iterator it;
603 for ( it = begin(); it != end(); ++it ) { 664 for ( it = begin(); it != end(); ++it ) {
604 if ( (*it).hasCategory( category) ) { 665 if ( (*it).hasCategory( category) ) {
605 results.append( *it ); 666 results.append( *it );
606 } 667 }
607 } 668 }
608 669
609 return results; 670 return results;
610} 671}
611 672
612void AddressBook::dump() const 673void AddressBook::dump() const
613{ 674{
614 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl; 675 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl;
615 676
616 ConstIterator it; 677 ConstIterator it;
617 for( it = begin(); it != end(); ++it ) { 678 for( it = begin(); it != end(); ++it ) {
618 (*it).dump(); 679 (*it).dump();
619 } 680 }
620 681
621 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl; 682 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl;
622} 683}
623 684
624QString AddressBook::identifier() 685QString AddressBook::identifier()
625{ 686{
626 QStringList identifier; 687 QStringList identifier;
627 688
628 689
629 KRES::Manager<Resource>::ActiveIterator it; 690 KRES::Manager<Resource>::ActiveIterator it;
630 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 691 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
631 if ( !(*it)->identifier().isEmpty() ) 692 if ( !(*it)->identifier().isEmpty() )
632 identifier.append( (*it)->identifier() ); 693 identifier.append( (*it)->identifier() );
633 } 694 }
634 695
635 return identifier.join( ":" ); 696 return identifier.join( ":" );
636} 697}
637 698
638Field::List AddressBook::fields( int category ) 699Field::List AddressBook::fields( int category )
639{ 700{
640 if ( d->mAllFields.isEmpty() ) { 701 if ( d->mAllFields.isEmpty() ) {
641 d->mAllFields = Field::allFields(); 702 d->mAllFields = Field::allFields();
642 } 703 }
643 704
644 if ( category == Field::All ) return d->mAllFields; 705 if ( category == Field::All ) return d->mAllFields;
645 706
646 Field::List result; 707 Field::List result;
647 Field::List::ConstIterator it; 708 Field::List::ConstIterator it;
648 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) { 709 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) {
649 if ( (*it)->category() & category ) result.append( *it ); 710 if ( (*it)->category() & category ) result.append( *it );
650 } 711 }
651 712
652 return result; 713 return result;
653} 714}
654 715
655bool AddressBook::addCustomField( const QString &label, int category, 716bool AddressBook::addCustomField( const QString &label, int category,
656 const QString &key, const QString &app ) 717 const QString &key, const QString &app )
657{ 718{
658 if ( d->mAllFields.isEmpty() ) { 719 if ( d->mAllFields.isEmpty() ) {
659 d->mAllFields = Field::allFields(); 720 d->mAllFields = Field::allFields();
660 } 721 }
661//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app; 722//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app;
662 QString a = app.isNull() ? KGlobal::getAppName() : app; 723 QString a = app.isNull() ? KGlobal::getAppName() : app;
663 724
664 QString k = key.isNull() ? label : key; 725 QString k = key.isNull() ? label : key;
665 726
666 Field *field = Field::createCustomField( label, category, k, a ); 727 Field *field = Field::createCustomField( label, category, k, a );
667 728
668 if ( !field ) return false; 729 if ( !field ) return false;
669 730
670 d->mAllFields.append( field ); 731 d->mAllFields.append( field );
671 732
672 return true; 733 return true;
673} 734}
674 735
675QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab ) 736QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab )
676{ 737{
677 if (!ab.d) return s; 738 if (!ab.d) return s;
678 739
679 return s << ab.d->mAddressees; 740 return s << ab.d->mAddressees;
680} 741}
681 742
682QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab ) 743QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab )
683{ 744{
684 if (!ab.d) return s; 745 if (!ab.d) return s;
685 746
686 s >> ab.d->mAddressees; 747 s >> ab.d->mAddressees;
687 748
688 return s; 749 return s;
689} 750}
690 751
691bool AddressBook::addResource( Resource *resource ) 752bool AddressBook::addResource( Resource *resource )
692{ 753{
693 if ( !resource->open() ) { 754 if ( !resource->open() ) {
694 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl; 755 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl;
695 return false; 756 return false;
696 } 757 }
697 758
698 resource->setAddressBook( this ); 759 resource->setAddressBook( this );
699 760
700 d->mManager->add( resource ); 761 d->mManager->add( resource );
701 return true; 762 return true;
702} 763}
703 764
704bool AddressBook::removeResource( Resource *resource ) 765bool AddressBook::removeResource( Resource *resource )
705{ 766{
706 resource->close(); 767 resource->close();
707 768
708 if ( resource == standardResource() ) 769 if ( resource == standardResource() )
709 d->mManager->setStandardResource( 0 ); 770 d->mManager->setStandardResource( 0 );
710 771
711 resource->setAddressBook( 0 ); 772 resource->setAddressBook( 0 );
712 773
713 d->mManager->remove( resource ); 774 d->mManager->remove( resource );
714 return true; 775 return true;
715} 776}
716 777
717QPtrList<Resource> AddressBook::resources() 778QPtrList<Resource> AddressBook::resources()
718{ 779{
719 QPtrList<Resource> list; 780 QPtrList<Resource> list;
720 781
721// qDebug("AddressBook::resources() 1"); 782// qDebug("AddressBook::resources() 1");
722 783
723 KRES::Manager<Resource>::ActiveIterator it; 784 KRES::Manager<Resource>::ActiveIterator it;
724 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 785 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
725 list.append( *it ); 786 list.append( *it );
726 787
727 return list; 788 return list;
728} 789}
729 790
730/*US 791/*US
731void AddressBook::setErrorHandler( ErrorHandler *handler ) 792void AddressBook::setErrorHandler( ErrorHandler *handler )
732{ 793{
733 delete d->mErrorHandler; 794 delete d->mErrorHandler;
734 d->mErrorHandler = handler; 795 d->mErrorHandler = handler;
735} 796}
736*/ 797*/
737 798
738void AddressBook::error( const QString& msg ) 799void AddressBook::error( const QString& msg )
739{ 800{
740/*US 801/*US
741 if ( !d->mErrorHandler ) // create default error handler 802 if ( !d->mErrorHandler ) // create default error handler
742 d->mErrorHandler = new ConsoleErrorHandler; 803 d->mErrorHandler = new ConsoleErrorHandler;
743 804
744 if ( d->mErrorHandler ) 805 if ( d->mErrorHandler )
745 d->mErrorHandler->error( msg ); 806 d->mErrorHandler->error( msg );
746 else 807 else
747 kdError(5700) << "no error handler defined" << endl; 808 kdError(5700) << "no error handler defined" << endl;
748*/ 809*/
749 kdDebug(5700) << "msg" << endl; 810 kdDebug(5700) << "msg" << endl;
750 qDebug(msg); 811 qDebug(msg);
751} 812}
752 813
753void AddressBook::deleteRemovedAddressees() 814void AddressBook::deleteRemovedAddressees()
754{ 815{
755 Addressee::List::Iterator it; 816 Addressee::List::Iterator it;
756 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) { 817 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) {
757 Resource *resource = (*it).resource(); 818 Resource *resource = (*it).resource();
758 if ( resource && !resource->readOnly() && resource->isOpen() ) 819 if ( resource && !resource->readOnly() && resource->isOpen() )
759 resource->removeAddressee( *it ); 820 resource->removeAddressee( *it );
760 } 821 }
761 822
762 d->mRemovedAddressees.clear(); 823 d->mRemovedAddressees.clear();
763} 824}
764 825
765void AddressBook::setStandardResource( Resource *resource ) 826void AddressBook::setStandardResource( Resource *resource )
766{ 827{
767// qDebug("AddressBook::setStandardResource 1"); 828// qDebug("AddressBook::setStandardResource 1");
768 d->mManager->setStandardResource( resource ); 829 d->mManager->setStandardResource( resource );
769} 830}
770 831
771Resource *AddressBook::standardResource() 832Resource *AddressBook::standardResource()
772{ 833{
773 return d->mManager->standardResource(); 834 return d->mManager->standardResource();
774} 835}
775 836
776KRES::Manager<Resource> *AddressBook::resourceManager() 837KRES::Manager<Resource> *AddressBook::resourceManager()
777{ 838{
778 return d->mManager; 839 return d->mManager;
779} 840}
780 841
781void AddressBook::cleanUp() 842void AddressBook::cleanUp()
782{ 843{
783 KRES::Manager<Resource>::ActiveIterator it; 844 KRES::Manager<Resource>::ActiveIterator it;
784 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 845 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
785 if ( !(*it)->readOnly() && (*it)->isOpen() ) 846 if ( !(*it)->readOnly() && (*it)->isOpen() )
786 (*it)->cleanUp(); 847 (*it)->cleanUp();
787 } 848 }
788} 849}