diff --git a/ActiveSync/SOGoActiveSyncDispatcher.m b/ActiveSync/SOGoActiveSyncDispatcher.m index e216a32f7..a40af26f7 100644 --- a/ActiveSync/SOGoActiveSyncDispatcher.m +++ b/ActiveSync/SOGoActiveSyncDispatcher.m @@ -829,7 +829,7 @@ void handle_eas_terminate(int signum) [o setObjectType: ActiveSyncFolderCacheObject]; [o setTableUrl: folderTableURL]; - foldersInCache = [o cacheEntriesForDeviceId: [context objectForKey: @"DeviceId"] newerThanVersion: -1]; + foldersInCache = [o cacheEntriesForDeviceId: [context objectForKey: @"DeviceId"] newerThanVersion: -1]; // get guids of folders stored in cache for (i = 0; i < [foldersInCache count]; i++) @@ -2893,6 +2893,7 @@ void handle_eas_terminate(int signum) inResponse: (WOResponse *) theResponse { NSString *folderId, *realCollectionId, *itemId; + NSMutableArray *folderIdentifiers; SOGoMailAccounts *accountsFolder; SOGoMailAccount *accountFolder; SOGoMailFolder *currentFolder; @@ -2904,19 +2905,7 @@ void handle_eas_terminate(int signum) NSData *d; SOGoMicrosoftActiveSyncFolderType folderType; - int i, total; - - // FIXME: support more than one CollectionId tag + DeepTraversal - folderId = [[(id)[[(id)[theDocumentElement getElementsByTagName: @"Query"] lastObject] getElementsByTagName: @"CollectionId"] lastObject] textValue]; - realCollectionId = [folderId realCollectionIdWithFolderType: &folderType]; - realCollectionId = [self globallyUniqueIDToIMAPFolderName: realCollectionId type: folderType]; - - userFolder = [[context activeUser] homeFolderInContext: context]; - accountsFolder = [userFolder lookupName: @"Mail" inContext: context acquire: NO]; - accountFolder = [accountsFolder lookupName: @"0" inContext: context acquire: NO]; - currentFolder = [accountFolder lookupName: [NSString stringWithFormat: @"folder%@", realCollectionId] - inContext: context - acquire: NO]; + int i, j, total; // We build the qualifier and we launch our search operation qualifier = [self _qualifierFromMailboxSearchQuery: [(id)[theDocumentElement getElementsByTagName: @"Query"] lastObject]]; @@ -2927,13 +2916,45 @@ void handle_eas_terminate(int signum) return; } - sortedUIDs = [currentFolder fetchUIDsMatchingQualifier: qualifier - sortOrdering: @"REVERSE ARRIVAL" - threaded: NO]; + // FIXME: support more than one CollectionId tag + DeepTraversal + folderId = [[(id)[[(id)[theDocumentElement getElementsByTagName: @"Query"] lastObject] getElementsByTagName: @"CollectionId"] lastObject] textValue]; + folderIdentifiers = [NSMutableArray array]; + + // Android 6 will send search requests with no collection ID - so we search in all folders. + if (!folderId) + { + NSArray *foldersInCache; + SOGoCacheGCSObject *o; + NSString *prefix; + + o = [SOGoCacheGCSObject objectWithName: @"0" inContainer: nil]; + [o setObjectType: ActiveSyncFolderCacheObject]; + [o setTableUrl: folderTableURL]; + + foldersInCache = [o cacheEntriesForDeviceId: [context objectForKey: @"DeviceId"] newerThanVersion: -1]; + prefix = [NSString stringWithFormat: @"/%@+folder", [context objectForKey: @"DeviceId"]]; + + for (i = 0; i < [foldersInCache count]; i++) + { + folderId = [foldersInCache objectAtIndex: i]; + if ([folderId hasPrefix: prefix]) + { + folderId = [NSString stringWithFormat: @"mail/%@", [folderId substringFromIndex: [prefix length]]]; + [folderIdentifiers addObject: folderId]; + } + } + } + else + { + [folderIdentifiers addObject: folderId]; + } + + userFolder = [[context activeUser] homeFolderInContext: context]; + accountsFolder = [userFolder lookupName: @"Mail" inContext: context acquire: NO]; + accountFolder = [accountsFolder lookupName: @"0" inContext: context acquire: NO]; // Prepare the response s = [NSMutableString string]; - [s appendString: @""]; [s appendString: @""]; [s appendString: @""]; @@ -2942,22 +2963,36 @@ void handle_eas_terminate(int signum) [s appendFormat: @""]; [s appendFormat: @"1"]; - total = [sortedUIDs count]; - for (i = 0; i < total; i++) + for (i = 0; i < [folderIdentifiers count]; i++) { - itemId = [[sortedUIDs objectAtIndex: i] stringValue]; - mailObject = [currentFolder lookupName: itemId inContext: context acquire: NO]; + folderId = [folderIdentifiers objectAtIndex: i]; + realCollectionId = [folderId realCollectionIdWithFolderType: &folderType]; + realCollectionId = [self globallyUniqueIDToIMAPFolderName: realCollectionId type: folderType]; - if ([mailObject isKindOfClass: [NSException class]]) - continue; + currentFolder = [accountFolder lookupName: [NSString stringWithFormat: @"folder%@", realCollectionId] + inContext: context + acquire: NO]; - [s appendString: @""]; - [s appendFormat: @"%@+%@", folderId, itemId]; - [s appendFormat: @"%@", folderId]; - [s appendString: @""]; - [s appendFormat: [mailObject activeSyncRepresentationInContext: context]]; - [s appendString: @""]; - [s appendFormat: @""]; + sortedUIDs = [currentFolder fetchUIDsMatchingQualifier: qualifier + sortOrdering: @"REVERSE ARRIVAL" + threaded: NO]; + total = [sortedUIDs count]; + for (j = 0; j < total; j++) + { + itemId = [[sortedUIDs objectAtIndex: j] stringValue]; + mailObject = [currentFolder lookupName: itemId inContext: context acquire: NO]; + + if ([mailObject isKindOfClass: [NSException class]]) + continue; + + [s appendString: @""]; + [s appendFormat: @"%@+%@", folderId, itemId]; + [s appendFormat: @"%@", folderId]; + [s appendString: @""]; + [s appendFormat: [mailObject activeSyncRepresentationInContext: context]]; + [s appendString: @""]; + [s appendFormat: @""]; + } } [s appendFormat: @"0-%d",(total ? total-1 : 0)];