Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #3 from tonymillion/master

minor additions to ABAddressBook and to ABPerson
  • Loading branch information...
commit ab6e9635e63fd0eaa1232433029ed4c196180f8c 2 parents 33ad184 + fdf249e
@AlanQuatermain authored
Showing with 55 additions and 15 deletions.
  1. +3 −0  ABAddressBook.h
  2. +20 −15 ABAddressBook.m
  3. +4 −0 ABPerson.h
  4. +28 −0 ABPerson.m
View
3  ABAddressBook.h
@@ -43,6 +43,8 @@
@class ABRecord, ABPerson, ABGroup, ABSource;
@protocol ABAddressBookDelegate;
+extern NSString *ABAddressBookDidChangeNotification;
+
enum
{
ABOperationNotPermittedByStoreError = kABOperationNotPermittedByStoreError
@@ -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;
View
35 ABAddressBook.m
@@ -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]];
@@ -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 );
}
@@ -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
@@ -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
@@ -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 );
View
4 ABPerson.h
@@ -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;
@@ -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
View
28 ABPerson.m 100644 → 100755
@@ -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) );
@@ -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) );
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.