From b0cc524f3a7bc6364bca71853521e4d0e22a57ad Mon Sep 17 00:00:00 2001 From: helge Date: Sun, 26 Sep 2004 23:44:59 +0000 Subject: [PATCH] added some basic IMAP4 folder support git-svn-id: http://svn.opengroupware.org/SOGo/trunk@321 d1b88da0-ebda-0310-925b-ed51d893ca5b --- SOGo/SoObjects/Mailer/ChangeLog | 8 +++ SOGo/SoObjects/Mailer/SOGoMailAccount.m | 7 +++ SOGo/SoObjects/Mailer/SOGoMailBaseObject.h | 3 ++ SOGo/SoObjects/Mailer/SOGoMailBaseObject.m | 63 +++++++++++++++++++++- SOGo/SoObjects/Mailer/SOGoMailFolder.m | 55 +++++++++++++++++++ SOGo/SoObjects/Mailer/Version | 2 +- SOGo/SoObjects/Mailer/common.h | 1 + 7 files changed, 136 insertions(+), 3 deletions(-) diff --git a/SOGo/SoObjects/Mailer/ChangeLog b/SOGo/SoObjects/Mailer/ChangeLog index a499dfd9..89e3cd50 100644 --- a/SOGo/SoObjects/Mailer/ChangeLog +++ b/SOGo/SoObjects/Mailer/ChangeLog @@ -1,3 +1,11 @@ +2004-09-27 Helge Hess + + * v0.9.6 + + * added basic folder listing + + * SOGoMailAccount.m: list "INBOX" as the sole account subfolder + 2004-09-26 Helge Hess * v0.9.5 diff --git a/SOGo/SoObjects/Mailer/SOGoMailAccount.m b/SOGo/SoObjects/Mailer/SOGoMailAccount.m index 95a857a0..d0ca36a9 100644 --- a/SOGo/SoObjects/Mailer/SOGoMailAccount.m +++ b/SOGo/SoObjects/Mailer/SOGoMailAccount.m @@ -26,6 +26,13 @@ @implementation SOGoMailAccount +/* listing the available folders */ + +- (NSArray *)toManyRelationshipKeys { + // TODO: hardcoded, if we want to support shared folders, need to change */ + return [NSArray arrayWithObjects:@"INBOX", nil]; +} + /* IMAP4 */ - (BOOL)useSSL { diff --git a/SOGo/SoObjects/Mailer/SOGoMailBaseObject.h b/SOGo/SoObjects/Mailer/SOGoMailBaseObject.h index b78c0a2c..4a8221ee 100644 --- a/SOGo/SoObjects/Mailer/SOGoMailBaseObject.h +++ b/SOGo/SoObjects/Mailer/SOGoMailBaseObject.h @@ -32,6 +32,7 @@ */ @class NSURL; +@class NGImap4Client; @interface SOGoMailBaseObject : SOGoObject { @@ -43,6 +44,8 @@ /* IMAP4 */ - (NSURL *)imap4URL; +- (NSString *)imap4FolderName; +- (NGImap4Client *)imap4Client; @end diff --git a/SOGo/SoObjects/Mailer/SOGoMailBaseObject.m b/SOGo/SoObjects/Mailer/SOGoMailBaseObject.m index 847445b0..56e0e55d 100644 --- a/SOGo/SoObjects/Mailer/SOGoMailBaseObject.m +++ b/SOGo/SoObjects/Mailer/SOGoMailBaseObject.m @@ -22,6 +22,7 @@ #include "SOGoMailBaseObject.h" #include "common.h" +#include @implementation SOGoMailBaseObject @@ -47,8 +48,17 @@ NSStringFromSelector(_cmd)]; return nil; } +- (NSURL *)baseImap4URL { + if (![[self container] respondsToSelector:@selector(imap4URL)]) { + [self logWithFormat:@"WARNING: container does not implement -imap4URL!"]; + return nil; + } + + return [[self container] imap4URL]; +} - (NSURL *)imap4URL { NSString *sn; + NSURL *base; if (self->imap4URL != nil) return self->imap4URL; @@ -61,9 +71,58 @@ return nil; } - self->imap4URL = [[NSURL alloc] initWithString:[self nameInContainer] - relativeToURL:[[self container] imap4URL]]; + base = [self baseImap4URL]; + sn = [[base path] stringByAppendingPathComponent:sn]; + self->imap4URL = [[NSURL alloc] initWithString:sn relativeToURL:base]; return self->imap4URL; } +- (NSString *)imap4Separator { + return @"."; +} + +- (NSString *)imap4FolderName { + /* a bit hackish, but should be OK */ + NSString *folderName; + + folderName = [[self imap4URL] path]; + if ([folderName length] == 0) + return nil; + if ([folderName characterAtIndex:0] == '/') + folderName = [folderName substringFromIndex:1]; + + [self logWithFormat:@"FOLDER: %@", folderName]; + return [[folderName pathComponents] componentsJoinedByString: + [self imap4Separator]]; +} + +- (NSString *)imap4Password { + return @""; +} + +- (NGImap4Client *)imap4ClientForURL:(NSURL *)_url password:(NSString *)_pwd { + // TODO: move to some global IMAP4 connection pool manager + NGImap4Client *client; + NSDictionary *result; + + if (_url == nil) + return nil; + + if ((client = [NGImap4Client clientWithURL:_url]) == nil) + return nil; + + result = [client login:[_url user] password:_pwd]; + if (![[result valueForKey:@"result"] boolValue]) { + [self logWithFormat:@"ERROR: IMAP4 login failed!"]; + return nil; + } + + return client; +} + +- (NGImap4Client *)imap4Client { + return [self imap4ClientForURL:[self imap4URL] + password:[self imap4Password]]; +} + @end /* SOGoMailBaseObject */ diff --git a/SOGo/SoObjects/Mailer/SOGoMailFolder.m b/SOGo/SoObjects/Mailer/SOGoMailFolder.m index fd993a62..f26d4c89 100644 --- a/SOGo/SoObjects/Mailer/SOGoMailFolder.m +++ b/SOGo/SoObjects/Mailer/SOGoMailFolder.m @@ -32,6 +32,61 @@ return [self nameInContainer]; } +/* listing the available folders */ + +- (NSArray *)_getDirectChildren:(NSArray *)_array folderName:(NSString *)_fn { + // TODO: we should get the full list of folders _once_ and work on that + // (we could cache it in the context) + NSMutableArray *ma; + unsigned i, count, prefixlen; + + if ((count = [_array count]) < 2) + /* one entry is the folder itself, so we need at least two */ + return [NSArray array]; + + prefixlen = [_fn length] + 1; + ma = [NSMutableArray arrayWithCapacity:count]; + for (i = 0; i < count; i++) { + NSString *p; + + p = [_array objectAtIndex:i]; + if ([p length] <= prefixlen) + continue; + p = [p substringFromIndex:prefixlen]; + + if ([p rangeOfString:@"/"].length > 0) + continue; + + [ma addObject:p]; + } + + [ma sortUsingSelector:@selector(compare:)]; + return ma; +} + +- (NSArray *)toManyRelationshipKeys { + // TODO + NGImap4Client *client; + NSDictionary *result; + NSString *folderName; + + if ((client = [self imap4Client]) == nil) + return nil; + + folderName = [self imap4FolderName]; + + /* maybe we want to use a cache over here */ + result = [client list:folderName pattern:@"*"]; + if (![[result valueForKey:@"result"] boolValue]) { + [self logWithFormat:@"ERROR: listing of folder failed!"]; + return nil; + } + + /* extract list */ + result = [result valueForKey:@"list"]; + return [self _getDirectChildren:[result allKeys] folderName:folderName]; +} + /* name lookup */ - (BOOL)isMessageKey:(NSString *)_key inContext:(id)_ctx { diff --git a/SOGo/SoObjects/Mailer/Version b/SOGo/SoObjects/Mailer/Version index 6b227f7e..0eb47da3 100644 --- a/SOGo/SoObjects/Mailer/Version +++ b/SOGo/SoObjects/Mailer/Version @@ -1,3 +1,3 @@ # $Id$ -SUBMINOR_VERSION:=5 +SUBMINOR_VERSION:=6 diff --git a/SOGo/SoObjects/Mailer/common.h b/SOGo/SoObjects/Mailer/common.h index 88fc5806..a03b8b0d 100644 --- a/SOGo/SoObjects/Mailer/common.h +++ b/SOGo/SoObjects/Mailer/common.h @@ -33,3 +33,4 @@ #include +#include -- 2.39.5