2 Copyright (C) 2004-2005 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
22 #include "SOGoDraftsFolder.h"
23 #include "SOGoDraftObject.h"
24 #include <SOGo/SOGoUserFolder.h>
25 #include <NGExtensions/NSFileManager+Extensions.h>
29 @implementation SOGoDraftsFolder
31 static NSString *spoolFolder = nil;
34 return [super version] + 0 /* v1 */;
38 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
40 NSAssert2([super version] == 1,
41 @"invalid superclass (%@) version %i !",
42 NSStringFromClass([self superclass]), [super version]);
44 spoolFolder = [[ud stringForKey:@"SOGoMailSpoolPath"] copy];
45 if ([spoolFolder length] < 3)
46 spoolFolder = @"/tmp/";
48 NSLog(@"Note: using SOGo mail spool folder: %@", spoolFolder);
53 - (NSString *)makeNewObjectNameInContext:(id)_ctx {
54 static int counter = 1; // THREAD
55 return [NSString stringWithFormat:@"draft_%08d_%08x", getpid(), counter++];
58 - (NSString *)newObjectBaseURLInContext:(id)_ctx {
61 n = [self makeNewObjectNameInContext:_ctx];
62 if (![n isNotNull]) return nil;
64 s = [self baseURLInContext:_ctx];
65 if (![s isNotNull]) return nil;
66 if (![s hasSuffix:@"/"]) s = [s stringByAppendingString:@"/"];
67 return [s stringByAppendingString:n];
70 - (id)newObjectInContext:(id)_ctx {
71 return [self lookupName:[self makeNewObjectNameInContext:_ctx]
72 inContext:_ctx acquire:NO];
75 /* draft folder functionality */
77 - (NSFileManager *)spoolFileManager {
78 return [NSFileManager defaultManager];
81 - (NSString *)spoolFolderPath {
84 - (NSString *)userSpoolFolderPath {
87 p = [self spoolFolderPath];
88 n = [[self lookupUserFolder] nameInContainer];
89 return [p stringByAppendingPathComponent:n];
92 - (BOOL)_ensureUserSpoolFolderPath {
95 if ((fm = [self spoolFileManager]) == nil) {
96 [self errorWithFormat:@"missing spool file manager!"];
99 return [fm createDirectoriesAtPath:[self userSpoolFolderPath]
103 - (NSArray *)fetchMailNames {
106 if ((p = [self userSpoolFolderPath]) == nil)
109 return [[self spoolFileManager] directoryContentsAtPath:p];
112 /* folder methods (used by template) */
114 - (NSArray *)fetchUIDsMatchingQualifier:(id)_q sortOrdering:(id)_so {
115 // TODO: retrieve contained objects
118 allUids = [self fetchMailNames];
119 if (![allUids isNotNull]) {
120 [self logWithFormat:@"Note: no uids in drafts folder: %@",
121 [self userSpoolFolderPath]];
122 return [NSArray array];
125 // TODO: should sort uids (q=%@,so=%@): %@", _q, _so, allUids];
128 - (NSArray *)fetchUIDs:(NSArray *)_uids parts:(NSArray *)_parts {
129 /* FLAGS, ENVELOPE, RFC822.SIZE */
130 NSMutableArray *drafts;
135 if ((count = [_uids count]) == 0)
136 return [NSArray array];
138 drafts = [NSMutableArray arrayWithCapacity:count];
139 for (i = 0; i < count; i++) {
140 SOGoDraftObject *draft;
143 draft = [self lookupName:[_uids objectAtIndex:i] inContext:nil acquire:NO];
144 if (![draft isNotNull] || [draft isKindOfClass:[NSException class]])
147 parts = [draft fetchParts:_parts];
148 if ([parts isNotNull])
149 [drafts addObject:parts];
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];
163 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
166 /* first check attributes directly bound to the application */
167 if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]) != nil)
170 if ((obj = [self lookupDraftMessage:_key inContext:_ctx]) != nil)
173 /* return 404 to stop acquisition */
174 return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
179 - (BOOL)davIsCollection {
183 - (NSArray *)toOneRelationshipKeys {
184 return [self fetchMailNames];
189 - (NSString *)outlookFolderClass {
190 return @"IPF.Drafts";
193 @end /* SOGoDraftsFolder */