]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoFolder.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1026 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 - (BOOL) nameExistsInFolder: (NSString *) objectName
146 {
147   NSArray *fields, *records;
148   EOQualifier *qualifier;
149
150   qualifier
151     = [EOQualifier qualifierWithQualifierFormat:
152                      [NSString stringWithFormat: @"c_name='%@'", objectName]];
153
154   fields = [NSArray arrayWithObject: @"c_name"];
155   records = [[self ocsFolder] fetchFields: fields
156                               matchingQualifier: qualifier];
157   return (records
158           && ![records isKindOfClass:[NSException class]]
159           && [records count] > 0);
160 }
161
162 - (NSDictionary *)fetchContentStringsAndNamesOfAllObjects {
163   NSDictionary *files;
164   
165   files = [[self ocsFolder] fetchContentsOfAllFiles];
166   if (![files isNotNull]) {
167     [self errorWithFormat:@"(%s): fetch failed!", __PRETTY_FUNCTION__];
168     return nil;
169   }
170   if ([files isKindOfClass:[NSException class]])
171     return files;
172   return files;
173 }
174
175 /* reflection */
176
177 - (NSString *)defaultFilenameExtension {
178   /* 
179      Override to add an extension to a filename
180      
181      Note: be careful with that, needs to be consistent with object lookup!
182   */
183   return nil;
184 }
185
186 - (NSArray *) davResourceType
187 {
188   NSArray *rType, *groupDavCollection;
189
190   if ([self respondsToSelector: @selector (groupDavResourceType)])
191     {
192       groupDavCollection = [NSArray arrayWithObjects: [self groupDavResourceType],
193                                     @"http://groupdav.org/", @"G", nil];
194       rType = [NSArray arrayWithObjects: @"collection", groupDavCollection, nil];
195     }
196   else
197     rType = [NSArray arrayWithObject: @"collection"];
198
199   return rType;
200 }
201
202 - (NSArray *) toOneRelationshipKeys {
203   /* toOneRelationshipKeys are the 'files' contained in a folder */
204   NSMutableArray *ma;
205   NSArray  *names;
206   NSString *name, *ext;
207   unsigned i, count;
208   NSRange  r;
209
210   names = [self fetchContentObjectNames];
211   count = [names count];
212   ext = [self defaultFilenameExtension];
213   if (count && [ext length] > 0)
214     {
215       ma = [NSMutableArray arrayWithCapacity: count];
216       for (i = 0; i < count; i++)
217         {
218           name = [names objectAtIndex: i];
219           r = [name rangeOfString: @"."];
220           if (r.length == 0)
221             name = [[name stringByAppendingString:@"."] stringByAppendingString: ext];
222           [ma addObject:name];
223         }
224
225       names = ma;
226     }
227
228   return names;
229 }
230
231 /* WebDAV */
232
233 - (BOOL)davIsCollection {
234   return [self isFolderish];
235 }
236
237 /* folder type */
238
239 - (NSString *)outlookFolderClass {
240   return nil;
241 }
242
243 /* description */
244
245 - (void)appendAttributesToDescription:(NSMutableString *)_ms {
246   [super appendAttributesToDescription:_ms];
247   
248   [_ms appendFormat:@" ocs=%@", [self ocsPath]];
249 }
250
251 - (NSString *)loggingPrefix {
252   return [NSString stringWithFormat:@"<0x%08X[%@]:%@>",
253                    self, NSStringFromClass([self class]),
254                    [self nameInContainer]];
255 }
256
257 @end /* SOGoFolder */