factorize contaccts in fast address book (and probably broke everything)

This commit is contained in:
Gautier Pelloux-Prayer 2015-07-28 14:52:02 +02:00
parent d1dbb5c5fb
commit cd38fb01a4
13 changed files with 78 additions and 226 deletions

View file

@ -188,8 +188,6 @@ static UICompositeViewDescription *compositeDescription = nil;
return;
}
NSString *displayName = nil;
UIImage *image = nil;
const LinphoneAddress *linphoneAddress = linphone_chat_room_get_peer_address(chatRoom);
if (linphoneAddress == NULL) {
[[PhoneMainView instance] popCurrentView];
@ -204,33 +202,10 @@ static UICompositeViewDescription *compositeDescription = nil;
[error show];
return;
}
char *tmp = linphone_address_as_string_uri_only(linphoneAddress);
NSString *normalizedSipAddress = [NSString stringWithUTF8String:tmp];
ms_free(tmp);
ABRecordRef acontact = [[[LinphoneManager instance] fastAddressBook] getContact:normalizedSipAddress];
if (acontact != nil) {
displayName = [FastAddressBook getContactDisplayName:acontact];
image = [FastAddressBook getContactImage:acontact thumbnail:true];
}
// Display name
if (displayName == nil) {
const char *username = linphone_address_get_username(linphoneAddress);
char *address = linphone_address_as_string(linphoneAddress);
displayName = [NSString stringWithUTF8String:username ?: address];
ms_free(address);
}
if (displayName == nil)
LOGF(@"No display name");
addressLabel.text = displayName;
addressLabel.accessibilityValue = displayName;
// Avatar
if (image == nil) {
image = [UIImage imageNamed:@"avatar_unknown_small.png"];
}
[avatarImage setImage:image];
[FastAddressBook setDisplayNameLabel:addressLabel forAddress:linphoneAddress];
addressLabel.accessibilityValue = addressLabel.text;
avatarImage.image =
[FastAddressBook getContactImage:[FastAddressBook getContactWithLinphoneAddress:linphoneAddress] thumbnail:YES];
}
static void message_status(LinphoneChatMessage *msg, LinphoneChatMessageState state, void *ud) {
@ -296,7 +271,6 @@ static void message_status(LinphoneChatMessage *msg, LinphoneChatMessageState st
- (void)chooseImageQuality:(UIImage *)image url:(NSURL *)url {
DTActionSheet *sheet = [[DTActionSheet alloc] initWithTitle:NSLocalizedString(@"Choose the image size", nil)];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// UIImage *image = [original_image normalizedImage];
for (NSString *key in [imageQualities allKeys]) {
NSNumber *number = [imageQualities objectForKey:key];
NSData *data = UIImageJPEGRepresentation(image, [number floatValue]);

View file

@ -821,7 +821,6 @@ static const ContactSections_e contactSections[ContactSections_MAX] = {ContactSe
}
return TRUE;
}
- (BOOL)isValid {
return true;
}

View file

@ -223,7 +223,7 @@ static void sync_address_book(ABAddressBookRef addressBook, CFDictionaryRef info
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [addressBookMap keyAtIndex:section];
return nil; // [addressBookMap keyAtIndex:section];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

View file

@ -362,11 +362,7 @@ static UICompositeViewDescription *compositeDescription = nil;
}
- (void)call:(NSString *)address {
NSString *displayName = nil;
ABRecordRef contact = [[[LinphoneManager instance] fastAddressBook] getContact:address];
if (contact) {
displayName = [FastAddressBook getContactDisplayName:contact];
}
NSString *displayName = [FastAddressBook getContactDisplayName:[FastAddressBook getContact:address]];
[self call:address displayName:displayName];
}

View file

@ -166,13 +166,13 @@ static UICompositeViewDescription *compositeDescription = nil;
assert(addr != NULL);
[FastAddressBook setDisplayNameLabel:_contactLabel forAddress:addr];
_avatarImage.image = [FastAddressBook avatarForAddress:addr];
_avatarImage.image =
[FastAddressBook getContactImage:[FastAddressBook getContactWithLinphoneAddress:addr] thumbnail:NO];
char *addrURI = linphone_address_as_string_uri_only(addr);
_addressLabel.text = [NSString stringWithUTF8String:addrURI];
ms_free(addrURI);
BOOL use_system = [[LinphoneManager instance] lpConfigBoolForKey:@"use_system_contacts"];
_addContactButton.hidden = contact || use_system;
_addContactButton.hidden = contact || [[LinphoneManager instance] lpConfigBoolForKey:@"use_system_contacts"];
}
#pragma mark - Action Functions

View file

@ -101,48 +101,10 @@ static UICompositeViewDescription *compositeDescription = nil;
- (void)update {
[self view]; // Force view load
[avatarImage setImage:[UIImage imageNamed:@"avatar_unknown.png"]];
NSString *address = nil;
const LinphoneAddress *addr = linphone_call_get_remote_address(call);
if (addr != NULL) {
BOOL useLinphoneAddress = true;
// contact name
char *lAddress = linphone_address_as_string_uri_only(addr);
if (lAddress) {
NSString *normalizedSipAddress = [FastAddressBook normalizeSipURI:[NSString stringWithUTF8String:lAddress]];
ABRecordRef contact = [[[LinphoneManager instance] fastAddressBook] getContact:normalizedSipAddress];
if (contact) {
UIImage *tmpImage = [FastAddressBook getContactImage:contact thumbnail:false];
if (tmpImage != nil) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL),
^(void) {
UIImage *tmpImage2 = [UIImage decodedImageWithImage:tmpImage];
dispatch_async(dispatch_get_main_queue(), ^{
avatarImage.image = tmpImage2;
});
});
}
address = [FastAddressBook getContactDisplayName:contact];
useLinphoneAddress = false;
}
ms_free(lAddress);
}
if (useLinphoneAddress) {
const char *lDisplayName = linphone_address_get_display_name(addr);
const char *lUserName = linphone_address_get_username(addr);
if (lDisplayName)
address = [NSString stringWithUTF8String:lDisplayName];
else if (lUserName)
address = [NSString stringWithUTF8String:lUserName];
}
}
// Set Address
if (address == nil) {
address = @"Unknown";
}
[addressLabel setText:address];
[FastAddressBook setDisplayNameLabel:addressLabel forAddress:addr];
avatarImage.image =
[FastAddressBook getContactImage:[FastAddressBook getContactWithLinphoneAddress:addr] thumbnail:NO];
}
#pragma mark - Property Functions

