2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
22 #include <GDLContentStore/GCSFieldExtractor.h>
24 @interface OCSContactFieldExtractor : GCSFieldExtractor
27 #include <NGCards/NGVCard.h>
30 @implementation OCSContactFieldExtractor
32 static NSString *fieldNames[] = {
33 /* quickfield, vCard KVC path */
34 @"givenName", @"n.given",
35 @"cn", @"fn.stringValue",
37 @"l", @"preferredAdr.locality",
38 @"mail", @"preferredEMail.stringValue",
40 @"ou", @"org.orgunit",
41 @"telephoneNumber", @"preferredTel.stringValue",
45 - (NSMutableDictionary *) extractQuickFieldsFromVCard: (NGVCard *) _vCard
47 NSMutableDictionary *fields;
56 fields = [NSMutableDictionary dictionaryWithCapacity:16];
60 [fields setObject: value forKey: @"cn"];
67 [fields setObject: [values objectAtIndex: 0] forKey: @"sn"];
69 [fields setObject: [values objectAtIndex: 1]
70 forKey: @"givenName"];
73 value = [_vCard preferredTel];
75 [fields setObject: value forKey: @"telephoneNumber"];
76 value = [_vCard preferredEMail];
78 [fields setObject: value forKey: @"mail"];
79 values = [_vCard org];
85 [fields setObject: [values objectAtIndex: 0] forKey: @"o"];
87 [fields setObject: [values objectAtIndex: 1] forKey: @"ou"];
90 adr = [_vCard preferredAdr];
92 [fields setObject: [adr value: 3] forKey: @"l"];
97 - (NSMutableDictionary *)extractQuickFieldsFromVCardString:(NSString *)_str {
100 if ((vCard = [NGVCard parseSingleFromSource: _str]) == nil) {
101 [self errorWithFormat:@"Could not parse content as a vCard."];
105 return [self extractQuickFieldsFromVCard: vCard];
108 - (NSMutableDictionary *)extractQuickFieldsFromContent:(NSString *)_content {
109 NSMutableDictionary *fields;
113 if ([_content length] == 0)
116 if ([_content hasPrefix:@"BEGIN:VCARD"])
117 return [self extractQuickFieldsFromVCardString:_content];
119 // TODO: we want to support vcard storage in the future?!
121 if ((plist = [_content propertyList]) == nil) {
122 [self logWithFormat:@"ERROR: could not parse property list content!"];
126 if (![plist isKindOfClass:[NSDictionary class]]) {
127 [self logWithFormat:@"ERROR: parsed property list is not a dictionary!"];
131 fields = [NSMutableDictionary dictionaryWithCapacity:16];
133 /* copy field values to quick record */
134 for (i = 0; fieldNames[i] != nil; i += 2) {
135 NSString *fieldName, *sqlName;
138 fieldName = fieldNames[i];
139 sqlName = [fieldName lowercaseString]; /* actually pgsql doesn't care */
141 value = [plist objectForKey:fieldName];
142 if ([value isNotNull])
143 [fields setObject:value forKey:sqlName];
145 [fields setObject:[NSNull null] forKey:sqlName];
151 @end /* OCSContactFieldExtractor */