forked from mirrors/linphone-iphone
refactor code
This commit is contained in:
parent
a1490a8c13
commit
af744afa24
2 changed files with 284 additions and 406 deletions
|
|
@ -100,434 +100,318 @@
|
|||
|
||||
#pragma mark - Getters
|
||||
- (UIImage *)avatar {
|
||||
if (_person) {
|
||||
@try {
|
||||
return [UIImage imageWithData:_person.imageData];
|
||||
} @catch (NSException *e) {
|
||||
LOGE(@"CNContact imageData CNPropertyNotFetchedException : %@", e);
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
@try {
|
||||
if (_person)
|
||||
return [UIImage imageWithData:_person.imageData];
|
||||
} @catch (NSException *e) {
|
||||
LOGE(@"CNContact imageData CNPropertyNotFetchedException : %@", e);
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSString *)displayName {
|
||||
if (_friend) {
|
||||
const char *friend_name = linphone_friend_get_name(_friend);
|
||||
if (friend_name) {
|
||||
if (friend_name)
|
||||
return [NSString stringWithUTF8String:friend_name];
|
||||
}
|
||||
}
|
||||
|
||||
if (_person != nil) {
|
||||
NSString *lFirstName = _person.givenName;
|
||||
NSString *lLocalizedFirstName =
|
||||
[FastAddressBook localizedLabel:lFirstName];
|
||||
if (_person) {
|
||||
NSString *lFirstName = _person.givenName;
|
||||
NSString *lLocalizedFirstName = [FastAddressBook localizedLabel:lFirstName];
|
||||
NSString *compositeName = _person.nickname;
|
||||
NSString *lLastName = _person.familyName;
|
||||
NSString *lLocalizedLastName = [FastAddressBook localizedLabel:lLastName];
|
||||
NSString *lOrganization = _person.organizationName;
|
||||
NSString *lLocalizedOrganization = [FastAddressBook localizedLabel:lOrganization];
|
||||
|
||||
NSString *compositeName = _person.nickname;
|
||||
if (compositeName)
|
||||
return compositeName;
|
||||
if (lLocalizedFirstName || lLocalizedLastName)
|
||||
return [NSString stringWithFormat:@"%@ %@", lLocalizedFirstName, lLocalizedLastName];
|
||||
return (NSString *)lLocalizedOrganization;
|
||||
}
|
||||
|
||||
NSString *lLastName = _person.familyName;
|
||||
NSString *lLocalizedLastName =
|
||||
[FastAddressBook localizedLabel:lLastName];
|
||||
BOOL firstName = _firstName && ![_firstName isEqualToString:@""];
|
||||
BOOL lastName = _lastName && ![_lastName isEqualToString:@""];
|
||||
if (lastName || firstName) {
|
||||
NSMutableString *str = NULL;
|
||||
if (firstName) {
|
||||
str = [_firstName copy];
|
||||
if (lastName)
|
||||
[str appendFormat:@" %@", _lastName];
|
||||
return str;
|
||||
}
|
||||
str = [_lastName copy];
|
||||
return str;
|
||||
}
|
||||
|
||||
NSString *lOrganization = _person.organizationName;
|
||||
NSString *lLocalizedOrganization =
|
||||
[FastAddressBook localizedLabel:lOrganization];
|
||||
|
||||
if (compositeName) {
|
||||
return compositeName;
|
||||
} else if (lLocalizedFirstName || lLocalizedLastName) {
|
||||
return [NSString stringWithFormat:@"%@ %@", lLocalizedFirstName,
|
||||
lLocalizedLastName];
|
||||
} else {
|
||||
return (NSString *)lLocalizedOrganization;
|
||||
}
|
||||
}
|
||||
|
||||
if (_lastName || _firstName) {
|
||||
NSMutableString *str;
|
||||
if (_firstName)
|
||||
[str appendString:_firstName];
|
||||
if (_firstName && _lastName)
|
||||
[str appendString:@" "];
|
||||
if (_lastName)
|
||||
[str appendString:_lastName];
|
||||
}
|
||||
|
||||
return NSLocalizedString(@"Unknown", nil);
|
||||
return NSLocalizedString(@"Unknown", nil);
|
||||
}
|
||||
|
||||
#pragma mark - Setters
|
||||
|
||||
- (void)setAvatar:(UIImage *)avatar {
|
||||
BOOL ret = FALSE;
|
||||
if (_person) {
|
||||
NSData *imageAvatar = UIImageJPEGRepresentation(avatar, 0.9f);
|
||||
[_person setValue:imageAvatar forKey:CNContactImageDataKey];
|
||||
ret = TRUE;
|
||||
} else {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping", __FUNCTION__);
|
||||
}
|
||||
if (!_person) {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping", __FUNCTION__);
|
||||
return;
|
||||
}
|
||||
|
||||
NSData *imageAvatar = UIImageJPEGRepresentation(avatar, 0.9f);
|
||||
[_person setValue:imageAvatar forKey:CNContactImageDataKey];
|
||||
}
|
||||
|
||||
- (void)setFirstName:(NSString *)firstName {
|
||||
BOOL ret = FALSE;
|
||||
if (![firstName isEqualToString:_firstName]) {
|
||||
if (_friend)
|
||||
ret = linphone_friend_set_name(
|
||||
_friend, [NSString stringWithFormat:@"%@ %@", firstName,
|
||||
_person.familyName]
|
||||
.UTF8String);
|
||||
if (_person) {
|
||||
[_person setValue:firstName forKey:CNContactGivenNameKey];
|
||||
[_person setValue:[NSString stringWithFormat:@"%@ %@", firstName,
|
||||
_person.familyName]
|
||||
forKey:CNContactNicknameKey];
|
||||
ret = TRUE;
|
||||
}
|
||||
if (ret) {
|
||||
_firstName = firstName;
|
||||
_displayName = [NSString
|
||||
stringWithFormat:@"%@ %@", firstName, _person.familyName];
|
||||
}
|
||||
}
|
||||
if ([firstName isEqualToString:_firstName])
|
||||
return;
|
||||
|
||||
if (_friend)
|
||||
ret = linphone_friend_set_name(_friend, [NSString stringWithFormat:@"%@ %@", firstName,_person.familyName].UTF8String);
|
||||
|
||||
if (_person) {
|
||||
[_person setValue:firstName forKey:CNContactGivenNameKey];
|
||||
[_person setValue:[NSString stringWithFormat:@"%@ %@", firstName, _person.familyName] forKey:CNContactNicknameKey];
|
||||
ret = TRUE;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
_firstName = firstName;
|
||||
_displayName = [NSString stringWithFormat:@"%@ %@", firstName, _person.familyName];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setLastName:(NSString *)lastName {
|
||||
BOOL ret = FALSE;
|
||||
if (_friend)
|
||||
ret = linphone_friend_set_name(
|
||||
_friend,
|
||||
[NSString stringWithFormat:@"%@ %@", _person.givenName, lastName]
|
||||
.UTF8String);
|
||||
if (_person) {
|
||||
[_person setValue:lastName forKey:CNContactFamilyNameKey];
|
||||
[_person
|
||||
setValue:[NSString stringWithFormat:@"%@ %@", _person.givenName,
|
||||
lastName]
|
||||
forKey:CNContactNicknameKey];
|
||||
ret = TRUE;
|
||||
}
|
||||
if (ret) {
|
||||
_lastName = lastName;
|
||||
_displayName =
|
||||
[NSString stringWithFormat:@"%@ %@", _person.givenName, lastName];
|
||||
}
|
||||
if ([lastName isEqualToString:_lastName])
|
||||
return;
|
||||
|
||||
if (_friend)
|
||||
ret = linphone_friend_set_name(_friend, [NSString stringWithFormat:@"%@ %@", _person.givenName, lastName].UTF8String);
|
||||
|
||||
if (_person) {
|
||||
[_person setValue:lastName forKey:CNContactFamilyNameKey];
|
||||
[_person setValue:[NSString stringWithFormat:@"%@ %@", _person.givenName, lastName] forKey:CNContactNicknameKey];
|
||||
ret = TRUE;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
_lastName = lastName;
|
||||
_displayName = [NSString stringWithFormat:@"%@ %@", _person.givenName, lastName];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)setSipAddress:(NSString *)sip atIndex:(NSInteger)index {
|
||||
BOOL ret = FALSE;
|
||||
NSString *normSip = NULL;
|
||||
if (_person && ![sip isEqualToString:@" "]) {
|
||||
if ((index + 1) > [_person.instantMessageAddresses count]) {
|
||||
if ([sip hasPrefix:@" "])
|
||||
normSip = [sip substringFromIndex:1];
|
||||
else
|
||||
normSip = sip;
|
||||
CNInstantMessageAddress *cNSipMsgAddr;
|
||||
if ([normSip containsString:@"@"])
|
||||
cNSipMsgAddr = [[CNInstantMessageAddress alloc]
|
||||
initWithUsername:normSip service:@"SIP"];//service:[normSip componentsSeparatedByString:@"@"][1]];
|
||||
else
|
||||
cNSipMsgAddr =
|
||||
[[CNInstantMessageAddress alloc] initWithUsername:normSip
|
||||
service:@"SIP"];
|
||||
CNLabeledValue *sipAddress =
|
||||
[CNLabeledValue labeledValueWithLabel:NULL value:cNSipMsgAddr];
|
||||
NSMutableArray<CNLabeledValue<CNInstantMessageAddress *> *>
|
||||
*tmpSipAddress = [_person.instantMessageAddresses mutableCopy];
|
||||
[tmpSipAddress addObject:sipAddress];
|
||||
[_person setValue:tmpSipAddress
|
||||
forKey:CNContactInstantMessageAddressesKey];
|
||||
ret = TRUE;
|
||||
_sipAddresses[index] = normSip;
|
||||
} else {
|
||||
normSip = sip;
|
||||
CNInstantMessageAddress *cNSipMsgAddr;
|
||||
if ([[FastAddressBook normalizeSipURI:normSip] containsString:@"@"])
|
||||
cNSipMsgAddr = [[CNInstantMessageAddress alloc]
|
||||
initWithUsername:sip service:@"SIP"];
|
||||
//service:[[FastAddressBook normalizeSipURI:normSip]
|
||||
// componentsSeparatedByString:@"@"][1]];
|
||||
else
|
||||
cNSipMsgAddr =
|
||||
[[CNInstantMessageAddress alloc] initWithUsername:normSip service:@"SIP"];
|
||||
//service:normSip];
|
||||
CNLabeledValue *sipAddress =
|
||||
[CNLabeledValue labeledValueWithLabel:NULL value:cNSipMsgAddr];
|
||||
NSMutableArray<CNLabeledValue<CNInstantMessageAddress *> *>
|
||||
*tmpSipAddress = [_person.instantMessageAddresses mutableCopy];
|
||||
[tmpSipAddress replaceObjectAtIndex:index withObject:sipAddress];
|
||||
[_person setValue:tmpSipAddress
|
||||
forKey:CNContactInstantMessageAddressesKey];
|
||||
ret = TRUE;
|
||||
}
|
||||
} else {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping",
|
||||
__FUNCTION__);
|
||||
}
|
||||
if (!_person || [sip isEqualToString:@" "]) {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping", __FUNCTION__);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
_sipAddresses[index] = sip;
|
||||
}
|
||||
return ret;
|
||||
NSString *normSip = [sip hasPrefix:@" "] ? [sip substringFromIndex:1] : sip;
|
||||
CNInstantMessageAddress *cNSipMsgAddr = [[CNInstantMessageAddress alloc] initWithUsername:normSip service:@"SIP"];
|
||||
CNLabeledValue *sipAddress = [CNLabeledValue labeledValueWithLabel:NULL value:cNSipMsgAddr];
|
||||
NSMutableArray<CNLabeledValue<CNInstantMessageAddress *> *> *tmpSipAddresses = [_person.instantMessageAddresses mutableCopy];
|
||||
if ((index + 1) > [_person.instantMessageAddresses count])
|
||||
[tmpSipAddresses addObject:sipAddress];
|
||||
else
|
||||
[tmpSipAddresses replaceObjectAtIndex:index withObject:sipAddress];
|
||||
|
||||
[_person setValue:tmpSipAddresses forKey:CNContactInstantMessageAddressesKey];
|
||||
_sipAddresses[index] = normSip;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
- (BOOL)setPhoneNumber:(NSString *)phone atIndex:(NSInteger)index {
|
||||
BOOL ret = FALSE;
|
||||
if (_person) {
|
||||
if ((index + 1) > [_person.phoneNumbers count]) {
|
||||
CNLabeledValue *mobileNumber = [CNLabeledValue
|
||||
labeledValueWithLabel:CNLabelPhoneNumberMobile
|
||||
value:[CNPhoneNumber
|
||||
phoneNumberWithStringValue:phone]];
|
||||
NSMutableArray<CNLabeledValue<CNPhoneNumber *> *> *tmpPhoneNumbers =
|
||||
[_person.phoneNumbers mutableCopy];
|
||||
[tmpPhoneNumbers addObject:mobileNumber];
|
||||
[_person setValue:tmpPhoneNumbers forKey:CNContactPhoneNumbersKey];
|
||||
ret = TRUE;
|
||||
} else {
|
||||
CNLabeledValue *mobileNumber = [CNLabeledValue
|
||||
labeledValueWithLabel:CNLabelPhoneNumberMobile
|
||||
value:[CNPhoneNumber
|
||||
phoneNumberWithStringValue:phone]];
|
||||
NSMutableArray<CNLabeledValue<CNPhoneNumber *> *> *tmpPhoneNumbers =
|
||||
[_person.phoneNumbers mutableCopy];
|
||||
[tmpPhoneNumbers replaceObjectAtIndex:index
|
||||
withObject:mobileNumber];
|
||||
[_person setValue:tmpPhoneNumbers forKey:CNContactPhoneNumbersKey];
|
||||
ret = TRUE;
|
||||
}
|
||||
} else {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping",
|
||||
__FUNCTION__);
|
||||
}
|
||||
if (ret)
|
||||
_phones[index] = phone;
|
||||
return ret;
|
||||
if (!_person) {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping", __FUNCTION__);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
CNLabeledValue *mobileNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMobile value:[CNPhoneNumber phoneNumberWithStringValue:phone]];
|
||||
NSMutableArray<CNLabeledValue<CNPhoneNumber *> *> *tmpPhoneNumbers = [_person.phoneNumbers mutableCopy];
|
||||
if ((index + 1) > [_person.phoneNumbers count])
|
||||
[tmpPhoneNumbers addObject:mobileNumber];
|
||||
else
|
||||
[tmpPhoneNumbers replaceObjectAtIndex:index withObject:mobileNumber];
|
||||
|
||||
[_person setValue:tmpPhoneNumbers forKey:CNContactPhoneNumbersKey];
|
||||
_phones[index] = phone;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
- (BOOL)setEmail:(NSString *)email atIndex:(NSInteger)index {
|
||||
BOOL ret = FALSE;
|
||||
if (_person) {
|
||||
if ((index + 1) > [_person.emailAddresses count]) {
|
||||
CNLabeledValue *emailAddress =
|
||||
[CNLabeledValue labeledValueWithLabel:NULL value:email];
|
||||
NSMutableArray<CNLabeledValue<NSString *> *> *tmpEmailAddress =
|
||||
[_person.emailAddresses mutableCopy];
|
||||
[tmpEmailAddress addObject:emailAddress];
|
||||
[_person setValue:tmpEmailAddress
|
||||
forKey:CNContactEmailAddressesKey];
|
||||
ret = TRUE;
|
||||
} else {
|
||||
CNLabeledValue *emailAddress =
|
||||
[CNLabeledValue labeledValueWithLabel:NULL value:email];
|
||||
NSMutableArray<CNLabeledValue<NSString *> *> *tmpEmailAddress =
|
||||
[_person.emailAddresses mutableCopy];
|
||||
[tmpEmailAddress replaceObjectAtIndex:index
|
||||
withObject:emailAddress];
|
||||
[_person setValue:tmpEmailAddress
|
||||
forKey:CNContactEmailAddressesKey];
|
||||
ret = TRUE;
|
||||
}
|
||||
} else {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping",
|
||||
__FUNCTION__);
|
||||
}
|
||||
if (ret)
|
||||
_emails[index] = email;
|
||||
return ret;
|
||||
if (!_person) {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping", __FUNCTION__);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
CNLabeledValue *emailAddress = [CNLabeledValue labeledValueWithLabel:NULL value:email];
|
||||
NSMutableArray<CNLabeledValue<NSString *> *> *tmpEmailAddress = [_person.emailAddresses mutableCopy];
|
||||
if ((index + 1) > [_person.emailAddresses count])
|
||||
[tmpEmailAddress addObject:emailAddress];
|
||||
else
|
||||
[tmpEmailAddress replaceObjectAtIndex:index withObject:emailAddress];
|
||||
|
||||
[_person setValue:tmpEmailAddress forKey:CNContactEmailAddressesKey];
|
||||
_emails[index] = email;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
- (BOOL)addSipAddress:(NSString *)sip {
|
||||
BOOL ret = FALSE;
|
||||
NSString *normSip = NULL;
|
||||
if (sip != NULL && ![sip isEqualToString:@""]) {
|
||||
if ([sip isEqualToString:@" "])
|
||||
ret = TRUE;
|
||||
else {
|
||||
if (_person) {
|
||||
normSip = sip;
|
||||
CNInstantMessageAddress *cNSipMsgAddr;
|
||||
if ([normSip containsString:@"@"])
|
||||
cNSipMsgAddr = [[CNInstantMessageAddress alloc]
|
||||
initWithUsername:[normSip
|
||||
componentsSeparatedByString:@"@"][0]
|
||||
service:@"SIP"];
|
||||
//service:[normSip componentsSeparatedByString:@"@"][1]];
|
||||
else
|
||||
cNSipMsgAddr =
|
||||
[[CNInstantMessageAddress alloc] initWithUsername:normSip
|
||||
service:@"SIP"];
|
||||
CNLabeledValue *sipAddress =
|
||||
[CNLabeledValue labeledValueWithLabel:NULL
|
||||
value:cNSipMsgAddr];
|
||||
NSMutableArray<CNLabeledValue<CNInstantMessageAddress *> *> *
|
||||
tmpSipAddress = [_person.instantMessageAddresses mutableCopy];
|
||||
[tmpSipAddress addObject:sipAddress];
|
||||
[_person setValue:tmpSipAddress
|
||||
forKey:CNContactInstantMessageAddressesKey];
|
||||
ret = TRUE;
|
||||
} else {
|
||||
LinphoneAddress *addr =
|
||||
linphone_core_interpret_url(LC, sip.UTF8String)
|
||||
?: linphone_address_new(sip.UTF8String);
|
||||
if (addr) {
|
||||
ret = TRUE;
|
||||
linphone_friend_add_address(_friend, addr);
|
||||
linphone_address_destroy(addr);
|
||||
// ensure that it was added by checking list size
|
||||
ret =
|
||||
(bctbx_list_size(linphone_friend_get_addresses(_friend)) ==
|
||||
_sipAddresses.count + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret) {
|
||||
if ([sip hasPrefix:@" "])
|
||||
[_sipAddresses addObject:[sip substringFromIndex:1]];
|
||||
else
|
||||
[_sipAddresses addObject:sip];
|
||||
}
|
||||
return ret;
|
||||
BOOL ret = TRUE;
|
||||
NSString *normSip = NULL;
|
||||
if (sip == NULL || [sip isEqualToString:@""])
|
||||
return FALSE;
|
||||
|
||||
if (![sip isEqualToString:@" "]) {
|
||||
if (_person) {
|
||||
normSip = sip;
|
||||
CNInstantMessageAddress *cNSipMsgAddr;
|
||||
if ([normSip containsString:@"@"])
|
||||
cNSipMsgAddr = [[CNInstantMessageAddress alloc] initWithUsername:[normSip componentsSeparatedByString:@"@"][0] service:@"SIP"]; //service:[normSip componentsSeparatedByString:@"@"][1]];
|
||||
else
|
||||
cNSipMsgAddr = [[CNInstantMessageAddress alloc] initWithUsername:normSip service:@"SIP"];
|
||||
|
||||
CNLabeledValue *sipAddress = [CNLabeledValue labeledValueWithLabel:NULL value:cNSipMsgAddr];
|
||||
NSMutableArray<CNLabeledValue<CNInstantMessageAddress *> *> *tmpSipAddresses = [_person.instantMessageAddresses mutableCopy];
|
||||
[tmpSipAddresses addObject:sipAddress];
|
||||
[_person setValue:tmpSipAddresses forKey:CNContactInstantMessageAddressesKey];
|
||||
ret = TRUE;
|
||||
} else {
|
||||
LinphoneAddress *addr = linphone_core_interpret_url(LC, sip.UTF8String) ?: linphone_address_new(sip.UTF8String);
|
||||
if (!addr)
|
||||
return FALSE;
|
||||
|
||||
linphone_friend_add_address(_friend, addr);
|
||||
linphone_address_destroy(addr);
|
||||
// ensure that it was added by checking list size
|
||||
ret = (bctbx_list_size(linphone_friend_get_addresses(_friend)) == _sipAddresses.count + 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
if ([sip hasPrefix:@" "])
|
||||
[_sipAddresses addObject:[sip substringFromIndex:1]];
|
||||
else
|
||||
[_sipAddresses addObject:sip];
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (BOOL)addPhoneNumber:(NSString *)phone {
|
||||
BOOL ret = FALSE;
|
||||
if (phone != NULL && ![phone isEqualToString:@""]) {
|
||||
if ([phone isEqualToString:@" "])
|
||||
ret = TRUE;
|
||||
else {
|
||||
if (_person) {
|
||||
CNLabeledValue *mobileNumber = [CNLabeledValue
|
||||
labeledValueWithLabel:CNLabelPhoneNumberMobile
|
||||
value:[CNPhoneNumber
|
||||
phoneNumberWithStringValue:phone]];
|
||||
NSMutableArray<CNLabeledValue<CNPhoneNumber *> *>
|
||||
*tmpPhoneNumbers = [_person.phoneNumbers mutableCopy];
|
||||
[tmpPhoneNumbers addObject:mobileNumber];
|
||||
[_person setValue:tmpPhoneNumbers
|
||||
forKey:CNContactPhoneNumbersKey];
|
||||
ret = TRUE;
|
||||
} else {
|
||||
char *cphone = ms_strdup(phone.UTF8String);
|
||||
if (cphone) {
|
||||
linphone_friend_add_phone_number(_friend, cphone);
|
||||
phone = [NSString stringWithUTF8String:cphone];
|
||||
ms_free(cphone);
|
||||
// ensure that it was added by checking list size
|
||||
ret = (bctbx_list_size(linphone_friend_get_phone_numbers(
|
||||
_friend)) == _phones.count + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret) {
|
||||
[_phones addObject:phone];
|
||||
}
|
||||
return ret;
|
||||
BOOL ret = TRUE;
|
||||
if (phone == NULL || [phone isEqualToString:@""])
|
||||
return FALSE;
|
||||
|
||||
if (![phone isEqualToString:@" "]){
|
||||
if (_person) {
|
||||
CNLabeledValue *mobileNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMobile
|
||||
value:[CNPhoneNumber phoneNumberWithStringValue:phone]];
|
||||
NSMutableArray<CNLabeledValue<CNPhoneNumber *> *>*tmpPhoneNumbers = [_person.phoneNumbers mutableCopy];
|
||||
[tmpPhoneNumbers addObject:mobileNumber];
|
||||
[_person setValue:tmpPhoneNumbers forKey:CNContactPhoneNumbersKey];
|
||||
ret = TRUE;
|
||||
} else {
|
||||
char *cphone = ms_strdup(phone.UTF8String);
|
||||
if (!cphone)
|
||||
return FALSE;
|
||||
|
||||
linphone_friend_add_phone_number(_friend, cphone);
|
||||
phone = [NSString stringWithUTF8String:cphone];
|
||||
ms_free(cphone);
|
||||
// ensure that it was added by checking list size
|
||||
ret = (bctbx_list_size(linphone_friend_get_phone_numbers( _friend)) == _phones.count + 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (ret)
|
||||
[_phones addObject:phone];
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (BOOL)addEmail:(NSString *)email {
|
||||
BOOL ret = FALSE;
|
||||
if (email != NULL && ![email isEqualToString:@""]) {
|
||||
if ([email isEqualToString:@" "])
|
||||
ret = TRUE;
|
||||
else {
|
||||
if (_person) {
|
||||
CNLabeledValue *emailAddress =
|
||||
[CNLabeledValue labeledValueWithLabel:NULL value:email];
|
||||
NSMutableArray<CNLabeledValue<NSString *> *> *tmpEmailAddress =
|
||||
[_person.emailAddresses mutableCopy];
|
||||
[tmpEmailAddress addObject:emailAddress];
|
||||
[_person setValue:tmpEmailAddress
|
||||
forKey:CNContactEmailAddressesKey];
|
||||
ret = TRUE;
|
||||
} else {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping",
|
||||
__FUNCTION__);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret) {
|
||||
[_emails addObject:email];
|
||||
}
|
||||
return ret;
|
||||
if (email == NULL || [email isEqualToString:@""])
|
||||
return FALSE;
|
||||
|
||||
if (![email isEqualToString:@" "]) {
|
||||
if (!_person) {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping", __FUNCTION__);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
CNLabeledValue *emailAddress = [CNLabeledValue labeledValueWithLabel:NULL value:email];
|
||||
NSMutableArray<CNLabeledValue<NSString *> *> *tmpEmailAddress = [_person.emailAddresses mutableCopy];
|
||||
[tmpEmailAddress addObject:emailAddress];
|
||||
[_person setValue:tmpEmailAddress forKey:CNContactEmailAddressesKey];
|
||||
}
|
||||
|
||||
[_emails addObject:email];
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
- (BOOL)removeSipAddressAtIndex:(NSInteger)index {
|
||||
BOOL ret = FALSE;
|
||||
if (_person ) {
|
||||
NSMutableArray<CNLabeledValue<CNInstantMessageAddress *> *>
|
||||
*tmpSipAddress = [_person.instantMessageAddresses mutableCopy];
|
||||
if([tmpSipAddress count] > index){
|
||||
[tmpSipAddress removeObjectAtIndex:index];
|
||||
[_person setValue:tmpSipAddress
|
||||
forKey:CNContactInstantMessageAddressesKey];
|
||||
if (_person) {
|
||||
NSMutableArray<CNLabeledValue<CNInstantMessageAddress *> *>*tmpSipAddress = [_person.instantMessageAddresses mutableCopy];
|
||||
if ([tmpSipAddress count] > index) {
|
||||
[tmpSipAddress removeObjectAtIndex:index];
|
||||
[_person setValue:tmpSipAddress forKey:CNContactInstantMessageAddressesKey];
|
||||
}
|
||||
ret = TRUE;
|
||||
} else {
|
||||
LinphoneAddress *addr = linphone_core_interpret_url(
|
||||
LC, ((NSString *)_sipAddresses[index]).UTF8String);
|
||||
if (addr) {
|
||||
linphone_friend_remove_address(_friend, addr);
|
||||
linphone_address_destroy(addr);
|
||||
// ensure that it was destroyed by checking list size
|
||||
ret =
|
||||
(bctbx_list_size(linphone_friend_get_addresses(_friend)) + 1 ==
|
||||
_sipAddresses.count);
|
||||
}
|
||||
}
|
||||
if (ret) {
|
||||
[_sipAddresses removeObjectAtIndex:index];
|
||||
}
|
||||
return ret;
|
||||
ret = TRUE;
|
||||
} else {
|
||||
LinphoneAddress *addr = linphone_core_interpret_url(LC, ((NSString *)_sipAddresses[index]).UTF8String);
|
||||
if (!addr)
|
||||
return FALSE;
|
||||
|
||||
linphone_friend_remove_address(_friend, addr);
|
||||
linphone_address_destroy(addr);
|
||||
// ensure that it was destroyed by checking list size
|
||||
ret = (bctbx_list_size(linphone_friend_get_addresses(_friend)) + 1 == _sipAddresses.count);
|
||||
}
|
||||
|
||||
if (ret)
|
||||
[_sipAddresses removeObjectAtIndex:index];
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (BOOL)removePhoneNumberAtIndex:(NSInteger)index {
|
||||
BOOL ret = FALSE;
|
||||
if (_person && _person.phoneNumbers.count > 0) {
|
||||
NSMutableArray<CNLabeledValue<CNPhoneNumber *> *> *tmpPhoneNumbers =
|
||||
[_person.phoneNumbers mutableCopy];
|
||||
if([tmpPhoneNumbers count] > index){
|
||||
[tmpPhoneNumbers removeObjectAtIndex:index];
|
||||
[_person setValue:tmpPhoneNumbers forKey:CNContactPhoneNumbersKey];
|
||||
}
|
||||
ret = TRUE;
|
||||
} else {
|
||||
const char *phone = ((NSString *)_phones[index]).UTF8String;
|
||||
linphone_friend_remove_phone_number(_friend, phone);
|
||||
// ensure that it was destroyed by checking list size
|
||||
// ret = (bctbx_list_size(linphone_friend_get_phone_numbers(_friend))
|
||||
// + 1 == _phoneNumbers.count);
|
||||
ret = TRUE;
|
||||
}
|
||||
if (ret) {
|
||||
[_phones removeObjectAtIndex:index];
|
||||
}
|
||||
return ret;
|
||||
if (_person && _person.phoneNumbers.count > 0) {
|
||||
NSMutableArray<CNLabeledValue<CNPhoneNumber *> *> *tmpPhoneNumbers = [_person.phoneNumbers mutableCopy];
|
||||
if ([tmpPhoneNumbers count] > index) {
|
||||
[tmpPhoneNumbers removeObjectAtIndex:index];
|
||||
[_person setValue:tmpPhoneNumbers forKey:CNContactPhoneNumbersKey];
|
||||
}
|
||||
ret = TRUE;
|
||||
} else {
|
||||
const char *phone = ((NSString *)_phones[index]).UTF8String;
|
||||
linphone_friend_remove_phone_number(_friend, phone);
|
||||
// ensure that it was destroyed by checking list size
|
||||
ret = (bctbx_list_size(linphone_friend_get_phone_numbers(_friend)) + 1 == _phones.count);
|
||||
}
|
||||
if (ret)
|
||||
[_phones removeObjectAtIndex:index];
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (BOOL)removeEmailAtIndex:(NSInteger)index {
|
||||
BOOL ret = FALSE;
|
||||
if (_person && _person.phoneNumbers.count > 0) {
|
||||
NSMutableArray<CNLabeledValue<NSString *> *> *tmpEmailAddresses =
|
||||
[_person.emailAddresses mutableCopy];
|
||||
if([tmpEmailAddresses count] > index){
|
||||
[tmpEmailAddresses removeObjectAtIndex:index];
|
||||
[_person setValue:tmpEmailAddresses
|
||||
forKey:CNContactEmailAddressesKey];
|
||||
}
|
||||
ret = TRUE;
|
||||
} else {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping",
|
||||
__FUNCTION__);
|
||||
}
|
||||
if (ret) {
|
||||
[_emails removeObjectAtIndex:index];
|
||||
}
|
||||
return ret;
|
||||
if (!_person || _person.phoneNumbers.count == 0) {
|
||||
LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping", __FUNCTION__);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
NSMutableArray<CNLabeledValue<NSString *> *> *tmpEmailAddresses = [_person.emailAddresses mutableCopy];
|
||||
if ([tmpEmailAddresses count] > index) {
|
||||
[tmpEmailAddresses removeObjectAtIndex:index];
|
||||
[_person setValue:tmpEmailAddresses forKey:CNContactEmailAddressesKey];
|
||||
}
|
||||
|
||||
[_emails removeObjectAtIndex:index];
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -535,39 +419,32 @@
|
|||
|
||||
- (void)loadFriend {
|
||||
// First and Last name
|
||||
{
|
||||
_firstName = [NSString stringWithUTF8String:linphone_friend_get_name(_friend) ?: ""];
|
||||
_lastName = nil;
|
||||
}
|
||||
_firstName = [NSString stringWithUTF8String:linphone_friend_get_name(_friend) ?: ""];
|
||||
_lastName = nil;
|
||||
|
||||
// Phone numbers
|
||||
{
|
||||
_phones = [[NSMutableArray alloc] init];
|
||||
MSList *numbers = linphone_friend_get_phone_numbers(_friend);
|
||||
while (numbers) {
|
||||
NSString *phone = [NSString stringWithUTF8String:numbers->data];
|
||||
[_phones addObject:phone];
|
||||
numbers = numbers->next;
|
||||
}
|
||||
}
|
||||
_phones = [[NSMutableArray alloc] init];
|
||||
MSList *numbers = linphone_friend_get_phone_numbers(_friend);
|
||||
while (numbers) {
|
||||
NSString *phone = [NSString stringWithUTF8String:numbers->data];
|
||||
[_phones addObject:phone];
|
||||
numbers = numbers->next;
|
||||
}
|
||||
|
||||
// SIP (IM)
|
||||
{
|
||||
_sipAddresses = [[NSMutableArray alloc] init];
|
||||
const MSList *sips = linphone_friend_get_addresses(_friend);
|
||||
while (sips) {
|
||||
LinphoneAddress *addr = sips->data;
|
||||
char *uri = linphone_address_as_string_uri_only(addr);
|
||||
NSString *sipaddr = [NSString stringWithUTF8String:uri];
|
||||
[_sipAddresses addObject:sipaddr];
|
||||
ms_free(uri);
|
||||
// SIP (IM)
|
||||
_sipAddresses = [[NSMutableArray alloc] init];
|
||||
const MSList *sips = linphone_friend_get_addresses(_friend);
|
||||
while (sips) {
|
||||
LinphoneAddress *addr = sips->data;
|
||||
char *uri = linphone_address_as_string_uri_only(addr);
|
||||
NSString *sipaddr = [NSString stringWithUTF8String:uri];
|
||||
[_sipAddresses addObject:sipaddr];
|
||||
ms_free(uri);
|
||||
sips = sips->next;
|
||||
}
|
||||
|
||||
sips = sips->next;
|
||||
}
|
||||
}
|
||||
|
||||
// Email - no support for LinphoneFriend
|
||||
{ _emails = [[NSMutableArray alloc] init]; }
|
||||
// Email - no support for LinphoneFriend
|
||||
_emails = [[NSMutableArray alloc] init];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -92,13 +92,14 @@
|
|||
+ (BOOL)isSipAddress:(CNLabeledValue<CNInstantMessageAddress *> *)sipAddr {
|
||||
NSString *username = sipAddr.value.username;
|
||||
NSString *service = sipAddr.value.service;
|
||||
if (!username)
|
||||
LOGI(@"Parsing contact with username : %@ and service : %@", username, service);
|
||||
if (!username || [username isEqualToString:@""])
|
||||
return FALSE;
|
||||
|
||||
if ([service isEqualToString:LinphoneManager.instance.contactSipField])
|
||||
return TRUE;
|
||||
if (!service || [service isEqualToString:@""])
|
||||
return [FastAddressBook isSipURI:username];
|
||||
|
||||
if (([service isEqualToString:@"INSTANT_MESSAGING_NAME"] || [service isEqualToString:@"IM_SERVICE_NAME"]) && [FastAddressBook isSipURI:username])
|
||||
if ([service isEqualToString:LinphoneManager.instance.contactSipField])
|
||||
return TRUE;
|
||||
|
||||
return FALSE;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue