]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoParentFolder.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1174 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / SOGoParentFolder.m
1 /* SOGoParentFolder.m - this file is part of SOGo
2  *
3  * Copyright (C) 2006, 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
23 #import <Foundation/NSDictionary.h>
24 #import <Foundation/NSString.h>
25 #import <Foundation/NSUserDefaults.h>
26
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>
33
34 #import "SOGoFolder.h"
35 #import "SOGoUser.h"
36
37 #import "SOGoParentFolder.h"
38
39 @implementation SOGoParentFolder
40
41 - (id) init
42 {
43   if ((self = [super init]))
44     {
45       subFolders = nil;
46       OCSPath = nil;
47       subFolderClass = Nil;
48     }
49
50   return self;
51 }
52
53 - (void) dealloc
54 {
55   [subFolders release];
56   [OCSPath release];
57   [super dealloc];
58 }
59
60 + (Class) subFolderClass
61 {
62   [self subclassResponsibility: _cmd];
63
64   return Nil;
65 }
66
67 + (NSString *) gcsFolderType
68 {
69   [self subclassResponsibility: _cmd];
70
71   return nil;
72 }
73
74 - (void) setBaseOCSPath: (NSString *) newOCSPath
75 {
76   ASSIGN (OCSPath, newOCSPath);
77 }
78
79 - (NSString *) defaultFolderName
80 {
81   return @"Personal";
82 }
83
84 - (void) _fetchPersonalFolders: (NSString *) sql
85                    withChannel: (EOAdaptorChannel *) fc
86 {
87   NSArray *attrs;
88   NSDictionary *row;
89   SOGoFolder *folder;
90   BOOL hasPersonal;
91   NSString *key, *path, *personalName;
92
93   if (!subFolderClass)
94     subFolderClass = [[self class] subFolderClass];
95
96   hasPersonal = NO;
97   [fc evaluateExpressionX: sql];
98   attrs = [fc describeResults: NO];
99   row = [fc fetchAttributes: attrs withZone: NULL];
100   while (row)
101     {
102       folder
103         = [subFolderClass objectWithName: [row objectForKey: @"c_path4"]
104                           inContainer: self];
105       key = [row objectForKey: @"c_path4"];
106       hasPersonal = (hasPersonal || [key isEqualToString: @"personal"]);
107       [folder setOCSPath: [NSString stringWithFormat: @"%@/%@",
108                                     OCSPath, key]];
109       [subFolders setObject: folder forKey: key];
110       row = [fc fetchAttributes: attrs withZone: NULL];
111     }
112
113   if (!hasPersonal)
114     {
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],
120                        nameInContainer];
121       [folder setOCSPath: path];
122       [subFolders setObject: folder forKey: @"personal"];
123     }
124 }
125
126 - (void) appendPersonalSources
127 {
128   GCSChannelManager *cm;
129   EOAdaptorChannel *fc;
130   NSURL *folderLocation;
131   NSString *sql, *gcsFolderType;
132
133   cm = [GCSChannelManager defaultChannelManager];
134   folderLocation
135     = [[GCSFolderManager defaultFolderManager] folderInfoLocation];
136   fc = [cm acquireOpenChannelForURL: folderLocation];
137   if (fc)
138     {
139       gcsFolderType = [[self class] gcsFolderType];
140       
141       sql
142         = [NSString stringWithFormat: (@"SELECT c_path4 FROM %@"
143                                        @" WHERE c_path2 = '%@'"
144                                        @" AND c_folder_type = '%@'"),
145                     [folderLocation gcsTableName],
146                     [self ownerInContext: context],
147                     gcsFolderType];
148       [self _fetchPersonalFolders: sql withChannel: fc];
149       [cm releaseChannel: fc];
150 //       sql = [sql stringByAppendingFormat:@" WHERE %@ = '%@'", 
151 //                  uidColumnName, [self uid]];
152     }
153 }
154
155 - (void) appendSystemSources
156 {
157 }
158
159 - (void) appendSubscribedSources
160 {
161   NSArray *subscribedReferences;
162   NSUserDefaults *settings;
163   NSEnumerator *allKeys;
164   NSString *currentKey;
165   SOGoFolder *subscribedFolder;
166
167   settings = [[context activeUser] userSettings];
168   subscribedReferences = [[settings objectForKey: nameInContainer]
169                            objectForKey: @"SubscribedFolders"];
170   if ([subscribedReferences isKindOfClass: [NSArray class]])
171     {
172       allKeys = [subscribedReferences objectEnumerator];
173       currentKey = [allKeys nextObject];
174       while (currentKey)
175         {
176           subscribedFolder
177             = [subFolderClass folderWithSubscriptionReference: currentKey
178                               inContainer: self];
179           [subFolders setObject: subscribedFolder
180                       forKey: [subscribedFolder nameInContainer]];
181           currentKey = [allKeys nextObject];
182         }
183     }
184 }
185
186 - (NSException *) newFolderWithName: (NSString *) name
187                     nameInContainer: (NSString **) newNameInContainer
188 {
189   NSString *newFolderID;
190   SOGoFolder *newFolder;
191   NSException *error;
192
193   if (!subFolderClass)
194     subFolderClass = [[self class] subFolderClass];
195
196   *newNameInContainer = nil;
197   newFolderID = [self globallyUniqueObjectId];
198   newFolder = [subFolderClass objectWithName: newFolderID inContainer: self];
199   if ([newFolder isKindOfClass: [NSException class]])
200     error = (NSException *) newFolder;
201   else
202     {
203       [newFolder setDisplayName: name];
204       [newFolder setOCSPath: [NSString stringWithFormat: @"%@/%@",
205                                        OCSPath, newFolderID]];
206       if ([newFolder create])
207         {
208           error = nil;
209           *newNameInContainer = newFolderID;
210         }
211       else
212         error = [NSException exceptionWithHTTPStatus: 400
213                              reason: @"The new folder could not be created"];
214     }
215
216   return error;
217 }
218
219 - (void) initSubFolders
220 {
221   NSString *login;
222
223   if (!subFolders)
224     {
225       subFolders = [NSMutableDictionary new];
226       [self appendPersonalSources];
227       [self appendSystemSources];
228       login = [[context activeUser] login];
229       if ([login isEqualToString: owner])
230         [self appendSubscribedSources];
231     }
232 }
233
234 - (id) lookupName: (NSString *) name
235         inContext: (WOContext *) lookupContext
236           acquire: (BOOL) acquire
237 {
238   id obj;
239
240   /* first check attributes directly bound to the application */
241   obj = [super lookupName: name inContext: lookupContext acquire: NO];
242   if (!obj)
243     {
244       if (!subFolders)
245         [self initSubFolders];
246
247       obj = [subFolders objectForKey: name];
248     }
249
250   return obj;
251 }
252
253 - (NSArray *) toManyRelationshipKeys
254 {
255   if (!subFolders)
256     [self initSubFolders];
257
258   return [subFolders allKeys];
259 }
260
261 - (NSArray *) subFolders
262 {
263   if (!subFolders)
264     [self initSubFolders];
265
266   return [subFolders allValues];
267 }
268
269 /* acls */
270 - (NSArray *) aclsForUser: (NSString *) uid
271 {
272   return nil;
273 }
274
275 - (BOOL) davIsCollection
276 {
277   return YES;
278 }
279
280 - (NSString *) davContentType
281 {
282   return @"httpd/unix-directory";
283 }
284
285 @end