]> err.no Git - scalable-opengroupware.org/blob - OGoContentStore/OCSContactFieldExtractor.m
7b53fd061840140723565e508d21610ff7d31564
[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 <NGCards/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 {
47   NSMutableDictionary *fields;
48   NSArray *values;
49   CardElement *adr;
50   NSString *value;
51   unsigned int max;
52
53   if (_vCard == nil)
54     return nil;
55
56   fields = [NSMutableDictionary dictionaryWithCapacity:16];
57
58   value = [_vCard fn];
59   if (value)
60     [fields setObject: value forKey: @"cn"];
61   values = [_vCard n];
62   if (values)
63     {
64       max = [values count];
65       if (max > 0)
66         {
67           [fields setObject: [values objectAtIndex: 0] forKey: @"sn"];
68           if (max > 1)
69             [fields setObject: [values objectAtIndex: 1]
70                     forKey: @"givenName"];
71         }
72     }
73   value = [_vCard preferredTel];
74   if (value)
75     [fields setObject: value forKey: @"telephoneNumber"];
76   value = [_vCard preferredEMail];
77   if (value)
78     [fields setObject: value forKey: @"mail"];
79   values = [_vCard org];
80   if (values)
81     {
82       max = [values count];
83       if (max > 0)
84         {
85           [fields setObject: [values objectAtIndex: 0] forKey: @"o"];
86           if (max > 1)
87             [fields setObject: [values objectAtIndex: 1] forKey: @"ou"];
88         }
89     }
90   adr = [_vCard preferredAdr];
91   if (adr)
92     [fields setObject: [adr value: 3] forKey: @"l"];
93
94   return fields;
95 }
96
97 - (NSMutableDictionary *)extractQuickFieldsFromVCardString:(NSString *)_str {
98   NGVCard *vCard;
99   
100   if ((vCard = [NGVCard parseSingleFromSource: _str]) == nil) {
101     [self errorWithFormat:@"Could not parse content as a vCard."];
102     return nil;
103   }
104   
105   return [self extractQuickFieldsFromVCard: vCard];
106 }
107
108 - (NSMutableDictionary *)extractQuickFieldsFromContent:(NSString *)_content {
109   NSMutableDictionary *fields;
110   NSDictionary *plist;
111   unsigned i;
112   
113   if ([_content length] == 0)
114     return nil;
115   
116   if ([_content hasPrefix:@"BEGIN:VCARD"])
117     return [self extractQuickFieldsFromVCardString:_content];
118   
119   // TODO: we want to support vcard storage in the future?!
120   
121   if ((plist = [_content propertyList]) == nil) {
122     [self logWithFormat:@"ERROR: could not parse property list content!"];
123     return nil;
124   }
125   
126   if (![plist isKindOfClass:[NSDictionary class]]) {
127     [self logWithFormat:@"ERROR: parsed property list is not a dictionary!"];
128     return nil;
129   }
130   
131   fields = [NSMutableDictionary dictionaryWithCapacity:16];
132   
133   /* copy field values to quick record */
134   for (i = 0; fieldNames[i] != nil; i += 2) {
135     NSString *fieldName, *sqlName;
136     id value;
137     
138     fieldName = fieldNames[i];
139     sqlName   = [fieldName lowercaseString]; /* actually pgsql doesn't care */
140     
141     value = [plist objectForKey:fieldName];
142     if ([value isNotNull])
143       [fields setObject:value forKey:sqlName];
144     else
145       [fields setObject:[NSNull null] forKey:sqlName];
146   }
147   
148   return fields;
149 }
150
151 @end /* OCSContactFieldExtractor */