Contact: revert rework which caused troubles when syncing with address book

This commit is contained in:
Gautier Pelloux-Prayer 2016-06-02 13:57:39 +02:00
parent 0b89abfc88
commit e5674a997b
7 changed files with 80 additions and 94 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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 {

View file

@ -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:)

View file

@ -197,6 +197,7 @@ static UICompositeViewDescription *compositeDescription = nil;
onConfirmationClick:^() {
[tableController removeSelectionUsing:nil];
[tableController loadData];
[self onEditionChangeClick:nil];
}];
}

View file

@ -28,6 +28,9 @@
@property(readonly, nonatomic) NSMutableDictionary *addressBookMap;
- (void)reload;
- (void)saveAddressBook;
- (int)removeContact:(Contact *)contact;
- (BOOL)saveContact:(Contact *)contact;
+ (BOOL)isAuthorized;

View file

@ -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