/* Copyright (C) 2000-2003 Max Berger This file is part of vCardReceiver, written for the OpenGroupware project (OGo) OGo is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. OGo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with OGo; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // $Id$ #import "AddressBookSyncDatabaseHandler.h" #import "AddressBookSyncSessionHandler.h" #import @implementation AddressBookDatabaseHandler - (id)initWithRemoteTarget:(NSString *)t { self = [super init]; if (self) { remoteTarget = nil; sessionHandler = nil; addCount = 0; syncType = SYNCML_SYNC_ALERT_NO; } return self; } - (void) dealloc { [remoteTarget release]; [super dealloc]; } - (void)setSessionHandler:(AddressBookSessionHandler*)handler { sessionHandler = handler; } - (void)setRemoteTarget:(NSString *)t { [t retain]; [remoteTarget release]; remoteTarget = t; } - (void)setSyncType:(int)newSyncType { syncType = newSyncType; } - (NSString *)databaseLocation { return @"./AppleAddressBook"; } - (NSString *)databaseDescription { return @"Contacts in Apple Address Book"; } - (NSString *)syncWithLocation { return remoteTarget; } - (int) syncAlert { return syncType; } - (NSArray *)allUIDs { NSMutableArray *uids = [[NSMutableArray alloc]init]; NSEnumerator *peopleEnum = [[[ABAddressBook sharedAddressBook] people] objectEnumerator]; id record; while ((record = [peopleEnum nextObject])) { [uids addObject:[record uniqueId]]; } //NSLog(@"%@",uids); return [uids autorelease]; } - (NSString *)representationForUID:(id)uid { id rec = [[ABAddressBook sharedAddressBook]recordForUniqueId:uid]; NSString *vCardRep = nil; if ([rec respondsToSelector:@selector(vCardRepresentation)]) { // This is a real mess! Addressbook either returns a 7-bit string // when there are no extra chars or a unicode representation. The // Only way to find out is to try both. vCardRep = [[[NSString alloc] initWithData:[rec vCardRepresentation] encoding:NSASCIIStringEncoding]autorelease]; if (vCardRep == NULL) vCardRep = [[[NSString alloc] initWithData:[rec vCardRepresentation] encoding:NSUnicodeStringEncoding]autorelease]; } //NSLog(@"%@",rec); //NSLog(@"%@",vCardRep); return vCardRep; } - (id)versionForUID:(id)UID { return [self representationForUID:UID]; } - (NSString*)addElement:(NSString *)data withUri:(NSString *)location { NSString *uid; NSData *unicodedata = [data dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES]; ABPerson *person = [[ABPerson alloc]init]; BOOL succeeded = NO; NSString *username = [sessionHandler provideUsername]; [person initWithVCardRepresentation:unicodedata]; succeeded = [[ABAddressBook sharedAddressBook] addRecord:person]; if (username) { if ([username caseInsensitiveCompare:[person valueForProperty:kABNicknameProperty]]==NSOrderedSame) { [[ABAddressBook sharedAddressBook] setMe:person]; } } uid = [person uniqueId]; [person release]; if (succeeded) { addCount++; return uid; } return nil; } - (int)addCount { return addCount; } - (BOOL)deleteAll { NSEnumerator* enumerator = [[[ABAddressBook sharedAddressBook]people]objectEnumerator]; id object; BOOL succeeded = YES; while ((object=[enumerator nextObject])!=nil) { succeeded = succeeded && [[ABAddressBook sharedAddressBook] removeRecord:object]; } return succeeded; } @end