]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Contacts/SOGoContactGCSFolder.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1073 d1b88da0-ebda-0310...
[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
93   /* first check attributes directly bound to the application */
94   obj = [super lookupName:_key inContext:_ctx acquire:NO];
95   if (!obj)
96     {
97       if ([[[_ctx request] method] isEqualToString: @"PUT"])
98         {
99           obj = [[SOGoContactGCSEntry alloc] initWithName: _key
100                                              inContainer: self];
101           [obj autorelease];
102         }
103       else
104         obj = [self lookupContactWithId: _key];
105     }
106   if (!obj)
107     obj = [NSException exceptionWithHTTPStatus:404 /* Not Found */];
108
109 // #if 0
110 //     if ([[self ocsFolder] versionOfContentWithName:_key])
111 // #endif
112 //       return [self contactWithName:_key inContext:_ctx];
113 //   }
114
115   /* return 404 to stop acquisition */
116   return obj;
117 }
118
119 /* fetching */
120 - (EOQualifier *) _qualifierForFilter: (NSString *) filter
121 {
122   NSString *qs;
123   EOQualifier *qualifier;
124
125   if (filter && [filter length] > 0)
126     {
127       qs = [NSString stringWithFormat:
128                        @"(sn isCaseInsensitiveLike: '%@%%') OR "
129                      @"(givenname isCaseInsensitiveLike: '%@%%') OR "
130                      @"(mail isCaseInsensitiveLike: '%@%%') OR "
131                      @"(telephonenumber isCaseInsensitiveLike: '%%%@%%')",
132                      filter, filter, filter, filter];
133       qualifier = [EOQualifier qualifierWithQualifierFormat: qs];
134     }
135   else
136     qualifier = nil;
137
138   return qualifier;
139 }
140
141 - (NSArray *) lookupContactsWithFilter: (NSString *) filter
142                                 sortBy: (NSString *) sortKey
143                               ordering: (NSComparisonResult) sortOrdering
144 {
145   NSArray *fields, *records;
146   EOQualifier *qualifier;
147   EOSortOrdering *ordering;
148
149 //   NSLog (@"fetching records matching '%@', sorted by '%@' in order %d",
150 //          filter, sortKey, sortOrdering);
151
152   fields = folderListingFields;
153   qualifier = [self _qualifierForFilter: filter];
154   records = [[self ocsFolder] fetchFields: fields
155                               matchingQualifier: qualifier];
156   if ([records count] > 1)
157     {
158       ordering
159         = [EOSortOrdering sortOrderingWithKey: sortKey
160                           selector: ((sortOrdering == NSOrderedDescending)
161                                      ? EOCompareCaseInsensitiveDescending
162                                      : EOCompareCaseInsensitiveAscending)];
163       records
164         = [records sortedArrayUsingKeyOrderArray:
165                      [NSArray arrayWithObject: ordering]];
166     }
167   else
168     [self errorWithFormat:@"(%s): fetch failed!", __PRETTY_FUNCTION__];
169
170   //[self debugWithFormat:@"fetched %i records.", [records count]];
171   return records;
172 }
173
174 - (NSString *) groupDavResourceType
175 {
176   return @"vcard-collection";
177 }
178
179 - (NSException *) delete
180 {
181   return (([nameInContainer isEqualToString: @"personal"])
182           ? [NSException exceptionWithHTTPStatus: 403
183                          reason: @"the 'personal' folder cannot be deleted"]
184           : [super delete]);
185 }
186
187 // /* GET */
188
189 // - (id) GETAction: (id)_ctx
190 // {
191 //   // TODO: I guess this should really be done by SOPE (redirect to
192 //   //       default method)
193 //   WOResponse *r;
194 //   NSString *uri;
195
196 //   uri = [[_ctx request] uri];
197 //   if (![uri hasSuffix:@"/"]) uri = [uri stringByAppendingString:@"/"];
198 //   uri = [uri stringByAppendingString:@"view"];
199   
200 //   r = [_ctx response];
201 //   [r setStatus:302 /* moved */];
202 //   [r setHeader:uri forKey:@"location"];
203 //   return r;
204 // }
205
206 /* folder type */
207
208 - (NSString *) folderType
209 {
210   return @"Contact";
211 }
212
213 - (NSString *) outlookFolderClass
214 {
215   return @"IPF.Contact";
216 }
217
218 @end /* SOGoContactGCSFolder */