2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "SOGoMailAccount.h"
23 #include "SOGoMailFolder.h"
24 #include "SOGoMailManager.h"
25 #include "SOGoDraftsFolder.h"
28 @implementation SOGoMailAccount
30 static NSArray *rootFolderNames = nil;
31 static NSString *inboxFolderName = @"INBOX";
32 static NSString *draftsFolderName = @"Drafts";
33 static NSString *sieveFolderName = @"Filters";
36 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
38 if ([ud boolForKey:@"SOGoEnableSieveFolder"]) {
39 rootFolderNames = [[NSArray alloc] initWithObjects:
46 rootFolderNames = [[NSArray alloc] initWithObjects:
53 /* listing the available folders */
55 - (NSArray *)toManyRelationshipKeys {
56 // TODO: hardcoded, if we want to support shared fldrs, this needs to change
57 return rootFolderNames;
62 - (SOGoMailAccount *)mailAccountFolder {
66 - (NSArray *)allFolderPathes {
69 pathes = [[self mailManager] allFoldersForURL:[self imap4URL]
70 password:[self imap4Password]];
71 pathes = [pathes sortedArrayUsingSelector:@selector(compare:)];
82 /* imap://agenortest@mail.opengroupware.org/INBOX/withsubdirs/subdir1 */
86 if (self->imap4URL != nil)
87 return self->imap4URL;
89 s = [self nameInContainer];
90 r = [s rangeOfString:@"@"];
92 [self errorWithFormat:@"missing login in account folder name: %@", s];
95 if ([s hasSuffix:@":80"]) { // HACK
96 [self logWithFormat:@"WARNING: incorrect value for IMAP4 URL: '%@'", s];
97 s = [s substringToIndex:([s length] - 3)];
100 s = [([self useSSL] ? @"imaps://" : @"imap://") stringByAppendingString:s];
101 s = [s stringByAppendingString:@"/"];
103 self->imap4URL = [[NSURL alloc] initWithString:s];
104 return self->imap4URL;
109 - (id)lookupFolder:(NSString *)_key ofClassNamed:(NSString *)_cn
114 if ((clazz = NSClassFromString(_cn)) == Nil) {
115 [self logWithFormat:@"ERROR: did not find class '%@' for key: '%@'",
117 return [NSException exceptionWithHTTPStatus:500 /* server error */
118 reason:@"did not find mail folder class!"];
120 return [[[clazz alloc] initWithName:_key inContainer:self] autorelease];
123 - (id)lookupImap4Folder:(NSString *)_key inContext:(id)_cx {
124 return [self lookupFolder:_key ofClassNamed:@"SOGoMailFolder" inContext:_cx];
126 - (id)lookupDraftsFolder:(NSString *)_key inContext:(id)_ctx {
127 return [self lookupFolder:_key ofClassNamed:@"SOGoDraftsFolder"
130 - (id)lookupFiltersFolder:(NSString *)_key inContext:(id)_ctx {
131 return [self lookupFolder:_key ofClassNamed:@"SOGoSieveScriptsFolder"
135 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
138 /* first check attributes directly bound to the application */
139 if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]) != nil)
142 // TODO: those should be product.plist bindings? (can't be class bindings
143 // though because they are 'per-account')
144 if ([_key isEqualToString:draftsFolderName]) {
145 if ((obj = [self lookupDraftsFolder:_key inContext:_ctx]) != nil)
148 if ([_key isEqualToString:sieveFolderName]) {
149 if ((obj = [self lookupFiltersFolder:_key inContext:_ctx]) != nil)
153 if ((obj = [self lookupImap4Folder:_key inContext:_ctx]) != nil)
156 /* return 404 to stop acquisition */
157 return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
160 /* special folders */
162 - (NSString *)inboxFolderNameInContext:(id)_ctx {
163 return inboxFolderName; /* cannot be changed in Cyrus ? */
165 - (NSString *)draftsFolderNameInContext:(id)_ctx {
166 return draftsFolderName; /* SOGo managed folder */
168 - (NSString *)sieveFolderNameInContext:(id)_ctx {
169 return sieveFolderName; /* SOGo managed folder */
171 - (NSString *)sentFolderNameInContext:(id)_ctx {
172 /* OGo issue #1225 */
173 static NSString *s = nil;
178 ud = [NSUserDefaults standardUserDefaults];
179 s = [[ud stringForKey:@"SOGoSentFolderName"] copy];
180 if ([s length] == 0) s = @"Sent";
181 NSLog(@"Note: using SOGoSentFolderName: '%@'", s);
185 - (NSString *)trashFolderNameInContext:(id)_ctx {
186 /* OGo issue #1225 */
187 static NSString *s = nil;
192 ud = [NSUserDefaults standardUserDefaults];
193 s = [[ud stringForKey:@"SOGoTrashFolderName"] copy];
194 if ([s length] == 0) s = @"Trash";
195 NSLog(@"Note: using SOGoTrashFolderName: '%@'", s);
200 - (SOGoMailFolder *)inboxFolderInContext:(id)_ctx {
201 // TODO: use some profile to determine real location, use a -traverse lookup
202 SOGoMailFolder *folder;
204 if (self->inboxFolder != nil)
205 return self->inboxFolder;
207 folder = [self lookupName:[self inboxFolderNameInContext:_ctx]
208 inContext:_ctx acquire:NO];
209 if ([folder isKindOfClass:[NSException class]]) return folder;
211 return ((self->inboxFolder = [folder retain]));
214 - (SOGoMailFolder *)sentFolderInContext:(id)_ctx {
215 // TODO: use some profile to determine real location, use a -traverse lookup
216 SOGoMailFolder *folder;
218 if (self->sentFolder != nil)
219 return self->sentFolder;
221 folder = [self inboxFolderInContext:_ctx];
222 if ([folder isKindOfClass:[NSException class]]) return folder;
224 folder = [folder lookupName:[self sentFolderNameInContext:_ctx]
225 inContext:_ctx acquire:NO];
226 if ([folder isKindOfClass:[NSException class]]) return folder;
228 if (![folder isNotNull]) {
229 return [NSException exceptionWithHTTPStatus:404 /* not found */
230 reason:@"did not find Sent folder!"];
233 return ((self->sentFolder = [folder retain]));
236 - (SOGoMailFolder *)trashFolderInContext:(id)_ctx {
237 // TODO: use some profile to determine real location
238 SOGoMailFolder *folder;
240 if (self->trashFolder != nil)
241 return self->trashFolder;
243 folder = [self inboxFolderInContext:_ctx];
244 if ([folder isKindOfClass:[NSException class]]) return folder;
246 folder = [folder lookupName:[self trashFolderNameInContext:_ctx]
247 inContext:_ctx acquire:NO];
248 if ([folder isKindOfClass:[NSException class]]) return folder;
250 if (![folder isNotNull]) {
251 return [NSException exceptionWithHTTPStatus:404 /* not found */
252 reason:@"did not find Trash folder!"];
255 return ((self->trashFolder = [folder retain]));
260 - (BOOL)davIsCollection {
264 - (NSString *)shortTitle {
265 NSString *s, *login, *host;
268 s = [self nameInContainer];
270 r = [s rangeOfString:@"@"];
272 login = [s substringToIndex:r.location];
273 host = [s substringFromIndex:(r.location + r.length)];
280 r = [host rangeOfString:@"."];
282 host = [host substringToIndex:r.location];
284 if ([login length] == 0)
287 return [NSString stringWithFormat:@"%@ (%@)", host, login];
290 - (NSString *)davDisplayName {
291 return [self shortTitle];
294 @end /* SOGoMailAccount */