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