Skip to content
Browse files

Moved API into its own project.

Tweaked some routines slightly. For instance, ABMultiValue now supports NSCopying.
  • Loading branch information...
0 parents commit 730d8b8ff07f983783480e75c583ce44e9f1be0e @AlanQuatermain committed Jul 1, 2009
Showing with 1,361 additions and 0 deletions.
  1. +97 −0 ABAddressBook.h
  2. +261 −0 ABAddressBook.m
  3. +58 −0 ABGroup.h
  4. +122 −0 ABGroup.m
  5. +80 −0 ABMultiValue.h
  6. +186 −0 ABMultiValue.m
  7. +60 −0 ABPerson.h
  8. +107 −0 ABPerson.m
  9. +94 −0 ABProperties.h
  10. +59 −0 ABRecord.h
  11. +142 −0 ABRecord.m
  12. +44 −0 ABRefInitialization.h
  13. +46 −0 AddressBook.h
  14. +5 −0 README.textile
97 ABAddressBook.h
@@ -0,0 +1,97 @@
+/*
+ * ABAddressBook.h
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 5/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#import <Foundation/Foundation.h>
+#import <AddressBook/ABAddressBook.h>
+#import "ABRefInitialization.h"
+
+@class ABRecord, ABPerson, ABGroup;
+@protocol ABAddressBookDelegate;
+
+enum
+{
+ ABOperationNotPermittedByStoreError = kABOperationNotPermittedByStoreError
+
+};
+
+@interface ABAddressBook : NSObject <ABRefInitialization>
+{
+ ABAddressBookRef _ref;
+ id<ABAddressBookDelegate> _delegate;
+}
+
++ (ABAddressBook *) sharedAddressBook;
+
+@property (nonatomic, readonly, getter=getAddressBookRef) ABAddressBookRef addressBookRef;
+
+@property (nonatomic, assign) id<ABAddressBookDelegate> delegate;
+
+- (BOOL) save: (NSError **) error;
+@property (nonatomic, readonly) BOOL hasUnsavedChanges;
+
+- (BOOL) addRecord: (ABRecord *) record error: (NSError **) error;
+- (BOOL) removeRecord: (ABRecord *) record error: (NSError **) error;
+
+- (NSString *) localizedStringForLabel: (NSString *) labelName;
+
+- (void) revert;
+
+@end
+
+@interface ABAddressBook (People)
+
+@property (nonatomic, readonly) NSUInteger personCount;
+
+- (ABPerson *) personWithRecordID: (ABRecordID) recordID;
+- (NSArray *) allPeople;
+- (NSArray *) allPeopleWithName: (NSString *) name;
+
+@end
+
+@interface ABAddressBook (Groups)
+
+@property (nonatomic, readonly) NSUInteger groupCount;
+
+- (ABGroup *) groupWithRecordID: (ABRecordID) recordID;
+- (NSArray *) allGroups;
+
+@end
+
+@protocol ABAddressBookDelegate <NSObject>
+- (void) addressBookDidChange: (ABAddressBook *) addressBook;
+@end
261 ABAddressBook.m
@@ -0,0 +1,261 @@
+/*
+ * ABAddressBook.m
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 5/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#import <libkern/OSAtomic.h>
+
+#import "ABAddressBook.h"
+#import "ABPerson.h"
+#import "ABGroup.h"
+
+NSArray * WrappedArrayOfRecords( NSArray * records, Class<ABRefInitialization> wrapperClass )
+{
+ NSMutableArray * wrapped = [[NSMutableArray alloc] initWithCapacity: [records count]];
+ for ( id record in records )
+ {
+ id obj = [[wrapperClass alloc] initWithABRef: (CFTypeRef)record];
+ [wrapped addObject: obj];
+ [obj release];
+ }
+
+ NSArray * result = [wrapped copy];
+ [wrapped release];
+
+ return ( [result autorelease] );
+}
+
+@interface ABAddressBook ()
+- (void) _handleExternalChangeCallback;
+@end
+
+static void _ExternalChangeCallback( ABAddressBookRef bookRef, CFDictionaryRef info, void * context )
+{
+ ABAddressBook * obj = (ABAddressBook *) context;
+ [obj _handleExternalChangeCallback];
+}
+
+@implementation ABAddressBook
+
+@synthesize addressBookRef=_ref;
+
++ (ABAddressBook *) sharedAddressBook
+{
+ static ABAddressBook * __shared = nil;
+
+ if ( __shared == nil )
+ {
+ ABAddressBook * tmp = [[ABAddressBook alloc] init];
+ if ( OSAtomicCompareAndSwapPtr(nil, tmp, &__shared) == false )
+ [tmp release];
+ }
+
+ return ( __shared );
+}
+
+- (id) initWithABRef: (CFTypeRef) ref
+{
+ if ( ref == NULL )
+ {
+ [self release];
+ return ( nil );
+ }
+
+ if ( [super init] == nil )
+ return ( nil );
+
+ // we can't to CFTypeID checking on AB types, so we have to trust the user
+ _ref = (ABAddressBookRef) CFRetain(ref);
+
+ return ( self );
+}
+
+- (id) init
+{
+ if ( [super init] == nil )
+ return ( nil );
+
+ _ref = ABAddressBookCreate();
+ if ( _ref == NULL )
+ {
+ [self release];
+ return ( nil );
+ }
+
+ return ( self );
+}
+
+- (void) dealloc
+{
+ self.delegate = nil;
+ if ( _ref != NULL )
+ CFRelease( _ref );
+ [super dealloc];
+}
+
+- (id<ABAddressBookDelegate>) delegate
+{
+ return ( _delegate );
+}
+
+- (void) setDelegate: (id<ABAddressBookDelegate>) delegate
+{
+ if ( (_delegate == nil) && (delegate != nil) )
+ ABAddressBookRegisterExternalChangeCallback( _ref, _ExternalChangeCallback, self );
+ else if ( (_delegate != nil) && (delegate == nil) )
+ ABAddressBookUnregisterExternalChangeCallback( _ref, _ExternalChangeCallback, self );
+
+ _delegate = delegate;
+}
+
+- (BOOL) save: (NSError **) error
+{
+ return ( (BOOL) ABAddressBookSave(_ref, (CFErrorRef *)error) );
+}
+
+- (BOOL) hasUnsavedChanges
+{
+ return ( (BOOL) ABAddressBookHasUnsavedChanges(_ref) );
+}
+
+- (BOOL) addRecord: (ABRecord *) record error: (NSError **) error
+{
+ return ( (BOOL) ABAddressBookAddRecord(_ref, record.recordRef, (CFErrorRef *)error) );
+}
+
+- (BOOL) removeRecord: (ABRecord *) record error: (NSError **) error
+{
+ return ( (BOOL) ABAddressBookRemoveRecord(_ref, record.recordRef, (CFErrorRef *)error) );
+}
+
+- (NSString *) localizedStringForLabel: (NSString *) label
+{
+ NSString * str = (NSString *) ABAddressBookCopyLocalizedLabel( (CFStringRef)label );
+ return ( [str autorelease] );
+}
+
+- (void) revert
+{
+ ABAddressBookRevert( _ref );
+}
+
+- (void) _handleExternalChangeCallback
+{
+ [_delegate addressBookDidChange: self];
+}
+
+@end
+
+@implementation ABAddressBook (People)
+
+- (NSUInteger) personCount
+{
+ return ( (NSUInteger) ABAddressBookGetPersonCount(_ref) );
+}
+
+- (ABPerson *) personWithRecordID: (ABRecordID) recordID
+{
+ ABRecordRef person = ABAddressBookGetPersonWithRecordID( _ref, recordID );
+ if ( person == NULL )
+ return ( nil );
+
+ return ( [[[ABPerson alloc] initWithABRef: person] autorelease] );
+}
+
+- (NSArray *) allPeople
+{
+ NSArray * people = (NSArray *) ABAddressBookCopyArrayOfAllPeople( _ref );
+ if ( [people count] == 0 )
+ {
+ [people release];
+ return ( nil );
+ }
+
+ NSArray * result = WrappedArrayOfRecords( people, [ABPerson class] );
+ [people release];
+
+ return ( result );
+}
+
+- (NSArray *) allPeopleWithName: (NSString *) name
+{
+ NSArray * people = (NSArray *) ABAddressBookCopyPeopleWithName( _ref, (CFStringRef)name );
+ if ( [people count] == 0 )
+ {
+ [people release];
+ return ( nil );
+ }
+
+ NSArray * result = WrappedArrayOfRecords( people, [ABPerson class] );
+ [people release];
+
+ return ( result );
+}
+
+@end
+
+@implementation ABAddressBook (Groups)
+
+- (NSUInteger) groupCount
+{
+ return ( (NSUInteger) ABAddressBookGetGroupCount(_ref) );
+}
+
+- (ABGroup *) groupWithRecordID: (ABRecordID) recordID
+{
+ ABRecordRef group = ABAddressBookGetGroupWithRecordID( _ref, recordID );
+ if ( group == NULL )
+ return ( nil );
+
+ return ( [[[ABGroup alloc] initWithABRef: group] autorelease] );
+}
+
+- (NSArray *) allGroups
+{
+ NSArray * groups = (NSArray *) ABAddressBookCopyArrayOfAllGroups( _ref );
+ if ( [groups count] == 0 )
+ {
+ [groups release];
+ return ( nil );
+ }
+
+ NSArray * result = WrappedArrayOfRecords( groups, [ABGroup class] );
+ [groups release];
+
+ return ( result );
+}
+
+@end
58 ABGroup.h
@@ -0,0 +1,58 @@
+/*
+ * ABGroup.h
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 6/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+
+#import <Foundation/Foundation.h>
+#import <AddressBook/ABGroup.h>
+#import "ABRecord.h"
+
+@class ABPerson;
+
+@interface ABGroup : ABRecord
+
+// use -init to create a new group
+
+- (NSArray *) allMembers;
+- (NSArray *) allMembersSortedInOrder: (ABPersonSortOrdering) order;
+
+- (BOOL) addMember: (ABPerson *) person error: (NSError **) error;
+- (BOOL) removeMember: (ABPerson *) person error: (NSError **) error;
+
+- (NSIndexSet *) indexSetWithAllMemberRecordIDs;
+
+@end
122 ABGroup.m
@@ -0,0 +1,122 @@
+/*
+ * ABGroup.m
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 6/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+
+#import "ABGroup.h"
+#import "ABPerson.h"
+
+extern NSArray * WrappedArrayOfRecords( NSArray * records, Class<ABRefInitialization> class );
+
+@implementation ABGroup
+
+- (id) init
+{
+ ABRecordRef group = ABGroupCreate();
+ if ( group == NULL )
+ {
+ [self release];
+ return ( nil );
+ }
+
+ return ( [self initWithABRef: group] );
+}
+
+- (NSArray *) allMembers
+{
+ NSArray * members = (NSArray *) ABGroupCopyArrayOfAllMembers( _ref );
+ if ( [members count] == 0 )
+ {
+ [members release];
+ return ( nil );
+ }
+
+ NSArray * result = (NSArray *) WrappedArrayOfRecords( members, [ABPerson class] );
+ [members release];
+
+ return ( result );
+}
+
+- (NSArray *) allMembersSortedInOrder: (ABPersonSortOrdering) order
+{
+ NSArray * members = (NSArray *) ABGroupCopyArrayOfAllMembersWithSortOrdering( _ref, order );
+ if ( [members count] == 0 )
+ {
+ [members release];
+ return ( nil );
+ }
+
+ NSArray * result = (NSArray *) WrappedArrayOfRecords( members, [ABPerson class] );
+ [members release];
+
+ return ( result );
+}
+
+- (BOOL) addMember: (ABPerson *) person error: (NSError **) error
+{
+ return ( (BOOL) ABGroupAddMember(_ref, person.recordRef, (CFErrorRef *)error) );
+}
+
+- (BOOL) removeMember: (ABPerson *) person error: (NSError **) error
+{
+ return ( (BOOL) ABGroupRemoveMember(_ref, person.recordRef, (CFErrorRef *)error) );
+}
+
+- (NSIndexSet *) indexSetWithAllMemberRecordIDs
+{
+ NSArray * members = (NSArray *) ABGroupCopyArrayOfAllMembers( _ref );
+ if ( [members count] == 0 )
+ {
+ [members release];
+ return ( nil );
+ }
+
+ NSMutableIndexSet * mutable = [[NSMutableIndexSet alloc] init];
+ for ( id person in members )
+ {
+ [mutable addIndex: (NSUInteger)ABRecordGetRecordID((ABRecordRef)person)];
+ }
+
+ [members release];
+
+ NSIndexSet * result = [mutable copy];
+ [mutable release];
+
+ return ( [result autorelease] );
+}
+
+@end
80 ABMultiValue.h
@@ -0,0 +1,80 @@
+/*
+ * ABMultiValue.h
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 6/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#import <Foundation/Foundation.h>
+#import <AddressBook/ABMultiValue.h>
+#import "ABRefInitialization.h"
+
+@interface ABMultiValue : NSObject <ABRefInitialization, NSCopying, NSMutableCopying>
+{
+ ABMultiValueRef _ref;
+}
+
+@property (nonatomic, readonly, getter=getMultiValueRef) ABMultiValueRef multiValueRef;
+
+@property (nonatomic, readonly) ABPropertyType propertyType;
+@property (nonatomic, readonly) NSUInteger count;
+
+- (id) valueAtIndex: (NSUInteger) index;
+- (NSArray *) allValues;
+
+- (NSString *) labelAtIndex: (NSUInteger) index;
+
+- (NSUInteger) indexForIdentifier: (ABMultiValueIdentifier) identifier;
+- (ABMultiValueIdentifier) identifierAtIndex: (NSUInteger) index;
+
+// returns the index of the first occurrence of the specified value
+- (NSUInteger) indexOfValue: (id) value;
+
+@end
+
+#pragma mark -
+
+@interface ABMutableMultiValue : ABMultiValue
+
+- (id) initWithPropertyType: (ABPropertyType) type;
+
+- (BOOL) addValue: (id) value withLabel: (NSString *) label identifier: (ABMultiValueIdentifier *) outIdentifier;
+- (BOOL) insertValue: (id) value withLabel: (NSString *) label atIndex: (NSUInteger) index identifier: (ABMultiValueIdentifier *) outIdentifier;
+
+- (BOOL) removeValueAndLabelAtIndex: (NSUInteger) index;
+
+- (BOOL) replaceValueAtIndex: (NSUInteger) index withValue: (id) value;
+- (BOOL) replaceLabelAtIndex: (NSUInteger) index withLabel: (NSString *) label;
+
+@end
186 ABMultiValue.m
@@ -0,0 +1,186 @@
+/*
+ * ABMultiValue.m
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 6/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#import "ABMultiValue.h"
+
+@implementation ABMultiValue
+
+@synthesize multiValueRef=_ref;
+
+- (id) initWithABRef: (CFTypeRef) ref
+{
+ if ( ref == NULL )
+ {
+ [self release];
+ return ( nil );
+ }
+
+ if ( [super init] == nil )
+ return ( nil );
+
+ _ref = (ABMultiValueRef) CFRetain(ref);
+
+ return ( self );
+}
+
+- (void) dealloc
+{
+ if ( _ref != NULL )
+ CFRelease( _ref );
+ [super dealloc];
+}
+
+- (id) copyWithZone: (NSZone *) zone
+{
+ return ( [self retain] );
+}
+
+- (id) mutableCopyWithZone: (NSZone *) zone
+{
+ ABMultiValueRef ref = ABMultiValueCreateMutableCopy( _ref );
+ ABMutableMultiValue * result = [[ABMutableMultiValue allocWithZone: zone] initWithABRef: (CFTypeRef)ref];
+ CFRelease( ref );
+ return ( result );
+}
+
+- (ABPropertyType) propertyType
+{
+ return ( ABMultiValueGetPropertyType(_ref) );
+}
+
+- (NSUInteger) count
+{
+ return ( (NSUInteger) ABMultiValueGetCount(_ref) );
+}
+
+- (id) valueAtIndex: (NSUInteger) index
+{
+ id value = (id) ABMultiValueCopyValueAtIndex( _ref, (CFIndex)index );
+ return ( [value autorelease] );
+}
+
+- (NSArray *) allValues
+{
+ NSArray * array = (NSArray *) ABMultiValueCopyArrayOfAllValues( _ref );
+ return ( [array autorelease] );
+}
+
+- (NSString *) labelAtIndex: (NSUInteger) index
+{
+ NSString * result = (NSString *) ABMultiValueCopyLabelAtIndex( _ref, (CFIndex)index );
+ return ( [result autorelease] );
+}
+
+- (NSUInteger) indexForIdentifier: (ABMultiValueIdentifier) identifier
+{
+ return ( (NSUInteger) ABMultiValueGetIndexForIdentifier(_ref, identifier) );
+}
+
+- (ABMultiValueIdentifier) identifierAtIndex: (NSUInteger) index
+{
+ return ( ABMultiValueGetIdentifierAtIndex(_ref, (CFIndex)index) );
+}
+
+- (NSUInteger) indexOfValue: (id) value
+{
+ return ( (NSUInteger) ABMultiValueGetFirstIndexOfValue(_ref, (CFTypeRef)value) );
+}
+
+@end
+
+#pragma mark -
+
+@implementation ABMutableMultiValue
+
+- (id) initWithPropertyType: (ABPropertyType) type
+{
+ ABMutableMultiValueRef ref = ABMultiValueCreateMutable(type);
+ if ( ref == NULL )
+ {
+ [self release];
+ return ( nil );
+ }
+
+ return ( [self initWithABRef: (CFTypeRef)ref] );
+}
+
+- (id) copyWithZone: (NSZone *) zone
+{
+ // no AB method to create an immutable copy, so we do a mutable copy but wrap it in an immutable class
+ CFTypeRef _obj = ABMultiValueCreateMutableCopy(_ref);
+ ABMultiValue * result = [[ABMultiValue allocWithZone: zone] initWithABRef: _obj];
+ CFRelease( _obj );
+ return ( result );
+}
+
+- (ABMutableMultiValueRef) _mutableRef
+{
+ return ( (ABMutableMultiValueRef)_ref );
+}
+
+- (BOOL) addValue: (id) value
+ withLabel: (NSString *) label
+ identifier: (ABMultiValueIdentifier *) outIdentifier
+{
+ return ( (BOOL) ABMultiValueAddValueAndLabel([self _mutableRef], (CFTypeRef)value, (CFStringRef)label, outIdentifier) );
+}
+
+- (BOOL) insertValue: (id) value
+ withLabel: (NSString *) label
+ atIndex: (NSUInteger) index
+ identifier: (ABMultiValueIdentifier *) outIdentifier
+{
+ return ( (BOOL) ABMultiValueInsertValueAndLabelAtIndex([self _mutableRef], (CFTypeRef)value, (CFStringRef)label, (CFIndex)index, outIdentifier) );
+}
+
+- (BOOL) removeValueAndLabelAtIndex: (NSUInteger) index
+{
+ return ( (BOOL) ABMultiValueRemoveValueAndLabelAtIndex([self _mutableRef], (CFIndex)index) );
+}
+
+- (BOOL) replaceValueAtIndex: (NSUInteger) index withValue: (id) value
+{
+ return ( (BOOL) ABMultiValueReplaceValueAtIndex([self _mutableRef], (CFTypeRef)value, (CFIndex)index) );
+}
+
+- (BOOL) replaceLabelAtIndex: (NSUInteger) index withLabel: (NSString *) label
+{
+ return ( (BOOL) ABMultiValueReplaceLabelAtIndex([self _mutableRef], (CFStringRef)label, (CFIndex)index) );
+}
+
+@end
60 ABPerson.h
@@ -0,0 +1,60 @@
+/*
+ * ABPerson.h
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 6/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#import <Foundation/Foundation.h>
+#import <AddressBook/ABPerson.h>
+#import "ABRecord.h"
+
+@interface ABPerson : ABRecord
+
+// use -init to create a new person
+
++ (ABPropertyType) typeOfProperty: (ABPropertyID) property;
++ (NSString *) localizedNameOfProperty: (ABPropertyID) property;
++ (ABPersonSortOrdering) sortOrdering;
++ (ABPersonCompositeNameFormat) compositeNameFormat;
+
+- (BOOL) setImageData: (NSData *) imageData error: (NSError **) error;
+- (NSData *) imageData;
+@property (nonatomic, readonly) BOOL hasImageData;
+- (BOOL) removeImageData: (NSError **) error;
+
+- (NSComparisonResult) compare: (ABPerson *) otherPerson;
+- (NSComparisonResult) compare: (ABPerson *) otherPerson sortOrdering: (ABPersonSortOrdering) order;
+
+@end
107 ABPerson.m
@@ -0,0 +1,107 @@
+/*
+ * ABPerson.m
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 6/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#import "ABPerson.h"
+
+@implementation ABPerson
+
++ (ABPropertyType) typeOfProperty: (ABPropertyID) property
+{
+ return ( ABPersonGetTypeOfProperty(property) );
+}
+
++ (NSString *) localizedNameOfProperty: (ABPropertyID) property
+{
+ NSString * name = (NSString *) ABPersonCopyLocalizedPropertyName(property);
+ return ( [name autorelease] );
+}
+
++ (ABPersonSortOrdering) sortOrdering
+{
+ return ( ABPersonGetSortOrdering() );
+}
+
++ (ABPersonCompositeNameFormat) compositeNameFormat
+{
+ return ( ABPersonGetCompositeNameFormat() );
+}
+
+- (id) init
+{
+ ABRecordRef person = ABPersonCreate();
+ if ( person == NULL )
+ {
+ [self release];
+ return ( nil );
+ }
+
+ return ( [self initWithABRef: person] );
+}
+
+- (BOOL) setImageData: (NSData *) imageData error: (NSError **) error
+{
+ return ( (BOOL) ABPersonSetImageData(_ref, (CFDataRef)imageData, (CFErrorRef *)error) );
+}
+
+- (NSData *) imageData
+{
+ NSData * imageData = (NSData *) ABPersonCopyImageData( _ref );
+ return ( [imageData autorelease] );
+}
+
+- (BOOL) hasImageData
+{
+ return ( (BOOL) ABPersonHasImageData(_ref) );
+}
+
+- (BOOL) removeImageData: (NSError **) error
+{
+ return ( (BOOL) ABPersonRemoveImageData(_ref, (CFErrorRef *)error) );
+}
+
+- (NSComparisonResult) compare: (ABPerson *) otherPerson
+{
+ return ( [self compare: otherPerson sortOrdering: ABPersonGetSortOrdering()] );
+}
+
+- (NSComparisonResult) compare: (ABPerson *) otherPerson sortOrdering: (ABPersonSortOrdering) order
+{
+ return ( (NSComparisonResult) ABPersonComparePeopleByName(_ref, otherPerson->_ref, order) );
+}
+
+@end
94 ABProperties.h
@@ -0,0 +1,94 @@
+/*
+ * ABProperties.h
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 6/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#import <AddressBook/AddressBook.h>
+
+// macros to avoid copious type-casts when using ObjC wrappers
+
+// Generic labels
+#define ABWorkLabel (NSString *) kABWorkLabel
+#define ABHomeLabel (NSString *) kABHomeLabel
+#define ABOtherLabel (NSString *) kABOtherLabel
+
+// Addresses
+#define ABPersonAddressStreetKey (NSString *) kABPersonAddressStreetKey
+#define ABPersonAddressCityKey (NSString *) kABPersonAddressCityKey
+#define ABPersonAddressStateKey (NSString *) kABPersonAddressStateKey
+#define ABPersonAddressZIPKey (NSString *) kABPersonAddressZIPKey
+#define ABPersonAddressCountryKey (NSString *) kABPersonAddressCountryKey
+#define ABPersonAddressCountryCodeKey (NSString *) kABPersonAddressCountryCodeKey
+
+// Dates
+#define ABPersonAnniversaryLabel (NSString *) kABPersonAnniversaryLabel
+
+// Kind
+#define ABPersonKindPerson (NSNumber *) kABPersonKindPerson
+#define ABPersonKindOrganization (NSNumber *) kABPersonKindOrganization
+
+// Phone Numbers
+#define ABPersonPhoneMobileLabel (NSString *) kABPersonPhoneMobileLabel
+#define ABPersonPhoneIPhoneLabel (NSString *) kABPersonPhoneIPhoneLabel
+#define ABPersonPhoneMainLabel (NSString *) kABPersonPhoneMainLabel
+#define ABPersonPhoneHomeFAXLabel (NSString *) kABPersonPhoneHomeFAXLabel
+#define ABPersonPhoneWorkFAXLabel (NSString *) kABPersonPhoneWorkFAXLabel
+#define ABPersonPhonePagerLabel (NSString *) kABPersonPhonePagerLabel
+
+// IM
+#define ABPersonInstantMessageServiceKey (NSString *) kABPersonInstantMessageServiceKey
+#define ABPersonInstantMessageServiceYahoo (NSString *) kABPersonInstantMessageServiceYahoo
+#define ABPersonInstantMessageServiceJabber (NSString *) kABPersonInstantMessageServiceJabber
+#define ABPersonInstantMessageServiceMSN (NSString *) kABPersonInstantMessageServiceMSN
+#define ABPersonInstantMessageServiceICQ (NSString *) kABPersonInstantMessageServiceICQ
+#define ABPersonInstantMessageServiceAIM (NSString *) kABPersonInstantMessageServiceAIM
+#define ABPersonInstantMessageUsernameKey (NSString *) kABPersonInstantMessageUsernameKey
+
+// URLs
+#define ABPersonHomePageLabel (NSString *) kABPersonHomePageLabel
+
+// Related Names
+#define ABPersonFatherLabel (NSString *) kABPersonFatherLabel
+#define ABPersonMotherLabel (NSString *) kABPersonMotherLabel
+#define ABPersonParentLabel (NSString *) kABPersonParentLabel
+#define ABPersonBrotherLabel (NSString *) kABPersonBrotherLabel
+#define ABPersonSisterLabel (NSString *) kABPersonSisterLabel
+#define ABPersonChildLabel (NSString *) kABPersonChildLabel
+#define ABPersonFriendLabel (NSString *) kABPersonFriendLabel
+#define ABPersonSpouseLabel (NSString *) kABPersonSpouseLabel
+#define ABPersonPartnerLabel (NSString *) kABPersonPartnerLabel
+#define ABPersonAssistantLabel (NSString *) kABPersonAssistantLabel
+#define ABPersonManagerLabel (NSString *) kABPersonManagerLabel
59 ABRecord.h
@@ -0,0 +1,59 @@
+/*
+ * ABRecord.h
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 5/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#import <Foundation/Foundation.h>
+#import <AddressBook/ABRecord.h>
+#import "ABRefInitialization.h"
+
+@interface ABRecord : NSObject <ABRefInitialization>
+{
+ ABRecordRef _ref;
+}
+
+@property (nonatomic, readonly, getter=getRecordRef) ABRecordRef recordRef;
+
+@property (nonatomic, readonly) ABRecordID recordID;
+@property (nonatomic, readonly) ABRecordType recordType;
+
+- (id) valueForProperty: (ABPropertyID) property;
+- (BOOL) setValue: (id) value forProperty: (ABPropertyID) property error: (NSError **) error;
+- (BOOL) removeValueForProperty: (ABPropertyID) property error: (NSError **) error;
+
+@property (nonatomic, readonly) NSString * compositeName;
+
+@end
142 ABRecord.m
@@ -0,0 +1,142 @@
+/*
+ * ABRecord.h
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 5/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#import "ABRecord.h"
+#import "ABMultiValue.h"
+
+#import <AddressBook/ABPerson.h>
+
+static NSMutableIndexSet * __multiValuePropertyIDSet = nil;
+
+@implementation ABRecord
+
+@synthesize recordRef=_ref;
+
++ (void) initialize
+{
+ if ( self != [ABRecord class] )
+ return;
+
+ __multiValuePropertyIDSet = [[NSMutableIndexSet alloc] init];
+ [__multiValuePropertyIDSet addIndex: kABPersonEmailProperty];
+ [__multiValuePropertyIDSet addIndex: kABPersonAddressProperty];
+ [__multiValuePropertyIDSet addIndex: kABPersonDateProperty];
+ [__multiValuePropertyIDSet addIndex: kABPersonPhoneProperty];
+ [__multiValuePropertyIDSet addIndex: kABPersonInstantMessageProperty];
+ [__multiValuePropertyIDSet addIndex: kABPersonURLProperty];
+ [__multiValuePropertyIDSet addIndex: kABPersonRelatedNamesProperty];
+}
+
++ (Class<ABRefInitialization>) wrapperClassForPropertyID: (ABPropertyID) propID
+{
+ if ( [__multiValuePropertyIDSet containsIndex: propID] )
+ return ( [ABMultiValue class] );
+
+ return ( Nil );
+}
+
+- (id) initWithABRef: (CFTypeRef) recordRef
+{
+ if ( recordRef == NULL )
+ {
+ [self release];
+ return ( nil );
+ }
+
+ if ( [super init] == nil )
+ return ( nil );
+
+ // we have to trust the user that the type is correct -- no CFTypeRef checking in AddressBook.framework
+ _ref = (ABRecordRef) CFRetain(recordRef);
+
+ return ( self );
+}
+
+- (void) dealloc
+{
+ if ( _ref != NULL )
+ CFRelease( _ref );
+ [super dealloc];
+}
+
+- (ABRecordID) recordID
+{
+ return ( ABRecordGetRecordID(_ref) );
+}
+
+- (ABRecordType) recordType
+{
+ return ( ABRecordGetRecordType(_ref) );
+}
+
+- (id) valueForProperty: (ABPropertyID) property
+{
+ CFTypeRef value = ABRecordCopyValue( _ref, property );
+ if ( value == NULL )
+ return ( nil );
+
+ id result = nil;
+
+ Class<ABRefInitialization> wrapperClass = [[self class] wrapperClassForPropertyID: property];
+ if ( wrapperClass != Nil )
+ result = [[wrapperClass alloc] initWithABRef: value];
+ else
+ result = (id) value;
+
+ return ( [result autorelease] );
+}
+
+- (BOOL) setValue: (id) value forProperty: (ABPropertyID) property error: (NSError **) error
+{
+ if ( [value isKindOfClass: [ABMultiValue class]] )
+ value = (id) [value getMultiValueRef];
+ return ( (BOOL) ABRecordSetValue(_ref, property, (CFTypeRef)value, (CFErrorRef *)error) );
+}
+
+- (BOOL) removeValueForProperty: (ABPropertyID) property error: (NSError **) error
+{
+ return ( (BOOL) ABRecordRemoveValue(_ref, property, (CFErrorRef *)error) );
+}
+
+- (NSString *) compositeName
+{
+ NSString * result = (NSString *) ABRecordCopyCompositeName( _ref );
+ return ( [result autorelease] );
+}
+
+@end
44 ABRefInitialization.h
@@ -0,0 +1,44 @@
+/*
+ * ABRefInitialization.h
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 6/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#import <CoreFoundation/CoreFoundation.h>
+
+@protocol ABRefInitialization <NSObject>
++ (id) alloc; // this keeps the compiler happy
+- (id) initWithABRef: (CFTypeRef) ref;
+@end
46 AddressBook.h
@@ -0,0 +1,46 @@
+/*
+ * AddressBook.h
+ * iPhoneContacts
+ *
+ * Created by Jim Dovey on 6/6/2009.
+ *
+ * Copyright (c) 2009 Jim Dovey
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the project's author nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#import <Foundation/Foundation.h>
+
+#import "ABProperties.h"
+#import "ABAddressBook.h"
+#import "ABRecord.h"
+#import "ABMultiValue.h"
+#import "ABPerson.h"
+#import "ABGroup.h"
5 README.textile
@@ -0,0 +1,5 @@
+h2. iPhone AddressBook Objective-C Wrapper APIs
+
+The classes in this project implement a (currently lightweight) wrapper around the iPhone's pure-C AddressBook framework. Currently it simply plays nicely with things like autorelease pools and implements other Objective-C paradigms such as property-based accessors. In the future I hope to make it look and behave in a manner similar to the ObjC AddressBook APIs in Mac OS X.
+
+This project was originally part of "AQToolkit":http://github.com/AlanQuatermain/aqtoolkit. For the brief history of this code prior to the start of this project, see that one.

0 comments on commit 730d8b8

Please sign in to comment.
Something went wrong with that request. Please try again.