View file

@ -628,32 +628,7 @@ static void linphone_iphone_display_status(struct _LinphoneCore *lc, const char
}
const LinphoneAddress *addr = linphone_call_get_remote_address(call);
NSString *address = nil;
if (addr != NULL) {
BOOL useLinphoneAddress = true;
// contact name
char *lAddress = linphone_address_as_string_uri_only(addr);
if (lAddress) {
NSString *normalizedSipAddress = [FastAddressBook normalizeSipURI:[NSString stringWithUTF8String:lAddress]];
ABRecordRef contact = [fastAddressBook getContact:normalizedSipAddress];
if (contact) {
address = [FastAddressBook getContactDisplayName:contact];
useLinphoneAddress = false;
}
ms_free(lAddress);
}
if (useLinphoneAddress) {
const char *lDisplayName = linphone_address_get_display_name(addr);
const char *lUserName = linphone_address_get_username(addr);
if (lDisplayName)
address = [NSString stringWithUTF8String:lDisplayName];
else if (lUserName)
address = [NSString stringWithUTF8String:lUserName];
}
}
if (address == nil) {
address = NSLocalizedString(@"Unknown", nil);
}
NSString *address = [FastAddressBook displayNameForAddress:addr];
if (state == LinphoneCallIncomingReceived) {
@ -886,41 +861,21 @@ static void linphone_iphone_registration_state(LinphoneCore *lc, LinphoneProxyCo
#pragma mark - Text Received Functions
- (void)onMessageReceived:(LinphoneCore *)lc room:(LinphoneChatRoom *)room message:(LinphoneChatMessage *)msg {
if (silentPushCompletion) {
// we were woken up by a silent push. Call the completion handler with NEWDATA
// so that the push is notified to the user
LOGI(@"onMessageReceived - handler %p", silentPushCompletion);
silentPushCompletion(UIBackgroundFetchResultNewData);
silentPushCompletion = nil;
}
NSString *callID = [NSString stringWithUTF8String:linphone_chat_message_get_custom_header(msg, "Call-ID")];
const LinphoneAddress *remoteAddress = linphone_chat_message_get_from_address(msg);
NSString *from = [FastAddressBook displayNameForAddress:remoteAddress];
char *c_address = linphone_address_as_string_uri_only(remoteAddress);
NSString *address = [NSString stringWithUTF8String:c_address];
NSString *remote_uri = [NSString stringWithUTF8String:c_address];
const char *call_id = linphone_chat_message_get_custom_header(msg, "Call-ID");
NSString *callID = [NSString stringWithUTF8String:call_id];
ms_free(c_address);
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
ABRecordRef contact = [fastAddressBook getContact:address];
if (contact) {
address = [FastAddressBook getContactDisplayName:contact];
} else {
if ([[LinphoneManager instance] lpConfigBoolForKey:@"show_contacts_emails_preference"] == true) {
LinphoneAddress *linphoneAddress =
linphone_address_new([address cStringUsingEncoding:[NSString defaultCStringEncoding]]);
address = [NSString stringWithUTF8String:linphone_address_get_username(linphoneAddress)];
linphone_address_destroy(linphoneAddress);
}
}
if (address == nil) {
address = NSLocalizedString(@"Unknown", nil);
}
// Create a new notification
UILocalNotification *notif = [[UILocalNotification alloc] init];
if (notif) {
@ -928,10 +883,10 @@ static void linphone_iphone_registration_state(LinphoneCore *lc, LinphoneProxyCo
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8) {
notif.category = @"incoming_msg";
}
notif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"IM_MSG", nil), address];
notif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"IM_MSG", nil), from];
notif.alertAction = NSLocalizedString(@"Show", nil);
notif.soundName = @"msg.caf";
notif.userInfo = @{ @"from" : address, @"from_addr" : remote_uri, @"call-id" : callID };
notif.userInfo = @{ @"from" : from, @"from_addr" : remote_uri, @"call-id" : callID };
[[UIApplication sharedApplication] presentLocalNotificationNow:notif];
}

