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