diff --git a/matrixConsole/API/ContactManager.m b/matrixConsole/API/ContactManager.m index 0b77806f3..a41623ea3 100644 --- a/matrixConsole/API/ContactManager.m +++ b/matrixConsole/API/ContactManager.m @@ -32,7 +32,8 @@ NSString *const kContactsDidInternationalizeNotification = @"kContactsDidInterna //#define CONTACTS_3PIDS_SYNC 1 // else checks the matrix IDs for each displayed contact -@interface ContactManager() { +@interface ContactManager() +{ NSDate *lastSyncDate; NSMutableDictionary* deviceContactByContactID; @@ -52,8 +53,10 @@ NSString *const kContactsDidInternationalizeNotification = @"kContactsDidInterna #pragma mark Singleton Methods static ContactManager* sharedContactManager = nil; -+ (id)sharedManager { - @synchronized(self) { ++ (id)sharedManager +{ + @synchronized(self) + { if(sharedContactManager == nil) sharedContactManager = [[self alloc] init]; } @@ -62,8 +65,10 @@ static ContactManager* sharedContactManager = nil; #pragma mark - --(ContactManager *)init { - if (self = [super init]) { +-(ContactManager *)init +{ + if (self = [super init]) + { NSString *label = [NSString stringWithFormat:@"ConsoleMatrix.%@.Contacts", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"]]; processingQueue = dispatch_queue_create([label UTF8String], NULL); @@ -79,9 +84,11 @@ static ContactManager* sharedContactManager = nil; return self; } --(void)dealloc { +-(void)dealloc +{ - if (matrixSessionStateObserver) { + if (matrixSessionStateObserver) + { [[NSNotificationCenter defaultCenter] removeObserver:matrixSessionStateObserver]; matrixSessionStateObserver = nil; } @@ -91,25 +98,31 @@ static ContactManager* sharedContactManager = nil; #pragma mark - -- (MXRestClient*)mxRestClient { +- (MXRestClient*)mxRestClient +{ // Ignore `mxRestClient` property if a matrix session has been defined - if (self.mxSession) { + if (self.mxSession) + { return self.mxSession.matrixRestClient; } return _mxRestClient; } -- (void)setMxSession:(MXSession *)session { +- (void)setMxSession:(MXSession *)session +{ // Remove potential session observer [[NSNotificationCenter defaultCenter] removeObserver:matrixSessionStateObserver]; - if (session) { + if (session) + { // Register session state observer - matrixSessionStateObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kMXSessionStateDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) { + matrixSessionStateObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kMXSessionStateDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) + { // Check whether the concerned session is the associated one - if (notif.object == _mxSession) { + if (notif.object == _mxSession) + { [self didMatrixSessionStateChange]; } }]; @@ -121,9 +134,11 @@ static ContactManager* sharedContactManager = nil; [self didMatrixSessionStateChange]; } -- (void)didMatrixSessionStateChange { +- (void)didMatrixSessionStateChange +{ - if (_mxSession && _mxSession.state == MXSessionStateRunning) { + if (_mxSession && _mxSession.state == MXSessionStateRunning) + { [self manage3PIDS]; } } @@ -131,7 +146,8 @@ static ContactManager* sharedContactManager = nil; #pragma mark - // delete contacts info -- (void)reset { +- (void)reset +{ contacts = nil; @@ -139,7 +155,8 @@ static ContactManager* sharedContactManager = nil; deviceContactByContactID = nil; matrixContactByMatrixUserID = nil; - if (matrixSessionStateObserver) { + if (matrixSessionStateObserver) + { [[NSNotificationCenter defaultCenter] removeObserver:matrixSessionStateObserver]; matrixSessionStateObserver = nil; } @@ -148,18 +165,20 @@ static ContactManager* sharedContactManager = nil; [self saveMatrixIDsDict]; [self saveDeviceContacts]; [self saveContactBookInfo]; - + // warn of the contacts list update [[NSNotificationCenter defaultCenter] postNotificationName:kContactManagerContactsListRefreshNotification object:nil userInfo:nil]; } // refresh the international phonenumber of the contacts -- (void)internationalizePhoneNumbers:(NSString*)countryCode { +- (void)internationalizePhoneNumbers:(NSString*)countryCode +{ dispatch_async(processingQueue, ^{ NSArray* contactsSnapshot = [deviceContactByContactID allValues]; - for(MXCContact* contact in contactsSnapshot) { + for(MXCContact* contact in contactsSnapshot) + { [contact internationalizePhonenumbers:countryCode]; } @@ -169,10 +188,12 @@ static ContactManager* sharedContactManager = nil; }); } -- (void)fullRefresh { +- (void)fullRefresh +{ // check if the user allowed to sync local contacts - if (![[MXKAppSettings standardAppSettings] syncLocalContacts]) { + if (![[MXKAppSettings standardAppSettings] syncLocalContacts]) + { contacts = nil; // if the user did not allow to sync local contacts // ignore this sync @@ -186,12 +207,15 @@ static ContactManager* sharedContactManager = nil; ABAuthorizationStatus cbStatus = ABAddressBookGetAuthorizationStatus(); // did not yet request the access - if (cbStatus == kABAuthorizationStatusNotDetermined) { + if (cbStatus == kABAuthorizationStatusNotDetermined) + { // request address book access ABAddressBookRef ab = ABAddressBookCreateWithOptions(nil, nil); - if (ab) { - ABAddressBookRequestAccessWithCompletion(ab, ^(bool granted, CFErrorRef error) { + if (ab) + { + ABAddressBookRequestAccessWithCompletion(ab, ^(bool granted, CFErrorRef error) + { dispatch_async(dispatch_get_main_queue(), ^{ [self fullRefresh]; }); @@ -206,25 +230,28 @@ static ContactManager* sharedContactManager = nil; pending3PIDs = [[NSMutableArray alloc] init]; checked3PIDs = [[NSMutableArray alloc] init]; - + // cold start // launch the dict from the file system // It is cached to improve UX. - if (!matrixIDBy3PID) { + if (!matrixIDBy3PID) + { [self loadMatrixIDsDict]; } - + dispatch_async(processingQueue, ^{ - + // in case of cold start // get the info from the file system - if (!lastSyncDate) { + if (!lastSyncDate) + { // load cached contacts [self loadDeviceContacts]; [self loadContactBookInfo]; // no local contact -> assume that the last sync date is useless - if (deviceContactByContactID.count == 0) { + if (deviceContactByContactID.count == 0) + { lastSyncDate = nil; } } @@ -234,7 +261,8 @@ static ContactManager* sharedContactManager = nil; NSMutableArray* deletedContactIDs = [[deviceContactByContactID allKeys] mutableCopy]; // can list tocal contacts - if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { + if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) + { NSString* countryCode = [[MXKAppSettings standardAppSettings] phonebookCountryCode]; @@ -244,10 +272,12 @@ static ContactManager* sharedContactManager = nil; CFIndex index; CFMutableArrayRef people = (CFMutableArrayRef)ABAddressBookCopyArrayOfAllPeople(ab); - if (nil != people) { + if (nil != people) + { CFIndex peopleCount = CFArrayGetCount(people); - for (index = 0; index < peopleCount; index++) { + for (index = 0; index < peopleCount; index++) + { contactRecord = (ABRecordRef)CFArrayGetValueAtIndex(people, index); @@ -256,10 +286,12 @@ static ContactManager* sharedContactManager = nil; // the contact still exists [deletedContactIDs removeObject:contactID]; - if (lastSyncDate) { + if (lastSyncDate) + { // ignore unchanged contacts since the previous sync CFDateRef lastModifDate = ABRecordCopyValue(contactRecord, kABPersonModificationDateProperty); if (kCFCompareGreaterThan != CFDateCompare (lastModifDate, (__bridge CFDateRef)lastSyncDate, nil)) + { CFRelease(lastModifDate); continue; @@ -271,7 +303,8 @@ static ContactManager* sharedContactManager = nil; MXCContact* contact = [[MXCContact alloc] initWithABRecord:contactRecord]; - if (countryCode) { + if (countryCode) + { [contact internationalizePhonenumbers:countryCode]; } @@ -282,30 +315,36 @@ static ContactManager* sharedContactManager = nil; CFRelease(people); } - if (ab) { + if (ab) + { CFRelease(ab); } } // some contacts have been deleted - for (NSString* contactID in deletedContactIDs) { + for (NSString* contactID in deletedContactIDs) + { contactBookUpdate = YES; [deviceContactByContactID removeObjectForKey:contactID]; } - + // something has been modified in the device contact book - if (contactBookUpdate) { + if (contactBookUpdate) + { [self saveDeviceContacts]; } lastSyncDate = [NSDate date]; [self saveContactBookInfo]; - + NSMutableArray* deviceContacts = [[deviceContactByContactID allValues] mutableCopy]; - if (_mxSession && _mxSession.state == MXSessionStateRunning) { + if (_mxSession && _mxSession.state == MXSessionStateRunning) + { [self manage3PIDS]; - } else { + } + else + { // display what you could have read dispatch_async(dispatch_get_main_queue(), ^{ contacts = deviceContacts; @@ -319,7 +358,8 @@ static ContactManager* sharedContactManager = nil; // the local contacts are listed // update their 3PIDs and their update -- (void) manage3PIDS { +- (void) manage3PIDS +{ dispatch_async(processingQueue, ^{ NSMutableArray* tmpContacts = nil; @@ -336,7 +376,7 @@ static ContactManager* sharedContactManager = nil; [self refreshMatrixIDs]; #else // nop - // wait that refreshContactMatrixIDs is called + // wait that refreshContactMatrixIDs is called #endif // at least, display the known contacts @@ -345,24 +385,31 @@ static ContactManager* sharedContactManager = nil; }); } -- (void) updateContactMatrixIDs:(MXCContact*) contact { +- (void) updateContactMatrixIDs:(MXCContact*) contact +{ // the phonenumbers wil be managed later - /*for(ConsolePhoneNumber* pn in contact.phoneNumbers) { - if (pn.textNumber.length > 0) { + /*for(ConsolePhoneNumber* pn in contact.phoneNumbers) + { + if (pn.textNumber.length > 0) + { // not yet added - if ([pids indexOfObject:pn.textNumber] == NSNotFound) { + if ([pids indexOfObject:pn.textNumber] == NSNotFound) + { [pids addObject:pn.textNumber]; [medias addObject:@"msisdn"]; } } }*/ - for(MXCEmail* email in contact.emailAddresses) { - if (email.emailAddress.length > 0) { + for(MXCEmail* email in contact.emailAddresses) + { + if (email.emailAddress.length > 0) + { id matrixID = [matrixIDBy3PID valueForKey:email.emailAddress]; - if ([matrixID isKindOfClass:[NSString class]]) { + if ([matrixID isKindOfClass:[NSString class]]) + { dispatch_async(dispatch_get_main_queue(), ^{ [email setMatrixID:matrixID]; }); @@ -371,12 +418,14 @@ static ContactManager* sharedContactManager = nil; } } -- (void) updateMatrixIDDeviceContacts { +- (void) updateMatrixIDDeviceContacts +{ NSArray* deviceContacts = [deviceContactByContactID allValues]; // update the contacts info - for(MXCContact* contact in deviceContacts) { + for(MXCContact* contact in deviceContacts) + { [self updateContactMatrixIDs:contact]; } } @@ -384,30 +433,38 @@ static ContactManager* sharedContactManager = nil; #ifdef CONTACTS_3PIDS_SYNC // refresh the 3PIDs -> Matrix ID list // update the contact is required -- (void)refreshMatrixIDs { +- (void)refreshMatrixIDs +{ // build the request parameters NSMutableArray* pids = [[NSMutableArray alloc] init]; NSMutableArray* medias = [[NSMutableArray alloc] init]; - for(MXCContact* contact in deviceContactsList) { + for(MXCContact* contact in deviceContactsList) + { // the phonenumbers are not managed - /*for(ConsolePhoneNumber* pn in contact.phoneNumbers) { - if (pn.textNumber.length > 0) { + /*for(ConsolePhoneNumber* pn in contact.phoneNumbers) + { + if (pn.textNumber.length > 0) + { // not yet added - if ([pids indexOfObject:pn.textNumber] == NSNotFound) { + if ([pids indexOfObject:pn.textNumber] == NSNotFound) + { [pids addObject:pn.textNumber]; [medias addObject:@"msisdn"]; } } }*/ - for(MXCEmail* email in contact.emailAddresses) { - if (email.emailAddress.length > 0) { + for(MXCEmail* email in contact.emailAddresses) + { + if (email.emailAddress.length > 0) + { // not yet added - if ([pids indexOfObject:email.emailAddress] == NSNotFound) { + if ([pids indexOfObject:email.emailAddress] == NSNotFound) + { [pids addObject:email.emailAddress]; [medias addObject:@"email"]; } @@ -416,34 +473,39 @@ static ContactManager* sharedContactManager = nil; } // get some pids - if (pids.count > 0) { - if (self.mxRestClient) { + if (pids.count > 0) + { + if (self.mxRestClient) + { [self.mxRestClient lookup3pids:pids forMedia:medias - success:^(NSArray *userIds) { - // sanity check - if (userIds.count == pids.count) { - - matrixIDBy3PID = [[NSMutableDictionary alloc] initWithObjects:userIds forKeys:pids]; - [self saveMatrixIDsDict]; - [self updateMatrixIDDeviceContactsList]; - - // add the MX users - NSMutableArray* tmpContacts = [deviceContactsList mutableCopy]; - [self mergeMXUsers:tmpContacts]; - - dispatch_async(dispatch_get_main_queue(), ^{ - contacts = tmpContacts; - [[NSNotificationCenter defaultCenter] postNotificationName:kContactManagerContactsListRefreshNotification object:nil userInfo:nil]; - }); - } - } - failure:^(NSError *error) { - // try later - dispatch_after(dispatch_walltime(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ - [self refreshMatrixIDs]; - }); - } + success:^(NSArray *userIds) + { + // sanity check + if (userIds.count == pids.count) + { + + matrixIDBy3PID = [[NSMutableDictionary alloc] initWithObjects:userIds forKeys:pids]; + [self saveMatrixIDsDict]; + [self updateMatrixIDDeviceContactsList]; + + // add the MX users + NSMutableArray* tmpContacts = [deviceContactsList mutableCopy]; + [self mergeMXUsers:tmpContacts]; + + dispatch_async(dispatch_get_main_queue(), ^{ + contacts = tmpContacts; + [[NSNotificationCenter defaultCenter] postNotificationName:kContactManagerContactsListRefreshNotification object:nil userInfo:nil]; + }); + } + } + failure:^(NSError *error) + { + // try later + dispatch_after(dispatch_walltime(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ + [self refreshMatrixIDs]; + }); + } ]; } } @@ -451,104 +513,129 @@ static ContactManager* sharedContactManager = nil; #endif // refresh matrix IDs -- (void)refreshContactMatrixIDs:(MXCContact*)contact { +- (void)refreshContactMatrixIDs:(MXCContact*)contact +{ #ifndef CONTACTS_3PIDS_SYNC - if (!contact.isMatrixContact) { + if (!contact.isMatrixContact) + { // check pending requests NSMutableArray* pids = [[NSMutableArray alloc] init]; NSMutableArray* medias = [[NSMutableArray alloc] init]; - for(MXCEmail* email in contact.emailAddresses) { - if (([pending3PIDs indexOfObject:email.emailAddress] == NSNotFound) && ([checked3PIDs indexOfObject:email.emailAddress] == NSNotFound)) { + for(MXCEmail* email in contact.emailAddresses) + { + if (([pending3PIDs indexOfObject:email.emailAddress] == NSNotFound) && ([checked3PIDs indexOfObject:email.emailAddress] == NSNotFound)) + { [pids addObject:email.emailAddress]; [medias addObject:@"email"]; } } - - if (pids.count > 0) { + + if (pids.count > 0) + { [pending3PIDs addObjectsFromArray:pids]; - if (self.mxRestClient) { + if (self.mxRestClient) + { [self.mxRestClient lookup3pids:pids forMedia:medias - success:^(NSArray *userIds) { - // sanity check - if (userIds.count == pids.count) { - - // update statuses table - [checked3PIDs addObjectsFromArray:pids]; - for(NSString* pid in pids) { - [pending3PIDs removeObject:pid]; - } - - BOOL isUpdated = NO; - NSMutableArray* matrixContactsToRemove = [[NSMutableArray alloc] init]; - - // apply updates - if (pids.count > 0) { - for(int index = 0; index < pids.count; index++) { - NSString* matrixID = [userIds objectAtIndex:index]; - NSString* pid = [pids objectAtIndex:index]; - - // the dict is created on demand - if (!matrixIDBy3PID) { - [self loadMatrixIDsDict]; - } - - id currentMatrixID = [matrixIDBy3PID valueForKey:pid]; - - // do not keep useless info - if ([matrixID isKindOfClass:[NSString class]]) { - - // do not update if not required - if (![currentMatrixID isKindOfClass:[NSString class]] || ![(NSString*)currentMatrixID isEqualToString:matrixID]) { - [matrixIDBy3PID setValue:matrixID forKey:pid]; - isUpdated = YES; - } - - } else { - if (currentMatrixID) { - [matrixIDBy3PID removeObjectForKey:pid]; - isUpdated = YES; - } - } - - // is there a matrix contact with the same - if ([matrixContactByMatrixUserID objectForKey:matrixID]) { - [matrixContactsToRemove addObject:[matrixContactByMatrixUserID objectForKey:matrixID]]; - } - } - - if (isUpdated) { - [self saveMatrixIDsDict]; - } - } - - // some matrix contacts will be replaced by this contact - if (matrixContactsToRemove.count > 0) { - [self updateContactMatrixIDs:contact]; - - for(MXCContact* contactToRemove in matrixContactsToRemove) { - [self.contacts removeObject:contactToRemove]; - } - - // warn there is a global refresh - [[NSNotificationCenter defaultCenter] postNotificationName:kContactManagerContactsListRefreshNotification object:nil userInfo:nil]; - } else { - // update only this contact - [self updateContactMatrixIDs:contact]; - } - } - } - failure:^(NSError *error) { - // try later - dispatch_after(dispatch_walltime(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ - [self refreshContactMatrixIDs:contact]; - }); - }]; + success:^(NSArray *userIds) + { + // sanity check + if (userIds.count == pids.count) + { + + // update statuses table + [checked3PIDs addObjectsFromArray:pids]; + for(NSString* pid in pids) + { + [pending3PIDs removeObject:pid]; + } + + BOOL isUpdated = NO; + NSMutableArray* matrixContactsToRemove = [[NSMutableArray alloc] init]; + + // apply updates + if (pids.count > 0) + { + for(int index = 0; index < pids.count; index++) + { + NSString* matrixID = [userIds objectAtIndex:index]; + NSString* pid = [pids objectAtIndex:index]; + + // the dict is created on demand + if (!matrixIDBy3PID) + { + [self loadMatrixIDsDict]; + } + + id currentMatrixID = [matrixIDBy3PID valueForKey:pid]; + + // do not keep useless info + if ([matrixID isKindOfClass:[NSString class]]) + { + + // do not update if not required + if (![currentMatrixID isKindOfClass:[NSString class]] || ![(NSString*)currentMatrixID isEqualToString:matrixID]) + { + [matrixIDBy3PID setValue:matrixID forKey:pid]; + isUpdated = YES; + } + + } + else + { + if (currentMatrixID) + { + [matrixIDBy3PID removeObjectForKey:pid]; + isUpdated = YES; + } + } + + // is there a matrix contact with the same + if ([matrixContactByMatrixUserID objectForKey:matrixID]) + { + [matrixContactsToRemove addObject:[matrixContactByMatrixUserID objectForKey:matrixID]]; + } + } + + if (isUpdated) + { + [self saveMatrixIDsDict]; + } + } + + // some matrix contacts will be replaced by this contact + if (matrixContactsToRemove.count > 0) + { + [self updateContactMatrixIDs:contact]; + + for(MXCContact* contactToRemove in matrixContactsToRemove) + { + [self.contacts removeObject:contactToRemove]; + } + + // warn there is a global refresh + [[NSNotificationCenter defaultCenter] postNotificationName:kContactManagerContactsListRefreshNotification object:nil userInfo:nil]; + } + else + { + // update only this contact + [self updateContactMatrixIDs:contact]; + } + } + } + failure:^(NSError *error) + { + // try later + dispatch_after(dispatch_walltime(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ + [self refreshContactMatrixIDs:contact]; + }); + }]; } - else { + else + { dispatch_after(dispatch_walltime(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [self refreshContactMatrixIDs:contact]; }); @@ -560,15 +647,18 @@ static ContactManager* sharedContactManager = nil; #pragma mark - KVO -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - if ([@"syncLocalContacts" isEqualToString:keyPath]) { +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + if ([@"syncLocalContacts" isEqualToString:keyPath]) + { dispatch_async(dispatch_get_main_queue(), ^{ [self fullRefresh]; }); } } -- (SectionedContacts *)getSectionedContacts:(NSArray*)contactsList { +- (SectionedContacts *)getSectionedContacts:(NSArray*)contactsList +{ UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; int indexOffset = 0; @@ -578,7 +668,8 @@ static ContactManager* sharedContactManager = nil; sectionTitlesCount += indexOffset; - for (index = 0; index < sectionTitlesCount; index++) { + for (index = 0; index < sectionTitlesCount; index++) + { NSMutableArray *array = [[NSMutableArray alloc] init]; [tmpSectionsArray addObject:array]; } @@ -586,6 +677,7 @@ static ContactManager* sharedContactManager = nil; int contactsCount = 0; for (MXCContact *aContact in contactsList) + { NSInteger section = [collation sectionForObject:aContact collationStringSelector:@selector(displayName)] + indexOffset; @@ -596,11 +688,13 @@ static ContactManager* sharedContactManager = nil; NSMutableArray *tmpSectionedContactsTitle = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount]; NSMutableArray *shortSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount]; - for (index = indexOffset; index < sectionTitlesCount; index++) { + for (index = indexOffset; index < sectionTitlesCount; index++) + { NSMutableArray *usersArrayForSection = [tmpSectionsArray objectAtIndex:index]; - if ([usersArrayForSection count] != 0) { + if ([usersArrayForSection count] != 0) + { NSArray* sortedUsersArrayForSection = [collation sortedArrayFromArray:usersArrayForSection collationStringSelector:@selector(displayName)]; [shortSectionsArray addObject:sortedUsersArrayForSection]; [tmpSectionedContactsTitle addObject:[[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:(index - indexOffset)]]; @@ -623,6 +717,7 @@ static NSString *contactsBookInfoFile = @"contacts"; NSString *dataFilePath = [documentsDirectory stringByAppendingPathComponent:matrixIDsDictFile]; if (matrixIDBy3PID) + { NSMutableData *theData = [NSMutableData data]; NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:theData]; @@ -633,6 +728,7 @@ static NSString *contactsBookInfoFile = @"contacts"; [theData writeToFile:dataFilePath atomically:YES]; } else + { NSFileManager *fileManager = [[NSFileManager alloc] init]; [fileManager removeItemAtPath:dataFilePath error:nil]; @@ -648,35 +744,42 @@ static NSString *contactsBookInfoFile = @"contacts"; NSFileManager *fileManager = [[NSFileManager alloc] init]; if ([fileManager fileExistsAtPath:dataFilePath]) + { // the file content could be corrupted - @try { + @try + { NSData* filecontent = [NSData dataWithContentsOfFile:dataFilePath options:(NSDataReadingMappedAlways | NSDataReadingUncached) error:nil]; NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:filecontent]; id object = [decoder decodeObjectForKey:@"matrixIDsDict"]; - if ([object isKindOfClass:[NSDictionary class]]) { + if ([object isKindOfClass:[NSDictionary class]]) + { matrixIDBy3PID = [object mutableCopy]; } - + [decoder finishDecoding]; - } @catch (NSException *exception) { + } @catch (NSException *exception) + { } } - - if (!matrixIDBy3PID) { + + if (!matrixIDBy3PID) + { matrixIDBy3PID = [[NSMutableDictionary alloc] init]; } } -- (void) saveDeviceContacts { +- (void) saveDeviceContacts +{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *dataFilePath = [documentsDirectory stringByAppendingPathComponent:localContactsFile]; if (deviceContactByContactID && (deviceContactByContactID.count > 0)) + { NSMutableData *theData = [NSMutableData data]; NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:theData]; @@ -688,13 +791,15 @@ static NSString *contactsBookInfoFile = @"contacts"; [theData writeToFile:dataFilePath atomically:YES]; } else + { NSFileManager *fileManager = [[NSFileManager alloc] init]; [fileManager removeItemAtPath:dataFilePath error:nil]; } } -- (void) loadDeviceContacts { +- (void)loadDeviceContacts +{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *dataFilePath = [documentsDirectory stringByAppendingPathComponent:localContactsFile]; @@ -702,36 +807,43 @@ static NSString *contactsBookInfoFile = @"contacts"; NSFileManager *fileManager = [[NSFileManager alloc] init]; if ([fileManager fileExistsAtPath:dataFilePath]) + { // the file content could be corrupted - @try { + @try + { NSData* filecontent = [NSData dataWithContentsOfFile:dataFilePath options:(NSDataReadingMappedAlways | NSDataReadingUncached) error:nil]; NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:filecontent]; id object = [decoder decodeObjectForKey:@"deviceContactByContactID"]; - if ([object isKindOfClass:[NSDictionary class]]) { + if ([object isKindOfClass:[NSDictionary class]]) + { deviceContactByContactID = [object mutableCopy]; } [decoder finishDecoding]; - } @catch (NSException *exception) { + } @catch (NSException *exception) + { lastSyncDate = nil; } } - if (!deviceContactByContactID) { + if (!deviceContactByContactID) + { deviceContactByContactID = [[NSMutableDictionary alloc] init]; } } -- (void) saveContactBookInfo { +- (void)saveContactBookInfo +{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *dataFilePath = [documentsDirectory stringByAppendingPathComponent:contactsBookInfoFile]; if (lastSyncDate) + { NSMutableData *theData = [NSMutableData data]; NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:theData]; @@ -743,13 +855,15 @@ static NSString *contactsBookInfoFile = @"contacts"; [theData writeToFile:dataFilePath atomically:YES]; } else + { NSFileManager *fileManager = [[NSFileManager alloc] init]; [fileManager removeItemAtPath:dataFilePath error:nil]; } } -- (void) loadContactBookInfo { +- (void)loadContactBookInfo +{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *dataFilePath = [documentsDirectory stringByAppendingPathComponent:contactsBookInfoFile]; @@ -757,9 +871,11 @@ static NSString *contactsBookInfoFile = @"contacts"; NSFileManager *fileManager = [[NSFileManager alloc] init]; if ([fileManager fileExistsAtPath:dataFilePath]) + { // the file content could be corrupted - @try { + @try + { NSData* filecontent = [NSData dataWithContentsOfFile:dataFilePath options:(NSDataReadingMappedAlways | NSDataReadingUncached) error:nil]; NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:filecontent]; @@ -767,7 +883,8 @@ static NSString *contactsBookInfoFile = @"contacts"; lastSyncDate = [decoder decodeObjectForKey:@"lastSyncDate"]; [decoder finishDecoding]; - } @catch (NSException *exception) { + } @catch (NSException *exception) + { lastSyncDate = nil; } }