jens@18: // jens@18: // MYAddressField.m jens@18: // YourMove jens@18: // jens@18: // Created by Jens Alfke on 7/16/08. jens@18: // Copyright 2008 Jens Alfke. All rights reserved. jens@18: // jens@18: jens@18: #import "MYAddressField.h" jens@18: #import "RegexKitLite.h" jens@18: #import jens@18: jens@18: jens@18: @interface MYAddressField () jens@18: @property (retain) MYAddressItem *selectedAddress; jens@18: @end jens@18: jens@18: jens@18: @implementation MYAddressField jens@18: jens@18: jens@18: @synthesize defaultAddresses=_defaultAddresses, addressProperty=_property, selectedAddress=_selectedAddress; jens@18: jens@18: jens@18: - (void) _computeAddresses jens@18: { jens@18: NSMutableArray *newAddresses = $marray(); jens@18: if( _property ) { jens@18: if( _prefix.length ) { jens@18: // Find all the people in the address book matching _prefix: jens@18: ABAddressBook *ab = [ABAddressBook sharedAddressBook]; jens@18: ABSearchElement *search = [ABPerson searchElementForProperty: _property jens@18: label: nil jens@18: key: nil jens@18: value: nil jens@18: comparison: kABNotEqual]; jens@18: for( ABPerson *person in [ab recordsMatchingSearchElement: search] ) { jens@18: ABMultiValue *values = [person valueForProperty: _property]; jens@18: NSString *first = [person valueForProperty: kABFirstNameProperty]; jens@18: NSString *last = [person valueForProperty: kABLastNameProperty]; jens@18: BOOL nameMatches = _prefix==nil || ([first.lowercaseString hasPrefix: _prefix] jens@18: || [last.lowercaseString hasPrefix: _prefix]); jens@18: for( int i=0; i=0 ?[self.addresses objectAtIndex: sel] :nil; jens@18: //Log(@"Address selection = %@",self.selectedAddress); jens@18: } jens@18: jens@18: jens@18: #pragma mark - jens@18: #pragma mark DATA SOURCE: jens@18: jens@18: jens@18: - (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox jens@18: { jens@18: return self.addresses.count; jens@18: } jens@18: jens@18: - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index jens@18: { jens@18: return [[self.addresses objectAtIndex: index] description]; jens@18: } jens@18: jens@18: jens@18: @end jens@18: jens@18: jens@18: jens@18: jens@18: @implementation MYAddressItem jens@18: jens@18: - (id) initWithName: (NSString*)name jens@18: addressType: (NSString*)addressType address: (NSString*)address jens@18: { jens@18: self = [super init]; jens@18: if( self ) { jens@18: _name = name.length ?[name copy] :nil; jens@18: _addressType = [addressType copy]; jens@18: _address = [address copy]; jens@18: } jens@18: return self; jens@18: } jens@18: jens@18: - (id) initWithPerson: (ABPerson*)person jens@18: addressType: (NSString*)addressType address: (NSString*)address jens@18: { jens@18: NSString *first = [person valueForProperty: kABFirstNameProperty] ?: @""; jens@18: NSString *last = [person valueForProperty: kABLastNameProperty] ?: @""; jens@18: NSString *name = $sprintf(@"%@ %@", first,last); jens@18: jens@18: self = [self initWithName: name addressType: addressType address: address]; jens@18: if( self ) jens@18: _uuid = person.uniqueId.copy; jens@18: return self; jens@18: } jens@18: jens@18: - (id) initWithString: (NSString*)str addressType: (NSString*)addressType jens@18: { jens@18: #define kJustAddrRegex "[-a-zA-Z0-9%_+.]+(?:@[-a-zA-Z0-9.]+)" jens@18: static NSString* const kNameAndAddrRegex = @"^\\s*(\\S+)?\\s*<("kJustAddrRegex")>\\s*$"; jens@18: static NSString* const kAddrRegex = @"^\\s*("kJustAddrRegex")\\s*$"; jens@18: jens@18: NSString *name = nil; jens@18: NSString *address = [str stringByMatching: kNameAndAddrRegex capture: 2]; jens@18: if( address ) { jens@18: name = [str stringByMatching: kNameAndAddrRegex capture: 1]; jens@18: } else { jens@18: address = [str stringByMatching: kAddrRegex]; jens@18: } jens@18: if( ! address ) { jens@18: [self release]; jens@18: return nil; jens@18: } jens@18: return [self initWithName: name addressType: addressType address: address]; jens@18: } jens@18: jens@18: @synthesize name=_name, addressType=_addressType, address=_address; jens@18: jens@18: - (ABPerson*) person jens@18: { jens@18: if( _uuid ) jens@18: return (ABPerson*) [[ABAddressBook sharedAddressBook] recordForUniqueId: _uuid]; jens@18: else jens@18: return nil; jens@18: } jens@18: jens@18: - (NSString*) description jens@18: { jens@18: return $sprintf(@"%@%@<%@>", _name,(_name ?@" ":@""),_address); jens@18: } jens@18: jens@18: - (NSComparisonResult) compare: (MYAddressItem*)other jens@18: { jens@18: NSString *str1 = _name ?:_address; jens@18: NSString *str2 = other->_name ?: other->_address; jens@18: return [str1 localizedCaseInsensitiveCompare: str2]; jens@18: } jens@18: jens@18: @end