1 /* SOGoParentFolder.m - this file is part of SOGo
3 * Copyright (C) 2006, 2007 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSDictionary.h>
24 #import <Foundation/NSString.h>
25 #import <Foundation/NSUserDefaults.h>
27 #import <NGObjWeb/NSException+HTTP.h>
28 #import <NGObjWeb/WOContext+SoObjects.h>
29 #import <GDLContentStore/GCSChannelManager.h>
30 #import <GDLContentStore/GCSFolderManager.h>
31 #import <GDLContentStore/NSURL+GCS.h>
32 #import <GDLAccess/EOAdaptorChannel.h>
34 #import "SOGoFolder.h"
37 #import "SOGoParentFolder.h"
39 @implementation SOGoParentFolder
43 if ((self = [super init]))
60 + (Class) subFolderClass
62 [self subclassResponsibility: _cmd];
67 + (NSString *) gcsFolderType
69 [self subclassResponsibility: _cmd];
74 - (void) setBaseOCSPath: (NSString *) newOCSPath
76 ASSIGN (OCSPath, newOCSPath);
79 - (NSString *) defaultFolderName
84 - (void) _fetchPersonalFolders: (NSString *) sql
85 withChannel: (EOAdaptorChannel *) fc
91 NSString *key, *path, *personalName;
94 subFolderClass = [[self class] subFolderClass];
97 [fc evaluateExpressionX: sql];
98 attrs = [fc describeResults: NO];
99 row = [fc fetchAttributes: attrs withZone: NULL];
103 = [subFolderClass objectWithName: [row objectForKey: @"c_path4"]
105 key = [row objectForKey: @"c_path4"];
106 hasPersonal = (hasPersonal || [key isEqualToString: @"personal"]);
107 [folder setOCSPath: [NSString stringWithFormat: @"%@/%@",
109 [subFolders setObject: folder forKey: key];
110 row = [fc fetchAttributes: attrs withZone: NULL];
115 folder = [subFolderClass objectWithName: @"personal" inContainer: self];
116 personalName = [self labelForKey: [self defaultFolderName]];
117 [folder setDisplayName: personalName];
118 path = [NSString stringWithFormat: @"/Users/%@/%@/personal",
119 [self ownerInContext: context],
121 [folder setOCSPath: path];
122 [subFolders setObject: folder forKey: @"personal"];
126 - (void) appendPersonalSources
128 GCSChannelManager *cm;
129 EOAdaptorChannel *fc;
130 NSURL *folderLocation;
131 NSString *sql, *gcsFolderType;
133 cm = [GCSChannelManager defaultChannelManager];
135 = [[GCSFolderManager defaultFolderManager] folderInfoLocation];
136 fc = [cm acquireOpenChannelForURL: folderLocation];
139 gcsFolderType = [[self class] gcsFolderType];
142 = [NSString stringWithFormat: (@"SELECT c_path4 FROM %@"
143 @" WHERE c_path2 = '%@'"
144 @" AND c_folder_type = '%@'"),
145 [folderLocation gcsTableName],
146 [self ownerInContext: context],
148 [self _fetchPersonalFolders: sql withChannel: fc];
149 [cm releaseChannel: fc];
150 // sql = [sql stringByAppendingFormat:@" WHERE %@ = '%@'",
151 // uidColumnName, [self uid]];
155 - (void) appendSystemSources
159 - (void) appendSubscribedSources
161 NSArray *subscribedReferences;
162 NSUserDefaults *settings;
163 NSEnumerator *allKeys;
164 NSString *currentKey;
165 SOGoFolder *subscribedFolder;
167 settings = [[context activeUser] userSettings];
168 subscribedReferences = [[settings objectForKey: nameInContainer]
169 objectForKey: @"SubscribedFolders"];
170 if ([subscribedReferences isKindOfClass: [NSArray class]])
172 allKeys = [subscribedReferences objectEnumerator];
173 currentKey = [allKeys nextObject];
177 = [subFolderClass folderWithSubscriptionReference: currentKey
179 [subFolders setObject: subscribedFolder
180 forKey: [subscribedFolder nameInContainer]];
181 currentKey = [allKeys nextObject];
186 - (NSException *) newFolderWithName: (NSString *) name
187 nameInContainer: (NSString **) newNameInContainer
189 NSString *newFolderID;
190 SOGoFolder *newFolder;
194 subFolderClass = [[self class] subFolderClass];
196 *newNameInContainer = nil;
197 newFolderID = [self globallyUniqueObjectId];
198 newFolder = [subFolderClass objectWithName: newFolderID inContainer: self];
199 if ([newFolder isKindOfClass: [NSException class]])
200 error = (NSException *) newFolder;
203 [newFolder setDisplayName: name];
204 [newFolder setOCSPath: [NSString stringWithFormat: @"%@/%@",
205 OCSPath, newFolderID]];
206 if ([newFolder create])
209 *newNameInContainer = newFolderID;
212 error = [NSException exceptionWithHTTPStatus: 400
213 reason: @"The new folder could not be created"];
219 - (void) initSubFolders
225 subFolders = [NSMutableDictionary new];
226 [self appendPersonalSources];
227 [self appendSystemSources];
228 login = [[context activeUser] login];
229 if ([login isEqualToString: owner])
230 [self appendSubscribedSources];
234 - (id) lookupName: (NSString *) name
235 inContext: (WOContext *) lookupContext
236 acquire: (BOOL) acquire
240 /* first check attributes directly bound to the application */
241 obj = [super lookupName: name inContext: lookupContext acquire: NO];
245 [self initSubFolders];
247 obj = [subFolders objectForKey: name];
253 - (NSArray *) toManyRelationshipKeys
256 [self initSubFolders];
258 return [subFolders allKeys];
261 - (NSArray *) subFolders
264 [self initSubFolders];
266 return [subFolders allValues];
270 - (NSArray *) aclsForUser: (NSString *) uid
275 - (BOOL) davIsCollection
280 - (NSString *) davContentType
282 return @"httpd/unix-directory";