View file

@ -60,7 +60,6 @@
- (void)touchUp:(id)sender {
NSString *address = [addressField text];
NSString *displayName = nil;
if ([address length] == 0) {
const MSList *logs = linphone_core_get_call_logs([LinphoneManager getLc]);
@ -96,10 +95,7 @@
}
if ([address length] > 0) {
ABRecordRef contact = [[[LinphoneManager instance] fastAddressBook] getContact:address];
if (contact) {
displayName = [FastAddressBook getContactDisplayName:contact];
}
NSString *displayName = [FastAddressBook getContactDisplayName:[FastAddressBook getContact:address]];
[[LinphoneManager instance] call:address displayName:displayName transfer:FALSE];
}
}

View file

@ -48,38 +48,17 @@
return;
}
const LinphoneAddress *addr = linphone_call_get_remote_address(call);
address = [FastAddressBook displayNameForAddress:addr];
if (addr != NULL) {
BOOL useLinphoneAddress = true;
// contact name
char *lAddress = linphone_address_as_string_uri_only(addr);
if (lAddress) {
NSString *normalizedSipAddress = [FastAddressBook normalizeSipURI:[NSString stringWithUTF8String:lAddress]];
ABRecordRef contact = [[[LinphoneManager instance] fastAddressBook] getContact:normalizedSipAddress];
if (contact) {
useLinphoneAddress = false;
self.address = [FastAddressBook getContactDisplayName:contact];
UIImage *tmpImage = [FastAddressBook getContactImage:contact thumbnail:false];
if (tmpImage != nil) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL),
^(void) {
UIImage *tmpImage2 = [UIImage decodedImageWithImage:tmpImage];
dispatch_async(dispatch_get_main_queue(), ^{
[self setImage:tmpImage2];
});
});
}
}
ms_free(lAddress);
}
if (useLinphoneAddress) {
const char *lDisplayName = linphone_address_get_display_name(addr);
const char *lUserName = linphone_address_get_username(addr);
if (lDisplayName)
self.address = [NSString stringWithUTF8String:lDisplayName];
else if (lUserName)
self.address = [NSString stringWithUTF8String:lUserName];
}
ABRecordRef contact = [FastAddressBook getContactWithLinphoneAddress:addr];
UIImage *tmpImage = [FastAddressBook getContactImage:contact thumbnail:false];
if (tmpImage != nil) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) {
UIImage *tmpImage2 = [UIImage decodedImageWithImage:tmpImage];
dispatch_async(dispatch_get_main_queue(), ^{
[self setImage:tmpImage2];
});
});
}
}

