]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Mailer/SOGoMailAccount.m
5a6b4b0f2d7eb889f51b9354ef0fa0f7a51cf4fb
[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   static NSArray *accFolderNames = nil;
36   if (accFolderNames == nil) {
37     accFolderNames = [[NSArray alloc] initWithObjects:
38                                         @"INBOX", @"Drafts", @"Filters", nil];
39   }
40   return accFolderNames;
41 }
42
43 /* hierarchy */
44
45 - (SOGoMailAccount *)mailAccountFolder {
46   return self;
47 }
48
49 - (NSArray *)allFolderPathes {
50   NSArray *pathes;
51
52   pathes = [[self mailManager] allFoldersForURL:[self imap4URL] 
53                                password:[self imap4Password]];
54   pathes = [pathes sortedArrayUsingSelector:@selector(compare:)];
55   return pathes;
56 }
57
58 /* IMAP4 */
59
60 - (BOOL)useSSL {
61   return NO;
62 }
63
64 - (NSURL *)imap4URL {
65   /* imap://agenortest@mail.opengroupware.org/INBOX/withsubdirs/subdir1 */
66   NSString *s;
67   NSRange  r;
68
69   if (self->imap4URL != nil)
70     return self->imap4URL;
71   
72   s = [self nameInContainer];
73   r = [s rangeOfString:@"@"];
74   if (r.length == 0) {
75     [self logWithFormat:@"ERROR: missing login in account folder name: %@", s];
76     return nil;
77   }
78
79   s = [([self useSSL] ? @"imaps://" : @"imap://") stringByAppendingString:s];
80   s = [s stringByAppendingString:@"/"];
81   
82   self->imap4URL = [[NSURL alloc] initWithString:s];
83   return self->imap4URL;
84 }
85
86 /* name lookup */
87
88 - (id)lookupFolder:(NSString *)_key ofClassNamed:(NSString *)_cn
89   inContext:(id)_cx
90 {
91   Class clazz;
92
93   if ((clazz = NSClassFromString(_cn)) == Nil) {
94     [self logWithFormat:@"ERROR: did not find class '%@' for key: '%@'", 
95             _cn, _key];
96     return [NSException exceptionWithHTTPStatus:500 /* server error */
97                         reason:@"did not find mail folder class!"];
98   }
99   return [[[clazz alloc] initWithName:_key inContainer:self] autorelease];
100 }
101
102 - (id)lookupImap4Folder:(NSString *)_key inContext:(id)_cx {
103   return [self lookupFolder:_key ofClassNamed:@"SOGoMailFolder" inContext:_cx];
104 }
105 - (id)lookupDraftsFolder:(NSString *)_key inContext:(id)_ctx {
106   return [self lookupFolder:_key ofClassNamed:@"SOGoDraftsFolder" 
107                inContext:_ctx];
108 }
109 - (id)lookupFiltersFolder:(NSString *)_key inContext:(id)_ctx {
110   return [self lookupFolder:_key ofClassNamed:@"SOGoSieveScriptsFolder" 
111                inContext:_ctx];
112 }
113
114 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
115   id obj;
116   
117   /* first check attributes directly bound to the application */
118   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]) != nil)
119     return obj;
120   
121   // TODO: those should be product.plist bindings? (can't be class bindings
122   //       though because they are 'per-account')
123   if ([_key isEqualToString:@"Drafts"]) {
124     if ((obj = [self lookupDraftsFolder:_key inContext:_ctx]) != nil)
125       return obj;
126   }
127   if ([_key isEqualToString:@"Filters"]) {
128     if ((obj = [self lookupFiltersFolder:_key inContext:_ctx]) != nil)
129       return obj;
130   }
131   
132   if ((obj = [self lookupImap4Folder:_key inContext:_ctx]) != nil)
133     return obj;
134   
135   /* return 404 to stop acquisition */
136   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
137 }
138
139 /* WebDAV */
140
141 - (BOOL)davIsCollection {
142   return YES;
143 }
144
145 - (NSString *)shortTitle {
146   NSString *s, *login, *host;
147   NSRange r;
148
149   s = [self nameInContainer];
150   
151   r = [s rangeOfString:@"@"];
152   if (r.length > 0) {
153     login = [s substringToIndex:r.location];
154     host  = [s substringFromIndex:(r.location + r.length)];
155   }
156   else {
157     login = nil;
158     host  = s;
159   }
160   
161   r = [host rangeOfString:@"."];
162   if (r.length > 0)
163     host = [host substringToIndex:r.location];
164   
165   if ([login length] == 0)
166     return host;
167   
168   return [NSString stringWithFormat:@"%@ (%@)", host, login];
169 }
170
171 - (NSString *)davDisplayName {
172   return [self shortTitle];
173 }
174
175 @end /* SOGoMailAccount */