]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Contacts/SOGoContactFolder.m
changes to use NGLogging in all places
[scalable-opengroupware.org] / SOGo / SoObjects / Contacts / SOGoContactFolder.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$
22
23 #include "SOGoContactFolder.h"
24 #include <SOGo/SOGoCustomGroupFolder.h>
25 #include <SOGoLogic/AgenorUserManager.h>
26 #include <OGoContentStore/OCSFolder.h>
27 #include <NGiCal/NGiCal.h>
28 #include "common.h"
29 #include <unistd.h>
30 #include <stdlib.h>
31
32 @implementation SOGoContactFolder
33
34 static BOOL debugOn = YES;
35
36 + (NSString *)globallyUniqueObjectId {
37   /*
38     4C08AE1A-A808-11D8-AC5A-000393BBAFF6
39     SOGo-Web-28273-18283-288182
40     printf( "%x", *(int *) &f);
41   */
42   static int   pid = 0;
43   static int   sequence = 0;
44   static float rndm = 0;
45   float f;
46
47   if (pid == 0) { /* break if we fork ;-) */
48       pid = getpid();
49       rndm = random();
50   }
51   sequence++;
52   f = [[NSDate date] timeIntervalSince1970];
53   return [NSString stringWithFormat:@"%0X-%0X-%0X-%0X",
54                    pid, *(int *)&f, sequence++, random];
55 }
56
57 - (void)dealloc {
58   [super dealloc];
59 }
60
61 /* name lookup */
62
63 - (BOOL)isValidContactName:(NSString *)_key {
64   if ([_key length] == 0)
65     return NO;
66   
67   return YES;
68 }
69
70 - (id)contactWithName:(NSString *)_key inContext:(id)_ctx {
71   static Class ctClass = Nil;
72   id ct;
73   
74   if (ctClass == Nil)
75     ctClass = NSClassFromString(@"SOGoContactObject");
76   if (ctClass == Nil) {
77     [self errorWithFormat:@"missing SOGoContactObject class!"];
78     return nil;
79   }
80   
81   ct = [[ctClass alloc] initWithName:_key inContainer:self];
82   return [ct autorelease];
83 }
84
85 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
86   id obj;
87   
88   /* first check attributes directly bound to the application */
89   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
90     return obj;
91   
92   if ([self isValidContactName:_key])
93     return [self contactWithName:_key inContext:_ctx];
94
95   /* return 404 to stop acquisition */
96   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
97 }
98
99 /* fetching */
100
101 - (NSArray *)fixupRecords:(NSArray *)_records {
102   return _records;
103 }
104
105 - (NSArray *)fetchCoreInfos {
106   NSArray     *fields, *records;
107
108   fields = [NSArray arrayWithObjects:
109                       @"c_name",
110                       @"sn", @"givenname", @"l", 
111                       @"mail", @"telephonenumber",
112                     nil];
113   records = [[self ocsFolder] fetchFields:fields matchingQualifier:nil];
114   if (records == nil) {
115     [self errorWithFormat:@"(%s): fetch failed!", __PRETTY_FUNCTION__];
116     return nil;
117   }
118   records = [self fixupRecords:records];
119   if (debugOn)
120     [self logWithFormat:@"fetched %i records.", [records count]];
121   return records;
122 }
123
124 /* GET */
125
126 - (id)GETAction:(WOContext *)_ctx {
127   // TODO: I guess this should really be done by SOPE (redirect to
128   //       default method)
129   WOResponse *r;
130   NSString *uri;
131
132   uri = [[_ctx request] uri];
133   if (![uri hasSuffix:@"/"]) uri = [uri stringByAppendingString:@"/"];
134   uri = [uri stringByAppendingString:@"view"];
135   
136   r = [_ctx response];
137   [r setStatus:302 /* moved */];
138   [r setHeader:uri forKey:@"location"];
139   return r;
140 }
141
142 @end /* SOGoContactFolder */