diff --git a/Classes/Contact.h b/Classes/Contact.h index 5248313d0..846884826 100644 --- a/Classes/Contact.h +++ b/Classes/Contact.h @@ -22,13 +22,13 @@ @property(nonatomic, strong) NSMutableArray *emails; @property(nonatomic, strong) NSMutableArray *phoneNumbers; +- (void)setAvatar:(UIImage *)avatar; - (UIImage *)avatar:(BOOL)thumbnail; - (NSString *)displayName; - (instancetype)initWithPerson:(ABRecordRef)person; - (instancetype)initWithFriend:(LinphoneFriend *) friend; -- (void)setAvatar:(UIImage *)avatar; - (BOOL)setSipAddress:(NSString *)sip atIndex:(NSInteger)index; - (BOOL)setEmail:(NSString *)email atIndex:(NSInteger)index; - (BOOL)setPhoneNumber:(NSString *)phone atIndex:(NSInteger)index; @@ -40,7 +40,4 @@ - (BOOL)removeSipAddressAtIndex:(NSInteger)index; - (BOOL)removePhoneNumberAtIndex:(NSInteger)index; - (BOOL)removeEmailAtIndex:(NSInteger)index; - -- (int)remove; -- (BOOL)save; @end diff --git a/Classes/Contact.m b/Classes/Contact.m index 2e6866fd9..27b739607 100644 --- a/Classes/Contact.m +++ b/Classes/Contact.m @@ -47,73 +47,6 @@ _person = nil; _friend = NULL; } -#pragma mark - Misc -- (int)remove { - // Remove contact from book - if (_person && ABRecordGetRecordID(_person) != kABRecordInvalidID) { - CFErrorRef error = NULL; - ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); - if (!addressBook) { - LOGE(@"Cannot retrieve address book"); - return -3; - } - ABAddressBookGetAuthorizationStatus(); - - ABAddressBookRemoveRecord(addressBook, _person, &error); - if (error != NULL) { - LOGE(@"Remove contact %p: Fail(%@)", self, [(__bridge NSError *)error localizedDescription]); - CFRelease(addressBook); - return -3; - } else { - LOGI(@"Remove contact %p: Success!", self); - } - - // Save address book - error = NULL; - ABAddressBookSave(addressBook, &error); - - _person = nil; - - if (error != NULL) { - LOGE(@"Save AddressBook: Fail(%@)", [(__bridge NSError *)error localizedDescription]); - } else { - LOGI(@"Save AddressBook: Success!"); - } - CFRelease(addressBook); - return error ? -1 : 0; - } - return -2; -} - -- (BOOL)save { - CFErrorRef error = NULL; - ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); - if (!addressBook) { - LOGE(@"Cannot retrieve address book"); - return FALSE; - } - if (ABRecordGetRecordID(_person) == kABRecordInvalidID) { - if (ABAddressBookAddRecord(addressBook, _person, &error)) { - LOGI(@"Add contact %p: Success!", _person); - } else { - LOGE(@"Add contact %p: Fail(%@)", _person, [(__bridge NSError *)error localizedDescription]); - CFRelease(addressBook); - return FALSE; - } - } - - // Save address book - error = NULL; - if (ABAddressBookSave(addressBook, &error)) { - LOGI(@"Save AddressBook: Success!"); - } else { - LOGE(@"Save AddressBook: Fail(%@)", - error ? [(__bridge NSError *)error localizedDescription] : @"unknown error"); - return FALSE; - } - CFRelease(addressBook); - return error == NULL; -} #pragma mark - Getters - (UIImage *)avatar:(BOOL)thumbnail { @@ -165,6 +98,25 @@ #pragma mark - Setters +- (void)setAvatar:(UIImage *)avatar { + if (_person) { + CFErrorRef error = NULL; + if (!ABPersonRemoveImageData(_person, &error)) { + LOGW(@"Can't remove entry: %@", [(__bridge NSError *)error localizedDescription]); + } + NSData *dataRef = UIImageJPEGRepresentation(avatar, 0.9f); + CFDataRef cfdata = CFDataCreate(NULL, [dataRef bytes], [dataRef length]); + + if (!ABPersonSetImageData(_person, cfdata, &error)) { + LOGW(@"Can't add entry: %@", [(__bridge NSError *)error localizedDescription]); + } + + CFRelease(cfdata); + } else { + LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping", __FUNCTION__); + } +} + - (void)setFirstName:(NSString *)firstName { BOOL ret = FALSE; if (_person) { @@ -191,25 +143,6 @@ } } -- (void)setAvatar:(UIImage *)avatar { - if (_person) { - CFErrorRef error = NULL; - if (!ABPersonRemoveImageData(_person, &error)) { - LOGW(@"Can't remove entry: %@", [(__bridge NSError *)error localizedDescription]); - } - NSData *dataRef = UIImageJPEGRepresentation(avatar, 0.9f); - CFDataRef cfdata = CFDataCreate(NULL, [dataRef bytes], [dataRef length]); - - if (!ABPersonSetImageData(_person, cfdata, &error)) { - LOGW(@"Can't add entry: %@", [(__bridge NSError *)error localizedDescription]); - } - - CFRelease(cfdata); - } else { - LOGW(@"%s: Cannot do it when using LinphoneFriend, skipping", __FUNCTION__); - } -} - - (BOOL)setSipAddress:(NSString *)sip atIndex:(NSInteger)index { BOOL ret = FALSE; if (_person) { diff --git a/Classes/ContactDetailsView.m b/Classes/ContactDetailsView.m index 15438ad49..914dbbdf8 100644 --- a/Classes/ContactDetailsView.m +++ b/Classes/ContactDetailsView.m @@ -61,7 +61,7 @@ - (void)removeContact { inhibUpdate = TRUE; - [_contact remove]; + [[LinphoneManager.instance fastAddressBook] removeContact:_contact]; inhibUpdate = FALSE; [PhoneMainView.instance popCurrentView]; } @@ -73,7 +73,7 @@ } // Add contact to book - [_contact save]; + [LinphoneManager.instance.fastAddressBook saveContact:_contact]; } - (void)selectContact:(Contact *)acontact andReload:(BOOL)reload { diff --git a/Classes/ContactsListTableView.m b/Classes/ContactsListTableView.m index 6061740d1..34c8a414f 100644 --- a/Classes/ContactsListTableView.m +++ b/Classes/ContactsListTableView.m @@ -252,7 +252,7 @@ static int ms_strcmpfuz(const char *fuzzy_word, const char *sentence) { [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade]; } - [contact remove]; + [[LinphoneManager.instance fastAddressBook] removeContact:contact]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; @@ -276,7 +276,7 @@ static int ms_strcmpfuz(const char *fuzzy_word, const char *sentence) { if ([self.tableView numberOfRowsInSection:indexPath.section] == 1) { [addressBookMap removeObjectForKey:firstChar]; } - [contact remove]; + [[LinphoneManager.instance fastAddressBook] removeContact:contact]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(onAddressBookUpdate:) diff --git a/Classes/ContactsListView.m b/Classes/ContactsListView.m index 65ffef68d..081a59ee0 100644 --- a/Classes/ContactsListView.m +++ b/Classes/ContactsListView.m @@ -197,6 +197,7 @@ static UICompositeViewDescription *compositeDescription = nil; onConfirmationClick:^() { [tableController removeSelectionUsing:nil]; [tableController loadData]; + [self onEditionChangeClick:nil]; }]; } diff --git a/Classes/Utils/FastAddressBook.h b/Classes/Utils/FastAddressBook.h index 2dd3e624b..72e462e63 100644 --- a/Classes/Utils/FastAddressBook.h +++ b/Classes/Utils/FastAddressBook.h @@ -28,6 +28,9 @@ @property(readonly, nonatomic) NSMutableDictionary *addressBookMap; - (void)reload; +- (void)saveAddressBook; +- (int)removeContact:(Contact *)contact; +- (BOOL)saveContact:(Contact *)contact; + (BOOL)isAuthorized; diff --git a/Classes/Utils/FastAddressBook.m b/Classes/Utils/FastAddressBook.m index 1adf7fb55..3b7f062d9 100644 --- a/Classes/Utils/FastAddressBook.m +++ b/Classes/Utils/FastAddressBook.m @@ -246,4 +246,56 @@ void sync_address_book(ABAddressBookRef addressBook, CFDictionaryRef info, void return ret; } +- (int)removeContact:(Contact *)contact { + // Remove contact from book + if (contact.person && ABRecordGetRecordID(contact.person) != kABRecordInvalidID) { + CFErrorRef error = NULL; + ABAddressBookRemoveRecord(addressBook, contact.person, (CFErrorRef *)&error); + if (error != NULL) { + LOGE(@"Remove contact %p: Fail(%@)", contact, [(__bridge NSError *)error localizedDescription]); + } else { + LOGI(@"Remove contact %p: Success!", contact); + } + contact = NULL; + // Save address book + error = NULL; + ABAddressBookSave(addressBook, (CFErrorRef *)&error); + + // TODO: stop reloading the whole address book but just clear the removed entries! + [self loadData]; + + if (error != NULL) { + LOGE(@"Save AddressBook: Fail(%@)", [(__bridge NSError *)error localizedDescription]); + } else { + LOGI(@"Save AddressBook: Success!"); + } + return error ? -1 : 0; + } + return -2; +} + +- (BOOL)saveContact:(Contact *)contact { + CFErrorRef error = NULL; + if (ABRecordGetRecordID(contact.person) == kABRecordInvalidID) { + if (ABAddressBookAddRecord(addressBook, contact.person, (CFErrorRef *)&error)) { + LOGI(@"Add contact %p: Success!", contact.person); + } else { + LOGE(@"Add contact %p: Fail(%@)", contact.person, [(__bridge NSError *)error localizedDescription]); + return FALSE; + } + } + + // Save address book + error = NULL; + if (ABAddressBookSave(addressBook, &error)) { + LOGI(@"Save AddressBook: Success!"); + } else { + LOGE(@"Save AddressBook: Fail(%@)", [(__bridge NSError *)error localizedDescription]); + return FALSE; + } + [self reload]; + + return error == NULL; +} + @end