Moved the folder metadata generation to SoObject and added 'tag based' sync'ing support for IMAP

pull/17/head
Ludovic Marcotte 2014-01-10 14:06:53 -05:00
parent 13721b961b
commit a4a3a735b4
9 changed files with 279 additions and 162 deletions

View File

@ -92,6 +92,10 @@ Mailer_RESOURCE_FILES += \
SOGoMailWelshReply.wo
Mailer_LANGUAGES = English French
Mailer_LOCALIZED_RESOURCE_FILES = Localizable.strings
ADDITIONAL_INCLUDE_DIRS += -I../../SOPE/
ADDITIONAL_INCLUDE_DIRS += $(shell xml2-config --cflags)
ADDITIONAL_LIB_DIRS += -L../../SOPE/GDLContentStore/obj/

View File

@ -80,10 +80,11 @@ typedef enum {
- (NSArray *) toManyRelationshipKeysWithNamespaces: (BOOL) withNSs;
- (NSArray *) allFolderPaths;
- (NSArray *) allFoldersMetadata;
- (BOOL) isInDraftsFolder;
/* special folders */
- (NSString *) inboxFolderNameInContext: (id)_ctx;
- (NSString *) draftsFolderNameInContext: (id)_ctx;
- (NSString *) sentFolderNameInContext: (id)_ctx;

View File

@ -21,6 +21,7 @@
*/
#import <Foundation/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSURL.h>
#import <Foundation/NSString.h>
@ -47,6 +48,7 @@
#import <SOGo/SOGoUser.h>
#import <SOGo/SOGoUserDefaults.h>
#import <SOGo/SOGoUserSettings.h>
#import <SOGo/SOGoUserManager.h>
#import <SOGo/SOGoSieveManager.h>
#import "SOGoDraftsFolder.h"
@ -339,6 +341,9 @@ static NSString *inboxFolderName = @"INBOX";
return folders;
}
//
//
//
- (NSArray *) allFolderPaths
{
NSMutableArray *folderPaths, *namespaces;
@ -382,8 +387,128 @@ static NSString *inboxFolderName = @"INBOX";
return folderPaths;
}
/* IMAP4 */
//
//
//
- (NSString *) _folderType: (NSString *) folderName
{
NSString *folderType;
if ([folderName isEqualToString: [NSString stringWithFormat: @"/%@", inboxFolderName]])
folderType = @"inbox";
else if ([folderName isEqualToString: [NSString stringWithFormat: @"/%@", [self draftsFolderNameInContext: context]]])
folderType = @"draft";
else if ([folderName isEqualToString: [NSString stringWithFormat: @"/%@", [self sentFolderNameInContext: context]]])
folderType = @"sent";
else if ([folderName isEqualToString: [NSString stringWithFormat: @"/%@", [self trashFolderNameInContext: context]]])
folderType = @"trash";
else
folderType = @"folder";
return folderType;
}
- (NSString *) _parentForFolder: (NSString *) folderName
foldersList: (NSArray *) theFolders
{
NSArray *pathComponents;
NSString *s;
int i;
pathComponents = [folderName pathComponents];
s = [[[pathComponents subarrayWithRange: NSMakeRange(0,[pathComponents count]-1)] componentsJoinedByString: @"/"] substringFromIndex: 1];
for (i = 0; i < [theFolders count]; i++)
{
if ([s isEqualToString: [theFolders objectAtIndex: i]])
return s;
}
return nil;
}
//
//
//
- (NSArray *) allFoldersMetadata
{
NSString *currentFolder, *currentDecodedFolder, *currentDisplayName, *currentFolderType, *login, *fullName, *parent;
NSMutableArray *pathComponents, *folders;
SOGoUserManager *userManager;
NSEnumerator *rawFolders;
NSDictionary *folderData;
NSAutoreleasePool *pool;
NSArray *allFolderPaths;
allFolderPaths = [self allFolderPaths];
rawFolders = [allFolderPaths objectEnumerator];
folders = [NSMutableArray array];
while ((currentFolder = [rawFolders nextObject]))
{
// Using a local pool to avoid using too many file descriptors. This could
// happen with tons of mailboxes under "Other Users" as LDAP connections
// are never reused and "autoreleased" at the end. This loop would consume
// lots of LDAP connections during its execution.
pool = [[NSAutoreleasePool alloc] init];
currentDecodedFolder = [currentFolder stringByDecodingImap4FolderName];
currentFolderType = [self _folderType: currentFolder];
// We translate the "Other Users" and "Shared Folders" namespaces.
// While we're at it, we also translate the user's mailbox names
// to the full name of the person.
if (otherUsersFolderName && [currentDecodedFolder hasPrefix: [NSString stringWithFormat: @"/%@", otherUsersFolderName]])
{
// We have a string like /Other Users/lmarcotte/... under Cyrus, but we could
// also have something like /shared under Dovecot. So we swap the username only
// if we have one, of course.
pathComponents = [NSMutableArray arrayWithArray: [currentDecodedFolder pathComponents]];
if ([pathComponents count] > 2)
{
login = [pathComponents objectAtIndex: 2];
userManager = [SOGoUserManager sharedUserManager];
fullName = [userManager getCNForUID: login];
[pathComponents removeObjectsInRange: NSMakeRange(0,3)];
currentDisplayName = [NSString stringWithFormat: @"/%@/%@/%@",
[self labelForKey: @"OtherUsersFolderName"],
(fullName != nil ? fullName : login),
[pathComponents componentsJoinedByString: @"/"]];
}
else
{
currentDisplayName = [NSString stringWithFormat: @"/%@%@",
[self labelForKey: @"OtherUsersFolderName"],
[currentDecodedFolder substringFromIndex:
[otherUsersFolderName length]+1]];
}
}
else if (sharedFoldersName && [currentDecodedFolder hasPrefix: [NSString stringWithFormat: @"/%@", sharedFoldersName]])
currentDisplayName = [NSString stringWithFormat: @"/%@%@", [self labelForKey: @"SharedFoldersName"],
[currentDecodedFolder substringFromIndex: [sharedFoldersName length]+1]];
else
currentDisplayName = currentDecodedFolder;
parent = [self _parentForFolder: currentFolder foldersList: allFolderPaths];
folderData = [NSDictionary dictionaryWithObjectsAndKeys:
currentFolder, @"path",
currentFolderType, @"type",
currentDisplayName, @"displayName",
parent, @"parent",
nil];
[folders addObject: folderData];
[pool release];
}
return folders;
}
/* IMAP4 */
- (NSDictionary *) _mailAccount
{
NSDictionary *mailAccount;

View File

@ -94,6 +94,8 @@
- (NSCalendarDate *) mostRecentMessageDate;
- (NSString *) davCollectionTag;
/* flags */
- (NSException *) addFlagsToAllMessages: (id) _f;

View File

@ -24,6 +24,7 @@
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSURL.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSTask.h>
#import <NGObjWeb/NSException+HTTP.h>
@ -1924,6 +1925,145 @@ static NSString *defaultUserID = @"anyone";
return date;
}
- (NSString *) davCollectionTag
{
NSString *tag;
tag = @"-1";
if ([self imap4Connection])
{
NSString *folderName;
NSDictionary *result;
folderName = [imap4 imap4FolderNameForURL: [self imap4URL]];
[[imap4 client] unselect];
result = [[imap4 client] select: folderName];
tag = [NSString stringWithFormat: @"%@-%@", [result objectForKey: @"uidnext"], [result objectForKey: @"highestmodseq"]];
}
return tag;
}
//
// FIXME - see below for code refactoring with MAPIStoreMailFolder.
//
- (EOQualifier *) _nonDeletedQualifier
{
static EOQualifier *nonDeletedQualifier = nil;
EOQualifier *deletedQualifier;
if (!nonDeletedQualifier)
{
deletedQualifier
= [[EOKeyValueQualifier alloc]
initWithKey: @"FLAGS"
operatorSelector: EOQualifierOperatorContains
value: [NSArray arrayWithObject: @"Deleted"]];
nonDeletedQualifier = [[EONotQualifier alloc]
initWithQualifier: deletedQualifier];
[deletedQualifier release];
}
return nonDeletedQualifier;
}
//
// Check updated items
//
//
//
// . uid fetch 1:* (FLAGS) (changedsince 171)
//
// Deleted: "UID FETCH 1:* (UID) (CHANGEDSINCE 171 VANISHED)"
// fetchUIDsOfVanishedItems ..
//
// . uid fetch 1:* (FLAGS) (changedsince 176 vanished)
// * VANISHED (EARLIER) 36
//
//
// FIXME: refactor MAPIStoreMailFolder.m - synchroniseCache to use this method
//
- (NSArray *) syncTokenFieldsWithProperties: (NSArray *) theProperties
matchingSyncToken: (NSString *) theSyncToken
{
EOQualifier *searchQualifier;
NSMutableArray *allTokens;
NSArray *a, *uids;
NSDictionary *d;
int uidnext, highestmodseq, i;
allTokens = [NSMutableArray array];
a = [theSyncToken componentsSeparatedByString: @"-"];
uidnext = [[a objectAtIndex: 0] intValue];
highestmodseq = [[a objectAtIndex: 1] intValue];
// We first make sure QRESYNC is enabled
[[self imap4Connection] enableExtensions: [NSArray arrayWithObject: @"QRESYNC"]];
// We fetch new messages and modified messages
if (highestmodseq)
{
EOKeyValueQualifier *kvQualifier;
NSNumber *nextModseq;
nextModseq = [NSNumber numberWithUnsignedLongLong: highestmodseq + 1];
kvQualifier = [[EOKeyValueQualifier alloc]
initWithKey: @"modseq"
operatorSelector: EOQualifierOperatorGreaterThanOrEqualTo
value: nextModseq];
searchQualifier = [[EOAndQualifier alloc]
initWithQualifiers:
kvQualifier, [self _nonDeletedQualifier], nil];
[kvQualifier release];
[searchQualifier autorelease];
}
else
{
searchQualifier = [self _nonDeletedQualifier];
}
// we fetch modified or added uids
uids = [self fetchUIDsMatchingQualifier: searchQualifier
sortOrdering: nil];
for (i = 0; i < [uids count]; i++)
{
// New messages
if ([[uids objectAtIndex: i] intValue] >= uidnext)
{
d = [NSDictionary dictionaryWithObject: @"added" forKey: [uids objectAtIndex: i]];
}
// Changed messages
else
{
d = [NSDictionary dictionaryWithObject: @"changed" forKey: [uids objectAtIndex: i]];
}
[allTokens addObject: d];
}
// We fetch deleted ones
uids = [self fetchUIDsOfVanishedItems: highestmodseq];
for (i = 0; i < [uids count]; i++)
{
d = [NSDictionary dictionaryWithObject: @"deleted" forKey: [uids objectAtIndex: i]];
[allTokens addObject: d];
}
return allTokens;
}
@end /* SOGoMailFolder */
@implementation SOGoSpecialMailFolder

