]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/SOGo/SOGoObject.m
minor cleanups
[scalable-opengroupware.org] / SOGo / SoObjects / SOGo / SOGoObject.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 #include "SOGoObject.h"
23 #include "SOGoUserFolder.h"
24 #include "common.h"
25
26 @interface SOGoObject(Content)
27 - (NSString *)contentAsString;
28 @end
29
30 @implementation SOGoObject
31
32 - (BOOL)doesRetainContainer {
33   return YES;
34 }
35
36 - (id)initWithName:(NSString *)_name inContainer:(id)_container {
37   if ((self = [super init])) {
38     self->nameInContainer = [_name copy];
39     self->container = 
40       [self doesRetainContainer] ? [_container retain] : _container;
41   }
42   return self;
43 }
44 - (id)init {
45   return [self initWithName:nil inContainer:nil];
46 }
47
48 - (void)dealloc {
49   if ([self doesRetainContainer])
50     [self->container release];
51   [self->nameInContainer release];
52   [super dealloc];
53 }
54
55 /* accessors */
56
57 - (NSString *)nameInContainer {
58   return self->nameInContainer;
59 }
60 - (id)container {
61   return self->container;
62 }
63
64 /* ownership */
65
66 - (NSString *)ownerInContext:(id)_ctx {
67   return [[self container] ownerInContext:_ctx];
68 }
69
70 /* hierarchy */
71
72 - (NSArray *)fetchSubfolders {
73   NSMutableArray *ma;
74   NSArray  *names;
75   unsigned i, count;
76   
77   if ((names = [self toManyRelationshipKeys]) == nil)
78     return nil;
79   
80   count = [names count];
81   ma    = [NSMutableArray arrayWithCapacity:count + 1];
82   for (i = 0; i < count; i++) {
83     id folder;
84     
85     folder = [self lookupName:[names objectAtIndex:i] inContext:nil 
86                    acquire:NO];
87     if (folder == nil)
88       continue;
89     if ([folder isKindOfClass:[NSException class]])
90       continue;
91     
92     [ma addObject:folder];
93   }
94   return ma;
95 }
96
97 /* looking up shared objects */
98
99 - (SOGoUserFolder *)lookupUserFolder {
100   if (![self->container respondsToSelector:_cmd])
101     return nil;
102   
103   return [self->container lookupUserFolder];
104 }
105 - (SOGoGroupsFolder *)lookupGroupsFolder {
106   return [[self lookupUserFolder] lookupGroupsFolder];
107 }
108
109 - (void)sleep {
110   if ([self doesRetainContainer])
111     [self->container release];
112   self->container = nil;
113 }
114
115 /* operations */
116
117 - (NSException *)delete {
118   return [NSException exceptionWithHTTPStatus:501 /* not implemented */
119                       reason:@"delete not yet implemented, sorry ..."];
120 }
121
122 /* KVC hacks */
123
124 - (id)valueForUndefinedKey:(NSString *)_key {
125   return nil;
126 }
127
128 /* WebDAV */
129
130 - (NSString *)davDisplayName {
131   return [self nameInContainer];
132 }
133
134 /* actions */
135
136 - (id)DELETEAction:(id)_ctx {
137   return [self delete];
138 }
139
140 - (id)GETAction:(id)_ctx {
141   // TODO: I guess this should really be done by SOPE (redirect to
142   //       default method)
143   WORequest  *rq;
144   WOResponse *r;
145   NSString *uri;
146   
147   rq = [_ctx request];
148   if ([rq isSoWebDAVRequest]) {
149     if ([self respondsToSelector:@selector(contentAsString)])
150       return [self contentAsString];
151     
152     return [NSException exceptionWithHTTPStatus:501 /* not implemented */
153                         reason:@"no WebDAV GET support?!"];
154   }
155   
156   uri = [rq uri];
157   if (![uri hasSuffix:@"/"]) uri = [uri stringByAppendingString:@"/"];
158   uri = [uri stringByAppendingString:@"view"];
159   
160   r = [_ctx response];
161   [r setStatus:302 /* moved */];
162   [r setHeader:uri forKey:@"location"];
163   return r;
164 }
165
166 /* description */
167
168 - (void)appendAttributesToDescription:(NSMutableString *)_ms {
169   if (self->nameInContainer != nil) 
170     [_ms appendFormat:@" name=%@", self->nameInContainer];
171   if (self->container != nil) {
172     [_ms appendFormat:@" container=0x%08X/%@", 
173            self->container, [self->container valueForKey:@"nameInContainer"]];
174   }
175 }
176
177 - (NSString *)description {
178   NSMutableString *ms;
179
180   ms = [NSMutableString stringWithCapacity:64];
181   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
182   [self appendAttributesToDescription:ms];
183   [ms appendString:@">"];
184   return ms;
185 }
186
187 @end /* SOGoObject */