@implementation NGImap4Connection
-static BOOL debugOn = NO;
-static BOOL debugCache = NO;
-static BOOL debugKeys = NO;
-static BOOL alwaysSelect = NO;
-static BOOL onlyFetchInbox = NO;
-static NSString *imap4Separator = nil;
+static BOOL debugOn = NO;
+static BOOL debugCache = NO;
+static BOOL debugKeys = NO;
+static BOOL alwaysSelect = NO;
+static BOOL onlyFetchInbox = NO;
+static NSString *imap4Separator = nil;
+ (void)initialize {
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
- debugOn = [ud boolForKey:@"SOGoEnableIMAP4Debug"];
- debugCache = [ud boolForKey:@"SOGoEnableIMAP4CacheDebug"];
- alwaysSelect = [ud boolForKey:@"SOGoAlwaysSelectIMAP4Folder"];
- if (debugOn) NSLog(@"Note: SOGoEnableIMAP4Debug is enabled!");
+ debugOn = [ud boolForKey:@"NGImap4ConnectionDebugEnabled"];
+ debugCache = [ud boolForKey:@"NGImap4ConnectionCacheDebugEnabled"];
+ debugKeys = [ud boolForKey:@"NGImap4ConnectionFolderDebugEnabled"];
+ alwaysSelect = [ud boolForKey:@"NGImap4ConnectionAlwaysSelect"];
+
+ if (debugOn) NSLog(@"Note: NGImap4ConnectionDebugEnabled is enabled!");
if (alwaysSelect)
- NSLog(@"WARNING: 'SOGoAlwaysSelectIMAP4Folder' enabled (slow down)");
+ NSLog(@"WARNING: 'NGImap4ConnectionAlwaysSelect' enabled (slow down)");
- imap4Separator = [[ud stringForKey:@"SOGoIMAP4StringSeparator"] copy];
+ imap4Separator =
+ [[ud stringForKey:@"NGImap4ConnectionStringSeparator"] copy];
if ([imap4Separator length] == 0)
imap4Separator = @"/";
- NSLog(@"Note(SOGoMailManager): using '%@' as the IMAP4 folder separator.",
+ NSLog(@"Note(NGImap4Connection): using '%@' as the IMAP4 folder separator.",
imap4Separator);
}
self->password = [_pwd copy];
self->creationTime = [[NSDate alloc] init];
+
+ // TODO: retrieve from IMAP4 instead of using a default
+ self->separator = imap4Separator;
}
return self;
}
}
- (void)dealloc {
+ [self->separator release];
[self->urlToRights release];
[self->cachedUIDs release];
[self->uidFolderURL release];
return ma;
}
-NSArray *SOGoMailExtractSubfolders(NSURL *_url, NSDictionary *_result) {
+- (NSArray *)extractSubfoldersForURL:(NSURL *)_url
+ fromResultSet:(NSDictionary *)_result
+{
NSString *folderName;
NSDictionary *result;
NSArray *names;
NSLog(@"%s: folder %@ has no children.", __PRETTY_FUNCTION__,folderName);
return nil;
}
-
+
if (debugKeys) {
NSLog(@"%s: all keys %@: %@", __PRETTY_FUNCTION__, folderName,
[[result allKeys] componentsJoinedByString:@", "]);
return names;
}
-- (NSArray *)_getDirectChildren:(NSArray *)_array folderName:(NSString *)_fn {
- return SOGoMailGetDirectChildren(_array, _fn);
-}
-- (NSArray *)extractSubfoldersForURL:(NSURL *)_url
- fromResultSet:(NSDictionary *)_result
-{
- return SOGoMailExtractSubfolders(_url, _result);
-}
-
- (NSString *)imap4Separator {
- // TODO: make server specific ivar!
- return imap4Separator;
+ return self->separator;
}
- (NSString *)imap4FolderNameForURL:(NSURL *)_url removeFileName:(BOOL)_delfn {
/* folder operations */
-- (NSArray *)subfoldersForURL:(NSURL *)_url {
- NSDictionary *result;
-
- /* check hierarchy cache */
+- (NSDictionary *)primaryFetchMailboxHierarchyForURL:(NSURL *)_url {
+ NSDictionary *result;
if ((result = [self cachedHierarchyResults]) != nil)
- return [self extractSubfoldersForURL:_url fromResultSet:result];
-
- [self debugWithFormat:@" no folders cached yet .."];
+ return [result isNotNull] ? result : nil;
- /* fetch _all_ folders */
+ if (debugCache) [self logWithFormat:@" no folders cached yet .."];
result = [[self client] list:(onlyFetchInbox ? @"INBOX" : @"*")
pattern:@"*"];
if (![[result valueForKey:@"result"] boolValue]) {
- [self errorWithFormat:@"listing of folder failed!"];
+ [self errorWithFormat:@"Could not list mailbox hierarchy!"];
return nil;
}
-
+
/* cache results */
if ([result isNotNull]) {
[self cacheHierarchyResults:result];
if (debugCache) {
- [self logWithFormat:@"cached results in entry %@: 0x%08X(%d)",
- self, result, [result count]];
+ [self logWithFormat:@"cached results: 0x%08X(%d)",
+ result, [result count]];
}
}
-
- /* extract list */
+ return result;
+}
+
+- (NSArray *)subfoldersForURL:(NSURL *)_url {
+ NSDictionary *result;
+
+ if ((result = [self primaryFetchMailboxHierarchyForURL:_url]) == nil)
+ return nil;
+ if ([result isKindOfClass:[NSException class]]) {
+ [self errorWithFormat:@"failed to retrieve hierarchy: %@", result];
+ return nil;
+ }
return [self extractSubfoldersForURL:_url fromResultSet:result];
}
- (NSArray *)allFoldersForURL:(NSURL *)_url {
NSDictionary *result;
-
- /* check hierarchy cache */
-
- if ((result = [self cachedHierarchyResults]) != nil)
- return [self extractFoldersFromResultSet:result];
-
- [self debugWithFormat:@" no folders cached yet .."];
-
- /* fetch _all_ folders */
-
- result = [[self client] list:@"INBOX" pattern:@"*"];
- if (![[result valueForKey:@"result"] boolValue]) {
- [self logWithFormat:@"ERROR: listing of folder failed!"];
+
+ if ((result = [self primaryFetchMailboxHierarchyForURL:_url]) == nil)
+ return nil;
+ if ([result isKindOfClass:[NSException class]]) {
+ [self errorWithFormat:@"failed to retrieve hierarchy: %@", result];
return nil;
}
- /* cache results */
-
- if ([result isNotNull]) {
- [self cacheHierarchyResults:result];
- if (debugCache) {
- [self logWithFormat:@"cached results in entry %@: 0x%08X(%d)",
- self, result, [result count]];
- }
- }
-
- /* extract list */
return [self extractFoldersFromResultSet:result];
}