]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoGroupFolder.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1045 d1b88da0-ebda-0310...
[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   NSMutableDictionary *md;
96   NSMutableArray      *ma;
97   NSArray  *luids;
98   unsigned i, count;
99   
100   if (self->uidToFolder != nil)
101     return;
102   if ((luids = [self uids]) == nil)
103     return;
104   
105   count = [luids count];
106   ma = [NSMutableArray arrayWithCapacity:count + 1];
107   md = [NSMutableDictionary dictionaryWithCapacity:count];
108   
109   for (i = 0; i < count; i++) {
110     NSString *uid;
111     id folder;
112     
113     uid    = [luids objectAtIndex:i];
114     folder = [self _primaryLookupFolderForUID:uid inContext: context];
115     
116     if ([folder isNotNull]) {
117       [md setObject:folder forKey:uid];
118       [ma addObject:folder];
119     }
120     else
121       [ma addObject:[NSNull null]];
122   }
123   
124   /* fix results */
125   self->uidToFolder = [md copy];
126   self->folders     = [[NSArray alloc] initWithArray:ma];
127 }
128
129 - (NSArray *)memberFolders {
130   [self _setupFolders];
131   return self->folders;
132 }
133
134 - (id)folderForUID:(NSString *)_uid {
135   [self _setupFolders];
136   
137   if ([_uid length] == 0)
138     return nil;
139   
140   return [self->uidToFolder objectForKey:_uid];
141 }
142
143 - (void)resetFolderCaches {
144   [self->uidToFolder release]; self->uidToFolder = nil;
145   [self->folders     release]; self->folders     = nil;
146 }
147 - (void)sleep {
148   [self resetFolderCaches];
149   [super sleep];
150 }
151
152 /* SOPE */
153
154 - (BOOL)isFolderish {
155   return YES;
156 }
157
158 /* looking up shared objects */
159
160 - (SOGoGroupsFolder *)lookupGroupsFolder {
161   return [[self container] lookupGroupsFolder];
162 }
163
164 /* pathes */
165
166 /* name lookup */
167
168 - (id)groupCalendar:(NSString *)_key inContext:(id)_ctx {
169   static Class calClass = Nil;
170   id calendar;
171   
172   if (calClass == Nil)
173     calClass = NSClassFromString(@"SOGoGroupAppointmentFolder");
174   if (calClass == Nil) {
175     [self errorWithFormat:@"missing SOGoGroupAppointmentFolder class!"];
176     return nil;
177   }
178   
179   calendar = [[calClass alloc] initWithName:_key inContainer:self];
180   
181   // TODO: should we pass over the uids in questions or should the
182   //       appointment folder query its container for that info?
183   
184   return [calendar autorelease];
185 }
186
187 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
188   id obj;
189   
190   /* first check attributes directly bound to the application */
191   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
192     return obj;
193   
194   if ([_key isEqualToString:@"Calendar"])
195     return [self groupCalendar:_key inContext:_ctx];
196   
197   /* return 404 to stop acquisition */
198   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
199 }
200
201 @end /* SOGoGroupFolder */