]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoFolder.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1021 d1b88da0-ebda-0310...
[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   return [GCSFolderManager defaultFolderManager];
94 }
95
96 - (GCSFolder *)ocsFolderForPath:(NSString *)_path {
97   return [[self folderManager] folderAtPath:_path];
98 }
99
100 - (GCSFolder *) ocsFolder {
101   GCSFolder *folder;
102
103   if (!ocsFolder)
104     ocsFolder = [[self ocsFolderForPath:[self ocsPath]] retain];
105
106   if ([ocsFolder isNotNull])
107     folder = ocsFolder;
108   else
109     folder = nil;
110
111   return folder;
112 }
113
114 - (NSString *) folderType
115 {
116   return @"";
117 }
118
119 - (BOOL) create
120 {
121   NSException *result;
122
123   [GCSFolderType setFolderNamePrefix: @"SOGo_"];
124
125   result = [[self folderManager] createFolderOfType: [self folderType]
126                                  atPath: ocsPath];
127
128   return (result == nil);
129 }
130
131 - (NSArray *)fetchContentObjectNames {
132   NSArray *fields, *records;
133   
134   fields = [NSArray arrayWithObject:@"c_name"];
135   records = [[self ocsFolder] fetchFields:fields matchingQualifier:nil];
136   if (![records isNotNull]) {
137     [self errorWithFormat:@"(%s): fetch failed!", __PRETTY_FUNCTION__];
138     return nil;
139   }
140   if ([records isKindOfClass:[NSException class]])
141     return records;
142   return [records valueForKey:@"c_name"];
143 }
144
145 - (NSDictionary *)fetchContentStringsAndNamesOfAllObjects {
146   NSDictionary *files;
147   
148   files = [[self ocsFolder] fetchContentsOfAllFiles];
149   if (![files isNotNull]) {
150     [self errorWithFormat:@"(%s): fetch failed!", __PRETTY_FUNCTION__];
151     return nil;
152   }
153   if ([files isKindOfClass:[NSException class]])
154     return files;
155   return files;
156 }
157
158 /* reflection */
159
160 - (NSString *)defaultFilenameExtension {
161   /* 
162      Override to add an extension to a filename
163      
164      Note: be careful with that, needs to be consistent with object lookup!
165   */
166   return nil;
167 }
168
169 - (NSArray *) davResourceType
170 {
171   NSArray *rType, *groupDavCollection;
172
173   if ([self respondsToSelector: @selector (groupDavResourceType)])
174     {
175       groupDavCollection = [NSArray arrayWithObjects: [self groupDavResourceType],
176                                     @"http://groupdav.org/", @"G", nil];
177       rType = [NSArray arrayWithObjects: @"collection", groupDavCollection, nil];
178     }
179   else
180     rType = [NSArray arrayWithObject: @"collection"];
181
182   return rType;
183 }
184
185 - (NSArray *) toOneRelationshipKeys {
186   /* toOneRelationshipKeys are the 'files' contained in a folder */
187   NSMutableArray *ma;
188   NSArray  *names;
189   NSString *name, *ext;
190   unsigned i, count;
191   NSRange  r;
192
193   names = [self fetchContentObjectNames];
194   count = [names count];
195   ext = [self defaultFilenameExtension];
196   if (count && [ext length] > 0)
197     {
198       ma = [NSMutableArray arrayWithCapacity: count];
199       for (i = 0; i < count; i++)
200         {
201           name = [names objectAtIndex: i];
202           r = [name rangeOfString: @"."];
203           if (r.length == 0)
204             name = [[name stringByAppendingString:@"."] stringByAppendingString: ext];
205           [ma addObject:name];
206         }
207
208       names = ma;
209     }
210
211   return names;
212 }
213
214 /* WebDAV */
215
216 - (BOOL)davIsCollection {
217   return [self isFolderish];
218 }
219
220 /* folder type */
221
222 - (NSString *)outlookFolderClass {
223   return nil;
224 }
225
226 /* description */
227
228 - (void)appendAttributesToDescription:(NSMutableString *)_ms {
229   [super appendAttributesToDescription:_ms];
230   
231   [_ms appendFormat:@" ocs=%@", [self ocsPath]];
232 }
233
234 - (NSString *)loggingPrefix {
235   return [NSString stringWithFormat:@"<0x%08X[%@]:%@>",
236                    self, NSStringFromClass([self class]),
237                    [self nameInContainer]];
238 }
239
240 @end /* SOGoFolder */