summaryrefslogtreecommitdiffabout
path: root/kabc/addressbook.cpp
authorzautrix <zautrix>2005-12-02 09:42:53 (UTC)
committer zautrix <zautrix>2005-12-02 09:42:53 (UTC)
commita75d2c26aba6cffae8c3f61e173940240042427b (patch) (unidiff)
tree68f4b61c150bc263a98c78fdf8671bbbc17cdaf8 /kabc/addressbook.cpp
parent64be8d64fc26d1025a24150d065197ba5d608f3d (diff)
downloadkdepimpi-a75d2c26aba6cffae8c3f61e173940240042427b.zip
kdepimpi-a75d2c26aba6cffae8c3f61e173940240042427b.tar.gz
kdepimpi-a75d2c26aba6cffae8c3f61e173940240042427b.tar.bz2
more sync
Diffstat (limited to 'kabc/addressbook.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addressbook.cpp42
1 files changed, 33 insertions, 9 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
index e04f4b1..bdc2762 100644
--- a/kabc/addressbook.cpp
+++ b/kabc/addressbook.cpp
@@ -1,1272 +1,1296 @@
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 ); 280 d->mManager = new KRES::Manager<Resource>( fami );
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 clear(); 344 clear();
345 KRES::Manager<Resource>::ActiveIterator it; 345 KRES::Manager<Resource>::ActiveIterator it;
346 bool ok = true; 346 bool ok = true;
347 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 347 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
348 if ( !(*it)->load() ) { 348 if ( !(*it)->load() ) {
349 qDebug( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); 349 qDebug( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) );
350 ok = false; 350 ok = false;
351 } else { 351 } else {
352 qDebug( i18n("Resource loaded: '%1'").arg( (*it)->resourceName() ) ); 352 qDebug( i18n("Resource loaded: '%1'").arg( (*it)->resourceName() ) );
353 } 353 }
354 // mark all addressees as unchanged 354 // mark all addressees as unchanged
355 Addressee::List::Iterator addrIt; 355 Addressee::List::Iterator addrIt;
356 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) { 356 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) {
357 (*addrIt).setChanged( false ); 357 (*addrIt).setChanged( false );
358 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" ); 358 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" );
359 if ( !id.isEmpty() ) { 359 if ( !id.isEmpty() ) {
360 //qDebug("setId aa %s ", id.latin1()); 360 //qDebug("setId aa %s ", id.latin1());
361 (*addrIt).setIDStr(id ); 361 (*addrIt).setIDStr(id );
362 } 362 }
363 } 363 }
364 blockLSEchange = true; 364 blockLSEchange = true;
365 return ok; 365 return ok;
366} 366}
367 367
368bool AddressBook::save( Ticket *ticket ) 368bool AddressBook::save( Ticket *ticket )
369{ 369{
370 kdDebug(5700) << "AddressBook::save()"<< endl; 370 kdDebug(5700) << "AddressBook::save()"<< endl;
371 371
372 if ( ticket->resource() ) { 372 if ( ticket->resource() ) {
373 deleteRemovedAddressees(); 373 deleteRemovedAddressees();
374 return ticket->resource()->save( ticket ); 374 return ticket->resource()->save( ticket );
375 } 375 }
376 376
377 return false; 377 return false;
378} 378}
379// exports all Addressees, which are syncable 379// exports all Addressees, which are syncable
380void AddressBook::export2File( QString fileName, QString resourceName ) 380void AddressBook::export2File( QString fileName, QString resourceName )
381{ 381{
382 382
383 QFile outFile( fileName ); 383 QFile outFile( fileName );
384 if ( !outFile.open( IO_WriteOnly ) ) { 384 if ( !outFile.open( IO_WriteOnly ) ) {
385 QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" ); 385 QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" );
386 KMessageBox::error( 0, text.arg( fileName ) ); 386 KMessageBox::error( 0, text.arg( fileName ) );
387 return ; 387 return ;
388 } 388 }
389 QTextStream t( &outFile ); 389 QTextStream t( &outFile );
390 t.setEncoding( QTextStream::UnicodeUTF8 ); 390 t.setEncoding( QTextStream::UnicodeUTF8 );
391 Iterator it; 391 Iterator it;
392 KABC::VCardConverter::Version version; 392 KABC::VCardConverter::Version version;
393 version = KABC::VCardConverter::v3_0; 393 version = KABC::VCardConverter::v3_0;
394 for ( it = begin(); it != end(); ++it ) { 394 for ( it = begin(); it != end(); ++it ) {
395 if ( (*it).resource() ) { 395 if ( (*it).resource() ) {
396 bool include = (*it).resource()->includeInSync(); 396 bool include = (*it).resource()->includeInSync();
397 if ( !resourceName.isEmpty() ) 397 if ( !resourceName.isEmpty() )
398 include = (resourceName == (*it).resource()->name() ); 398 include = (resourceName == (*it).resource()->name() );
399 if ( include ) { 399 if ( include ) {
400 qDebug(QString ("Exporting resource %1 to file %2").arg( (*it).resource()->name() ).arg( fileName ) ); 400 qDebug(QString ("Exporting resource %1 to file %2").arg( (*it).resource()->name() ).arg( fileName ) );
401 if ( !(*it).IDStr().isEmpty() ) { 401 if ( !(*it).IDStr().isEmpty() ) {
402 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() ); 402 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() );
403 } 403 }
404 KABC::VCardConverter converter; 404 KABC::VCardConverter converter;
405 QString vcard; 405 QString vcard;
406 //Resource *resource() const; 406 //Resource *resource() const;
407 converter.addresseeToVCard( *it, vcard, version ); 407 converter.addresseeToVCard( *it, vcard, version );
408 t << vcard << "\r\n"; 408 t << vcard << "\r\n";
409 } 409 }
410 } 410 }
411 } 411 }
412 t << "\r\n\r\n"; 412 t << "\r\n\r\n";
413 outFile.close(); 413 outFile.close();
414} 414}
415// if QStringList uids is empty, all are exported 415// if QStringList uids is empty, all are exported
416bool AddressBook::export2PhoneFormat( QStringList uids ,QString fileName ) 416bool AddressBook::export2PhoneFormat( QStringList uids ,QString fileName )
417{ 417{
418 KABC::VCardConverter converter; 418 KABC::VCardConverter converter;
419 QString datastream; 419 QString datastream;
420 Iterator it; 420 Iterator it;
421 bool all = uids.isEmpty(); 421 bool all = uids.isEmpty();
422 for ( it = begin(); it != end(); ++it ) { 422 for ( it = begin(); it != end(); ++it ) {
423 // for( QStringList::ConstIterator it = uids.begin(); it != uids.end(); ++it ) { 423 // for( QStringList::ConstIterator it = uids.begin(); it != uids.end(); ++it ) {
424 if ( ! all ) { 424 if ( ! all ) {
425 if ( ! ( uids.contains((*it).uid() ) )) 425 if ( ! ( uids.contains((*it).uid() ) ))
426 continue; 426 continue;
427 } 427 }
428 KABC::Addressee a = ( *it ); 428 KABC::Addressee a = ( *it );
429 if ( a.isEmpty() ) 429 if ( a.isEmpty() )
430 continue; 430 continue;
431 if ( all && a.resource() && !a.resource()->includeInSync() ) 431 if ( all && a.resource() && !a.resource()->includeInSync() )
432 continue; 432 continue;
433 a.simplifyEmails(); 433 a.simplifyEmails();
434 a.simplifyPhoneNumbers(); 434 a.simplifyPhoneNumbers();
435 a.simplifyPhoneNumberTypes(); 435 a.simplifyPhoneNumberTypes();
436 a.simplifyAddresses(); 436 a.simplifyAddresses();
437 437
438 QString vcard; 438 QString vcard;
439 QString vcardnew; 439 QString vcardnew;
440 converter.addresseeToVCard( a, vcard ); 440 converter.addresseeToVCard( a, vcard );
441 int start = 0; 441 int start = 0;
442 int next; 442 int next;
443 while ( (next = vcard.find("TYPE=", start) )>= 0 ) { 443 while ( (next = vcard.find("TYPE=", start) )>= 0 ) {
444 int semi = vcard.find(";", next); 444 int semi = vcard.find(";", next);
445 int dopp = vcard.find(":", next); 445 int dopp = vcard.find(":", next);
446 int sep; 446 int sep;
447 if ( semi < dopp && semi >= 0 ) 447 if ( semi < dopp && semi >= 0 )
448 sep = semi ; 448 sep = semi ;
449 else 449 else
450 sep = dopp; 450 sep = dopp;
451 vcardnew +=vcard.mid( start, next - start); 451 vcardnew +=vcard.mid( start, next - start);
452 vcardnew +=vcard.mid( next+5,sep -next -5 ).upper(); 452 vcardnew +=vcard.mid( next+5,sep -next -5 ).upper();
453 start = sep; 453 start = sep;
454 } 454 }
455 vcardnew += vcard.mid( start,vcard.length() ); 455 vcardnew += vcard.mid( start,vcard.length() );
456 vcard = ""; 456 vcard = "";
457 start = 0; 457 start = 0;
458 while ( (next = vcardnew.find("ADR", start) )>= 0 ) { 458 while ( (next = vcardnew.find("ADR", start) )>= 0 ) {
459 int sep = vcardnew.find(":", next); 459 int sep = vcardnew.find(":", next);
460 vcard +=vcardnew.mid( start, next - start+3); 460 vcard +=vcardnew.mid( start, next - start+3);
461 start = sep; 461 start = sep;
462 } 462 }
463 vcard += vcardnew.mid( start,vcardnew.length() ); 463 vcard += vcardnew.mid( start,vcardnew.length() );
464 vcard.replace ( QRegExp(";;;") , "" ); 464 vcard.replace ( QRegExp(";;;") , "" );
465 vcard.replace ( QRegExp(";;") , "" ); 465 vcard.replace ( QRegExp(";;") , "" );
466 datastream += vcard; 466 datastream += vcard;
467 467
468 } 468 }
469 469
470 QFile outFile(fileName); 470 QFile outFile(fileName);
471 if ( outFile.open(IO_WriteOnly) ) { 471 if ( outFile.open(IO_WriteOnly) ) {
472 datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" ); 472 datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" );
473 QTextStream t( &outFile ); // use a text stream 473 QTextStream t( &outFile ); // use a text stream
474 t.setEncoding( QTextStream::UnicodeUTF8 ); 474 t.setEncoding( QTextStream::UnicodeUTF8 );
475 t <<datastream; 475 t <<datastream;
476 t << "\r\n\r\n"; 476 t << "\r\n\r\n";
477 outFile.close(); 477 outFile.close();
478 478
479 } else { 479 } else {
480 qDebug("Error open temp file "); 480 qDebug("Error open temp file ");
481 return false; 481 return false;
482 } 482 }
483 return true; 483 return true;
484 484
485} 485}
486int AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld ) 486int AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld, QString resource )
487{ 487{
488 488
489 if ( removeOld ) 489 if ( removeOld )
490 setUntagged( true ); 490 setUntagged( true, resource );
491 KABC::Addressee::List list; 491 KABC::Addressee::List list;
492 QFile file( fileName ); 492 QFile file( fileName );
493 file.open( IO_ReadOnly ); 493 file.open( IO_ReadOnly );
494 QByteArray rawData = file.readAll(); 494 QByteArray rawData = file.readAll();
495 file.close(); 495 file.close();
496 QString data; 496 QString data;
497 if ( replaceLabel ) { 497 if ( replaceLabel ) {
498 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); 498 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 );
499 data.replace ( QRegExp("LABEL") , "ADR" ); 499 data.replace ( QRegExp("LABEL") , "ADR" );
500 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); 500 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" );
501 } else 501 } else
502 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 502 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
503 KABC::VCardTool tool; 503 KABC::VCardTool tool;
504 list = tool.parseVCards( data ); 504 list = tool.parseVCards( data );
505 KABC::Addressee::List::Iterator it; 505 KABC::Addressee::List::Iterator it;
506
507 Resource * setRes = 0;
508 if ( !resource.isEmpty() ) {
509 KRES::Manager<Resource>::ActiveIterator it;
510 KRES::Manager<Resource> *manager = d->mManager;
511 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
512 qDebug("SaveAB::checking resource..." );
513 if ( (*it)->name() == resource ) {
514 setRes = (*it);
515 qDebug("Inserting imported contacs to resource %s", resource.latin1());
516 break;
517 }
518 }
519 }
506 for ( it = list.begin(); it != list.end(); ++it ) { 520 for ( it = list.begin(); it != list.end(); ++it ) {
507 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); 521 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" );
508 if ( !id.isEmpty() ) 522 if ( !id.isEmpty() )
509 (*it).setIDStr(id ); 523 (*it).setIDStr(id );
510 (*it).setResource( 0 ); 524 (*it).setResource( setRes );
511 if ( replaceLabel ) 525 if ( replaceLabel )
512 (*it).removeVoice(); 526 (*it).removeVoice();
513 if ( removeOld ) 527 if ( removeOld )
514 (*it).setTagged( true ); 528 (*it).setTagged( true );
515 insertAddressee( (*it), false, true ); 529 insertAddressee( (*it), false, true );
516 } 530 }
517 if ( removeOld ) 531 if ( removeOld )
518 removeUntagged(); 532 removeUntagged();
519 return list.count(); 533 return list.count();
520} 534}
521void AddressBook::setUntagged(bool setNonSyncTagged) // = false) 535void AddressBook::setUntagged(bool setNonSyncTagged, QString resource) // = false , "")
522{ 536{
523 Iterator ait; 537 Iterator ait;
524 for ( ait = begin(); ait != end(); ++ait ) { 538 if ( !resource.isEmpty() ) {
525 if ( setNonSyncTagged ) { 539 for ( ait = begin(); ait != end(); ++ait ) {
526 if ( (*ait).resource() && ! (*ait).resource()->includeInSync() ) { 540 if ( (*ait).resource() && (*ait).resource()->name() == resource ) {
541 (*ait).setTagged( false );
542 }
543 else
527 (*ait).setTagged( true ); 544 (*ait).setTagged( true );
545 }
546 } else {
547 for ( ait = begin(); ait != end(); ++ait ) {
548 if ( setNonSyncTagged ) {
549 if ( (*ait).resource() && ! (*ait).resource()->includeInSync() ) {
550 (*ait).setTagged( true );
551 } else
552 (*ait).setTagged( false );
528 } else 553 } else
529 (*ait).setTagged( false ); 554 (*ait).setTagged( false );
530 } else 555 }
531 (*ait).setTagged( false );
532 } 556 }
533} 557}
534void AddressBook::removeUntagged() 558void AddressBook::removeUntagged()
535{ 559{
536 Iterator ait; 560 Iterator ait;
537 bool todelete = false; 561 bool todelete = false;
538 Iterator todel; 562 Iterator todel;
539 for ( ait = begin(); ait != end(); ++ait ) { 563 for ( ait = begin(); ait != end(); ++ait ) {
540 if ( todelete ) 564 if ( todelete )
541 removeAddressee( todel ); 565 removeAddressee( todel );
542 if (!(*ait).tagged()) { 566 if (!(*ait).tagged()) {
543 todelete = true; 567 todelete = true;
544 todel = ait; 568 todel = ait;
545 } else 569 } else
546 todelete = false; 570 todelete = false;
547 } 571 }
548 if ( todelete ) 572 if ( todelete )
549 removeAddressee( todel ); 573 removeAddressee( todel );
550 deleteRemovedAddressees(); 574 deleteRemovedAddressees();
551} 575}
552void AddressBook::smplifyAddressees() 576void AddressBook::smplifyAddressees()
553{ 577{
554 Iterator ait; 578 Iterator ait;
555 for ( ait = begin(); ait != end(); ++ait ) { 579 for ( ait = begin(); ait != end(); ++ait ) {
556 (*ait).simplifyEmails(); 580 (*ait).simplifyEmails();
557 (*ait).simplifyPhoneNumbers(); 581 (*ait).simplifyPhoneNumbers();
558 (*ait).simplifyPhoneNumberTypes(); 582 (*ait).simplifyPhoneNumberTypes();
559 (*ait).simplifyAddresses(); 583 (*ait).simplifyAddresses();
560 } 584 }
561} 585}
562void AddressBook::removeSyncInfo( QString syncProfile) 586void AddressBook::removeSyncInfo( QString syncProfile)
563{ 587{
564 Iterator ait; 588 Iterator ait;
565 for ( ait = begin(); ait != end(); ++ait ) { 589 for ( ait = begin(); ait != end(); ++ait ) {
566 (*ait).removeID( syncProfile ); 590 (*ait).removeID( syncProfile );
567 } 591 }
568 if ( syncProfile.isEmpty() ) { 592 if ( syncProfile.isEmpty() ) {
569 Iterator it = begin(); 593 Iterator it = begin();
570 Iterator it2 ; 594 Iterator it2 ;
571 while ( it != end() ) { 595 while ( it != end() ) {
572 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 596 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
573 it2 = it; 597 it2 = it;
574 //qDebug("removing %s ",(*it).uid().latin1() ); 598 //qDebug("removing %s ",(*it).uid().latin1() );
575 ++it; 599 ++it;
576 removeAddressee( it2 ); 600 removeAddressee( it2 );
577 } else { 601 } else {
578 //qDebug("skipping %s ",(*it).uid().latin1() ); 602 //qDebug("skipping %s ",(*it).uid().latin1() );
579 ++it; 603 ++it;
580 } 604 }
581 } 605 }
582 } else { 606 } else {
583 Addressee lse; 607 Addressee lse;
584 lse = findByUid( "last-syncAddressee-"+ syncProfile ); 608 lse = findByUid( "last-syncAddressee-"+ syncProfile );
585 if ( ! lse.isEmpty() ) 609 if ( ! lse.isEmpty() )
586 removeAddressee( lse ); 610 removeAddressee( lse );
587 } 611 }
588 612
589} 613}
590void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync ) 614void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync )
591{ 615{
592 Iterator ait; 616 Iterator ait;
593 for ( ait = begin(); ait != end(); ++ait ) { 617 for ( ait = begin(); ait != end(); ++ait ) {
594 QString id = (*ait).IDStr(); 618 QString id = (*ait).IDStr();
595 (*ait).setIDStr( ":"); 619 (*ait).setIDStr( ":");
596 (*ait).setExternalUID( id ); 620 (*ait).setExternalUID( id );
597 (*ait).setOriginalExternalUID( id ); 621 (*ait).setOriginalExternalUID( id );
598 if ( isPreSync ) 622 if ( isPreSync )
599 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL ); 623 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL );
600 else { 624 else {
601 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); 625 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
602 (*ait).setID( currentSyncDevice,id ); 626 (*ait).setID( currentSyncDevice,id );
603 627
604 } 628 }
605 } 629 }
606} 630}
607void AddressBook::findNewExtIds( QString fileName, QString currentSyncDevice ) 631void AddressBook::findNewExtIds( QString fileName, QString currentSyncDevice )
608{ 632{
609 633
610 setUntagged(); 634 setUntagged();
611 KABC::Addressee::List list; 635 KABC::Addressee::List list;
612 QFile file( fileName ); 636 QFile file( fileName );
613 file.open( IO_ReadOnly ); 637 file.open( IO_ReadOnly );
614 QByteArray rawData = file.readAll(); 638 QByteArray rawData = file.readAll();
615 file.close(); 639 file.close();
616 QString data; 640 QString data;
617 641
618 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 642 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
619 KABC::VCardTool tool; 643 KABC::VCardTool tool;
620 list = tool.parseVCards( data ); 644 list = tool.parseVCards( data );
621 KABC::Addressee::List::Iterator it; 645 KABC::Addressee::List::Iterator it;
622 for ( it = list.begin(); it != list.end(); ++it ) { 646 for ( it = list.begin(); it != list.end(); ++it ) {
623 Iterator ait; 647 Iterator ait;
624 for ( ait = begin(); ait != end(); ++ait ) { 648 for ( ait = begin(); ait != end(); ++ait ) {
625 if ( !(*ait).tagged() ) { 649 if ( !(*ait).tagged() ) {
626 if ( (*ait).containsAdr(*it)) { 650 if ( (*ait).containsAdr(*it)) {
627 (*ait).setTagged(true); 651 (*ait).setTagged(true);
628 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); 652 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" );
629 (*it).setIDStr( ":"); 653 (*it).setIDStr( ":");
630 (*it).setID( currentSyncDevice,id ); 654 (*it).setID( currentSyncDevice,id );
631 (*it).setExternalUID( id ); 655 (*it).setExternalUID( id );
632 (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); 656 (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
633 (*it).setUid( ( (*ait).uid() )); 657 (*it).setUid( ( (*ait).uid() ));
634 break; 658 break;
635 } 659 }
636 } 660 }
637 661
638 } 662 }
639 if ( ait == end() ) 663 if ( ait == end() )
640 qDebug("ERROR:: no ext ID found for uid: %s", (*it).uid().latin1()); 664 qDebug("ERROR:: no ext ID found for uid: %s", (*it).uid().latin1());
641 } 665 }
642 clear(); 666 clear();
643 for ( it = list.begin(); it != list.end(); ++it ) { 667 for ( it = list.begin(); it != list.end(); ++it ) {
644 insertAddressee( (*it) ); 668 insertAddressee( (*it) );
645 } 669 }
646} 670}
647 671
648bool AddressBook::saveABphone( QString fileName ) 672bool AddressBook::saveABphone( QString fileName )
649{ 673{
650 //smplifyAddressees(); 674 //smplifyAddressees();
651 qDebug("saveABphone:: saving AB... "); 675 qDebug("saveABphone:: saving AB... ");
652 if ( ! export2PhoneFormat( QStringList() ,fileName ) ) 676 if ( ! export2PhoneFormat( QStringList() ,fileName ) )
653 return false; 677 return false;
654 qDebug("saveABphone:: writing to phone... "); 678 qDebug("saveABphone:: writing to phone... ");
655 if ( !PhoneAccess::writeToPhone( fileName) ) { 679 if ( !PhoneAccess::writeToPhone( fileName) ) {
656 return false; 680 return false;
657 } 681 }
658 qDebug("saveABphone:: re-reading from phone... "); 682 qDebug("saveABphone:: re-reading from phone... ");
659 if ( !PhoneAccess::readFromPhone( fileName) ) { 683 if ( !PhoneAccess::readFromPhone( fileName) ) {
660 return false; 684 return false;
661 } 685 }
662 return true; 686 return true;
663} 687}
664bool AddressBook::saveAB() 688bool AddressBook::saveAB()
665{ 689{
666 bool ok = true; 690 bool ok = true;
667 691
668 deleteRemovedAddressees(); 692 deleteRemovedAddressees();
669 Iterator ait; 693 Iterator ait;
670 for ( ait = begin(); ait != end(); ++ait ) { 694 for ( ait = begin(); ait != end(); ++ait ) {
671 if ( !(*ait).IDStr().isEmpty() ) { 695 if ( !(*ait).IDStr().isEmpty() ) {
672 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); 696 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() );
673 } 697 }
674 } 698 }
675 KRES::Manager<Resource>::ActiveIterator it; 699 KRES::Manager<Resource>::ActiveIterator it;
676 KRES::Manager<Resource> *manager = d->mManager; 700 KRES::Manager<Resource> *manager = d->mManager;
677 qDebug("SaveAB::saving..." ); 701 qDebug("SaveAB::saving..." );
678 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 702 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
679 qDebug("SaveAB::checking resource..." ); 703 qDebug("SaveAB::checking resource..." );
680 if ( (*it)->readOnly() ) 704 if ( (*it)->readOnly() )
681 qDebug("SaveAB::resource is readonly." ); 705 qDebug("SaveAB::resource is readonly." );
682 if ( (*it)->isOpen() ) 706 if ( (*it)->isOpen() )
683 qDebug("SaveAB::resource is open" ); 707 qDebug("SaveAB::resource is open" );
684 708
685 if ( !(*it)->readOnly() && (*it)->isOpen() ) { 709 if ( !(*it)->readOnly() && (*it)->isOpen() ) {
686 Ticket *ticket = requestSaveTicket( *it ); 710 Ticket *ticket = requestSaveTicket( *it );
687 qDebug("SaveAB::StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); 711 qDebug("SaveAB::StdAddressBook::save '%s'", (*it)->resourceName().latin1() );
688 if ( !ticket ) { 712 if ( !ticket ) {
689 qDebug( i18n( "SaveAB::Unable to save to resource '%1'. It is locked." ) 713 qDebug( i18n( "SaveAB::Unable to save to resource '%1'. It is locked." )
690 .arg( (*it)->resourceName() ) ); 714 .arg( (*it)->resourceName() ) );
691 return false; 715 return false;
692 } 716 }
693 717
694 //if ( !save( ticket ) ) 718 //if ( !save( ticket ) )
695 if ( ticket->resource() ) { 719 if ( ticket->resource() ) {
696 QString name = ticket->resource()->resourceName(); 720 QString name = ticket->resource()->resourceName();
697 if ( ! ticket->resource()->save( ticket ) ) 721 if ( ! ticket->resource()->save( ticket ) )
698 ok = false; 722 ok = false;
699 else 723 else
700 qDebug("SaveAB::resource saved '%s'", name.latin1() ); 724 qDebug("SaveAB::resource saved '%s'", name.latin1() );
701 725
702 } else 726 } else
703 ok = false; 727 ok = false;
704 728
705 } 729 }
706 } 730 }
707 return ok; 731 return ok;
708} 732}
709 733
710AddressBook::Iterator AddressBook::begin() 734AddressBook::Iterator AddressBook::begin()
711{ 735{
712 Iterator it = Iterator(); 736 Iterator it = Iterator();
713 it.d->mIt = d->mAddressees.begin(); 737 it.d->mIt = d->mAddressees.begin();
714 return it; 738 return it;
715} 739}
716 740
717AddressBook::ConstIterator AddressBook::begin() const 741AddressBook::ConstIterator AddressBook::begin() const
718{ 742{
719 ConstIterator it = ConstIterator(); 743 ConstIterator it = ConstIterator();
720 it.d->mIt = d->mAddressees.begin(); 744 it.d->mIt = d->mAddressees.begin();
721 return it; 745 return it;
722} 746}
723 747
724AddressBook::Iterator AddressBook::end() 748AddressBook::Iterator AddressBook::end()
725{ 749{
726 Iterator it = Iterator(); 750 Iterator it = Iterator();
727 it.d->mIt = d->mAddressees.end(); 751 it.d->mIt = d->mAddressees.end();
728 return it; 752 return it;
729} 753}
730 754
731AddressBook::ConstIterator AddressBook::end() const 755AddressBook::ConstIterator AddressBook::end() const
732{ 756{
733 ConstIterator it = ConstIterator(); 757 ConstIterator it = ConstIterator();
734 it.d->mIt = d->mAddressees.end(); 758 it.d->mIt = d->mAddressees.end();
735 return it; 759 return it;
736} 760}
737 761
738void AddressBook::clear() 762void AddressBook::clear()
739{ 763{
740 d->mAddressees.clear(); 764 d->mAddressees.clear();
741} 765}
742 766
743Ticket *AddressBook::requestSaveTicket( Resource *resource ) 767Ticket *AddressBook::requestSaveTicket( Resource *resource )
744{ 768{
745 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; 769 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl;
746 770
747 if ( !resource ) 771 if ( !resource )
748 { 772 {
749 qDebug("AddressBook::requestSaveTicket no resource" ); 773 qDebug("AddressBook::requestSaveTicket no resource" );
750 resource = standardResource(); 774 resource = standardResource();
751 } 775 }
752 776
753 KRES::Manager<Resource>::ActiveIterator it; 777 KRES::Manager<Resource>::ActiveIterator it;
754 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 778 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
755 if ( (*it) == resource ) { 779 if ( (*it) == resource ) {
756 if ( (*it)->readOnly() || !(*it)->isOpen() ) 780 if ( (*it)->readOnly() || !(*it)->isOpen() )
757 return 0; 781 return 0;
758 else 782 else
759 return (*it)->requestSaveTicket(); 783 return (*it)->requestSaveTicket();
760 } 784 }
761 } 785 }
762 786
763 return 0; 787 return 0;
764} 788}
765//void insertAddressee( const Addressee &, bool setRev = true, bool takeResource = false); 789//void insertAddressee( const Addressee &, bool setRev = true, bool takeResource = false);
766void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource ) 790void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource )
767{ 791{
768 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { 792 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) {
769 //qDebug("block insert "); 793 //qDebug("block insert ");
770 return; 794 return;
771 } 795 }
772 //qDebug("inserting.... %s ",a.uid().latin1() ); 796 //qDebug("inserting.... %s ",a.uid().latin1() );
773 bool found = false; 797 bool found = false;
774 Addressee::List::Iterator it; 798 Addressee::List::Iterator it;
775 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { 799 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) {
776 if ( a.uid() == (*it).uid() ) { 800 if ( a.uid() == (*it).uid() ) {
777 801
778 bool changed = false; 802 bool changed = false;
779 Addressee addr = a; 803 Addressee addr = a;
780 if ( addr != (*it) ) 804 if ( addr != (*it) )
781 changed = true; 805 changed = true;
782 806
783 if ( takeResource ) { 807 if ( takeResource ) {
784 Resource * res = (*it).resource(); 808 Resource * res = (*it).resource();
785 (*it) = a; 809 (*it) = a;
786 (*it).setResource( res ); 810 (*it).setResource( res );
787 } else { 811 } else {
788 (*it) = a; 812 (*it) = a;
789 if ( (*it).resource() == 0 ) 813 if ( (*it).resource() == 0 )
790 (*it).setResource( standardResource() ); 814 (*it).setResource( standardResource() );
791 } 815 }
792 if ( changed ) { 816 if ( changed ) {
793 if ( setRev ) { 817 if ( setRev ) {
794 (*it).setRevision( QDateTime::currentDateTime() ); 818 (*it).setRevision( QDateTime::currentDateTime() );
795 } 819 }
796 (*it).setChanged( true ); 820 (*it).setChanged( true );
797 } 821 }
798 822
799 found = true; 823 found = true;
800 } else { 824 } else {
801 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 825 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
802 QString name = (*it).uid().mid( 19 ); 826 QString name = (*it).uid().mid( 19 );
803 Addressee b = a; 827 Addressee b = a;
804 QString id = b.getID( name ); 828 QString id = b.getID( name );
805 if ( ! id.isEmpty() ) { 829 if ( ! id.isEmpty() ) {
806 QString des = (*it).note(); 830 QString des = (*it).note();
807 int startN; 831 int startN;
808 if( (startN = des.find( id ) ) >= 0 ) { 832 if( (startN = des.find( id ) ) >= 0 ) {
809 int endN = des.find( ",", startN+1 ); 833 int endN = des.find( ",", startN+1 );
810 des = des.left( startN ) + des.mid( endN+1 ); 834 des = des.left( startN ) + des.mid( endN+1 );
811 (*it).setNote( des ); 835 (*it).setNote( des );
812 } 836 }
813 } 837 }
814 } 838 }
815 } 839 }
816 } 840 }
817 if ( found ) 841 if ( found )
818 return; 842 return;
819 843
820 d->mAddressees.append( a ); 844 d->mAddressees.append( a );
821 Addressee& addr = d->mAddressees.last(); 845 Addressee& addr = d->mAddressees.last();
822 if ( addr.resource() == 0 ) 846 if ( addr.resource() == 0 )
823 addr.setResource( standardResource() ); 847 addr.setResource( standardResource() );
824 addr.setChanged( true ); 848 addr.setChanged( true );
825} 849}
826 850
827void AddressBook::removeAddressee( const Addressee &a ) 851void AddressBook::removeAddressee( const Addressee &a )
828{ 852{
829 Iterator it; 853 Iterator it;
830 Iterator it2; 854 Iterator it2;
831 bool found = false; 855 bool found = false;
832 for ( it = begin(); it != end(); ++it ) { 856 for ( it = begin(); it != end(); ++it ) {
833 if ( a.uid() == (*it).uid() ) { 857 if ( a.uid() == (*it).uid() ) {
834 found = true; 858 found = true;
835 it2 = it; 859 it2 = it;
836 } else { 860 } else {
837 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 861 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
838 QString name = (*it).uid().mid( 19 ); 862 QString name = (*it).uid().mid( 19 );
839 Addressee b = a; 863 Addressee b = a;
840 QString id = b.getID( name ); 864 QString id = b.getID( name );
841 if ( ! id.isEmpty() ) { 865 if ( ! id.isEmpty() ) {
842 QString des = (*it).note(); 866 QString des = (*it).note();
843 if( des.find( id ) < 0 ) { 867 if( des.find( id ) < 0 ) {
844 des += id + ","; 868 des += id + ",";
845 (*it).setNote( des ); 869 (*it).setNote( des );
846 } 870 }
847 } 871 }
848 } 872 }
849 873
850 } 874 }
851 } 875 }
852 876
853 if ( found ) 877 if ( found )
854 removeAddressee( it2 ); 878 removeAddressee( it2 );
855 879
856} 880}
857 881
858void AddressBook::removeSyncAddressees( bool removeDeleted ) 882void AddressBook::removeSyncAddressees( bool removeDeleted )
859{ 883{
860 Iterator it = begin(); 884 Iterator it = begin();
861 Iterator it2 ; 885 Iterator it2 ;
862 QDateTime dt ( QDate( 2003,1,1) ); 886 QDateTime dt ( QDate( 2003,1,1) );
863 while ( it != end() ) { 887 while ( it != end() ) {
864 (*it).setRevision( dt ); 888 (*it).setRevision( dt );
865 if (( *it).IDStr() != "changed" ) { 889 if (( *it).IDStr() != "changed" ) {
866 // "changed" is used for tagging changed addressees when syncing with KDE or OL 890 // "changed" is used for tagging changed addressees when syncing with KDE or OL
867 (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" ); 891 (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" );
868 (*it).setIDStr(":"); 892 (*it).setIDStr(":");
869 } 893 }
870 if ( ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE && removeDeleted )|| (*it).uid().left( 19 ) == QString("last-syncAddressee-")) { 894 if ( ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE && removeDeleted )|| (*it).uid().left( 19 ) == QString("last-syncAddressee-")) {
871 it2 = it; 895 it2 = it;
872 //qDebug("removing %s ",(*it).uid().latin1() ); 896 //qDebug("removing %s ",(*it).uid().latin1() );
873 ++it; 897 ++it;
874 removeAddressee( it2 ); 898 removeAddressee( it2 );
875 } else { 899 } else {
876 //qDebug("skipping %s ",(*it).uid().latin1() ); 900 //qDebug("skipping %s ",(*it).uid().latin1() );
877 if ( removeDeleted ) { 901 if ( removeDeleted ) {
878 // we have no postprocessing in the resource, we have to do it here 902 // we have no postprocessing in the resource, we have to do it here
879 // we have to compute csum for all, because it could be the first sync 903 // we have to compute csum for all, because it could be the first sync
880 (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); 904 (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
881 } 905 }
882 ++it; 906 ++it;
883 } 907 }
884 } 908 }
885 deleteRemovedAddressees(); 909 deleteRemovedAddressees();
886} 910}
887 911
888void AddressBook::removeAddressee( const Iterator &it ) 912void AddressBook::removeAddressee( const Iterator &it )
889{ 913{
890 d->mRemovedAddressees.append( (*it) ); 914 d->mRemovedAddressees.append( (*it) );
891 d->mAddressees.remove( it.d->mIt ); 915 d->mAddressees.remove( it.d->mIt );
892} 916}
893 917
894AddressBook::Iterator AddressBook::find( const Addressee &a ) 918AddressBook::Iterator AddressBook::find( const Addressee &a )
895{ 919{
896 Iterator it; 920 Iterator it;
897 for ( it = begin(); it != end(); ++it ) { 921 for ( it = begin(); it != end(); ++it ) {
898 if ( a.uid() == (*it).uid() ) { 922 if ( a.uid() == (*it).uid() ) {
899 return it; 923 return it;
900 } 924 }
901 } 925 }
902 return end(); 926 return end();
903} 927}
904 928
905Addressee AddressBook::findByUid( const QString &uid ) 929Addressee AddressBook::findByUid( const QString &uid )
906{ 930{
907 Iterator it; 931 Iterator it;
908 for ( it = begin(); it != end(); ++it ) { 932 for ( it = begin(); it != end(); ++it ) {
909 if ( uid == (*it).uid() ) { 933 if ( uid == (*it).uid() ) {
910 return *it; 934 return *it;
911 } 935 }
912 } 936 }
913 return Addressee(); 937 return Addressee();
914} 938}
915void AddressBook::preExternSync( AddressBook* aBook, const QString& csd , bool isSubset ) 939void AddressBook::preExternSync( AddressBook* aBook, const QString& csd , bool isSubset )
916{ 940{
917 //qDebug("AddressBook::preExternSync "); 941 //qDebug("AddressBook::preExternSync ");
918 AddressBook::Iterator it; 942 AddressBook::Iterator it;
919 for ( it = begin(); it != end(); ++it ) { 943 for ( it = begin(); it != end(); ++it ) {
920 (*it).setID( csd, (*it).externalUID() ); 944 (*it).setID( csd, (*it).externalUID() );
921 (*it).computeCsum( csd ); 945 (*it).computeCsum( csd );
922 } 946 }
923 mergeAB( aBook ,csd, isSubset ); 947 mergeAB( aBook ,csd, isSubset );
924} 948}
925void AddressBook::postExternSync( AddressBook* aBook , const QString& csd, bool setID) 949void AddressBook::postExternSync( AddressBook* aBook , const QString& csd, bool setID)
926{ 950{
927 //qDebug("AddressBook::postExternSync "); 951 //qDebug("AddressBook::postExternSync ");
928 AddressBook::Iterator it; 952 AddressBook::Iterator it;
929 int foundEmpty = 0; 953 int foundEmpty = 0;
930 for ( it = begin(); it != end(); ++it ) { 954 for ( it = begin(); it != end(); ++it ) {
931 //qDebug("check uid %s ", (*it).uid().latin1() ); 955 //qDebug("check uid %s ", (*it).uid().latin1() );
932 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID || 956 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID ||
933 (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_CSUM || 957 (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_CSUM ||
934 (*it).tempSyncStat() == SYNC_TEMPSTATE_ADDED_EXTERNAL) { 958 (*it).tempSyncStat() == SYNC_TEMPSTATE_ADDED_EXTERNAL) {
935 Addressee ad = aBook->findByUid( ( (*it).uid() )); 959 Addressee ad = aBook->findByUid( ( (*it).uid() ));
936 if ( ad.isEmpty() ) { 960 if ( ad.isEmpty() ) {
937 ++foundEmpty; 961 ++foundEmpty;
938 //qDebug("postExternSync:addressee is empty: %s ", (*it).uid().latin1()); 962 //qDebug("postExternSync:addressee is empty: %s ", (*it).uid().latin1());
939 //qDebug("-- formatted name %s ",(*it).formattedName().latin1() ); 963 //qDebug("-- formatted name %s ",(*it).formattedName().latin1() );
940 } else { 964 } else {
941 (*it).setIDStr(":"); 965 (*it).setIDStr(":");
942 if ( setID ) { 966 if ( setID ) {
943 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID ) 967 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID )
944 ad.setID( csd, (*it).externalUID() ); 968 ad.setID( csd, (*it).externalUID() );
945 } else 969 } else
946 ad.setID( csd, (*it).uid() ); 970 ad.setID( csd, (*it).uid() );
947 (*it).computeCsum( csd ); 971 (*it).computeCsum( csd );
948 ad.setCsum( csd, (*it).getCsum( csd ) ); 972 ad.setCsum( csd, (*it).getCsum( csd ) );
949 //qDebug("CSUM %s ",(*it).getCsum( csd ).latin1() ); 973 //qDebug("CSUM %s ",(*it).getCsum( csd ).latin1() );
950 aBook->insertAddressee( ad , false); 974 aBook->insertAddressee( ad , false);
951 } 975 }
952 } 976 }
953 } 977 }
954 if ( foundEmpty ) { 978 if ( foundEmpty ) {
955 qDebug("postExternSync:%d empty addressees found:\n probably filtered out by incoming sync filter.",foundEmpty ); 979 qDebug("postExternSync:%d empty addressees found:\n probably filtered out by incoming sync filter.",foundEmpty );
956 } 980 }
957 981
958} 982}
959 983
960bool AddressBook::containsExternalUid( const QString& uid ) 984bool AddressBook::containsExternalUid( const QString& uid )
961{ 985{
962 Iterator it; 986 Iterator it;
963 for ( it = begin(); it != end(); ++it ) { 987 for ( it = begin(); it != end(); ++it ) {
964 if ( uid == (*it).externalUID( ) ) 988 if ( uid == (*it).externalUID( ) )
965 return true; 989 return true;
966 } 990 }
967 return false; 991 return false;
968} 992}
969const Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile ) const 993const Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile ) const
970{ 994{
971 ConstIterator it; 995 ConstIterator it;
972 for ( it = begin(); it != end(); ++it ) { 996 for ( it = begin(); it != end(); ++it ) {
973 if ( uid == (*it).getID( profile ) ) 997 if ( uid == (*it).getID( profile ) )
974 return (*it); 998 return (*it);
975 } 999 }
976 return Addressee(); 1000 return Addressee();
977} 1001}
978void AddressBook::mergeAB( AddressBook *aBook, const QString& profile , bool isSubset ) 1002void AddressBook::mergeAB( AddressBook *aBook, const QString& profile , bool isSubset )
979{ 1003{
980 Iterator it; 1004 Iterator it;
981 Addressee ad; 1005 Addressee ad;
982 for ( it = begin(); it != end(); ++it ) { 1006 for ( it = begin(); it != end(); ++it ) {
983 ad = aBook->findByExternUid( (*it).externalUID(), profile ); 1007 ad = aBook->findByExternUid( (*it).externalUID(), profile );
984 if ( !ad.isEmpty() ) { 1008 if ( !ad.isEmpty() ) {
985 (*it).mergeContact( ad ,isSubset); 1009 (*it).mergeContact( ad ,isSubset);
986 } 1010 }
987 } 1011 }
988#if 0 1012#if 0
989 // test only 1013 // test only
990 for ( it = begin(); it != end(); ++it ) { 1014 for ( it = begin(); it != end(); ++it ) {
991 1015
992 qDebug("uid %s ", (*it).uid().latin1()); 1016 qDebug("uid %s ", (*it).uid().latin1());
993 } 1017 }
994#endif 1018#endif
995} 1019}
996 1020
997#if 0 1021#if 0
998Addressee::List AddressBook::getExternLastSyncAddressees() 1022Addressee::List AddressBook::getExternLastSyncAddressees()
999{ 1023{
1000 Addressee::List results; 1024 Addressee::List results;
1001 1025
1002 Iterator it; 1026 Iterator it;
1003 for ( it = begin(); it != end(); ++it ) { 1027 for ( it = begin(); it != end(); ++it ) {
1004 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) { 1028 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) {
1005 if ( (*it).familyName().left(4) == "!E: " ) 1029 if ( (*it).familyName().left(4) == "!E: " )
1006 results.append( *it ); 1030 results.append( *it );
1007 } 1031 }
1008 } 1032 }
1009 1033
1010 return results; 1034 return results;
1011} 1035}
1012#endif 1036#endif
1013void AddressBook::resetTempSyncStat() 1037void AddressBook::resetTempSyncStat()
1014{ 1038{
1015 Iterator it; 1039 Iterator it;
1016 for ( it = begin(); it != end(); ++it ) { 1040 for ( it = begin(); it != end(); ++it ) {
1017 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL ); 1041 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL );
1018 } 1042 }
1019 1043
1020} 1044}
1021 1045
1022QStringList AddressBook:: uidList() 1046QStringList AddressBook:: uidList()
1023{ 1047{
1024 QStringList results; 1048 QStringList results;
1025 Iterator it; 1049 Iterator it;
1026 for ( it = begin(); it != end(); ++it ) { 1050 for ( it = begin(); it != end(); ++it ) {
1027 results.append( (*it).uid() ); 1051 results.append( (*it).uid() );
1028 } 1052 }
1029 return results; 1053 return results;
1030} 1054}
1031 1055
1032 1056
1033Addressee::List AddressBook::allAddressees() 1057Addressee::List AddressBook::allAddressees()
1034{ 1058{
1035 return d->mAddressees; 1059 return d->mAddressees;
1036 1060
1037} 1061}
1038 1062
1039Addressee::List AddressBook::findByName( const QString &name ) 1063Addressee::List AddressBook::findByName( const QString &name )
1040{ 1064{
1041 Addressee::List results; 1065 Addressee::List results;
1042 1066
1043 Iterator it; 1067 Iterator it;
1044 for ( it = begin(); it != end(); ++it ) { 1068 for ( it = begin(); it != end(); ++it ) {
1045 if ( name == (*it).realName() ) { 1069 if ( name == (*it).realName() ) {
1046 results.append( *it ); 1070 results.append( *it );
1047 } 1071 }
1048 } 1072 }
1049 1073
1050 return results; 1074 return results;
1051} 1075}
1052 1076
1053Addressee::List AddressBook::findByEmail( const QString &email ) 1077Addressee::List AddressBook::findByEmail( const QString &email )
1054{ 1078{
1055 Addressee::List results; 1079 Addressee::List results;
1056 QStringList mailList; 1080 QStringList mailList;
1057 1081
1058 Iterator it; 1082 Iterator it;
1059 for ( it = begin(); it != end(); ++it ) { 1083 for ( it = begin(); it != end(); ++it ) {
1060 mailList = (*it).emails(); 1084 mailList = (*it).emails();
1061 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) { 1085 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) {
1062 if ( email == (*ite) ) { 1086 if ( email == (*ite) ) {
1063 results.append( *it ); 1087 results.append( *it );
1064 } 1088 }
1065 } 1089 }
1066 } 1090 }
1067 1091
1068 return results; 1092 return results;
1069} 1093}
1070 1094
1071Addressee::List AddressBook::findByCategory( const QString &category ) 1095Addressee::List AddressBook::findByCategory( const QString &category )
1072{ 1096{
1073 Addressee::List results; 1097 Addressee::List results;
1074 1098
1075 Iterator it; 1099 Iterator it;
1076 for ( it = begin(); it != end(); ++it ) { 1100 for ( it = begin(); it != end(); ++it ) {
1077 if ( (*it).hasCategory( category) ) { 1101 if ( (*it).hasCategory( category) ) {
1078 results.append( *it ); 1102 results.append( *it );
1079 } 1103 }
1080 } 1104 }
1081 1105
1082 return results; 1106 return results;
1083} 1107}
1084 1108
1085void AddressBook::dump() const 1109void AddressBook::dump() const
1086{ 1110{
1087 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl; 1111 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl;
1088 1112
1089 ConstIterator it; 1113 ConstIterator it;
1090 for( it = begin(); it != end(); ++it ) { 1114 for( it = begin(); it != end(); ++it ) {
1091 (*it).dump(); 1115 (*it).dump();
1092 } 1116 }
1093 1117
1094 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl; 1118 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl;
1095} 1119}
1096 1120
1097QString AddressBook::identifier() 1121QString AddressBook::identifier()
1098{ 1122{
1099 QStringList identifier; 1123 QStringList identifier;
1100 1124
1101 1125
1102 KRES::Manager<Resource>::ActiveIterator it; 1126 KRES::Manager<Resource>::ActiveIterator it;
1103 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 1127 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
1104 if ( !(*it)->identifier().isEmpty() ) 1128 if ( !(*it)->identifier().isEmpty() )
1105 identifier.append( (*it)->identifier() ); 1129 identifier.append( (*it)->identifier() );
1106 } 1130 }
1107 1131
1108 return identifier.join( ":" ); 1132 return identifier.join( ":" );
1109} 1133}
1110 1134
1111Field::List AddressBook::fields( int category ) 1135Field::List AddressBook::fields( int category )
1112{ 1136{
1113 if ( d->mAllFields.isEmpty() ) { 1137 if ( d->mAllFields.isEmpty() ) {
1114 d->mAllFields = Field::allFields(); 1138 d->mAllFields = Field::allFields();
1115 } 1139 }
1116 1140
1117 if ( category == Field::All ) return d->mAllFields; 1141 if ( category == Field::All ) return d->mAllFields;
1118 1142
1119 Field::List result; 1143 Field::List result;
1120 Field::List::ConstIterator it; 1144 Field::List::ConstIterator it;
1121 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) { 1145 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) {
1122 if ( (*it)->category() & category ) result.append( *it ); 1146 if ( (*it)->category() & category ) result.append( *it );
1123 } 1147 }
1124 1148
1125 return result; 1149 return result;
1126} 1150}
1127 1151
1128bool AddressBook::addCustomField( const QString &label, int category, 1152bool AddressBook::addCustomField( const QString &label, int category,
1129 const QString &key, const QString &app ) 1153 const QString &key, const QString &app )
1130{ 1154{
1131 if ( d->mAllFields.isEmpty() ) { 1155 if ( d->mAllFields.isEmpty() ) {
1132 d->mAllFields = Field::allFields(); 1156 d->mAllFields = Field::allFields();
1133 } 1157 }
1134//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app; 1158//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app;
1135 QString a = app.isNull() ? KGlobal::getAppName() : app; 1159 QString a = app.isNull() ? KGlobal::getAppName() : app;
1136 1160
1137 QString k = key.isNull() ? label : key; 1161 QString k = key.isNull() ? label : key;
1138 1162
1139 Field *field = Field::createCustomField( label, category, k, a ); 1163 Field *field = Field::createCustomField( label, category, k, a );
1140 1164
1141 if ( !field ) return false; 1165 if ( !field ) return false;
1142 1166
1143 d->mAllFields.append( field ); 1167 d->mAllFields.append( field );
1144 1168
1145 return true; 1169 return true;
1146} 1170}
1147 1171
1148QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab ) 1172QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab )
1149{ 1173{
1150 if (!ab.d) return s; 1174 if (!ab.d) return s;
1151 1175
1152 return s << ab.d->mAddressees; 1176 return s << ab.d->mAddressees;
1153} 1177}
1154 1178
1155QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab ) 1179QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab )
1156{ 1180{
1157 if (!ab.d) return s; 1181 if (!ab.d) return s;
1158 1182
1159 s >> ab.d->mAddressees; 1183 s >> ab.d->mAddressees;
1160 1184
1161 return s; 1185 return s;
1162} 1186}
1163 1187
1164bool AddressBook::addResource( Resource *resource ) 1188bool AddressBook::addResource( Resource *resource )
1165{ 1189{
1166 if ( !resource->open() ) { 1190 if ( !resource->open() ) {
1167 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl; 1191 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl;
1168 return false; 1192 return false;
1169 } 1193 }
1170 1194
1171 resource->setAddressBook( this ); 1195 resource->setAddressBook( this );
1172 1196
1173 d->mManager->add( resource ); 1197 d->mManager->add( resource );
1174 return true; 1198 return true;
1175} 1199}
1176void AddressBook::removeResources() 1200void AddressBook::removeResources()
1177{ 1201{
1178 //remove all possible resources. This should cleanup the configfile. 1202 //remove all possible resources. This should cleanup the configfile.
1179 QPtrList<KABC::Resource> mResources = resources(); 1203 QPtrList<KABC::Resource> mResources = resources();
1180 1204
1181 QPtrListIterator<KABC::Resource> it(mResources); 1205 QPtrListIterator<KABC::Resource> it(mResources);
1182 for ( ; it.current(); ++it ) { 1206 for ( ; it.current(); ++it ) {
1183 KABC::Resource *res = it.current(); 1207 KABC::Resource *res = it.current();
1184 removeResource(res); 1208 removeResource(res);
1185 } 1209 }
1186} 1210}
1187 1211
1188bool AddressBook::removeResource( Resource *resource ) 1212bool AddressBook::removeResource( Resource *resource )
1189{ 1213{
1190 resource->close(); 1214 resource->close();
1191 1215
1192 if ( resource == standardResource() ) 1216 if ( resource == standardResource() )
1193 d->mManager->setStandardResource( 0 ); 1217 d->mManager->setStandardResource( 0 );
1194 1218
1195 resource->setAddressBook( 0 ); 1219 resource->setAddressBook( 0 );
1196 1220
1197 d->mManager->remove( resource ); 1221 d->mManager->remove( resource );
1198 return true; 1222 return true;
1199} 1223}
1200 1224
1201QPtrList<Resource> AddressBook::resources() 1225QPtrList<Resource> AddressBook::resources()
1202{ 1226{
1203 QPtrList<Resource> list; 1227 QPtrList<Resource> list;
1204 1228
1205// qDebug("AddressBook::resources() 1"); 1229// qDebug("AddressBook::resources() 1");
1206 1230
1207 KRES::Manager<Resource>::ActiveIterator it; 1231 KRES::Manager<Resource>::ActiveIterator it;
1208 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 1232 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
1209 list.append( *it ); 1233 list.append( *it );
1210 1234
1211 return list; 1235 return list;
1212} 1236}
1213 1237
1214/*US 1238/*US
1215void AddressBook::setErrorHandler( ErrorHandler *handler ) 1239void AddressBook::setErrorHandler( ErrorHandler *handler )
1216{ 1240{
1217 delete d->mErrorHandler; 1241 delete d->mErrorHandler;
1218 d->mErrorHandler = handler; 1242 d->mErrorHandler = handler;
1219} 1243}
1220*/ 1244*/
1221 1245
1222void AddressBook::error( const QString& msg ) 1246void AddressBook::error( const QString& msg )
1223{ 1247{
1224/*US 1248/*US
1225 if ( !d->mErrorHandler ) // create default error handler 1249 if ( !d->mErrorHandler ) // create default error handler
1226 d->mErrorHandler = new ConsoleErrorHandler; 1250 d->mErrorHandler = new ConsoleErrorHandler;
1227 1251
1228 if ( d->mErrorHandler ) 1252 if ( d->mErrorHandler )
1229 d->mErrorHandler->error( msg ); 1253 d->mErrorHandler->error( msg );
1230 else 1254 else
1231 kdError(5700) << "no error handler defined" << endl; 1255 kdError(5700) << "no error handler defined" << endl;
1232*/ 1256*/
1233 kdDebug(5700) << "msg" << endl; 1257 kdDebug(5700) << "msg" << endl;
1234 qDebug(msg); 1258 qDebug(msg);
1235} 1259}
1236 1260
1237void AddressBook::deleteRemovedAddressees() 1261void AddressBook::deleteRemovedAddressees()
1238{ 1262{
1239 Addressee::List::Iterator it; 1263 Addressee::List::Iterator it;
1240 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) { 1264 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) {
1241 Resource *resource = (*it).resource(); 1265 Resource *resource = (*it).resource();
1242 if ( resource && !resource->readOnly() && resource->isOpen() ) 1266 if ( resource && !resource->readOnly() && resource->isOpen() )
1243 resource->removeAddressee( *it ); 1267 resource->removeAddressee( *it );
1244 } 1268 }
1245 1269
1246 d->mRemovedAddressees.clear(); 1270 d->mRemovedAddressees.clear();
1247} 1271}
1248 1272
1249void AddressBook::setStandardResource( Resource *resource ) 1273void AddressBook::setStandardResource( Resource *resource )
1250{ 1274{
1251// qDebug("AddressBook::setStandardResource 1"); 1275// qDebug("AddressBook::setStandardResource 1");
1252 d->mManager->setStandardResource( resource ); 1276 d->mManager->setStandardResource( resource );
1253} 1277}
1254 1278
1255Resource *AddressBook::standardResource() 1279Resource *AddressBook::standardResource()
1256{ 1280{
1257 return d->mManager->standardResource(); 1281 return d->mManager->standardResource();
1258} 1282}
1259 1283
1260KRES::Manager<Resource> *AddressBook::resourceManager() 1284KRES::Manager<Resource> *AddressBook::resourceManager()
1261{ 1285{
1262 return d->mManager; 1286 return d->mManager;
1263} 1287}
1264 1288
1265void AddressBook::cleanUp() 1289void AddressBook::cleanUp()
1266{ 1290{
1267 KRES::Manager<Resource>::ActiveIterator it; 1291 KRES::Manager<Resource>::ActiveIterator it;
1268 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 1292 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
1269 if ( !(*it)->readOnly() && (*it)->isOpen() ) 1293 if ( !(*it)->readOnly() && (*it)->isOpen() )
1270 (*it)->cleanUp(); 1294 (*it)->cleanUp();
1271 } 1295 }
1272} 1296}