From 8553bac55d4a7ed505dc70d3b1d57a90aa8c14bd Mon Sep 17 00:00:00 2001 From: Jehan Monnier Date: Tue, 28 May 2013 17:10:50 +0200 Subject: [PATCH] better address book sip address handling/filtering --- Classes/ChatViewController.m | 2 +- Classes/ContactDetailsTableViewController.m | 44 ++++++++++++++++++--- Classes/ContactsViewController.h | 5 ++- Classes/ContactsViewController.m | 10 ++--- Classes/LinphoneManager.h | 5 ++- Classes/LinphoneManager.m | 8 +++- Classes/Utils/FastAddressBook.m | 2 +- Resources/linphonerc-factory | 3 ++ 8 files changed, 61 insertions(+), 18 deletions(-) diff --git a/Classes/ChatViewController.m b/Classes/ChatViewController.m index 4f52378a2..453f11927 100644 --- a/Classes/ChatViewController.m +++ b/Classes/ChatViewController.m @@ -122,7 +122,7 @@ static UICompositeViewDescription *compositeDescription = nil; if ([[addressField text ]length] == 0) { // if no address is manually set, lauch address book [ContactSelection setSelectionMode:ContactSelectionModeMessage]; [ContactSelection setAddAddress:nil]; - [ContactSelection setSipFilter:TRUE]; + [ContactSelection setSipFilter:@"*"]; [ContactSelection setEmailFilter:FALSE]; [[PhoneMainView instance] changeCurrentView:[ContactsViewController compositeViewDescription] push:TRUE]; } else { diff --git a/Classes/ContactDetailsTableViewController.m b/Classes/ContactDetailsTableViewController.m index 7fe664112..f43280bdc 100644 --- a/Classes/ContactDetailsTableViewController.m +++ b/Classes/ContactDetailsTableViewController.m @@ -198,7 +198,7 @@ static const int contactSections[ContactSections_MAX] = {ContactSections_None, C CFDictionaryRef lDict = ABMultiValueCopyValueAtIndex(lMap, i); BOOL add = false; if(CFDictionaryContainsKey(lDict, kABPersonInstantMessageServiceKey)) { - if(CFStringCompare((CFStringRef)kContactSipField, CFDictionaryGetValue(lDict, kABPersonInstantMessageServiceKey), kCFCompareCaseInsensitive) == 0) { + if(CFStringCompare((CFStringRef)[LinphoneManager instance].contactSipField, CFDictionaryGetValue(lDict, kABPersonInstantMessageServiceKey), kCFCompareCaseInsensitive) == 0) { add = true; } } else { @@ -285,7 +285,7 @@ static const int contactSections[ContactSections_MAX] = {ContactSections_None, C lMap = ABMultiValueCreateMutable(kABDictionaryPropertyType); } CFStringRef keys[] = { kABPersonInstantMessageUsernameKey, kABPersonInstantMessageServiceKey }; - CFTypeRef values[] = { [value copy], kContactSipField }; + CFTypeRef values[] = { [value copy], [LinphoneManager instance].contactSipField }; CFDictionaryRef lDict = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, 1, NULL, NULL); CFStringRef label = (CFStringRef)[labelArray objectAtIndex:0]; if(!ABMultiValueAddValueAndLabel(lMap, lDict, label, &identifier)) { @@ -497,7 +497,17 @@ static const int contactSections[ContactSections_MAX] = {ContactSections_None, C CFDictionaryRef lDict = ABMultiValueCopyValueAtIndex(lMap, index); CFStringRef valueRef = CFDictionaryGetValue(lDict, kABPersonInstantMessageUsernameKey); if(valueRef != NULL) { - value = [NSString stringWithString:(NSString*) valueRef]; + LinphoneAddress* addr=NULL; + if ([[LinphoneManager instance] lpConfigBoolForKey:@"contact_display_username_only"] + && (addr=linphone_address_new([(NSString *)valueRef UTF8String]))) { + if (linphone_address_get_username(addr)) { + value = [NSString stringWithCString:linphone_address_get_username(addr) + encoding:[NSString defaultCStringEncoding]]; + } /*else value=@""*/ + } else { + value = [NSString stringWithString:(NSString*) valueRef]; + } + if (addr) linphone_address_destroy(addr); } CFRelease(lDict); CFRelease(lMap); @@ -825,11 +835,33 @@ static const int contactSections[ContactSections_MAX] = {ContactSections_None, C CFRelease(lcMap); int index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); CFStringRef keys[] = { kABPersonInstantMessageUsernameKey, kABPersonInstantMessageServiceKey}; - CFTypeRef values[] = { [value copy], kContactSipField }; + CFTypeRef values[] = { [value copy], [LinphoneManager instance].contactSipField }; CFDictionaryRef lDict = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, 2, NULL, NULL); ABMultiValueReplaceValueAtIndex(lMap, lDict, index); - CFRelease(lDict); - ABRecordSetValue(contact, kABPersonInstantMessageProperty, lMap, nil); + ABRecordSetValue(contact, kABPersonInstantMessageProperty, lMap, nil); + CFRelease(lDict); + /*check if message type is kept or not*/ + lcMap = ABRecordCopyValue(contact, kABPersonInstantMessageProperty); + lMap = ABMultiValueCreateMutableCopy(lcMap); + CFRelease(lcMap); + index = ABMultiValueGetIndexForIdentifier(lMap, [entry identifier]); + lDict = ABMultiValueCopyValueAtIndex(lMap,index); + if(!CFDictionaryContainsKey(lDict, kABPersonInstantMessageServiceKey)) { + /*too bad probably a gtalk number, storing uri*/ + NSString* username = CFDictionaryGetValue(lDict, kABPersonInstantMessageUsernameKey); + LinphoneAddress* address = linphone_core_interpret_url([LinphoneManager getLc] + ,[username UTF8String]); + char* uri = linphone_address_as_string_uri_only(address); + CFStringRef keys[] = { kABPersonInstantMessageUsernameKey, kABPersonInstantMessageServiceKey}; + CFTypeRef values[] = { [NSString stringWithCString:uri encoding:[NSString defaultCStringEncoding]], [LinphoneManager instance].contactSipField }; + CFDictionaryRef lDict2 = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, 2, NULL, NULL); + ABMultiValueReplaceValueAtIndex(lMap, lDict2, index); + ABRecordSetValue(contact, kABPersonInstantMessageProperty, lMap, nil); + CFRelease(lDict2); + linphone_address_destroy(address); + ms_free(uri); + } + CFRelease(lDict); CFRelease(lMap); } else if(contactSections[[path section]] == ContactSections_Email) { ABMultiValueRef lcMap = ABRecordCopyValue(contact, kABPersonEmailProperty); diff --git a/Classes/ContactsViewController.h b/Classes/ContactsViewController.h index 00da1d3ad..c21948896 100644 --- a/Classes/ContactsViewController.h +++ b/Classes/ContactsViewController.h @@ -36,8 +36,9 @@ typedef enum _ContactSelectionMode { + (ContactSelectionMode)getSelectionMode; + (void)setAddAddress:(NSString*)address; + (NSString*)getAddAddress; -+ (void)setSipFilter:(BOOL)enable; -+ (BOOL)getSipFilter; +/* define sip filter, can be * or sip domain*/ ++ (void)setSipFilter:(NSString*) domain; ++ (NSString*)getSipFilter; + (void)setEmailFilter:(BOOL)enable; + (BOOL)getEmailFilter; diff --git a/Classes/ContactsViewController.m b/Classes/ContactsViewController.m index cf23d9d01..342ee059f 100644 --- a/Classes/ContactsViewController.m +++ b/Classes/ContactsViewController.m @@ -27,7 +27,7 @@ static ContactSelectionMode sSelectionMode = ContactSelectionModeNone; static NSString* sAddAddress = nil; -static BOOL sSipFilter = FALSE; +static NSString* sSipFilter = nil; static BOOL sEmailFilter = FALSE; + (void)setSelectionMode:(ContactSelectionMode)selectionMode { @@ -52,11 +52,11 @@ static BOOL sEmailFilter = FALSE; return sAddAddress; } -+ (void)setSipFilter:(BOOL)enable { - sSipFilter = enable; ++ (void)setSipFilter:(NSString*)domain { + sSipFilter = domain; } -+ (BOOL)getSipFilter { ++ (NSString*)getSipFilter { return sSipFilter; } @@ -206,7 +206,7 @@ static UICompositeViewDescription *compositeDescription = nil; } if(view == History_Linphone) { - [ContactSelection setSipFilter:TRUE]; + [ContactSelection setSipFilter:@"*"]; [ContactSelection setEmailFilter:FALSE]; [tableController loadData]; linphoneButton.selected = TRUE; diff --git a/Classes/LinphoneManager.h b/Classes/LinphoneManager.h index b90e36f74..4f9a27d06 100644 --- a/Classes/LinphoneManager.h +++ b/Classes/LinphoneManager.h @@ -47,8 +47,6 @@ extern NSString *const kLinphoneLogsUpdate; extern NSString *const kLinphoneSettingsUpdate; extern NSString *const kLinphoneBluetoothAvailabilityUpdate; -extern NSString *const kContactSipField; - typedef enum _NetworkType { network_none = 0, network_2g, @@ -117,6 +115,7 @@ typedef struct _LinphoneManagerSounds { + (NSSet *)unsupportedCodecs; + (NSString *)getUserAgent; + - (void)startLibLinphone; - (void)destroyLibLinphone; - (BOOL)resignActive; @@ -155,6 +154,7 @@ typedef struct _LinphoneManagerSounds { - (void)lpConfigSetBool:(BOOL)value forKey:(NSString*)key forSection:(NSString*)section; - (BOOL)lpConfigBoolForKey:(NSString*)key forSection:(NSString*)section; + @property (readonly) FastAddressBook* fastAddressBook; @property Connectivity connectivity; @property (readonly) NetworkType network; @@ -168,6 +168,7 @@ typedef struct _LinphoneManagerSounds { @property (nonatomic, assign) BOOL bluetoothAvailable; @property (nonatomic, assign) BOOL bluetoothEnabled; @property (readonly) ALAssetsLibrary *photoLibrary; +@property (readonly) NSString* contactSipField; @end diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index 0cf52f4be..c24357942 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -57,7 +57,7 @@ NSString *const kLinphoneMainViewChange = @"LinphoneMainViewChange"; NSString *const kLinphoneLogsUpdate = @"LinphoneLogsUpdate"; NSString *const kLinphoneSettingsUpdate = @"LinphoneSettingsUpdate"; NSString *const kLinphoneBluetoothAvailabilityUpdate = @"LinphoneBluetoothAvailabilityUpdate"; -NSString *const kContactSipField = @"SIP"; + extern void libmsilbc_init(); @@ -246,6 +246,7 @@ struct codec_name_pref_table codec_pref_table[]={ [self copyDefaultSettings]; pendindCallIdFromRemoteNotif = [[NSMutableArray alloc] init ]; photoLibrary = [[ALAssetsLibrary alloc] init]; + } return self; } @@ -925,6 +926,11 @@ static LinphoneCoreVTable linphonec_vtable = { //go directly to bg mode [self resignActive]; } + + _contactSipField = [self lpConfigStringForKey:@"contact_im_type_value"]; + if (_contactSipField == nil) + _contactSipField =@"SIP"; + // Post event NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSValue valueWithPointer:theLinphoneCore] forKey:@"core"]; diff --git a/Classes/Utils/FastAddressBook.m b/Classes/Utils/FastAddressBook.m index ab5f0b1a3..9831c2812 100644 --- a/Classes/Utils/FastAddressBook.m +++ b/Classes/Utils/FastAddressBook.m @@ -179,7 +179,7 @@ static void sync_address_book (ABAddressBookRef addressBook, CFDictionaryRef inf CFDictionaryRef lDict = ABMultiValueCopyValueAtIndex(lMap, i); BOOL add = false; if(CFDictionaryContainsKey(lDict, kABPersonInstantMessageServiceKey)) { - if(CFStringCompare((CFStringRef)kContactSipField, CFDictionaryGetValue(lDict, kABPersonInstantMessageServiceKey), kCFCompareCaseInsensitive) == 0) { + if(CFStringCompare((CFStringRef)[LinphoneManager instance].contactSipField, CFDictionaryGetValue(lDict, kABPersonInstantMessageServiceKey), kCFCompareCaseInsensitive) == 0) { add = true; } } else { diff --git a/Resources/linphonerc-factory b/Resources/linphonerc-factory index fb48ebbdf..ec3d25d96 100644 --- a/Resources/linphonerc-factory +++ b/Resources/linphonerc-factory @@ -47,3 +47,6 @@ stun=stun.linphone.org [video] display_filter_auto_rotate=0 + +[app] +#contact_display_username_only=1