2 Copyright (C) 2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
23 #include "SOGoDraftsFolder.h"
24 #include "SOGoDraftObject.h"
25 #include <SOGo/SOGoUserFolder.h>
26 #include <NGExtensions/NSFileManager+Extensions.h>
30 @implementation SOGoDraftsFolder
32 static NSString *spoolFolder = nil;
35 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
37 spoolFolder = [[ud stringForKey:@"SOGoMailSpoolPath"] copy];
38 if ([spoolFolder length] < 3)
39 spoolFolder = @"/tmp/";
41 NSLog(@"Note: using SOGo mail spool folder: %@", spoolFolder);
46 - (NSString *)makeNewObjectNameInContext:(id)_ctx {
47 static int counter = 1; // THREAD
48 return [NSString stringWithFormat:@"draft_%08d_%08x", getpid(), counter++];
51 - (NSString *)newObjectBaseURLInContext:(id)_ctx {
54 n = [self makeNewObjectNameInContext:_ctx];
55 if (![n isNotNull]) return nil;
57 s = [self baseURLInContext:_ctx];
58 if (![s isNotNull]) return nil;
59 if (![s hasSuffix:@"/"]) s = [s stringByAppendingString:@"/"];
60 return [s stringByAppendingString:n];
63 - (id)newObjectInContext:(id)_ctx {
64 return [self lookupName:[self makeNewObjectNameInContext:_ctx]
65 inContext:_ctx acquire:NO];
68 /* draft folder functionality */
70 - (NSFileManager *)spoolFileManager {
71 return [NSFileManager defaultManager];
74 - (NSString *)spoolFolderPath {
77 - (NSString *)userSpoolFolderPath {
80 p = [self spoolFolderPath];
81 n = [[self lookupUserFolder] nameInContainer];
82 return [p stringByAppendingPathComponent:n];
85 - (BOOL)_ensureUserSpoolFolderPath {
88 if ((fm = [self spoolFileManager]) == nil) {
89 [self errorWithFormat:@"missing spool file manager!"];
92 return [fm createDirectoriesAtPath:[self userSpoolFolderPath]
96 - (NSArray *)fetchMailNames {
99 if ((p = [self userSpoolFolderPath]) == nil)
102 return [[self spoolFileManager] directoryContentsAtPath:p];
105 /* folder methods (used by template) */
107 - (NSArray *)fetchUIDsMatchingQualifier:(id)_q sortOrdering:(id)_so {
108 // TODO: retrieve contained objects
111 allUids = [self fetchMailNames];
112 if (![allUids isNotNull]) {
113 [self logWithFormat:@"Note: no uids in drafts folder: %@",
114 [self userSpoolFolderPath]];
115 return [NSArray array];
118 [self logWithFormat:@"TODO: should sort uids (q=%@,so=%@): %@", _q, _so,
122 - (NSArray *)fetchUIDs:(NSArray *)_uids parts:(NSArray *)_parts {
123 /* FLAGS, ENVELOPE, RFC822.SIZE */
124 NSMutableArray *drafts;
129 if ((count = [_uids count]) == 0)
130 return [NSArray array];
132 drafts = [NSMutableArray arrayWithCapacity:count];
133 for (i = 0; i < count; i++) {
134 SOGoDraftObject *draft;
137 draft = [self lookupName:[_uids objectAtIndex:i] inContext:nil acquire:NO];
138 if (![draft isNotNull] || [draft isKindOfClass:[NSException class]])
141 parts = [draft fetchParts:_parts];
142 if ([parts isNotNull])
143 [drafts addObject:parts];
151 - (id)lookupDraftMessage:(NSString *)_key inContext:(id)_ctx {
152 // TODO: we might want to check for existence prior controller creation
153 return [[[SOGoDraftObject alloc] initWithName:_key
154 inContainer:self] autorelease];
157 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
160 /* first check attributes directly bound to the application */
161 if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]) != nil)
164 if ((obj = [self lookupDraftMessage:_key inContext:_ctx]) != nil)
167 /* return 404 to stop acquisition */
168 return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
173 - (BOOL)davIsCollection {
177 @end /* SOGoDraftsFolder */