From 01ab2ed4c53d0e179b29740b424291cae28dad7b Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Thu, 10 Mar 2016 15:45:40 +0100 Subject: [PATCH] contact: convert phone numbers to sip uri when needed --- Classes/ContactDetailsTableView.m | 2 +- Classes/LinphoneUI/UIContactDetailsCell.h | 2 +- Classes/LinphoneUI/UIContactDetailsCell.m | 13 +++++- Classes/Utils/FastAddressBook.h | 2 - Classes/Utils/FastAddressBook.m | 48 ++++------------------- 5 files changed, 21 insertions(+), 46 deletions(-) diff --git a/Classes/ContactDetailsTableView.m b/Classes/ContactDetailsTableView.m index 4149d7821..7f57ce47a 100644 --- a/Classes/ContactDetailsTableView.m +++ b/Classes/ContactDetailsTableView.m @@ -180,7 +180,7 @@ [cell.editTextfield setKeyboardType:UIKeyboardTypeEmailAddress]; } - [cell setAddress:value]; + [cell setAddress:value isPhone:(indexPath.section == ContactSections_Number)]; return cell; } diff --git a/Classes/LinphoneUI/UIContactDetailsCell.h b/Classes/LinphoneUI/UIContactDetailsCell.h index e97b42529..1db9398b5 100644 --- a/Classes/LinphoneUI/UIContactDetailsCell.h +++ b/Classes/LinphoneUI/UIContactDetailsCell.h @@ -36,7 +36,7 @@ @property(weak, nonatomic) IBOutlet UIIconButton *chatButton; - (id)initWithIdentifier:(NSString *)identifier; -- (void)setAddress:(NSString *)address; +- (void)setAddress:(NSString *)address isPhone:(BOOL)phone; - (void)hideDeleteButton:(BOOL)hidden; - (IBAction)onCallClick:(id)sender; diff --git a/Classes/LinphoneUI/UIContactDetailsCell.m b/Classes/LinphoneUI/UIContactDetailsCell.m index 692a6d058..03d1a3631 100644 --- a/Classes/LinphoneUI/UIContactDetailsCell.m +++ b/Classes/LinphoneUI/UIContactDetailsCell.m @@ -40,9 +40,18 @@ #pragma mark - UITableViewCell Functions -- (void)setAddress:(NSString *)address { - _addressLabel.text = _editTextfield.text = address; +- (void)setAddress:(NSString *)address isPhone:(BOOL)phone { + NSString *name = address; + if (phone) { + char *normalizedPhone = linphone_proxy_config_normalize_phone_number(linphone_core_get_default_proxy_config(LC), + address.UTF8String); + if (normalizedPhone) { + name = [NSString stringWithUTF8String:normalizedPhone]; + ms_free(normalizedPhone); + } + } + _addressLabel.text = _editTextfield.text = name; LinphoneAddress *addr = linphone_core_interpret_url(LC, _addressLabel.text.UTF8String); _chatButton.enabled = _callButton.enabled = (addr != NULL); diff --git a/Classes/Utils/FastAddressBook.h b/Classes/Utils/FastAddressBook.h index c818840d5..e7e229abe 100644 --- a/Classes/Utils/FastAddressBook.h +++ b/Classes/Utils/FastAddressBook.h @@ -47,8 +47,6 @@ + (NSString *)displayNameForAddress:(const LinphoneAddress *)addr; + (BOOL)isSipURI:(NSString *)address; // should be removed -+ (NSString *)appendCountryCodeIfPossible:(NSString *)number; // should be removed -+ (NSString *)normalizePhoneNumber:(NSString *)number; // should be removed + (NSString *)normalizeSipURI:(NSString *)address; // should be removed + (NSString *)localizedLabel:(NSString *)label; diff --git a/Classes/Utils/FastAddressBook.m b/Classes/Utils/FastAddressBook.m index b86eb6e9e..8d95be363 100644 --- a/Classes/Utils/FastAddressBook.m +++ b/Classes/Utils/FastAddressBook.m @@ -74,17 +74,6 @@ static void sync_address_book(ABAddressBookRef addressBook, CFDictionaryRef info return [address hasPrefix:@"sip:"] || [address hasPrefix:@"sips:"]; } -+ (NSString *)appendCountryCodeIfPossible:(NSString *)number { - if (![number hasPrefix:@"+"] && ![number hasPrefix:@"00"]) { - NSString *lCountryCode = [LinphoneManager.instance lpConfigStringForKey:@"countrycode_preference"]; - if (lCountryCode && [lCountryCode length] > 0) { - // append country code - return [lCountryCode stringByAppendingString:number]; - } - } - return number; -} - + (NSString *)normalizeSipURI:(NSString *)address { // replace all whitespaces (non-breakable, utf8 nbsp etc.) by the "classical" whitespace address = [[address componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] @@ -107,27 +96,6 @@ static void sync_address_book(ABAddressBookRef addressBook, CFDictionaryRef info return normalizedSipAddress; } -+ (NSString *)normalizePhoneNumber:(NSString *)address { - NSMutableString *lNormalizedAddress = [NSMutableString stringWithString:address]; - [lNormalizedAddress replaceOccurrencesOfString:@" " - withString:@"" - options:0 - range:NSMakeRange(0, [lNormalizedAddress length])]; - [lNormalizedAddress replaceOccurrencesOfString:@"(" - withString:@"" - options:0 - range:NSMakeRange(0, [lNormalizedAddress length])]; - [lNormalizedAddress replaceOccurrencesOfString:@")" - withString:@"" - options:0 - range:NSMakeRange(0, [lNormalizedAddress length])]; - [lNormalizedAddress replaceOccurrencesOfString:@"-" - withString:@"" - options:0 - range:NSMakeRange(0, [lNormalizedAddress length])]; - return [FastAddressBook appendCountryCodeIfPossible:lNormalizedAddress]; -} - + (BOOL)isAuthorized { return ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized; } @@ -189,14 +157,14 @@ static void sync_address_book(ABAddressBookRef addressBook, CFDictionaryRef info if (lMap) { for (int i = 0; i < ABMultiValueGetCount(lMap); i++) { CFStringRef lValue = ABMultiValueCopyValueAtIndex(lMap, i); - - NSString *lNormalizedKey = [FastAddressBook normalizePhoneNumber:(__bridge NSString *)(lValue)]; - NSString *lNormalizedSipKey = [FastAddressBook normalizeSipURI:lNormalizedKey]; - if (lNormalizedSipKey != NULL) - lNormalizedKey = lNormalizedSipKey; - - [_addressBookMap setObject:(__bridge id)(lPerson) forKey:lNormalizedKey]; - + char *normalizedPhone = linphone_proxy_config_normalize_phone_number( + linphone_core_get_default_proxy_config(LC), ((__bridge NSString *)(lValue)).UTF8String); + NSString *name = [FastAddressBook + normalizeSipURI:normalizedPhone ? [NSString stringWithUTF8String:normalizedPhone] + : (__bridge NSString *)(lValue)]; + [_addressBookMap setObject:(__bridge id)(lPerson) forKey:name]; + if (normalizedPhone) + ms_free(normalizedPhone); CFRelease(lValue); } CFRelease(lMap);