Skip to content

Commit

Permalink
Merge pull request #3 from tonymillion/master
Browse files Browse the repository at this point in the history
minor additions to ABAddressBook and to ABPerson
  • Loading branch information
AlanQuatermain committed Jun 26, 2011
2 parents 33ad184 + fdf249e commit ab6e963
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
3 changes: 3 additions & 0 deletions ABAddressBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
@class ABRecord, ABPerson, ABGroup, ABSource;
@protocol ABAddressBookDelegate;

extern NSString *ABAddressBookDidChangeNotification;

enum
{
ABOperationNotPermittedByStoreError = kABOperationNotPermittedByStoreError
Expand Down Expand Up @@ -78,6 +80,7 @@ enum
@property (nonatomic, readonly) NSUInteger personCount;

- (ABPerson *) personWithRecordID: (ABRecordID) recordID;
- (ABPerson *) personWithRecordRef:(ABRecordRef) recordRef;
- (NSArray *) allPeople;
- (NSArray *) allPeopleSorted;
- (NSArray *) allPeopleWithName: (NSString *) name;
Expand Down
35 changes: 20 additions & 15 deletions ABAddressBook.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#import "ABGroup.h"
#import "ABSource.h"

NSString *ABAddressBookDidChangeNotification = @"ABAddressBookDidChange";

NSArray * WrappedArrayOfRecords( NSArray * records, Class<ABRefInitialization> wrapperClass )
{
NSMutableArray * wrapped = [[NSMutableArray alloc] initWithCapacity: [records count]];
Expand Down Expand Up @@ -100,21 +102,19 @@ - (id) initWithABRef: (CFTypeRef) ref

// we can't to CFTypeID checking on AB types, so we have to trust the user
_ref = (ABAddressBookRef) CFRetain(ref);

ABAddressBookRegisterExternalChangeCallback( _ref, _ExternalChangeCallback, self );

return ( self );
}

- (id) init
{
if ( [super init] == nil )
return ( nil );
ABAddressBookRef ref = ABAddressBookCreate();

_ref = ABAddressBookCreate();
if ( _ref == NULL )
{
[self release];
return ( nil );
}
self = [self initWithABRef:ref];

CFRelease(ref);

return ( self );
}
Expand All @@ -134,17 +134,14 @@ - (void) dealloc

- (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 result = (BOOL) ABAddressBookSave(_ref, (CFErrorRef *)error);
[[NSNotificationCenter defaultCenter] postNotificationName:ABAddressBookDidChangeNotification object:self];
return ( result );
}

- (BOOL) hasUnsavedChanges
Expand Down Expand Up @@ -175,7 +172,10 @@ - (void) revert

- (void) _handleExternalChangeCallback
{
[_delegate addressBookDidChange: self];
if(_delegate && [_delegate respondsToSelector:@selector(addressBookDidChange:)])
[_delegate addressBookDidChange: self];

[[NSNotificationCenter defaultCenter] postNotificationName:ABAddressBookDidChangeNotification object:self];
}

@end
Expand All @@ -187,6 +187,11 @@ - (NSUInteger) personCount
return ( (NSUInteger) ABAddressBookGetPersonCount(_ref) );
}

-(ABPerson *) personWithRecordRef:(ABRecordRef) recordRef
{
return ( [[[ABPerson alloc] initWithABRef: recordRef] autorelease] );
}

- (ABPerson *) personWithRecordID: (ABRecordID) recordID
{
ABRecordRef person = ABAddressBookGetPersonWithRecordID( _ref, recordID );
Expand Down
4 changes: 4 additions & 0 deletions ABPerson.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
@interface ABPerson : ABRecord

// use -init to create a new person
@property (readonly, getter = getFirstName) NSString * firstName;
@property (readonly, getter = getLastName) NSString * lastName;

+ (ABPropertyType) typeOfProperty: (ABPropertyID) property;
+ (NSString *) localizedNameOfProperty: (ABPropertyID) property;
Expand All @@ -51,10 +53,12 @@

- (BOOL) setImageData: (NSData *) imageData error: (NSError **) error;
- (NSData *) imageData;
- (NSData *) thumbnailImageData;
@property (nonatomic, readonly) BOOL hasImageData;
- (BOOL) removeImageData: (NSError **) error;

- (NSComparisonResult) compare: (ABPerson *) otherPerson;
- (NSComparisonResult) compare: (ABPerson *) otherPerson sortOrdering: (ABPersonSortOrdering) order;


@end
28 changes: 28 additions & 0 deletions ABPerson.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ - (id) init
return ( [self initWithABRef: person] );
}

- (NSString*) description
{
return [NSString stringWithFormat: @"ABPerson %d - %@",[self recordID], [self compositeName]];
}

- (BOOL) setImageData: (NSData *) imageData error: (NSError **) error
{
return ( (BOOL) ABPersonSetImageData(_ref, (CFDataRef)imageData, (CFErrorRef *)error) );
Expand All @@ -84,6 +89,12 @@ - (NSData *) imageData
return ( [imageData autorelease] );
}

- (NSData *) thumbnailImageData
{
NSData * imageData = (NSData *) ABPersonCopyImageDataWithFormat( _ref, kABPersonImageFormatThumbnail );
return ( [imageData autorelease] );
}

- (BOOL) hasImageData
{
return ( (BOOL) ABPersonHasImageData(_ref) );
Expand All @@ -104,4 +115,21 @@ - (NSComparisonResult) compare: (ABPerson *) otherPerson sortOrdering: (ABPerson
return ( (NSComparisonResult) ABPersonComparePeopleByName(_ref, otherPerson->_ref, order) );
}

-(NSString*)getFirstName
{
return [self valueForProperty:kABPersonFirstNameProperty];
}

-(NSString*)getLastName
{
NSString * lastName = [self valueForProperty:kABPersonLastNameProperty];

if(lastName == nil)
{
lastName = [self compositeName];
}

return lastName;
}

@end

0 comments on commit ab6e963

Please sign in to comment.