|
| 1 | +#import "ContactsView.h" |
| 2 | +#import <sqlite3.h> |
| 3 | +#import <AddressBook/AddressBook.h> |
| 4 | + |
| 5 | +@implementation ContactsView |
| 6 | + |
| 7 | +- (id)initWithFrame:(struct CGRect)rect { |
| 8 | + if ((self == [ super initWithFrame: rect ]) != nil) { |
| 9 | + |
| 10 | + colContactName = [ [ UITableColumn alloc ] |
| 11 | + initWithTitle: @"ContactsPreview" |
| 12 | + identifier:@"contactspreview" |
| 13 | + width: rect.size.width |
| 14 | + ]; |
| 15 | + [ self addTableColumn: colContactName ]; |
| 16 | + |
| 17 | + [ self setSeparatorStyle: 1 ]; |
| 18 | + [ self setDelegate: self ]; |
| 19 | + [ self setDataSource: self ]; |
| 20 | + [ self setRowHeight: 48 ]; |
| 21 | + |
| 22 | + ContactsList = [ [ NSMutableArray alloc] init ]; |
| 23 | + contactNames = [ [ NSMutableArray alloc] init ]; |
| 24 | + |
| 25 | + gContactsList = [ [ NSMutableArray alloc] init ]; |
| 26 | + gcontactNames = [ [ NSMutableArray alloc] init ]; |
| 27 | + |
| 28 | + contactsAreLoaded = NO; |
| 29 | + } |
| 30 | + |
| 31 | + return self; |
| 32 | +} |
| 33 | + |
| 34 | +- (void) reloadData { |
| 35 | + //[ ContactsList removeAllObjects ]; |
| 36 | + // [ contactNames removeAllObjects ]; |
| 37 | + |
| 38 | + if (contactsAreLoaded == YES) return; |
| 39 | + |
| 40 | + contactsAreLoaded = YES; |
| 41 | + |
| 42 | +// NSMutableArray *list = [[NSMutableArray alloc] init]; |
| 43 | + |
| 44 | + ABAddressBookRef addressBook = ABAddressBookCreate(); |
| 45 | + |
| 46 | + NSArray *addresses = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook); |
| 47 | + |
| 48 | + [SuperView gomerLOG:@"0"]; |
| 49 | + |
| 50 | + int addressesCount = [addresses count]; |
| 51 | + |
| 52 | + for (int i = 0; i < addressesCount; i++) { |
| 53 | + ABRecordRef record = [addresses objectAtIndex:i]; |
| 54 | + |
| 55 | + NSString *tableEntry = @""; |
| 56 | + NSString *fullInfo = @""; |
| 57 | + |
| 58 | + //Get name |
| 59 | + NSMutableString *contactName = (NSMutableString *)ABRecordCopyCompositeName(record); |
| 60 | + if (contactName != nil) |
| 61 | + { |
| 62 | + contactName = [NSString stringWithFormat: @"%@", contactName]; |
| 63 | + tableEntry = contactName; |
| 64 | + fullInfo = [fullInfo stringByAppendingString: [NSString stringWithFormat: @"%@ ", contactName]]; |
| 65 | + } |
| 66 | + |
| 67 | + //Get phone numbers |
| 68 | + ABMultiValueRef phoneNumbers =(NSString*)ABRecordCopyValue(record,kABPersonPhoneProperty); |
| 69 | + NSString* mobile=@""; |
| 70 | + for(int j=0;j<ABMultiValueGetCount(phoneNumbers);j++) |
| 71 | + { |
| 72 | + mobile=(NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers,j); |
| 73 | + fullInfo = [fullInfo stringByAppendingString: [NSString stringWithFormat: @"%@ ", mobile]]; |
| 74 | + |
| 75 | + if ([tableEntry isEqualToString:@""] == YES) |
| 76 | + tableEntry = mobile; |
| 77 | + } |
| 78 | + |
| 79 | + //Get eMail addresses |
| 80 | + ABMultiValueRef emailAddresses =(NSString*)ABRecordCopyValue(record,kABPersonEmailProperty); |
| 81 | + for(int j=0;j<ABMultiValueGetCount(emailAddresses);j++) |
| 82 | + { |
| 83 | + NSString* email=@""; |
| 84 | + email=(NSString*)ABMultiValueCopyValueAtIndex(emailAddresses,j); |
| 85 | + fullInfo = [fullInfo stringByAppendingString: [NSString stringWithFormat: @"%@ ", email]]; |
| 86 | + |
| 87 | + if ([tableEntry isEqualToString:@""] == YES) |
| 88 | + tableEntry = email; |
| 89 | + } |
| 90 | + |
| 91 | + //Get Street Addresses |
| 92 | + ABMutableMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonAddressProperty); |
| 93 | + for(CFIndex k=0;k<ABMultiValueGetCount(multiValue);k++) |
| 94 | + { |
| 95 | + CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multiValue, k); |
| 96 | + CFStringRef street = CFDictionaryGetValue(dict, kABPersonAddressStreetKey); |
| 97 | + CFStringRef city = CFDictionaryGetValue(dict, kABPersonAddressCityKey); |
| 98 | + CFStringRef country = CFDictionaryGetValue(dict, kABPersonAddressCountryKey); |
| 99 | + CFRelease(dict); |
| 100 | + |
| 101 | + if (street == nil) street = @""; |
| 102 | + if (city == nil) city = @""; |
| 103 | + if (country == nil) country = @""; |
| 104 | + |
| 105 | + NSString *address = [NSString stringWithFormat:@"%@ %@ %@",street,city,country]; |
| 106 | + |
| 107 | + fullInfo = [fullInfo stringByAppendingString: [NSString stringWithFormat: @"%@ ", address]]; |
| 108 | + |
| 109 | + if ([tableEntry isEqualToString:@""] == YES) |
| 110 | + tableEntry = address; |
| 111 | + } |
| 112 | + |
| 113 | + [gcontactNames addObject: tableEntry]; |
| 114 | + [gContactsList addObject: fullInfo]; |
| 115 | + } |
| 116 | + |
| 117 | + [SuperView gomerLOG:@"1"]; |
| 118 | + contactNames = [NSMutableArray arrayWithArray:(NSMutableArray*)[gcontactNames sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]]; |
| 119 | + [SuperView gomerLOG:@"2"]; |
| 120 | + ContactsList = [NSMutableArray arrayWithArray:(NSMutableArray*)[gContactsList sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]]; |
| 121 | + [SuperView gomerLOG:@"3"]; |
| 122 | + |
| 123 | + [ super reloadData ]; |
| 124 | +} |
| 125 | + |
| 126 | +- (int)numberOfRowsInTable:(UITable *)_table { |
| 127 | + return [ ContactsList count ]; |
| 128 | +} |
| 129 | + |
| 130 | +- (UITableCell *)table:(UITable *)table |
| 131 | + cellForRow:(int)row |
| 132 | + column:(UITableColumn *)col |
| 133 | +{ |
| 134 | + UIImageAndTextTableCell *cell = [ [ UIImageAndTextTableCell alloc ] init ]; |
| 135 | + //[ cell setTable: self ]; |
| 136 | + |
| 137 | + contactsIcon = [[ UIImage imageAtPath:@"/Applications/MobileAddressBook.app/icon.png" ] |
| 138 | + _imageScaledToSize:CGSizeMake(40.0f, 40.0f) interpolationQuality:1]; |
| 139 | + [cell setImage: contactsIcon ]; |
| 140 | + |
| 141 | + //[ cell setTitle: [ contactNames objectAtIndex: row ] ]; |
| 142 | + [ cell setTitle: [ contactNames objectAtIndex: 0 ] ]; |
| 143 | + |
| 144 | + return [ cell autorelease ]; |
| 145 | +} |
| 146 | + |
| 147 | +- (void)tableRowSelected:(NSNotification *)notification { |
| 148 | + |
| 149 | + NSString *selectedText = [ ContactsList objectAtIndex: [ self selectedRow ] ]; |
| 150 | + |
| 151 | + [SuperView setTextToEdit: selectedText]; |
| 152 | + [SuperView flipTo: enumTextPage]; |
| 153 | +} |
| 154 | + |
| 155 | +- (void)dealloc { |
| 156 | + [ colContactName release ]; |
| 157 | + [ ContactsList release ]; |
| 158 | + [ contactNames release ]; |
| 159 | + [ SuperView release ]; |
| 160 | + [ contactsIcon release ]; |
| 161 | + [ super dealloc ]; |
| 162 | +} |
| 163 | + |
| 164 | +- (void) setSuperView: (UIView *) sv |
| 165 | +{ |
| 166 | + SuperView = sv; |
| 167 | +} |
| 168 | +@end |
0 commit comments