]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoGroupFolder.m
moved SOGo files up
[scalable-opengroupware.org] / SoObjects / SOGo / SOGoGroupFolder.m
1 /*
2   Copyright (C) 2004 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 // $Id: SOGoGroupFolder.m 115 2004-06-30 11:57:37Z helge $
22
23 #include "SOGoGroupFolder.h"
24 #include "common.h"
25
26 @implementation SOGoGroupFolder
27
28 static NGLogger *logger = nil;
29
30 + (void)initialize {
31   NGLoggerManager *lm;
32   static BOOL     didInit = NO;
33   
34   if (didInit) return;
35   didInit = YES;
36   
37   lm     = [NGLoggerManager defaultLoggerManager];
38   logger = [lm loggerForDefaultKey:@"SOGoGroupFolderDebugEnabled"];
39 }
40
41 - (void)dealloc {
42   [self->uidToFolder release];
43   [self->folders     release];
44   [super dealloc];
45 }
46
47 /* logging */
48
49 - (id)debugLogger {
50   return logger;
51 }
52
53 /* accessors */
54
55 - (NSArray *)uids {
56   [self errorWithFormat:@"instantiated abstract Group folder class!"];
57   return nil;
58 }
59
60 /* folder management */
61
62 - (id)_primaryLookupFolderForUID:(NSString *)_uid inContext:(id)_ctx {
63   NSException *error = nil;
64   NSArray     *path;
65   id          ctx, result;
66
67   /* create subcontext, so that we don't destroy our environment */
68   
69   if ((ctx = [_ctx createSubContext]) == nil) {
70     [self errorWithFormat:@"could not create SOPE subcontext!"];
71     return nil;
72   }
73   
74   /* build path */
75   
76   path = _uid != nil ? [NSArray arrayWithObjects:&_uid count:1] : nil;
77   
78   /* traverse path */
79   
80   result = [[ctx application] traversePathArray:path inContext:ctx
81                               error:&error acquire:NO];
82   if (error != nil) {
83     [self errorWithFormat:@"folder lookup failed (uid=%@): %@",
84             _uid, error];
85     return nil;
86   }
87   
88   if (logger)
89     [self debugWithFormat:@"Note: got folder for uid %@ path %@: %@",
90                                 _uid, [path componentsJoinedByString:@"=>"], result];
91   return result;
92 }
93
94 - (void)_setupFolders {
95   WOContext           *ctx;
96   NSMutableDictionary *md;
97   NSMutableArray      *ma;
98   NSArray  *luids;
99   unsigned i, count;
100   
101   if (self->uidToFolder != nil)
102     return;
103   if ((luids = [self uids]) == nil)
104     return;
105   
106   ctx = [[WOApplication application] context];
107   
108   count = [luids count];
109   ma = [NSMutableArray arrayWithCapacity:count + 1];
110   md = [NSMutableDictionary dictionaryWithCapacity:count];
111   
112   for (i = 0; i < count; i++) {
113     NSString *uid;
114     id folder;
115     
116     uid    = [luids objectAtIndex:i];
117     folder = [self _primaryLookupFolderForUID:uid inContext:ctx];
118     
119     if ([folder isNotNull]) {
120       [md setObject:folder forKey:uid];
121       [ma addObject:folder];
122     }
123     else
124       [ma addObject:[NSNull null]];
125   }
126   
127   /* fix results */
128   self->uidToFolder = [md copy];
129   self->folders     = [[NSArray alloc] initWithArray:ma];
130 }
131
132 - (NSArray *)memberFolders {
133   [self _setupFolders];
134   return self->folders;
135 }
136
137 - (id)folderForUID:(NSString *)_uid {
138   [self _setupFolders];
139   
140   if ([_uid length] == 0)
141     return nil;
142   
143   return [self->uidToFolder objectForKey:_uid];
144 }
145
146 - (void)resetFolderCaches {
147   [self->uidToFolder release]; self->uidToFolder = nil;
148   [self->folders     release]; self->folders     = nil;
149 }
150 - (void)sleep {
151   [self resetFolderCaches];
152   [super sleep];
153 }
154
155 /* SOPE */
156
157 - (BOOL)isFolderish {
158   return YES;
159 }
160
161 /* looking up shared objects */
162
163 - (SOGoGroupsFolder *)lookupGroupsFolder {
164   return [[self container] lookupGroupsFolder];
165 }
166
167 /* pathes */
168
169 /* name lookup */
170
171 - (id)groupCalendar:(NSString *)_key inContext:(id)_ctx {
172   static Class calClass = Nil;
173   id calendar;
174   
175   if (calClass == Nil)
176     calClass = NSClassFromString(@"SOGoGroupAppointmentFolder");
177   if (calClass == Nil) {
178     [self errorWithFormat:@"missing SOGoGroupAppointmentFolder class!"];
179     return nil;
180   }
181   
182   calendar = [[calClass alloc] initWithName:_key inContainer:self];
183   
184   // TODO: should we pass over the uids in questions or should the
185   //       appointment folder query its container for that info?
186   
187   return [calendar autorelease];
188 }
189
190 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
191   id obj;
192   
193   /* first check attributes directly bound to the application */
194   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
195     return obj;
196   
197   if ([_key isEqualToString:@"Calendar"])
198     return [self groupCalendar:_key inContext:_ctx];
199   
200   /* return 404 to stop acquisition */
201   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
202 }
203
204 @end /* SOGoGroupFolder */