View file

@ -68,9 +68,10 @@
LOGW(@"Cannot update chat cell: null chat");
return;
}
const LinphoneAddress *linphoneAddress = linphone_chat_room_get_peer_address(chatRoom);
[FastAddressBook setDisplayNameLabel:addressLabel forAddress:linphoneAddress];
avatarImage.image = [FastAddressBook avatarForAddress:linphoneAddress];
const LinphoneAddress *addr = linphone_chat_room_get_peer_address(chatRoom);
[FastAddressBook setDisplayNameLabel:addressLabel forAddress:addr];
avatarImage.image =
[FastAddressBook getContactImage:[FastAddressBook getContactWithLinphoneAddress:addr] thumbnail:NO];
LinphoneChatMessage *last_message = linphone_chat_room_get_user_data(chatRoom);
if (last_message) {

View file

@ -116,10 +116,11 @@
image = [UIImage imageNamed:@"call_status_outgoing.png"];
addr = linphone_call_log_get_to_address(callLog);
}
[FastAddressBook setDisplayNameLabel:displayNameLabel forAddress:addr];
[_avatarImage setImage:[FastAddressBook avatarForAddress:addr]];
[historyStateButton setImage:image forState:UIControlStateNormal];
ABRecordRef contact = [FastAddressBook getContactWithLinphoneAddress:addr];
[FastAddressBook setDisplayNameLabel:displayNameLabel forContact:contact];
_avatarImage.image = [FastAddressBook getContactImage:contact thumbnail:TRUE];
}
- (void)setEditing:(BOOL)editing {

View file

@ -23,30 +23,33 @@
#include "linphone/linphonecore.h"
@interface FastAddressBook : NSObject {
NSMutableDictionary* addressBookMap;
ABAddressBookRef addressBook;
NSMutableDictionary *addressBookMap;
ABAddressBookRef addressBook;
}
+ (BOOL)isSipURI:(NSString*)address;
+ (NSString*)getContactDisplayName:(ABRecordRef)contact;
+ (UIImage*)getContactImage:(ABRecordRef)contact thumbnail:(BOOL)thumbnail;
- (ABRecordRef)getContact:(NSString*)address;
- (void)reload;
- (void)saveAddressBook;
+ (BOOL)isAuthorized;
+ (NSString*)appendCountryCodeIfPossible:(NSString*)number;
+ (NSString*)normalizePhoneNumber:(NSString*)number;
+ (NSString*)normalizeSipURI:(NSString*)address;
// TOOLS
+(NSString*)localizedLabel:(NSString*)label;
+ (UIImage *)avatarForAddress:(const LinphoneAddress *)addr;
+ (ABRecordRef)getContact:(NSString *)address;
+ (ABRecordRef)getContactWithLinphoneAddress:(const LinphoneAddress *)address;
+ (NSString *)getContactDisplayName:(ABRecordRef)contact;
+ (UIImage *)getContactImage:(ABRecordRef)contact thumbnail:(BOOL)thumbnail;
+ (BOOL)contactHasValidSipDomain:(ABRecordRef)person;
+ (void)setDisplayNameLabel:(UILabel *)label forAddress:(const LinphoneAddress *)addr;
+ (void)setDisplayNameLabel:(UILabel *)label forContact:(ABRecordRef)contact;
+ (NSString *)displayNameForAddress:(const LinphoneAddress *)addr;
+ (NSString *)displayNameForContact:(ABRecordRef)contact;
+ (void)setDisplayNameLabel:(UILabel *)label forAddress:(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;
@end

View file

@ -64,22 +64,34 @@ static void sync_address_book(ABAddressBookRef addressBook, CFDictionaryRef info
contact, thumbnail ? kABPersonImageFormatThumbnail : kABPersonImageFormatOriginalSize));
retImage = [UIImage imageWithData:imgData];
if (retImage != nil && retImage.size.width != retImage.size.height) {
LOGI(@"Image is not square : cropping it.");
return [self squareImageCrop:retImage];
}
}
if (retImage == nil) {
retImage = [UIImage imageNamed:@"avatar"];
}
if (retImage.size.width != retImage.size.height) {
LOGW(@"Image is not square (%@): cropping it.", retImage.size);
retImage = [self squareImageCrop:retImage];
}
return retImage;
}
- (ABRecordRef)getContact:(NSString *)address {
@synchronized(addressBookMap) {
return (__bridge ABRecordRef)[addressBookMap objectForKey:address];
+ (ABRecordRef)getContact:(NSString *)address {
@synchronized(LinphoneManager.instance.fastAddressBook->addressBookMap) {
return (__bridge ABRecordRef)[LinphoneManager.instance.fastAddressBook->addressBookMap objectForKey:address];
}
}
+ (ABRecordRef)getContactWithLinphoneAddress:(const LinphoneAddress *)address {
ABRecordRef contact = nil;
if (address) {
char *uri = linphone_address_as_string_uri_only(address);
NSString *normalizedSipAddress = [FastAddressBook normalizeSipURI:[NSString stringWithUTF8String:uri]];
contact = [FastAddressBook getContact:normalizedSipAddress];
ms_free(uri);
}
return contact;
}
+ (BOOL)isSipURI:(NSString *)address {
return [address hasPrefix:@"sip:"] || [address hasPrefix:@"sips:"];
}
@ -341,16 +353,10 @@ void sync_address_book(ABAddressBookRef addressBook, CFDictionaryRef info, void
+ (NSString *)displayNameForAddress:(const LinphoneAddress *)addr {
NSString *ret = NSLocalizedString(@"Unknown", nil);
if (addr != NULL) {
char *lAddress = linphone_address_as_string_uri_only(addr);
if (lAddress) {
NSString *normalizedSipAddress = [FastAddressBook normalizeSipURI:[NSString stringWithUTF8String:lAddress]];
ms_free(lAddress);
ABRecordRef contact = [[[LinphoneManager instance] fastAddressBook] getContact:normalizedSipAddress];
if (contact) {
return [FastAddressBook displayNameForContact:contact];
}
}
ABRecordRef contact = [FastAddressBook getContactWithLinphoneAddress:addr];
if (contact) {
ret = [FastAddressBook displayNameForContact:contact];
} else {
const char *lDisplayName = linphone_address_get_display_name(addr);
const char *lUserName = linphone_address_get_username(addr);
if (lDisplayName) {
@ -366,24 +372,4 @@ void sync_address_book(ABAddressBookRef addressBook, CFDictionaryRef info, void
label.text = [FastAddressBook displayNameForAddress:addr];
}
+ (UIImage *)avatarForAddress:(const LinphoneAddress *)addr {
UIImage *avatar = nil;
if (addr != NULL) {
// contact name
char *lAddress = linphone_address_as_string_uri_only(addr);
if (lAddress) {
NSString *normalizedSipAddress = [FastAddressBook normalizeSipURI:[NSString stringWithUTF8String:lAddress]];
ABRecordRef contact = [[[LinphoneManager instance] fastAddressBook] getContact:normalizedSipAddress];
if (contact) {
avatar = [FastAddressBook getContactImage:contact thumbnail:TRUE];
}
ms_free(lAddress);
}
}
if (avatar == nil) {
avatar = [UIImage imageNamed:@"avatar"];
}
return avatar;
}
@end