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"];
83 [fields setObject: [values objectAtIndex: 0] forKey: @"o"];
85 [fields setObject: [values objectAtIndex: 1] forKey: @"ou"];
87 adr = [vCard preferredAdr];
89 [fields setObject: [adr value: 3] forKey: @"l"];
90 value = [[vCard uniqueChildWithTag: @"X-AIM"] value: 0];
91 [fields setObject: value forKey: @"screenname"];
96 - (NSMutableDictionary *)extractQuickFieldsFromVCardString:(NSString *)_str {
99 if ((vCard = [NGVCard parseSingleFromSource: _str]) == nil) {
100 [self errorWithFormat:@"Could not parse content as a vCard."];
104 return [self extractQuickFieldsFromVCard: vCard];
107 - (NSMutableDictionary *)extractQuickFieldsFromContent:(NSString *)_content {
108 NSMutableDictionary *fields;
112 if ([_content length] == 0)
115 if ([_content hasPrefix:@"BEGIN:VCARD"])
116 return [self extractQuickFieldsFromVCardString:_content];
118 // TODO: we want to support vcard storage in the future?!
120 if ((plist = [_content propertyList]) == nil) {
121 [self logWithFormat:@"ERROR: could not parse property list content!"];
125 if (![plist isKindOfClass:[NSDictionary class]]) {
126 [self logWithFormat:@"ERROR: parsed property list is not a dictionary!"];
130 fields = [NSMutableDictionary dictionaryWithCapacity:16];
132 /* copy field values to quick record */
133 for (i = 0; fieldNames[i] != nil; i += 2) {
134 NSString *fieldName, *sqlName;
137 fieldName = fieldNames[i];
138 sqlName = [fieldName lowercaseString]; /* actually pgsql doesn't care */
140 value = [plist objectForKey:fieldName];
141 if ([value isNotNull])
142 [fields setObject:value forKey:sqlName];
144 [fields setObject:[NSNull null] forKey:sqlName];
150 @end /* OCSContactFieldExtractor */