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