]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoFolder.m
c79c0df949770111324e2145aa2e797c48eed7c7
[scalable-opengroupware.org] / SoObjects / SOGo / SOGoFolder.m
1 /*
2   Copyright (C) 2004-2005 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
22 #import <NGObjWeb/SoObject.h>
23 #import <GDLContentStore/GCSFolderManager.h>
24 #import <GDLContentStore/GCSFolder.h>
25 #import <GDLContentStore/GCSFolderType.h>
26
27 #import "SOGoFolder.h"
28 #import "common.h"
29 #import <unistd.h>
30 #import <stdlib.h>
31
32 #import "SOGoAclsFolder.h"
33
34 @implementation SOGoFolder
35
36 + (int)version {
37   return [super version] + 0 /* v0 */;
38 }
39 + (void)initialize {
40   NSAssert2([super version] == 0,
41             @"invalid superclass (%@) version %i !",
42             NSStringFromClass([self superclass]), [super version]);
43 }
44
45 + (NSString *)globallyUniqueObjectId {
46   /*
47     4C08AE1A-A808-11D8-AC5A-000393BBAFF6
48     SOGo-Web-28273-18283-288182
49     printf( "%x", *(int *) &f);
50   */
51   static int   pid = 0;
52   static int   sequence = 0;
53   static float rndm = 0;
54   float f;
55
56   if (pid == 0) { /* break if we fork ;-) */
57     pid = getpid();
58     rndm = random();
59   }
60   sequence++;
61   f = [[NSDate date] timeIntervalSince1970];
62   return [NSString stringWithFormat:@"%0X-%0X-%0X-%0X",
63                    pid, *(int *)&f, sequence++, random];
64 }
65
66 - (void)dealloc {
67   [self->ocsFolder release];
68   [self->ocsPath   release];
69   [super dealloc];
70 }
71
72 /* accessors */
73
74 - (BOOL)isFolderish {
75   return YES;
76 }
77
78 - (void)setOCSPath:(NSString *)_path {
79   if ([self->ocsPath isEqualToString:_path])
80     return;
81   
82   if (self->ocsPath)
83     [self warnWithFormat:@"GCS path is already set! '%@'", _path];
84   
85   ASSIGNCOPY(self->ocsPath, _path);
86 }
87
88 - (NSString *)ocsPath {
89   return self->ocsPath;
90 }
91
92 - (GCSFolderManager *)folderManager {
93   static GCSFolderManager *folderManager = nil;
94
95   if (!folderManager)
96     {
97       folderManager = [GCSFolderManager defaultFolderManager];
98       [folderManager setFolderNamePrefix: @"SOGo_"];
99     }
100
101   return folderManager;
102 }
103
104 - (GCSFolder *)ocsFolderForPath:(NSString *)_path {
105   return [[self folderManager] folderAtPath:_path];
106 }
107
108 - (GCSFolder *) ocsFolder {
109   GCSFolder *folder;
110
111   if (!ocsFolder)
112     ocsFolder = [[self ocsFolderForPath:[self ocsPath]] retain];
113
114   if ([ocsFolder isNotNull])
115     folder = ocsFolder;
116   else
117     folder = nil;
118
119   return folder;
120 }
121
122 - (NSString *) folderType
123 {
124   return @"";
125 }
126
127 - (BOOL) create
128 {
129   NSException *result;
130
131   result = [[self folderManager] createFolderOfType: [self folderType]
132                                  atPath: ocsPath];
133
134   return (result == nil);
135 }
136
137 - (NSException *) delete
138 {
139   return [[self folderManager] deleteFolderAtPath: ocsPath];
140 }
141
142 - (NSArray *)fetchContentObjectNames {
143   NSArray *fields, *records;
144   
145   fields = [NSArray arrayWithObject:@"c_name"];
146   records = [[self ocsFolder] fetchFields:fields matchingQualifier:nil];
147   if (![records isNotNull]) {
148     [self errorWithFormat:@"(%s): fetch failed!", __PRETTY_FUNCTION__];
149     return nil;
150   }
151   if ([records isKindOfClass:[NSException class]])
152     return records;
153   return [records valueForKey:@"c_name"];
154 }
155
156 - (BOOL) nameExistsInFolder: (NSString *) objectName
157 {
158   NSArray *fields, *records;
159   EOQualifier *qualifier;
160
161   qualifier
162     = [EOQualifier qualifierWithQualifierFormat:
163                      [NSString stringWithFormat: @"c_name='%@'", objectName]];
164
165   fields = [NSArray arrayWithObject: @"c_name"];
166   records = [[self ocsFolder] fetchFields: fields
167                               matchingQualifier: qualifier];
168   return (records
169           && ![records isKindOfClass:[NSException class]]
170           && [records count] > 0);
171 }
172
173 - (NSDictionary *)fetchContentStringsAndNamesOfAllObjects {
174   NSDictionary *files;
175   
176   files = [[self ocsFolder] fetchContentsOfAllFiles];
177   if (![files isNotNull]) {
178     [self errorWithFormat:@"(%s): fetch failed!", __PRETTY_FUNCTION__];
179     return nil;
180   }
181   if ([files isKindOfClass:[NSException class]])
182     return files;
183   return files;
184 }
185
186 /* reflection */
187
188 - (NSString *)defaultFilenameExtension {
189   /* 
190      Override to add an extension to a filename
191      
192      Note: be careful with that, needs to be consistent with object lookup!
193   */
194   return nil;
195 }
196
197 - (NSArray *) davResourceType
198 {
199   NSArray *rType, *groupDavCollection;
200
201   if ([self respondsToSelector: @selector (groupDavResourceType)])
202     {
203       groupDavCollection = [NSArray arrayWithObjects: [self groupDavResourceType],
204                                     @"http://groupdav.org/", @"G", nil];
205       rType = [NSArray arrayWithObjects: @"collection", groupDavCollection, nil];
206     }
207   else
208     rType = [NSArray arrayWithObject: @"collection"];
209
210   return rType;
211 }
212
213 - (NSArray *) toOneRelationshipKeys {
214   /* toOneRelationshipKeys are the 'files' contained in a folder */
215   NSMutableArray *ma;
216   NSArray  *names;
217   NSString *name, *ext;
218   unsigned i, count;
219   NSRange  r;
220
221   names = [self fetchContentObjectNames];
222   count = [names count];
223   ext = [self defaultFilenameExtension];
224   if (count && [ext length] > 0)
225     {
226       ma = [NSMutableArray arrayWithCapacity: count];
227       for (i = 0; i < count; i++)
228         {
229           name = [names objectAtIndex: i];
230           r = [name rangeOfString: @"."];
231           if (r.length == 0)
232             name = [[name stringByAppendingString:@"."] stringByAppendingString: ext];
233           [ma addObject:name];
234         }
235
236       names = ma;
237     }
238
239   return names;
240 }
241
242 /* WebDAV */
243
244 - (BOOL)davIsCollection {
245   return [self isFolderish];
246 }
247
248 /* folder type */
249
250 - (NSString *)outlookFolderClass {
251   return nil;
252 }
253
254 /* description */
255
256 - (void)appendAttributesToDescription:(NSMutableString *)_ms {
257   [super appendAttributesToDescription:_ms];
258   
259   [_ms appendFormat:@" ocs=%@", [self ocsPath]];
260 }
261
262 - (NSString *)loggingPrefix {
263   return [NSString stringWithFormat:@"<0x%08X[%@]:%@>",
264                    self, NSStringFromClass([self class]),
265                    [self nameInContainer]];
266 }
267
268 @end /* SOGoFolder */