forked from playup/ABContactHelper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ABGroup.m
116 lines (97 loc) · 3.13 KB
/
ABGroup.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook, 3.0 Edition
BSD License, Use at your own risk
*/
#import "ABGroup.h"
#import "ABContactsHelper.h"
#define CFAutorelease(obj) ({CFTypeRef _obj = (obj); (_obj == NULL) ? NULL : [(id)CFMakeCollectable(_obj) autorelease]; })
@implementation ABGroup
@synthesize record;
// Thanks to Quentarez, Ciaran
- (id) initWithRecord: (ABRecordRef) aRecord
{
if ((self = [super init]))
{
record = CFRetain(aRecord);
}
return self;
}
+ (id) groupWithRecord: (ABRecordRef) grouprec
{
return [[[ABGroup alloc] initWithRecord:grouprec] autorelease];
}
+ (id) groupWithRecordID: (ABRecordID) recordID
{
ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
ABRecordRef grouprec = ABAddressBookGetGroupWithRecordID(addressBook, recordID);
return [self groupWithRecord:grouprec];
}
// Thanks to Ciaran
+ (id) group
{
ABRecordRef grouprec = ABGroupCreate();
id group = [ABGroup groupWithRecord:grouprec];
CFRelease(grouprec);
return group;
}
- (void) dealloc
{
if (record) CFRelease(record);
[super dealloc];
}
- (BOOL) removeSelfFromAddressBook: (NSError **) error
{
ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
if (!ABAddressBookRemoveRecord(addressBook, self.record, (CFErrorRef *) error)) return NO;
return ABAddressBookSave(addressBook, (CFErrorRef *) error);
}
#pragma mark Record ID and Type
- (ABRecordID) recordID {return ABRecordGetRecordID(record);}
- (ABRecordType) recordType {return ABRecordGetRecordType(record);}
- (BOOL) isPerson {return self.recordType == kABPersonType;}
#pragma mark management
- (NSArray *) members
{
NSArray *contacts = (NSArray *)ABGroupCopyArrayOfAllMembers(self.record);
NSMutableArray *array = [NSMutableArray arrayWithCapacity:contacts.count];
for (id contact in contacts)
[array addObject:[ABContact contactWithRecord:(ABRecordRef)contact]];
[contacts release];
return array;
}
// kABPersonSortByFirstName = 0, kABPersonSortByLastName = 1
- (NSArray *) membersWithSorting: (ABPersonSortOrdering) ordering
{
NSArray *contacts = (NSArray *)ABGroupCopyArrayOfAllMembersWithSortOrdering(self.record, ordering);
NSMutableArray *array = [NSMutableArray arrayWithCapacity:contacts.count];
for (id contact in contacts)
[array addObject:[ABContact contactWithRecord:(ABRecordRef)contact]];
[contacts release];
return array;
}
- (BOOL) addMember: (ABContact *) contact withError: (NSError **) error
{
return ABGroupAddMember(self.record, contact.record, (CFErrorRef *) error);
}
- (BOOL) removeMember: (ABContact *) contact withError: (NSError **) error
{
return ABGroupRemoveMember(self.record, contact.record, (CFErrorRef *) error);
}
#pragma mark name
- (NSString *) getRecordString:(ABPropertyID) anID
{
return [(NSString *) ABRecordCopyValue(record, anID) autorelease];
}
- (NSString *) name
{
NSString *string = [self getRecordString:kABGroupNameProperty];
return string;
}
- (void) setName: (NSString *) aString
{
CFErrorRef error;
BOOL success = ABRecordSetValue(record, kABGroupNameProperty, (CFStringRef) aString, &error);
if (!success) NSLog(@"Error: %@", [(NSError *)error localizedDescription]);
}
@end