View File

@ -81,6 +81,8 @@ NSArray *SOGoMailCoreInfoKeys;
- (id) bodyStructure;
- (id) lookupInfoForBodyPart:(id)_path;
- (id) lookupImap4BodyPartKey: (NSString *) _key
inContext: (id) _ctx;
/* content */

View File

@ -1,8 +1,6 @@
/* UIxMailAccountActions.h - this file is part of SOGo
*
* Copyright (C) 2007-2011 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
* Copyright (C) 2007-2013 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -29,12 +27,6 @@
@interface UIxMailAccountActions : WODirectAction
{
NSString *inboxFolderName;
NSString *draftsFolderName;
NSString *sentFolderName;
NSString *trashFolderName;
NSString *otherUsersFolderName;
NSString *sharedFoldersName;
}
- (WOResponse *) listMailboxesAction;

View File

@ -2,9 +2,6 @@
*
* Copyright (C) 2007-2013 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
* Ludovic Marcotte <lmarcotte@inverse.ca>
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
@ -51,159 +48,16 @@
@implementation UIxMailAccountActions
- (id) init
{
if ((self = [super init]))
{
inboxFolderName = nil;
draftsFolderName = nil;
sentFolderName = nil;
trashFolderName = nil;
otherUsersFolderName = nil;
sharedFoldersName = nil;
}
return self;
}
- (void) dealloc
{
[inboxFolderName release];
[draftsFolderName release];
[sentFolderName release];
[trashFolderName release];
[otherUsersFolderName release];
[sharedFoldersName release];
[super dealloc];
}
- (NSString *) _folderType: (NSString *) folderName
{
NSString *folderType;
SOGoMailAccount *co;
NSArray *specialFolders;
if (!inboxFolderName)
{
co = [self clientObject];
specialFolders = [[NSArray arrayWithObjects:
[co inboxFolderNameInContext: context],
[co draftsFolderNameInContext: context],
[co sentFolderNameInContext: context],
[co trashFolderNameInContext: context],
[co otherUsersFolderNameInContext: context],
[co sharedFoldersNameInContext: context],
nil] stringsWithFormat: @"/%@"];
ASSIGN(inboxFolderName, [specialFolders objectAtIndex: 0]);
ASSIGN(draftsFolderName, [specialFolders objectAtIndex: 1]);
ASSIGN(sentFolderName, [specialFolders objectAtIndex: 2]);
ASSIGN(trashFolderName, [specialFolders objectAtIndex: 3]);
if ([specialFolders count] > 4)
ASSIGN(otherUsersFolderName, [specialFolders objectAtIndex: 4]);
if ([specialFolders count] > 5)
ASSIGN(sharedFoldersName, [specialFolders objectAtIndex: 5]);
}
if ([folderName isEqualToString: inboxFolderName])
folderType = @"inbox";
else if ([folderName isEqualToString: draftsFolderName])
folderType = @"draft";
else if ([folderName isEqualToString: sentFolderName])
folderType = @"sent";
else if ([folderName isEqualToString: trashFolderName])
folderType = @"trash";
else if ([folderName hasPrefix: [NSString stringWithFormat: @"%@/", draftsFolderName]])
folderType = @"draft/folder";
else if ([folderName hasPrefix: [NSString stringWithFormat: @"%@/", sentFolderName]])
folderType = @"sent/folder";
else
folderType = @"folder";
return folderType;
}
- (NSArray *) _jsonFolders: (NSEnumerator *) rawFolders
{
NSString *currentFolder, *currentDecodedFolder, *currentDisplayName, *currentFolderType, *login, *fullName;
NSMutableArray *pathComponents;
SOGoUserManager *userManager;
NSDictionary *folderData;
NSMutableArray *folders;
NSAutoreleasePool *pool;
folders = [NSMutableArray array];
while ((currentFolder = [rawFolders nextObject]))
{
// Using a local pool to avoid using too many file descriptors. This could
// happen with tons of mailboxes under "Other Users" as LDAP connections
// are never reused and "autoreleased" at the end. This loop would consume
// lots of LDAP connections during its execution.
pool = [[NSAutoreleasePool alloc] init];
currentDecodedFolder = [currentFolder stringByDecodingImap4FolderName];
currentFolderType = [self _folderType: currentFolder];
// We translate the "Other Users" and "Shared Folders" namespaces.
// While we're at it, we also translate the user's mailbox names
// to the full name of the person.
if (otherUsersFolderName && [currentDecodedFolder hasPrefix: otherUsersFolderName])
{
// We have a string like /Other Users/lmarcotte/... under Cyrus, but we could
// also have something like /shared under Dovecot. So we swap the username only
// if we have one, of course.
pathComponents = [NSMutableArray arrayWithArray: [currentDecodedFolder pathComponents]];
if ([pathComponents count] > 2)
{
login = [pathComponents objectAtIndex: 2];
userManager = [SOGoUserManager sharedUserManager];
fullName = [userManager getCNForUID: login];
[pathComponents removeObjectsInRange: NSMakeRange(0,3)];
currentDisplayName = [NSString stringWithFormat: @"/%@/%@/%@",
[self labelForKey: @"OtherUsersFolderName"],
(fullName != nil ? fullName : login),
[pathComponents componentsJoinedByString: @"/"]];
}
else
{
currentDisplayName = [NSString stringWithFormat: @"/%@%@",
[self labelForKey: @"OtherUsersFolderName"],
[currentDecodedFolder substringFromIndex:
[otherUsersFolderName length]]];
}
}
else if (sharedFoldersName && [currentDecodedFolder hasPrefix: sharedFoldersName])
currentDisplayName = [NSString stringWithFormat: @"/%@%@", [self labelForKey: @"SharedFoldersName"],
[currentDecodedFolder substringFromIndex: [sharedFoldersName length]]];
else
currentDisplayName = currentDecodedFolder;
folderData = [NSDictionary dictionaryWithObjectsAndKeys:
currentFolder, @"path",
currentFolderType, @"type",
currentDisplayName, @"displayName",
nil];
[folders addObject: folderData];
[pool release];
}
return folders;
}
- (WOResponse *) listMailboxesAction
{
SOGoMailAccount *co;
NSEnumerator *rawFolders;
NSArray *folders;
NSDictionary *data;
WOResponse *response;
co = [self clientObject];
rawFolders = [[co allFolderPaths] objectEnumerator];
folders = [self _jsonFolders: rawFolders];
folders = [co allFoldersMetadata];
data = [NSDictionary dictionaryWithObjectsAndKeys: folders, @"mailboxes", nil];
response = [self responseWithStatus: 200

View File

@ -1,9 +1,6 @@
/* UIxMailFolderActions.m - this file is part of SOGo
*
* Copyright (C) 2007-2011 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
* Francis Lachapelle <flachapelle@inverse.ca>
* Copyright (C) 2007-2013 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by