]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Mailer/SOGoMailAccount.m
added methods to retrieve full folder hierarchy
[scalable-opengroupware.org] / SOGo / SoObjects / Mailer / SOGoMailAccount.m
1 /*
2   Copyright (C) 2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id: SOGoMailAccount.m 274 2004-08-26 13:10:49Z znek $
22
23 #include "SOGoMailAccount.h"
24 #include "SOGoMailFolder.h"
25 #include "SOGoMailManager.h"
26 #include "SOGoDraftsFolder.h"
27 #include "common.h"
28
29 @implementation SOGoMailAccount
30
31 /* listing the available folders */
32
33 - (NSArray *)toManyRelationshipKeys {
34   // TODO: hardcoded, if we want to support shared fldrs, this needs to change
35   return [NSArray arrayWithObjects:@"INBOX", @"Drafts", nil];
36 }
37
38 /* hierarchy */
39
40 - (SOGoMailAccount *)mailAccountFolder {
41   return self;
42 }
43
44 - (NSArray *)allFolderPathes {
45   NSArray *pathes;
46
47   pathes = [[self mailManager] allFoldersForURL:[self imap4URL] 
48                                password:[self imap4Password]];
49   pathes = [pathes sortedArrayUsingSelector:@selector(compare:)];
50   return pathes;
51 }
52
53 /* IMAP4 */
54
55 - (BOOL)useSSL {
56   return NO;
57 }
58
59 - (NSURL *)imap4URL {
60   /* imap://agenortest@mail.opengroupware.org/INBOX/withsubdirs/subdir1 */
61   NSString *s;
62   NSRange  r;
63
64   if (self->imap4URL != nil)
65     return self->imap4URL;
66   
67   s = [self nameInContainer];
68   r = [s rangeOfString:@"@"];
69   if (r.length == 0) {
70     [self logWithFormat:@"ERROR: missing login in account folder name: %@", s];
71     return nil;
72   }
73
74   s = [([self useSSL] ? @"imaps://" : @"imap://") stringByAppendingString:s];
75   s = [s stringByAppendingString:@"/"];
76   
77   self->imap4URL = [[NSURL alloc] initWithString:s];
78   return self->imap4URL;
79 }
80
81 /* name lookup */
82
83 - (id)lookupImap4Folder:(NSString *)_key inContext:(id)_ctx {
84   return [[[SOGoMailFolder alloc] initWithName:_key 
85                                   inContainer:self] autorelease];
86 }
87
88 - (id)lookupDraftsFolder:(NSString *)_key inContext:(id)_ctx {
89   return [[[SOGoDraftsFolder alloc] initWithName:_key 
90                                     inContainer:self] autorelease];
91 }
92
93 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
94   id obj;
95   
96   /* first check attributes directly bound to the application */
97   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]) != nil)
98     return obj;
99   
100   if ([_key isEqualToString:@"Drafts"]) {
101     if ((obj = [self lookupDraftsFolder:_key inContext:_ctx]) != nil)
102       return obj;
103   }
104   
105   if ((obj = [self lookupImap4Folder:_key inContext:_ctx]) != nil)
106     return obj;
107   
108   /* return 404 to stop acquisition */
109   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
110 }
111
112 /* WebDAV */
113
114 - (BOOL)davIsCollection {
115   return YES;
116 }
117
118 - (NSString *)shortTitle {
119   NSString *s, *login, *host;
120   NSRange r;
121
122   s = [self nameInContainer];
123   
124   r = [s rangeOfString:@"@"];
125   if (r.length > 0) {
126     login = [s substringToIndex:r.location];
127     host  = [s substringFromIndex:(r.location + r.length)];
128   }
129   else {
130     login = nil;
131     host  = s;
132   }
133   
134   r = [host rangeOfString:@"."];
135   if (r.length > 0)
136     host = [host substringToIndex:r.location];
137   
138   if ([login length] == 0)
139     return host;
140   
141   return [NSString stringWithFormat:@"%@ (%@)", host, login];
142 }
143
144 - (NSString *)davDisplayName {
145   return [self shortTitle];
146 }
147
148 @end /* SOGoMailAccount */