| 1 |
#import "AddressBookController.h" |
| 2 |
#import <AddressBook/AddressBook.h> |
| 3 |
|
| 4 |
@implementation AddressBookController |
| 5 |
- (id)init{ |
| 6 |
self = [super initWithWindowNibName:@"AddressBook"]; |
| 7 |
return self; |
| 8 |
} |
| 9 |
-(void)dealloc{ |
| 10 |
[nameTable release]; |
| 11 |
[group release]; |
| 12 |
[currentGroup release]; |
| 13 |
[super dealloc]; |
| 14 |
} |
| 15 |
- (void)windowDidLoad |
| 16 |
{ |
| 17 |
[self updateGroup]; |
| 18 |
}/* windowDidLoad */ |
| 19 |
|
| 20 |
- (NSMutableArray *)listGroup |
| 21 |
{ |
| 22 |
NSArray *everygroup = [[[NSArray alloc] init] autorelease]; |
| 23 |
NSMutableArray *aGroup = [[[NSMutableArray alloc] init] autorelease]; |
| 24 |
|
| 25 |
[aGroup addObject:@"All"]; |
| 26 |
|
| 27 |
ABAddressBook *ab = [ABAddressBook sharedAddressBook]; |
| 28 |
|
| 29 |
everygroup = [ab groups]; |
| 30 |
|
| 31 |
id obj; |
| 32 |
NSEnumerator *enumerator = [everygroup objectEnumerator]; |
| 33 |
while ((obj = [enumerator nextObject]) != nil){ |
| 34 |
ABGroup *someGroup = obj; |
| 35 |
[aGroup addObject:[someGroup valueForProperty:kABGroupNameProperty]]; |
| 36 |
} |
| 37 |
|
| 38 |
return aGroup; |
| 39 |
}/* listGroup*/ |
| 40 |
|
| 41 |
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification |
| 42 |
{ |
| 43 |
ABAddressBook *ab = [ABAddressBook sharedAddressBook]; |
| 44 |
//NSLog(@"aNotification"); |
| 45 |
//NSLog(@"%d",[[aNotification object] selectedRow]); |
| 46 |
//NSLog(@"%@",[[[aNotification object] dataSource] group]); |
| 47 |
if ([[aNotification object] selectedRow] == -1){ |
| 48 |
[self setCurrentGroup:nil]; |
| 49 |
[self updateUI]; |
| 50 |
} else if (![[aNotification object] selectedRow]){ |
| 51 |
|
| 52 |
ABGroup *all =[[[ABGroup alloc] init] autorelease]; |
| 53 |
NSEnumerator *enumerator = [[ab people] objectEnumerator]; |
| 54 |
id obj; |
| 55 |
|
| 56 |
while ((obj = [enumerator nextObject]) != nil){ |
| 57 |
[all addMember:obj]; |
| 58 |
} |
| 59 |
|
| 60 |
[self setCurrentGroup:all]; |
| 61 |
[self updateUI]; |
| 62 |
} else { |
| 63 |
NSArray *everygroup = [[[NSArray alloc] init] autorelease]; |
| 64 |
everygroup = [ab groups]; |
| 65 |
ABGroup *someGroup = [everygroup objectAtIndex:[[aNotification object] selectedRow]-1]; |
| 66 |
[self setCurrentGroup:someGroup]; |
| 67 |
[self updateUI]; |
| 68 |
} |
| 69 |
}/* tableViewSelectionDidChange */ |
| 70 |
|
| 71 |
-(void) updateUI |
| 72 |
{ |
| 73 |
[nameTable reloadData]; |
| 74 |
}/* updateUI */ |
| 75 |
|
| 76 |
- (void) updateGroup |
| 77 |
{ |
| 78 |
group = [[NSMutableArray alloc] init]; |
| 79 |
[self setGroup:[self listGroup]]; |
| 80 |
}/* updateGroup */ |
| 81 |
|
| 82 |
// ---------------------------------------------------------------------------------------- |
| 83 |
// NSTableDataSource |
| 84 |
// ---------------------------------------------------------------------------------------- |
| 85 |
|
| 86 |
- (int)numberOfRowsInTableView:(NSTableView *)aTableView |
| 87 |
{ |
| 88 |
return [[currentGroup members] count]; |
| 89 |
}/* numberOfRowsInTableView */ |
| 90 |
|
| 91 |
- (id)tableView:(NSTableView *)aTableView |
| 92 |
objectValueForTableColumn:(NSTableColumn *)aTableColumn |
| 93 |
row:(int)rowIndex |
| 94 |
{ |
| 95 |
//NSDictionary *dictionary = [array objectAtIndex:rowIndex]; |
| 96 |
//return [dictionary objectForKey:[aTableColumn identifier]]; |
| 97 |
|
| 98 |
ABPerson *theRecord; |
| 99 |
id theValue; |
| 100 |
ABPropertyType type = [ABPerson typeOfProperty:[aTableColumn identifier]]; |
| 101 |
theRecord = [[currentGroup members] objectAtIndex:rowIndex]; |
| 102 |
|
| 103 |
//First,Last,Email,Phone |
| 104 |
|
| 105 |
if ( type & kABMultiValueMask ) { |
| 106 |
ABMultiValue *mValue = [theRecord valueForProperty:[aTableColumn identifier]]; |
| 107 |
int index = [mValue indexForIdentifier:[mValue primaryIdentifier]]; |
| 108 |
theValue = [mValue valueAtIndex:index]; |
| 109 |
} else { |
| 110 |
theValue = [theRecord valueForProperty:[aTableColumn identifier]]; |
| 111 |
} |
| 112 |
|
| 113 |
return theValue; |
| 114 |
}/* tableView */ |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
// ---------------------------------------------------------------------------------------- |
| 119 |
// �A�N�Z�b�T���\�b�h |
| 120 |
// ---------------------------------------------------------------------------------------- |
| 121 |
|
| 122 |
|
| 123 |
- (void) setGroup:(NSMutableArray *)aGroup |
| 124 |
{ |
| 125 |
[aGroup retain]; |
| 126 |
[group release]; |
| 127 |
group = aGroup; |
| 128 |
}/* setGroup */ |
| 129 |
|
| 130 |
- (NSMutableArray *)group |
| 131 |
{ |
| 132 |
return group; |
| 133 |
}/* group */ |
| 134 |
|
| 135 |
- (void) setCurrentGroup:(ABGroup *)cGroup |
| 136 |
{ |
| 137 |
[cGroup retain]; |
| 138 |
[currentGroup release]; |
| 139 |
currentGroup = cGroup; |
| 140 |
}/* setCurrentGroup */ |
| 141 |
- (ABGroup *)currentGroup |
| 142 |
{ |
| 143 |
return currentGroup; |
| 144 |
}/* currentGroup */ |
| 145 |
@end |