]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Mailer/SOGoDraftsFolder.m
git-svn-id: http://svn.opengroupware.org/SOGo/trunk@900 d1b88da0-ebda-0310-925b-ed51d...
[scalable-opengroupware.org] / SoObjects / Mailer / SOGoDraftsFolder.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 "SOGoDraftsFolder.h"
23 #include "SOGoDraftObject.h"
24 #include <SOGo/SOGoUserFolder.h>
25 #include <NGExtensions/NSFileManager+Extensions.h>
26 #include "common.h"
27 #include <unistd.h>
28
29 @implementation SOGoDraftsFolder
30
31 static NSString *spoolFolder = nil;
32
33 + (int)version {
34   return [super version] + 0 /* v1 */;
35 }
36
37 + (void)initialize {
38   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
39
40   NSAssert2([super version] == 1,
41             @"invalid superclass (%@) version %i !",
42             NSStringFromClass([self superclass]), [super version]);
43   
44   spoolFolder = [[ud stringForKey:@"SOGoMailSpoolPath"] copy];
45   if ([spoolFolder length] < 3)
46     spoolFolder = @"/tmp/";
47   
48   NSLog(@"Note: using SOGo mail spool folder: %@", spoolFolder);
49 }
50
51 /* new objects */
52
53 - (NSString *)makeNewObjectNameInContext:(id)_ctx {
54   static int counter = 1; // THREAD
55   return [NSString stringWithFormat:@"draft_%08d_%08x", getpid(), counter++];
56 }
57
58 - (NSString *)newObjectBaseURLInContext:(id)_ctx {
59   NSString *s, *n;
60   
61   n = [self makeNewObjectNameInContext:_ctx];
62   if (![n isNotNull]) return nil;
63   
64   s = [self baseURLInContext:_ctx];
65   if (![s isNotNull]) return nil;
66   if (![s hasSuffix:@"/"]) s = [s stringByAppendingString:@"/"];
67   return [s stringByAppendingString:n];
68 }
69
70 - (id)newObjectInContext:(id)_ctx {
71   return [self lookupName:[self makeNewObjectNameInContext:_ctx]
72                inContext:_ctx acquire:NO];
73 }
74
75 /* draft folder functionality */
76
77 - (NSFileManager *)spoolFileManager {
78   return [NSFileManager defaultManager];
79 }
80
81 - (NSString *)spoolFolderPath {
82   return spoolFolder;
83 }
84 - (NSString *)userSpoolFolderPath {
85   NSString *p, *n;
86   
87   p = [self spoolFolderPath];
88   n = [[self lookupUserFolder] nameInContainer];
89   return [p stringByAppendingPathComponent:n];
90 }
91
92 - (BOOL)_ensureUserSpoolFolderPath {
93   NSFileManager *fm;
94   
95   if ((fm = [self spoolFileManager]) == nil) {
96     [self errorWithFormat:@"missing spool file manager!"];
97     return NO;
98   }
99   return [fm createDirectoriesAtPath:[self userSpoolFolderPath]
100              attributes:nil];
101 }
102
103 - (NSArray *)fetchMailNames {
104   NSString *p;
105   
106   if ((p = [self userSpoolFolderPath]) == nil)
107     return nil;
108   
109   return [[self spoolFileManager] directoryContentsAtPath:p];
110 }
111
112 /* folder methods (used by template) */
113
114 - (NSArray *)fetchUIDsMatchingQualifier:(id)_q sortOrdering:(id)_so {
115   // TODO: retrieve contained objects
116   NSArray *allUids;
117   
118   allUids = [self fetchMailNames];
119   if (![allUids isNotNull]) {
120     [self logWithFormat:@"Note: no uids in drafts folder: %@",
121             [self userSpoolFolderPath]];
122     return [NSArray array];
123   }
124   
125   // TODO: should sort uids (q=%@,so=%@): %@", _q, _so, allUids];
126   return allUids;
127 }
128 - (NSArray *)fetchUIDs:(NSArray *)_uids parts:(NSArray *)_parts {
129   /* FLAGS, ENVELOPE, RFC822.SIZE */
130   NSMutableArray  *drafts;
131   unsigned i, count;
132   
133   if (_uids == nil)
134     return nil;
135   if ((count = [_uids count]) == 0)
136     return [NSArray array];
137   
138   drafts = [NSMutableArray arrayWithCapacity:count];
139   for (i = 0; i < count; i++) {
140     SOGoDraftObject *draft;
141     id parts;
142     
143     draft = [self lookupName:[_uids objectAtIndex:i] inContext:nil acquire:NO];
144     if (![draft isNotNull] || [draft isKindOfClass:[NSException class]])
145       continue;
146     
147     parts = [draft fetchParts:_parts];
148     if ([parts isNotNull])
149       [drafts addObject:parts];
150   }
151   
152   return drafts;
153 }
154
155 /* name lookup */
156
157 - (id)lookupDraftMessage:(NSString *)_key inContext:(id)_ctx {
158   // TODO: we might want to check for existence prior controller creation
159   return [[[SOGoDraftObject alloc] initWithName:_key 
160                                    inContainer:self] autorelease];
161 }
162
163 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
164   id obj;
165   
166   /* first check attributes directly bound to the application */
167   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]) != nil)
168     return obj;
169   
170   if ((obj = [self lookupDraftMessage:_key inContext:_ctx]) != nil)
171     return obj;
172   
173   /* return 404 to stop acquisition */
174   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
175 }
176
177 /* WebDAV */
178
179 - (BOOL)davIsCollection {
180   return YES;
181 }
182
183 - (NSArray *)toOneRelationshipKeys {
184   return [self fetchMailNames];
185 }
186
187 /* folder type */
188
189 - (NSString *)outlookFolderClass {
190   return @"IPF.Drafts";
191 }
192
193 @end /* SOGoDraftsFolder */