]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Contacts/SOGoContactGCSFolder.m
d47e8ee36292cd4808e38a0432e45e416d38d0c9
[scalable-opengroupware.org] / SoObjects / Contacts / SOGoContactGCSFolder.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 #import <GDLContentStore/GCSFolder.h>
23
24 #import "common.h"
25
26 #import "SOGoContactGCSEntry.h"
27 #import "SOGoContactGCSFolder.h"
28
29 #define folderListingFields [NSArray arrayWithObjects: @"c_name", @"cn", \
30                                      @"givenname", @"screenname", \
31                                      @"o", @"mail", @"telephonenumber", \
32                                      nil]
33
34 @implementation SOGoContactGCSFolder
35
36 + (id <SOGoContactFolder>) contactFolderWithName: (NSString *) aName
37                                   andDisplayName: (NSString *) aDisplayName
38                                      inContainer: (SOGoObject *) aContainer
39 {
40   SOGoContactGCSFolder *folder;
41
42   folder = [[self alloc] initWithName: aName
43                          andDisplayName: aDisplayName
44                          inContainer: aContainer];
45   [folder autorelease];
46
47   return folder;
48 }
49
50 - (void) dealloc
51 {
52   [displayName release];
53   [super dealloc];
54 }
55
56 - (id <SOGoContactFolder>) initWithName: (NSString *) newName
57                          andDisplayName: (NSString *) newDisplayName
58                             inContainer: (SOGoObject *) newContainer
59 {
60   if ((self = [self initWithName: newName
61                     inContainer: newContainer]))
62     ASSIGN (displayName, newDisplayName);
63
64   return self;
65 }
66
67 - (NSString *) displayName
68 {
69   return displayName;
70 }
71
72 /* name lookup */
73
74 - (id <SOGoContactObject>) lookupContactWithId: (NSString *) recordId
75 {
76   SOGoContactGCSEntry *contact;
77
78   if ([recordId length] > 0)
79     contact = [SOGoContactGCSEntry objectWithName: recordId
80                                    inContainer: self];
81   else
82     contact = nil;
83
84   return contact;
85 }
86
87 - (id) lookupName: (NSString *) _key
88         inContext: (WOContext *) _ctx
89           acquire: (BOOL) _flag
90 {
91   id obj;
92   BOOL isPut;
93
94   isPut = NO;
95   /* first check attributes directly bound to the application */
96   obj = [super lookupName:_key inContext:_ctx acquire:NO];
97   if (!obj)
98     {
99       if ([[[_ctx request] method] isEqualToString: @"PUT"])
100         {
101           if ([_key isEqualToString: @"PUT"])
102             isPut = YES;
103           else
104             obj = [SOGoContactGCSEntry objectWithName: _key
105                                        inContainer: self];
106         }
107       else
108         obj = [self lookupContactWithId: _key];
109     }
110 //   if (!(obj || isPut))
111 //     obj = [NSException exceptionWithHTTPStatus:404 /* Not Found */];
112
113 // #if 0
114 //     if ([[self ocsFolder] versionOfContentWithName:_key])
115 // #endif
116 //       return [self contactWithName:_key inContext:_ctx];
117 //   }
118
119   /* return 404 to stop acquisition */
120   return obj;
121 }
122
123 /* fetching */
124 - (EOQualifier *) _qualifierForFilter: (NSString *) filter
125 {
126   NSString *qs;
127   EOQualifier *qualifier;
128
129   if (filter && [filter length] > 0)
130     {
131       qs = [NSString stringWithFormat:
132                        @"(sn isCaseInsensitiveLike: '%@%%') OR "
133                      @"(givenname isCaseInsensitiveLike: '%@%%') OR "
134                      @"(mail isCaseInsensitiveLike: '%@%%') OR "
135                      @"(telephonenumber isCaseInsensitiveLike: '%%%@%%')",
136                      filter, filter, filter, filter];
137       qualifier = [EOQualifier qualifierWithQualifierFormat: qs];
138     }
139   else
140     qualifier = nil;
141
142   return qualifier;
143 }
144
145 - (NSArray *) lookupContactsWithFilter: (NSString *) filter
146                                 sortBy: (NSString *) sortKey
147                               ordering: (NSComparisonResult) sortOrdering
148 {
149   NSArray *fields, *records;
150   EOQualifier *qualifier;
151   EOSortOrdering *ordering;
152
153 //   NSLog (@"fetching records matching '%@', sorted by '%@' in order %d",
154 //          filter, sortKey, sortOrdering);
155
156   fields = folderListingFields;
157   qualifier = [self _qualifierForFilter: filter];
158   records = [[self ocsFolder] fetchFields: fields
159                               matchingQualifier: qualifier];
160   if ([records count] > 1)
161     {
162       ordering
163         = [EOSortOrdering sortOrderingWithKey: sortKey
164                           selector: ((sortOrdering == NSOrderedDescending)
165                                      ? EOCompareCaseInsensitiveDescending
166                                      : EOCompareCaseInsensitiveAscending)];
167       records
168         = [records sortedArrayUsingKeyOrderArray:
169                      [NSArray arrayWithObject: ordering]];
170     }
171   else
172     [self errorWithFormat:@"(%s): fetch failed!", __PRETTY_FUNCTION__];
173
174   //[self debugWithFormat:@"fetched %i records.", [records count]];
175   return records;
176 }
177
178 - (NSString *) groupDavResourceType
179 {
180   return @"vcard-collection";
181 }
182
183 - (NSException *) delete
184 {
185   return (([nameInContainer isEqualToString: @"personal"])
186           ? [NSException exceptionWithHTTPStatus: 403
187                          reason: @"the 'personal' folder cannot be deleted"]
188           : [super delete]);
189 }
190
191 // /* GET */
192
193 // - (id) GETAction: (id)_ctx
194 // {
195 //   // TODO: I guess this should really be done by SOPE (redirect to
196 //   //       default method)
197 //   WOResponse *r;
198 //   NSString *uri;
199
200 //   uri = [[_ctx request] uri];
201 //   if (![uri hasSuffix:@"/"]) uri = [uri stringByAppendingString:@"/"];
202 //   uri = [uri stringByAppendingString:@"view"];
203   
204 //   r = [_ctx response];
205 //   [r setStatus:302 /* moved */];
206 //   [r setHeader:uri forKey:@"location"];
207 //   return r;
208 // }
209
210 /* folder type */
211
212 - (NSString *) folderType
213 {
214   return @"Contact";
215 }
216
217 - (NSString *) outlookFolderClass
218 {
219   return @"IPF.Contact";
220 }
221
222 @end /* SOGoContactGCSFolder */