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