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 <NGiCal/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 {
46 NSMutableDictionary *fields;
52 fields = [NSMutableDictionary dictionaryWithCapacity:16];
54 for (i = 0; fieldNames[i] != nil; i += 2) {
57 value = ([fieldNames[i + 1] length] > 0)
58 ? [_vCard valueForKeyPath:fieldNames[i + 1]]
60 if (![value isNotNull]) value = [NSNull null];
62 [fields setObject:value forKey:[fieldNames[i] lowercaseString]];
67 - (NSMutableDictionary *)extractQuickFieldsFromVCardString:(NSString *)_str {
70 if ((vCards = [NGVCard parseVCardsFromSource:_str]) == nil) {
71 [self errorWithFormat:@"Could not parse content as a vCard."];
74 if ([vCards count] == 0) {
75 [self errorWithFormat:@"Could not parse content as a vCard."];
79 if ([vCards count] > 1)
80 [self warnWithFormat:@"More than one vCard in content, using first."];
82 return [self extractQuickFieldsFromVCard:[vCards objectAtIndex:0]];
85 - (NSMutableDictionary *)extractQuickFieldsFromContent:(NSString *)_content {
86 NSMutableDictionary *fields;
90 if ([_content length] == 0)
93 if ([_content hasPrefix:@"BEGIN:VCARD"])
94 return [self extractQuickFieldsFromVCardString:_content];
96 // TODO: we want to support vcard storage in the future?!
98 if ((plist = [_content propertyList]) == nil) {
99 [self logWithFormat:@"ERROR: could not parse property list content!"];
103 if (![plist isKindOfClass:[NSDictionary class]]) {
104 [self logWithFormat:@"ERROR: parsed property list is not a dictionary!"];
108 fields = [NSMutableDictionary dictionaryWithCapacity:16];
110 /* copy field values to quick record */
111 for (i = 0; fieldNames[i] != nil; i += 2) {
112 NSString *fieldName, *sqlName;
115 fieldName = fieldNames[i];
116 sqlName = [fieldName lowercaseString]; /* actually pgsql doesn't care */
118 value = [plist objectForKey:fieldName];
119 if ([value isNotNull])
120 [fields setObject:value forKey:sqlName];
122 [fields setObject:[NSNull null] forKey:sqlName];
128 @end /* OCSContactFieldExtractor */