-rw-r--r-- | kabc/addressbook.cpp | 101 |
1 files changed, 99 insertions, 2 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp index 47d298a..70eda1b 100644 --- a/kabc/addressbook.cpp +++ b/kabc/addressbook.cpp | |||
@@ -173,184 +173,281 @@ const Addressee &AddressBook::ConstIterator::operator*() const | |||
173 | { | 173 | { |
174 | return *(d->mIt); | 174 | return *(d->mIt); |
175 | } | 175 | } |
176 | 176 | ||
177 | const Addressee* AddressBook::ConstIterator::operator->() const | 177 | const Addressee* AddressBook::ConstIterator::operator->() const |
178 | { | 178 | { |
179 | return &(*(d->mIt)); | 179 | return &(*(d->mIt)); |
180 | } | 180 | } |
181 | 181 | ||
182 | AddressBook::ConstIterator &AddressBook::ConstIterator::operator++() | 182 | AddressBook::ConstIterator &AddressBook::ConstIterator::operator++() |
183 | { | 183 | { |
184 | (d->mIt)++; | 184 | (d->mIt)++; |
185 | return *this; | 185 | return *this; |
186 | } | 186 | } |
187 | 187 | ||
188 | AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int) | 188 | AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int) |
189 | { | 189 | { |
190 | (d->mIt)++; | 190 | (d->mIt)++; |
191 | return *this; | 191 | return *this; |
192 | } | 192 | } |
193 | 193 | ||
194 | AddressBook::ConstIterator &AddressBook::ConstIterator::operator--() | 194 | AddressBook::ConstIterator &AddressBook::ConstIterator::operator--() |
195 | { | 195 | { |
196 | (d->mIt)--; | 196 | (d->mIt)--; |
197 | return *this; | 197 | return *this; |
198 | } | 198 | } |
199 | 199 | ||
200 | AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int) | 200 | AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int) |
201 | { | 201 | { |
202 | (d->mIt)--; | 202 | (d->mIt)--; |
203 | return *this; | 203 | return *this; |
204 | } | 204 | } |
205 | 205 | ||
206 | bool AddressBook::ConstIterator::operator==( const ConstIterator &it ) | 206 | bool AddressBook::ConstIterator::operator==( const ConstIterator &it ) |
207 | { | 207 | { |
208 | return ( d->mIt == it.d->mIt ); | 208 | return ( d->mIt == it.d->mIt ); |
209 | } | 209 | } |
210 | 210 | ||
211 | bool AddressBook::ConstIterator::operator!=( const ConstIterator &it ) | 211 | bool AddressBook::ConstIterator::operator!=( const ConstIterator &it ) |
212 | { | 212 | { |
213 | return ( d->mIt != it.d->mIt ); | 213 | return ( d->mIt != it.d->mIt ); |
214 | } | 214 | } |
215 | 215 | ||
216 | 216 | ||
217 | AddressBook::AddressBook() | 217 | AddressBook::AddressBook() |
218 | { | 218 | { |
219 | init(0, "contact"); | 219 | init(0, "contact"); |
220 | } | 220 | } |
221 | 221 | ||
222 | AddressBook::AddressBook( const QString &config ) | 222 | AddressBook::AddressBook( const QString &config ) |
223 | { | 223 | { |
224 | init(config, "contact"); | 224 | init(config, "contact"); |
225 | } | 225 | } |
226 | 226 | ||
227 | AddressBook::AddressBook( const QString &config, const QString &family ) | 227 | AddressBook::AddressBook( const QString &config, const QString &family ) |
228 | { | 228 | { |
229 | init(config, family); | 229 | init(config, family); |
230 | 230 | ||
231 | } | 231 | } |
232 | 232 | ||
233 | // the default family is "contact" | 233 | // the default family is "contact" |
234 | void AddressBook::init(const QString &config, const QString &family ) | 234 | void AddressBook::init(const QString &config, const QString &family ) |
235 | { | 235 | { |
236 | d = new AddressBookData; | 236 | d = new AddressBookData; |
237 | QString fami = family; | ||
238 | qDebug("new ab "); | ||
237 | if (config != 0) { | 239 | if (config != 0) { |
240 | qDebug("config != 0 "); | ||
241 | if ( family == "syncContact" ) { | ||
242 | qDebug("creating sync config "); | ||
243 | fami = "contact"; | ||
244 | KConfig* con = new KConfig( locateLocal("config", "syncContactrc") ); | ||
245 | con->setGroup( "General" ); | ||
246 | con->writeEntry( "ResourceKeys", QString("sync") ); | ||
247 | con->writeEntry( "Standard", QString("sync") ); | ||
248 | con->setGroup( "Resource_sync" ); | ||
249 | con->writeEntry( "FileFormat", QString("vcard") ); | ||
250 | con->writeEntry( "FileName", config ); | ||
251 | con->writeEntry( "ResourceIdentifier", QString("sync") ); | ||
252 | con->writeEntry( "ResourceName", QString("sync_res") ); | ||
253 | con->writeEntry( "ResourceType", QString("file") ); | ||
254 | //con->sync(); | ||
255 | d->mConfig = con; | ||
256 | } | ||
257 | else | ||
238 | d->mConfig = new KConfig( locateLocal("config", config) ); | 258 | d->mConfig = new KConfig( locateLocal("config", config) ); |
239 | // qDebug("AddressBook::init 1 config=%s",config.latin1() ); | 259 | // qDebug("AddressBook::init 1 config=%s",config.latin1() ); |
240 | } | 260 | } |
241 | else { | 261 | else { |
242 | d->mConfig = 0; | 262 | d->mConfig = 0; |
243 | // qDebug("AddressBook::init 1 config=0"); | 263 | // qDebug("AddressBook::init 1 config=0"); |
244 | } | 264 | } |
245 | 265 | ||
246 | //US d->mErrorHandler = 0; | 266 | //US d->mErrorHandler = 0; |
247 | d->mManager = new KRES::Manager<Resource>( family, false ); | 267 | d->mManager = new KRES::Manager<Resource>( fami, false ); |
248 | d->mManager->readConfig( d->mConfig ); | 268 | d->mManager->readConfig( d->mConfig ); |
269 | if ( family == "syncContact" ) { | ||
270 | KRES::Manager<Resource> *manager = d->mManager; | ||
271 | KRES::Manager<Resource>::ActiveIterator it; | ||
272 | for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { | ||
273 | (*it)->setAddressBook( this ); | ||
274 | if ( !(*it)->open() ) | ||
275 | error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) ); | ||
276 | } | ||
277 | Resource *res = standardResource(); | ||
278 | if ( !res ) { | ||
279 | qDebug("ERROR: no standard resource"); | ||
280 | res = manager->createResource( "file" ); | ||
281 | if ( res ) | ||
282 | { | ||
283 | addResource( res ); | ||
284 | } | ||
285 | else | ||
286 | qDebug(" No resource available!!!"); | ||
287 | } | ||
288 | setStandardResource( res ); | ||
289 | manager->writeConfig(); | ||
290 | } | ||
291 | addCustomField( i18n( "Department" ), KABC::Field::Organization, | ||
292 | "X-Department", "KADDRESSBOOK" ); | ||
293 | addCustomField( i18n( "Profession" ), KABC::Field::Organization, | ||
294 | "X-Profession", "KADDRESSBOOK" ); | ||
295 | addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization, | ||
296 | "X-AssistantsName", "KADDRESSBOOK" ); | ||
297 | addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization, | ||
298 | "X-ManagersName", "KADDRESSBOOK" ); | ||
299 | addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal, | ||
300 | "X-SpousesName", "KADDRESSBOOK" ); | ||
301 | addCustomField( i18n( "Office" ), KABC::Field::Personal, | ||
302 | "X-Office", "KADDRESSBOOK" ); | ||
303 | addCustomField( i18n( "IM Address" ), KABC::Field::Personal, | ||
304 | "X-IMAddress", "KADDRESSBOOK" ); | ||
305 | addCustomField( i18n( "Anniversary" ), KABC::Field::Personal, | ||
306 | "X-Anniversary", "KADDRESSBOOK" ); | ||
307 | |||
308 | //US added this field to become compatible with Opie/qtopia addressbook | ||
309 | // values can be "female" or "male" or "". An empty field represents undefined. | ||
310 | addCustomField( i18n( "Gender" ), KABC::Field::Personal, | ||
311 | "X-Gender", "KADDRESSBOOK" ); | ||
312 | addCustomField( i18n( "Children" ), KABC::Field::Personal, | ||
313 | "X-Children", "KADDRESSBOOK" ); | ||
314 | addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal, | ||
315 | "X-FreeBusyUrl", "KADDRESSBOOK" ); | ||
316 | addCustomField( i18n( "ExternalID" ), KABC::Field::Personal, | ||
317 | "X-ExternalID", "KADDRESSBOOK" ); | ||
249 | } | 318 | } |
250 | 319 | ||
251 | AddressBook::~AddressBook() | 320 | AddressBook::~AddressBook() |
252 | { | 321 | { |
253 | delete d->mConfig; d->mConfig = 0; | 322 | delete d->mConfig; d->mConfig = 0; |
254 | delete d->mManager; d->mManager = 0; | 323 | delete d->mManager; d->mManager = 0; |
255 | //US delete d->mErrorHandler; d->mErrorHandler = 0; | 324 | //US delete d->mErrorHandler; d->mErrorHandler = 0; |
256 | delete d; d = 0; | 325 | delete d; d = 0; |
257 | } | 326 | } |
258 | 327 | ||
259 | bool AddressBook::load() | 328 | bool AddressBook::load() |
260 | { | 329 | { |
261 | 330 | ||
262 | 331 | ||
263 | clear(); | 332 | clear(); |
264 | 333 | ||
265 | KRES::Manager<Resource>::ActiveIterator it; | 334 | KRES::Manager<Resource>::ActiveIterator it; |
266 | bool ok = true; | 335 | bool ok = true; |
267 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) | 336 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) |
268 | if ( !(*it)->load() ) { | 337 | if ( !(*it)->load() ) { |
269 | error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); | 338 | error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); |
270 | ok = false; | 339 | ok = false; |
271 | } | 340 | } |
272 | 341 | ||
273 | // mark all addressees as unchanged | 342 | // mark all addressees as unchanged |
274 | Addressee::List::Iterator addrIt; | 343 | Addressee::List::Iterator addrIt; |
275 | for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) | 344 | for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) |
276 | (*addrIt).setChanged( false ); | 345 | (*addrIt).setChanged( false ); |
277 | 346 | ||
278 | return ok; | 347 | return ok; |
279 | } | 348 | } |
280 | 349 | ||
281 | bool AddressBook::save( Ticket *ticket ) | 350 | bool AddressBook::save( Ticket *ticket ) |
282 | { | 351 | { |
283 | kdDebug(5700) << "AddressBook::save()"<< endl; | 352 | kdDebug(5700) << "AddressBook::save()"<< endl; |
284 | 353 | ||
285 | if ( ticket->resource() ) { | 354 | if ( ticket->resource() ) { |
286 | deleteRemovedAddressees(); | 355 | deleteRemovedAddressees(); |
287 | |||
288 | return ticket->resource()->save( ticket ); | 356 | return ticket->resource()->save( ticket ); |
289 | } | 357 | } |
290 | 358 | ||
291 | return false; | 359 | return false; |
292 | } | 360 | } |
361 | bool AddressBook::saveAB() | ||
362 | { | ||
363 | bool ok = true; | ||
364 | |||
365 | deleteRemovedAddressees(); | ||
366 | |||
367 | KRES::Manager<Resource>::ActiveIterator it; | ||
368 | KRES::Manager<Resource> *manager = d->mManager; | ||
369 | for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { | ||
370 | if ( !(*it)->readOnly() && (*it)->isOpen() ) { | ||
371 | Ticket *ticket = requestSaveTicket( *it ); | ||
372 | // qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); | ||
373 | if ( !ticket ) { | ||
374 | error( i18n( "Unable to save to resource '%1'. It is locked." ) | ||
375 | .arg( (*it)->resourceName() ) ); | ||
376 | return false; | ||
377 | } | ||
378 | |||
379 | //if ( !save( ticket ) ) | ||
380 | if ( ticket->resource() ) { | ||
381 | if ( ! ticket->resource()->save( ticket ) ) | ||
382 | ok = false; | ||
383 | } else | ||
384 | ok = false; | ||
385 | |||
386 | } | ||
387 | } | ||
388 | return ok; | ||
389 | } | ||
293 | 390 | ||
294 | AddressBook::Iterator AddressBook::begin() | 391 | AddressBook::Iterator AddressBook::begin() |
295 | { | 392 | { |
296 | Iterator it = Iterator(); | 393 | Iterator it = Iterator(); |
297 | it.d->mIt = d->mAddressees.begin(); | 394 | it.d->mIt = d->mAddressees.begin(); |
298 | return it; | 395 | return it; |
299 | } | 396 | } |
300 | 397 | ||
301 | AddressBook::ConstIterator AddressBook::begin() const | 398 | AddressBook::ConstIterator AddressBook::begin() const |
302 | { | 399 | { |
303 | ConstIterator it = ConstIterator(); | 400 | ConstIterator it = ConstIterator(); |
304 | it.d->mIt = d->mAddressees.begin(); | 401 | it.d->mIt = d->mAddressees.begin(); |
305 | return it; | 402 | return it; |
306 | } | 403 | } |
307 | 404 | ||
308 | AddressBook::Iterator AddressBook::end() | 405 | AddressBook::Iterator AddressBook::end() |
309 | { | 406 | { |
310 | Iterator it = Iterator(); | 407 | Iterator it = Iterator(); |
311 | it.d->mIt = d->mAddressees.end(); | 408 | it.d->mIt = d->mAddressees.end(); |
312 | return it; | 409 | return it; |
313 | } | 410 | } |
314 | 411 | ||
315 | AddressBook::ConstIterator AddressBook::end() const | 412 | AddressBook::ConstIterator AddressBook::end() const |
316 | { | 413 | { |
317 | ConstIterator it = ConstIterator(); | 414 | ConstIterator it = ConstIterator(); |
318 | it.d->mIt = d->mAddressees.end(); | 415 | it.d->mIt = d->mAddressees.end(); |
319 | return it; | 416 | return it; |
320 | } | 417 | } |
321 | 418 | ||
322 | void AddressBook::clear() | 419 | void AddressBook::clear() |
323 | { | 420 | { |
324 | d->mAddressees.clear(); | 421 | d->mAddressees.clear(); |
325 | } | 422 | } |
326 | 423 | ||
327 | Ticket *AddressBook::requestSaveTicket( Resource *resource ) | 424 | Ticket *AddressBook::requestSaveTicket( Resource *resource ) |
328 | { | 425 | { |
329 | kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; | 426 | kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; |
330 | 427 | ||
331 | if ( !resource ) | 428 | if ( !resource ) |
332 | { | 429 | { |
333 | qDebug("AddressBook::requestSaveTicket no resource" ); | 430 | qDebug("AddressBook::requestSaveTicket no resource" ); |
334 | resource = standardResource(); | 431 | resource = standardResource(); |
335 | } | 432 | } |
336 | 433 | ||
337 | KRES::Manager<Resource>::ActiveIterator it; | 434 | KRES::Manager<Resource>::ActiveIterator it; |
338 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { | 435 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { |
339 | if ( (*it) == resource ) { | 436 | if ( (*it) == resource ) { |
340 | if ( (*it)->readOnly() || !(*it)->isOpen() ) | 437 | if ( (*it)->readOnly() || !(*it)->isOpen() ) |
341 | return 0; | 438 | return 0; |
342 | else | 439 | else |
343 | return (*it)->requestSaveTicket(); | 440 | return (*it)->requestSaveTicket(); |
344 | } | 441 | } |
345 | } | 442 | } |
346 | 443 | ||
347 | return 0; | 444 | return 0; |
348 | } | 445 | } |
349 | 446 | ||
350 | void AddressBook::insertAddressee( const Addressee &a ) | 447 | void AddressBook::insertAddressee( const Addressee &a ) |
351 | { | 448 | { |
352 | Addressee::List::Iterator it; | 449 | Addressee::List::Iterator it; |
353 | for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { | 450 | for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { |
354 | if ( a.uid() == (*it).uid() ) { | 451 | if ( a.uid() == (*it).uid() ) { |
355 | bool changed = false; | 452 | bool changed = false; |
356 | Addressee addr = a; | 453 | Addressee addr = a; |