]> err.no Git - scalable-opengroupware.org/blob - OGoContentStore/OCSContactFieldExtractor.m
36e8a7c228bb2ebd9583884fe5c63e53e19a1db0
[scalable-opengroupware.org] / OGoContentStore / OCSContactFieldExtractor.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 #include <GDLContentStore/GCSFieldExtractor.h>
23
24 @interface OCSContactFieldExtractor : GCSFieldExtractor
25 @end
26
27 #include <NGiCal/NGVCard.h>
28 #include "common.h"
29
30 @implementation OCSContactFieldExtractor
31
32 static NSString *fieldNames[] = {
33   /* quickfield,      vCard KVC path */
34   @"givenName",       @"n.given",
35   @"cn",              @"fn.stringValue",
36   @"sn",              @"n.family",
37   @"l",               @"preferredAdr.locality",
38   @"mail",            @"preferredEMail.stringValue",
39   @"o",               @"org.orgnam",
40   @"ou",              @"org.orgunit",
41   @"telephoneNumber", @"preferredTel.stringValue",
42   nil, nil
43 };
44
45 - (NSMutableDictionary *)extractQuickFieldsFromVCard:(NGVCard *)_vCard {
46   NSMutableDictionary *fields;
47   unsigned i;
48
49   if (_vCard == nil)
50     return nil;
51   
52   fields = [NSMutableDictionary dictionaryWithCapacity:16];
53
54   for (i = 0; fieldNames[i] != nil; i += 2) {
55     id value;
56
57     value = ([fieldNames[i + 1] length] > 0)
58       ? [_vCard valueForKeyPath:fieldNames[i + 1]]
59       : nil;
60     if (![value isNotNull]) value = [NSNull null];
61     
62     [fields setObject:value forKey:[fieldNames[i] lowercaseString]];
63   }
64   return fields;
65 }
66
67 - (NSMutableDictionary *)extractQuickFieldsFromVCardString:(NSString *)_str {
68   NSArray *vCards;
69   
70   if ((vCards = [NGVCard parseVCardsFromSource:_str]) == nil) {
71     [self errorWithFormat:@"Could not parse content as a vCard."];
72     return nil;
73   }
74   if ([vCards count] == 0) {
75     [self errorWithFormat:@"Could not parse content as a vCard."];
76     return nil;
77   }
78   
79   if ([vCards count] > 1)
80     [self warnWithFormat:@"More than one vCard in content, using first."];
81   
82   return [self extractQuickFieldsFromVCard:[vCards objectAtIndex:0]];
83 }
84
85 - (NSMutableDictionary *)extractQuickFieldsFromContent:(NSString *)_content {
86   NSMutableDictionary *fields;
87   NSDictionary *plist;
88   unsigned i;
89   
90   if ([_content length] == 0)
91     return nil;
92   
93   if ([_content hasPrefix:@"BEGIN:VCARD"])
94     return [self extractQuickFieldsFromVCardString:_content];
95   
96   // TODO: we want to support vcard storage in the future?!
97   
98   if ((plist = [_content propertyList]) == nil) {
99     [self logWithFormat:@"ERROR: could not parse property list content!"];
100     return nil;
101   }
102   
103   if (![plist isKindOfClass:[NSDictionary class]]) {
104     [self logWithFormat:@"ERROR: parsed property list is not a dictionary!"];
105     return nil;
106   }
107   
108   fields = [NSMutableDictionary dictionaryWithCapacity:16];
109   
110   /* copy field values to quick record */
111   for (i = 0; fieldNames[i] != nil; i += 2) {
112     NSString *fieldName, *sqlName;
113     id value;
114     
115     fieldName = fieldNames[i];
116     sqlName   = [fieldName lowercaseString]; /* actually pgsql doesn't care */
117     
118     value = [plist objectForKey:fieldName];
119     if ([value isNotNull])
120       [fields setObject:value forKey:sqlName];
121     else
122       [fields setObject:[NSNull null] forKey:sqlName];
123   }
124   
125   return fields;
126 }
127
128 @end /* OCSContactFieldExtractor */