Factor some code in Contact management

This commit is contained in:
Guillaume BIENKOWSKI 2014-11-25 17:51:20 +01:00
parent 8c03c66710
commit c7c3b22347

View file

@ -156,6 +156,15 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
return nil; return nil;
} }
- (ABPropertyID)propertyIDForSection:(ContactSections_e)section {
switch (section) {
case ContactSections_Sip: return kABPersonInstantMessageProperty;
case ContactSections_Number: return kABPersonPhoneProperty;
case ContactSections_Email: return kABPersonEmailProperty;
default: return kABInvalidPropertyType;
}
}
+ (NSString*)localizeLabel:(NSString*)str { + (NSString*)localizeLabel:(NSString*)str {
CFStringRef lLocalizedLabel = ABAddressBookCopyLocalizedLabel((CFStringRef) str); CFStringRef lLocalizedLabel = ABAddressBookCopyLocalizedLabel((CFStringRef) str);
NSString * retStr = [NSString stringWithString:(NSString*) lLocalizedLabel]; NSString * retStr = [NSString stringWithString:(NSString*) lLocalizedLabel];
@ -279,7 +288,6 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
ABMultiValueReplaceValueAtIndex(lMap, lDict, index); ABMultiValueReplaceValueAtIndex(lMap, lDict, index);
} else { } else {
CFStringRef label = (CFStringRef)[labelArray objectAtIndex:0]; CFStringRef label = (CFStringRef)[labelArray objectAtIndex:0];
ABMultiValueAddValueAndLabel(lMap, lDict, label, &index); ABMultiValueAddValueAndLabel(lMap, lDict, label, &index);
} }
@ -411,37 +419,21 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
- (void)removeEmptyEntry:(UITableView*)tableview section:(NSInteger)section animated:(BOOL)animated { - (void)removeEmptyEntry:(UITableView*)tableview section:(NSInteger)section animated:(BOOL)animated {
NSMutableArray *sectionDict = [self getSectionData:section]; NSMutableArray *sectionDict = [self getSectionData:section];
int row = [sectionDict count] - 1; NSInteger row = [sectionDict count] - 1;
if(row >= 0) { if(row >= 0) {
Entry *entry = [sectionDict objectAtIndex:row]; Entry *entry = [sectionDict objectAtIndex:row];
if(contactSections[section] == ContactSections_Number) {
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); ABPropertyID property = [self propertyIDForSection:contactSections[section]];
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); if( property != kABInvalidPropertyType ){
CFStringRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index); ABMultiValueRef lMap = ABRecordCopyValue(contact, property);
if(![(NSString*) valueRef length]) { NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
[self removeEntry:tableview path:[NSIndexPath indexPathForRow:row inSection:section] animated:animated]; CFTypeRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index);
}
CFRelease(valueRef);
CFRelease(lMap);
} else if(contactSections[section] == ContactSections_Sip) {
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
CFDictionaryRef lDict = ABMultiValueCopyValueAtIndex(lMap, index);
CFStringRef valueRef = CFDictionaryGetValue(lDict, kABPersonInstantMessageUsernameKey);
if(![(NSString*) valueRef length]) {
[self removeEntry:tableview path:[NSIndexPath indexPathForRow:row inSection:section] animated:animated];
}
CFRelease(lDict);
CFRelease(lMap);
} else if(contactSections[section] == ContactSections_Email) {
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonEmailProperty);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
CFStringRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index);
if(![(NSString*) valueRef length]) { if(![(NSString*) valueRef length]) {
[self removeEntry:tableview path:[NSIndexPath indexPathForRow:row inSection:section] animated:animated]; [self removeEntry:tableview path:[NSIndexPath indexPathForRow:row inSection:section] animated:animated];
} }
CFRelease(valueRef); CFRelease(valueRef);
CFRelease(lMap); CFRelease(lMap);
} }
} }
if(contactDetailsDelegate != nil) { if(contactDetailsDelegate != nil) {
@ -452,31 +444,18 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
- (void)removeEntry:(UITableView*)tableview path:(NSIndexPath*)indexPath animated:(BOOL)animated { - (void)removeEntry:(UITableView*)tableview path:(NSIndexPath*)indexPath animated:(BOOL)animated {
NSMutableArray *sectionArray = [self getSectionData:[indexPath section]]; NSMutableArray *sectionArray = [self getSectionData:[indexPath section]];
Entry *entry = [sectionArray objectAtIndex:[indexPath row]]; Entry *entry = [sectionArray objectAtIndex:[indexPath row]];
if(contactSections[[indexPath section]] == ContactSections_Number) { ABPropertyID property = [self propertyIDForSection:contactSections[indexPath.section]];
ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonPhoneProperty);
if( property != kABInvalidPropertyType ){
ABMultiValueRef lcMap = ABRecordCopyValue(contact, property);
ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap); ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap);
CFRelease(lcMap); CFRelease(lcMap);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
ABMultiValueRemoveValueAndLabelAtIndex(lMap, index); ABMultiValueRemoveValueAndLabelAtIndex(lMap, index);
ABRecordSetValue(contact, kABPersonPhoneProperty, lMap, nil); ABRecordSetValue(contact, property, lMap, nil);
CFRelease(lMap);
} else if(contactSections[[indexPath section]] == ContactSections_Sip) {
ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty);
ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap);
CFRelease(lcMap);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
ABMultiValueRemoveValueAndLabelAtIndex(lMap, index);
ABRecordSetValue(contact, kABPersonInstantMessageProperty, lMap, nil);
CFRelease(lMap);
} else if(contactSections[[indexPath section]] == ContactSections_Email) {
ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonEmailProperty);
ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap);
CFRelease(lcMap);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
ABMultiValueRemoveValueAndLabelAtIndex(lMap, index);
ABRecordSetValue(contact, kABPersonEmailProperty, lMap, nil);
CFRelease(lMap); CFRelease(lMap);
} }
[sectionArray removeObjectAtIndex:[indexPath row]]; [sectionArray removeObjectAtIndex:[indexPath row]];
NSArray *tagInsertIndexPath = [NSArray arrayWithObject:indexPath]; NSArray *tagInsertIndexPath = [NSArray arrayWithObject:indexPath];
@ -545,7 +524,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
if(contactSections[[indexPath section]] == ContactSections_Number) { if(contactSections[[indexPath section]] == ContactSections_Number) {
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonPhoneProperty);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index); CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index);
if(labelRef != NULL) { if(labelRef != NULL) {
label = [ContactDetailsTableViewController localizeLabel:(NSString*) labelRef]; label = [ContactDetailsTableViewController localizeLabel:(NSString*) labelRef];
@ -559,7 +538,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
CFRelease(lMap); CFRelease(lMap);
} else if(contactSections[[indexPath section]] == ContactSections_Sip) { } else if(contactSections[[indexPath section]] == ContactSections_Sip) {
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty); ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index); CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index);
if(labelRef != NULL) { if(labelRef != NULL) {
label = [ContactDetailsTableViewController localizeLabel:(NSString*) labelRef]; label = [ContactDetailsTableViewController localizeLabel:(NSString*) labelRef];
@ -584,7 +563,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
CFRelease(lMap); CFRelease(lMap);
} else if(contactSections[[indexPath section]] == ContactSections_Email) { } else if(contactSections[[indexPath section]] == ContactSections_Email) {
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonEmailProperty); ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonEmailProperty);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index); CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index);
if(labelRef != NULL) { if(labelRef != NULL) {
label = [ContactDetailsTableViewController localizeLabel:(NSString*) labelRef]; label = [ContactDetailsTableViewController localizeLabel:(NSString*) labelRef];
@ -621,7 +600,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
NSString *dest=NULL;; NSString *dest=NULL;;
if(contactSections[[indexPath section]] == ContactSections_Number) { if(contactSections[[indexPath section]] == ContactSections_Number) {
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonPhoneProperty);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
CFStringRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index); CFStringRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index);
if(valueRef != NULL) { if(valueRef != NULL) {
dest = [ContactDetailsTableViewController localizeLabel:(NSString*) valueRef]; dest = [ContactDetailsTableViewController localizeLabel:(NSString*) valueRef];
@ -630,7 +609,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
CFRelease(lMap); CFRelease(lMap);
} else if(contactSections[[indexPath section]] == ContactSections_Sip) { } else if(contactSections[[indexPath section]] == ContactSections_Sip) {
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty); ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
CFDictionaryRef lDict = ABMultiValueCopyValueAtIndex(lMap, index); CFDictionaryRef lDict = ABMultiValueCopyValueAtIndex(lMap, index);
CFStringRef valueRef = CFDictionaryGetValue(lDict, kABPersonInstantMessageUsernameKey); CFStringRef valueRef = CFDictionaryGetValue(lDict, kABPersonInstantMessageUsernameKey);
dest = [FastAddressBook normalizeSipURI:[NSString stringWithString:(NSString*) valueRef]]; dest = [FastAddressBook normalizeSipURI:[NSString stringWithString:(NSString*) valueRef]];
@ -638,7 +617,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
CFRelease(lMap); CFRelease(lMap);
} else if(contactSections[[indexPath section]] == ContactSections_Email) { } else if(contactSections[[indexPath section]] == ContactSections_Email) {
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonEmailProperty); ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonEmailProperty);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
CFStringRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index); CFStringRef valueRef = ABMultiValueCopyValueAtIndex(lMap, index);
if(valueRef != NULL) { if(valueRef != NULL) {
dest = [FastAddressBook normalizeSipURI:[NSString stringWithString:(NSString*) valueRef]]; dest = [FastAddressBook normalizeSipURI:[NSString stringWithString:(NSString*) valueRef]];
@ -666,28 +645,12 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
} }
} else { } else {
NSString *key = nil; NSString *key = nil;
if(contactSections[[indexPath section]] == ContactSections_Number) { ABPropertyID property = [self propertyIDForSection:contactSections[indexPath.section]];
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonPhoneProperty);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); if( property != kABInvalidPropertyType ){
CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index); ABMultiValueRef lMap = ABRecordCopyValue(contact, property);
if(labelRef != NULL) { NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
key = [NSString stringWithString:(NSString*) labelRef]; CFTypeRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index);
CFRelease(labelRef);
}
CFRelease(lMap);
} else if(contactSections[[indexPath section]] == ContactSections_Sip) {
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index);
if(labelRef != NULL) {
key = [NSString stringWithString:(NSString*) labelRef];
CFRelease(labelRef);
}
CFRelease(lMap);
} else if(contactSections[[indexPath section]] == ContactSections_Email) {
ABMultiValueRef lMap = ABRecordCopyValue(contact, kABPersonEmailProperty);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(lMap, index);
if(labelRef != NULL) { if(labelRef != NULL) {
key = [NSString stringWithString:(NSString*) labelRef]; key = [NSString stringWithString:(NSString*) labelRef];
CFRelease(labelRef); CFRelease(labelRef);
@ -772,7 +735,7 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
} }
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
int last_index = [[self getSectionData:[indexPath section]] count] - 1; NSInteger last_index = [[self getSectionData:[indexPath section]] count] - 1;
if (indexPath.row == last_index) { if (indexPath.row == last_index) {
return UITableViewCellEditingStyleInsert; return UITableViewCellEditingStyleInsert;
} }
@ -843,10 +806,12 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
- (void)changeContactDetailsLabel:(NSString *)value { - (void)changeContactDetailsLabel:(NSString *)value {
if(value != nil) { if(value != nil) {
NSMutableArray *sectionDict = [self getSectionData:[editingIndexPath section]]; NSInteger section = editingIndexPath.section;
Entry *entry = [sectionDict objectAtIndex:[editingIndexPath row]]; NSMutableArray *sectionDict = [self getSectionData:section];
ContactSections_e thesection = contactSections[[editingIndexPath section]]; ABPropertyID property = [self propertyIDForSection:(int)section];
if(thesection == ContactSections_Number) { Entry *entry = [sectionDict objectAtIndex:editingIndexPath.row];
if( property != kABInvalidPropertyType ){
ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonPhoneProperty);
ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap); ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap);
CFRelease(lcMap); CFRelease(lcMap);
@ -854,23 +819,8 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
ABMultiValueReplaceLabelAtIndex(lMap, (CFStringRef)(value), index); ABMultiValueReplaceLabelAtIndex(lMap, (CFStringRef)(value), index);
ABRecordSetValue(contact, kABPersonPhoneProperty, lMap, nil); ABRecordSetValue(contact, kABPersonPhoneProperty, lMap, nil);
CFRelease(lMap); CFRelease(lMap);
} else if(thesection == ContactSections_Sip) {
ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty);
ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap);
CFRelease(lcMap);
NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
ABMultiValueReplaceLabelAtIndex(lMap, (CFStringRef)(value), index);
ABRecordSetValue(contact, kABPersonInstantMessageProperty, lMap, nil);
CFRelease(lMap);
} else if(thesection == ContactSections_Email) {
ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonEmailProperty);
ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap);
CFRelease(lcMap);
NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
ABMultiValueReplaceLabelAtIndex(lMap, (CFStringRef)(value), index);
ABRecordSetValue(contact, kABPersonEmailProperty, lMap, nil);
CFRelease(lMap);
} }
[self.tableView beginUpdates]; [self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject: editingIndexPath] withRowAnimation:FALSE]; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject: editingIndexPath] withRowAnimation:FALSE];
[self.tableView reloadSectionIndexTitles]; [self.tableView reloadSectionIndexTitles];
@ -906,26 +856,21 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
Entry *entry = [sectionDict objectAtIndex:[path row]]; Entry *entry = [sectionDict objectAtIndex:[path row]];
ContactSections_e sect = contactSections[[path section]]; ContactSections_e sect = contactSections[[path section]];
ABPropertyID property = [self propertyIDForSection:sect];
NSString *value = [textField text]; NSString *value = [textField text];
if(sect == ContactSections_Number) {
ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonPhoneProperty); if(sect == ContactSections_Sip) {
ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap);
CFRelease(lcMap);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
ABMultiValueReplaceValueAtIndex(lMap, (CFStringRef)value, index);
ABRecordSetValue(contact, kABPersonPhoneProperty, lMap, nil);
CFRelease(lMap);
} else if(sect == ContactSections_Sip) {
[self setSipContactEntry:entry withValue:value]; [self setSipContactEntry:entry withValue:value];
} else if(sect == ContactSections_Email) { } else if( property != kABInvalidPropertyType ){
ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonEmailProperty); ABMultiValueRef lcMap = ABRecordCopyValue(contact, property);
ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap); ABMutableMultiValueRef lMap = ABMultiValueCreateMutableCopy(lcMap);
CFRelease(lcMap); CFRelease(lcMap);
int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); NSInteger index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]);
ABMultiValueReplaceValueAtIndex(lMap, (CFStringRef)value, index); ABMultiValueReplaceValueAtIndex(lMap, (CFStringRef)value, index);
ABRecordSetValue(contact, kABPersonEmailProperty, lMap, nil); ABRecordSetValue(contact, property, lMap, nil);
CFRelease(lMap); CFRelease(lMap);
} }
[cell.detailTextLabel setText:value]; [cell.detailTextLabel setText:value];
} else { } else {
[LinphoneLogger logc:LinphoneLoggerError format:"Not valid UIEditableTableViewCell"]; [LinphoneLogger logc:LinphoneLoggerError format:"Not valid UIEditableTableViewCell"];