From d70cc9d76dc3d6504ffb6f405429d4df6ea038a2 Mon Sep 17 00:00:00 2001 From: Simon Morlat Date: Fri, 31 Aug 2012 09:28:51 +0200 Subject: [PATCH] fix bugs with chat --- Classes/ChatRoomTableViewController.h | 2 +- Classes/ChatRoomViewController.h | 1 + Classes/ChatRoomViewController.m | 50 +++++++++++++-------------- Classes/LinphoneUI/UIChatCell.m | 15 +++++--- 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/Classes/ChatRoomTableViewController.h b/Classes/ChatRoomTableViewController.h index 5d3b01ca2..c29786f6b 100644 --- a/Classes/ChatRoomTableViewController.h +++ b/Classes/ChatRoomTableViewController.h @@ -26,7 +26,7 @@ NSMutableArray *data; } -@property (nonatomic, retain) NSString *remoteAddress; +@property (nonatomic, copy) NSString *remoteAddress; - (void)addChatEntry:(ChatModel*)chat; - (void)updateChatEntry:(ChatModel*)chat; diff --git a/Classes/ChatRoomViewController.h b/Classes/ChatRoomViewController.h index c39d7d93d..82a8a2a75 100644 --- a/Classes/ChatRoomViewController.h +++ b/Classes/ChatRoomViewController.h @@ -29,6 +29,7 @@ @interface ChatRoomViewController : UIViewController { @private LinphoneChatRoom *chatRoom; + NSString *_remoteAddress; } diff --git a/Classes/ChatRoomViewController.m b/Classes/ChatRoomViewController.m index 8d3282c71..a144d846c 100644 --- a/Classes/ChatRoomViewController.m +++ b/Classes/ChatRoomViewController.m @@ -28,7 +28,7 @@ @synthesize sendButton; @synthesize messageField; @synthesize editButton; -@synthesize remoteAddress; +@synthesize remoteAddress = _remoteAddress; @synthesize addressLabel; @synthesize avatarImage; @synthesize headerView; @@ -53,7 +53,7 @@ [messageField release]; [sendButton release]; [editButton release]; - [remoteAddress release]; + [_remoteAddress release]; [addressLabel release]; [avatarImage release]; [headerView release]; @@ -150,33 +150,40 @@ static UICompositeViewDescription *compositeDescription = nil; #pragma mark - - (void)setRemoteAddress:(NSString*)aRemoteAddress { - if(remoteAddress != nil) { - [remoteAddress release]; + if(_remoteAddress != nil) { + [_remoteAddress release]; } - remoteAddress = [aRemoteAddress copy]; + _remoteAddress = [aRemoteAddress copy]; [messageField setText:@""]; - [tableController setRemoteAddress: remoteAddress]; [self update]; + [tableController setRemoteAddress: _remoteAddress]; } - (void)update { - if(remoteAddress == NULL) { + if(_remoteAddress == NULL) { [LinphoneLogger logc:LinphoneLoggerWarning format:"Cannot update chat room header: null contact"]; return; } NSString *displayName = nil; UIImage *image = nil; - NSString *normalizedSipAddress = [FastAddressBook normalizeSipURI:remoteAddress]; + LinphoneAddress* linphoneAddress =linphone_core_interpret_url([LinphoneManager getLc],[_remoteAddress cStringUsingEncoding: NSUTF8StringEncoding]); + if (linphoneAddress==NULL) + 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]; } - + [_remoteAddress release]; + _remoteAddress =[normalizedSipAddress retain]; // Display name if(displayName == nil) { - displayName = remoteAddress; + displayName = [NSString stringWithUTF8String:linphone_address_get_username(linphoneAddress)]; } [addressLabel setText:displayName]; @@ -185,7 +192,9 @@ static UICompositeViewDescription *compositeDescription = nil; image = [UIImage imageNamed:@"avatar_unknown_small.png"]; } [avatarImage setImage:image]; + linphone_address_destroy(linphoneAddress); } + static void message_status(LinphoneChatMessage* msg,LinphoneChatMessageState state,void* ud) { ChatRoomViewController* thiz=(ChatRoomViewController*)ud; ChatModel *chat = (ChatModel *)linphone_chat_message_get_user_data(msg); @@ -201,31 +210,20 @@ static void message_status(LinphoneChatMessage* msg,LinphoneChatMessageState sta [LinphoneLogger logc:LinphoneLoggerWarning format:"Cannot send message: Linphone core not ready"]; return FALSE; } - if(remoteAddress == nil) { + if(_remoteAddress == nil) { [LinphoneLogger logc:LinphoneLoggerWarning format:"Cannot send message: Null remoteAddress"]; return FALSE; } if(chatRoom == NULL) { - LinphoneProxyConfig* proxyCfg; - linphone_core_get_default_proxy([LinphoneManager getLc], &proxyCfg); - if (![remoteAddress hasPrefix:@"sip:"] && proxyCfg) { - //hmm probably a username only - char normalizedUserName[256]; - LinphoneAddress* linphoneAddress = linphone_address_new(linphone_core_get_identity([LinphoneManager getLc])); - linphone_proxy_config_normalize_number(proxyCfg,[remoteAddress cStringUsingEncoding:[NSString defaultCStringEncoding]],normalizedUserName,sizeof(normalizedUserName)); - linphone_address_set_username(linphoneAddress, normalizedUserName); - remoteAddress=[NSString stringWithUTF8String:normalizedUserName]; - linphone_address_destroy(linphoneAddress); - - } + - chatRoom = linphone_core_create_chat_room([LinphoneManager getLc], [remoteAddress UTF8String]); + chatRoom = linphone_core_create_chat_room([LinphoneManager getLc], [_remoteAddress UTF8String]); } // Save message in database ChatModel *chat = [[ChatModel alloc] init]; - [chat setRemoteContact:remoteAddress]; + [chat setRemoteContact:_remoteAddress]; [chat setLocalContact:@""]; [chat setMessage:message]; [chat setDirection:[NSNumber numberWithInt:0]]; @@ -253,7 +251,7 @@ static void message_status(LinphoneChatMessage* msg,LinphoneChatMessageState sta char *fromStr = linphone_address_as_string_uri_only(from); if(fromStr != NULL) { if([[NSString stringWithUTF8String:fromStr] - caseInsensitiveCompare:remoteAddress] == NSOrderedSame) { + caseInsensitiveCompare:_remoteAddress] == NSOrderedSame) { [chat setRead:[NSNumber numberWithInt:1]]; [chat update]; [[NSNotificationCenter defaultCenter] postNotificationName:kLinphoneTextReceived object:self]; diff --git a/Classes/LinphoneUI/UIChatCell.m b/Classes/LinphoneUI/UIChatCell.m index acfecb71a..b6ad2bd80 100644 --- a/Classes/LinphoneUI/UIChatCell.m +++ b/Classes/LinphoneUI/UIChatCell.m @@ -71,14 +71,18 @@ #pragma mark - - (void)update { + NSString *displayName = nil; + UIImage *image = nil; if(chat == nil) { [LinphoneLogger logc:LinphoneLoggerWarning format:"Cannot update chat cell: null chat"]; return; } - - NSString *displayName = nil; - UIImage *image = nil; - NSString *normalizedSipAddress = [FastAddressBook normalizeSipURI:[chat remoteContact]]; + LinphoneAddress* linphoneAddress =linphone_core_interpret_url([LinphoneManager getLc],[[chat remoteContact] cStringUsingEncoding: NSUTF8StringEncoding]); + if (linphoneAddress==NULL) + return ; + char *tmp=linphone_address_as_string_uri_only(linphoneAddress); + NSString *normalizedSipAddress=[NSString stringWithUTF8String:tmp]; + ms_free(tmp); ABRecordRef contact =[[[LinphoneManager instance] fastAddressBook] getContact:normalizedSipAddress]; if(contact != nil) { displayName = [FastAddressBook getContactDisplayName:contact]; @@ -87,7 +91,7 @@ // Display name if(displayName == nil) { - displayName = [chat remoteContact]; + displayName = [NSString stringWithCString:linphone_address_get_username(linphoneAddress) encoding:NSUTF8StringEncoding]; } [addressLabel setText:displayName]; @@ -99,6 +103,7 @@ // Message [chatContentLabel setText:[chat message]]; + linphone_address_destroy(linphoneAddress); } - (void)layoutSubviews {