]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/SOGo/SOGoContentObject.m
minor cleanups
[scalable-opengroupware.org] / SOGo / SoObjects / SOGo / SOGoContentObject.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 "SOGoContentObject.h"
23 #include "common.h"
24 #include <OGoContentStore/OCSFolder.h>
25
26 @implementation SOGoContentObject
27
28 - (void)dealloc {
29   [self->content release];
30   [self->ocsPath release];
31   [super dealloc];
32 }
33
34 /* notifications */
35
36 - (void)sleep {
37   [self->content release]; self->content = nil;
38   [super sleep];
39 }
40
41 /* accessors */
42
43 - (BOOL)isFolderish {
44   return NO;
45 }
46
47 - (void)setOCSPath:(NSString *)_path {
48   if ([self->ocsPath isEqualToString:_path])
49     return;
50   
51   if (self->ocsPath)
52     [self warnWithFormat:@"OCS path is already set! '%@'", _path];
53   
54   ASSIGNCOPY(self->ocsPath, _path);
55 }
56
57 - (NSString *)ocsPath {
58   if (self->ocsPath == nil) {
59     NSString *p;
60     
61     if ((p = [self ocsPathOfContainer]) != nil) {
62       if (![p hasSuffix:@"/"]) p = [p stringByAppendingString:@"/"];
63       p = [p stringByAppendingString:[self nameInContainer]];
64       self->ocsPath = [p copy];
65     }
66   }
67   return self->ocsPath;
68 }
69
70 - (NSString *)ocsPathOfContainer {
71   if (![[self container] respondsToSelector:@selector(ocsPath)])
72     return nil;
73
74   return [[self container] ocsPath];
75 }
76
77 - (OCSFolder *)ocsFolder {
78   if (![[self container] respondsToSelector:@selector(ocsFolder)])
79     return nil;
80   
81   return [[self container] ocsFolder];
82 }
83
84 /* content */
85
86 - (NSString *)contentAsString {
87   OCSFolder *folder;
88
89   if (self->content != nil)
90     return self->content;
91   
92   if ((folder = [self ocsFolder]) == nil) {
93     [self logWithFormat:@"did not find folder of appointment."];
94     return nil;
95   }
96   
97   self->content = [[folder fetchContentWithName:[self nameInContainer]] copy];
98   return self->content;
99 }
100
101 - (NSException *)saveContentString:(NSString *)_str {
102   /* Note: "iCal multifolder saves" are implemented in the apt subclass! */
103   OCSFolder   *folder;
104   NSException *ex;
105
106   if ((folder = [self ocsFolder]) == nil) {
107     [self logWithFormat:@"did not find folder of appointment."];
108     return nil;
109   }
110   if ((ex = [folder writeContent:_str toName:[self nameInContainer]])) {
111     [self logWithFormat:@"write failed: %@", ex];
112     return ex;
113   }
114   return nil;
115 }
116
117 - (NSException *)delete {
118   /* Note: "iCal multifolder saves" are implemented in the apt subclass! */
119   OCSFolder   *folder;
120   NSException *ex;
121   
122   if ((folder = [self ocsFolder]) == nil) {
123     [self logWithFormat:@"did not find folder of appointment."];
124     return nil;
125   }
126   
127   if ((ex = [folder deleteContentWithName:[self nameInContainer]])) {
128     [self logWithFormat:@"delete failed: %@", ex];
129     return ex;
130   }
131   return nil;
132 }
133
134 /* actions */
135
136 - (id)PUTAction:(WOContext *)_ctx {
137   WORequest   *rq;
138   NSException *error;
139   
140   rq = [_ctx request];
141   if ((error = [self saveContentString:[rq contentAsString]]) != nil)
142     return error;
143   
144   // TODO: this should be automatic if we return nil?
145   [[_ctx response] setStatus:201 /* Created */];
146   return [_ctx response];
147 }
148
149 /* WebDAV */
150
151 - (BOOL)davIsCollection {
152   return [self isFolderish];
153 }
154
155 /* message type */
156
157 - (NSString *)outlookMessageClass {
158   return nil;
159 }
160
161 /* description */
162
163 - (void)appendAttributesToDescription:(NSMutableString *)_ms {
164   [super appendAttributesToDescription:_ms];
165   
166   [_ms appendFormat:@" ocs=%@", [self ocsPath]];
167 }
168
169 @end /* SOGoContentObject */