From 7e0cddc06bc520c8eefe1b9190e92443853a15bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20J=2E=20Hern=C3=A1ndez=20Blasco?= Date: Tue, 7 Oct 2014 00:51:45 +0200 Subject: [PATCH] oc-mailfolder: Avoid setting seen flag on preloading message bodies Fetching a body[text] property using IMAP makes IMAP server set seen flag. This commit fetches the flag beforehand to restore the previous state once the body has been fetched. --- OpenChange/MAPIStoreMailFolder.m | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/OpenChange/MAPIStoreMailFolder.m b/OpenChange/MAPIStoreMailFolder.m index 585b684b4..40857d39c 100644 --- a/OpenChange/MAPIStoreMailFolder.m +++ b/OpenChange/MAPIStoreMailFolder.m @@ -1274,8 +1274,10 @@ _parseCOPYUID (NSString *line, NSArray **destUIDsP) NSUInteger count, max; NSString *messageKey, *messageUid, *bodyPartKey; NGImap4Client *client; - NSArray *fetch; + NSArray *fetch, *flags; NSData *bodyContent; + BOOL unseen; + NSMutableArray *unseenUIDs; if (tableType == MAPISTORE_MESSAGE_TABLE) { @@ -1305,6 +1307,25 @@ _parseCOPYUID (NSString *line, NSArray **destUIDsP) client = [[(SOGoMailFolder *) sogoObject imap4Connection] client]; [client select: [sogoObject absoluteImap4Name]]; + + /* Fetch flags to remove seen flag if required, + as fetching a message body set the seen flag */ + response = [client fetchUids: [keyAssoc allKeys] + parts: [NSArray arrayWithObjects: @"flags", nil]]; + fetch = [response objectForKey: @"fetch"]; + max = [fetch count]; + unseenUIDs = [NSMutableArray arrayWithCapacity: max]; + for (count = 0; count < max; count++) + { + response = [fetch objectAtIndex: count]; + messageUid = [[response objectForKey: @"uid"] stringValue]; + flags = [response objectForKey: @"flags"]; + unseen = [flags indexOfObject: @"seen"] == NSNotFound; + if (unseen) { + [unseenUIDs addObject: messageUid]; + } + } + response = [client fetchUids: [keyAssoc allKeys] parts: [bodyPartKeys allObjects]]; fetch = [response objectForKey: @"fetch"]; @@ -1326,6 +1347,14 @@ _parseCOPYUID (NSString *line, NSArray **destUIDsP) } } } + + // Restore unseen state once the body has been fetched + if ([unseenUIDs count] > 0) + { + response = [client storeFlags: [NSArray arrayWithObjects: @"seen", nil] + forUIDs: unseenUIDs + addOrRemove: NO]; + } } }