]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Mailer/SOGoMailFolder.m
partially implemented mail delete #1212
[scalable-opengroupware.org] / SOGo / SoObjects / Mailer / SOGoMailFolder.m
1 /*
2   Copyright (C) 2004-2005 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
22 #include "SOGoMailFolder.h"
23 #include "SOGoMailObject.h"
24 #include "SOGoMailManager.h"
25 #include "common.h"
26
27 @implementation SOGoMailFolder
28
29 - (void)dealloc {
30   [self->filenames release];
31   [super dealloc];
32 }
33
34 /* IMAP4 */
35
36 - (NSString *)relativeImap4Name {
37   return [self nameInContainer];
38 }
39
40 /* listing the available folders */
41
42 - (NSArray *)toManyRelationshipKeys {
43   return [[self mailManager] subfoldersForURL:[self imap4URL] 
44                              password:[self imap4Password]];
45 }
46 - (NSArray *)toOneRelationshipKeys {
47   NSArray  *uids;
48   unsigned count;
49   
50   if (self->filenames != nil)
51     return [self->filenames isNotNull] ? self->filenames : nil;
52   
53   uids = [self fetchUIDsMatchingQualifier:nil sortOrdering:@"DATE"];
54   if ([uids isKindOfClass:[NSException class]])
55     return nil;
56   
57   if ((count = [uids count]) == 0) {
58     self->filenames = [[NSArray alloc] init];
59   }
60   else {
61     NSMutableArray *keys;
62     unsigned i;
63     
64     keys = [[NSMutableArray alloc] initWithCapacity:count];
65     for (i = 0; i < count; i++) {
66       NSString *k;
67       
68       k = [[uids objectAtIndex:i] stringValue];
69       k = [k stringByAppendingString:@".mail"];
70       [keys addObject:k];
71     }
72     self->filenames = [keys copy];
73     [keys release];
74   }
75   return self->filenames;
76 }
77
78 /* messages */
79
80 - (NSArray *)fetchUIDsMatchingQualifier:(id)_q sortOrdering:(id)_so {
81   /* seems to return an NSArray of NSNumber's */
82   return [[self mailManager] fetchUIDsInURL:[self imap4URL]
83                              qualifier:_q sortOrdering:_so
84                              password:[self imap4Password]];
85 }
86
87 - (NSArray *)fetchUIDs:(NSArray *)_uids parts:(NSArray *)_parts {
88   return [[self mailManager] fetchUIDs:_uids inURL:[self imap4URL]
89                              parts:_parts
90                              password:[self imap4Password]];
91 }
92
93 - (NSException *)postData:(NSData *)_data flags:(id)_flags {
94   return [[self mailManager] postData:_data flags:_flags
95                              toFolderURL:[self imap4URL]
96                              password:[self imap4Password]];
97 }
98
99 /* name lookup */
100
101 - (BOOL)isMessageKey:(NSString *)_key inContext:(id)_ctx {
102   /*
103     Every key starting with a digit is consider an IMAP4 message key. This is
104     not entirely correct since folders could also start with a number.
105     
106     If we want to support folders beginning with numbers, we would need to
107     scan the folder list for the _key, which would make everything quite a bit
108     slower.
109     TODO: support this mode using a default.
110   */
111   if ([_key length] == 0)
112     return NO;
113   
114   if (isdigit([_key characterAtIndex:0]))
115     return YES;
116   
117   return NO;
118 }
119
120 - (id)lookupImap4Folder:(NSString *)_key inContext:(id)_ctx {
121   // TODO: we might want to check for existence prior controller creation
122   return [[[SOGoMailFolder alloc] initWithName:_key 
123                                   inContainer:self] autorelease];
124 }
125 - (id)lookupImap4Message:(NSString *)_key inContext:(id)_ctx {
126   // TODO: we might want to check for existence prior controller creation
127   return [[[SOGoMailObject alloc] initWithName:_key 
128                                   inContainer:self] autorelease];
129 }
130
131 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
132   id obj;
133   
134   /* first check attributes directly bound to the application */
135   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]) != nil)
136     return obj;
137   
138   obj = [self isMessageKey:_key inContext:_ctx]
139     ? [self lookupImap4Message:_key inContext:_ctx]
140     : [self lookupImap4Folder:_key  inContext:_ctx];
141   if (obj != nil)
142     return obj;
143   
144   /* return 404 to stop acquisition */
145   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
146 }
147
148 /* WebDAV */
149
150 - (BOOL)davIsCollection {
151   return YES;
152 }
153
154 - (NSException *)davCreateCollection:(NSString *)_name inContext:(id)_ctx {
155   return [[self mailManager] createMailbox:_name atURL:[self imap4URL]
156                              password:[self imap4Password]];
157 }
158
159 /* operations */
160
161 - (NSException *)delete {
162   /* Note: overrides SOGoObject -delete */
163   return [[self mailManager] deleteMailboxAtURL:[self imap4URL]
164                              password:[self imap4Password]];
165 }
166
167 @end /* SOGoMailFolder */