]> err.no Git - scalable-opengroupware.org/blob - OGoContentStore/OCSContactFieldExtractor.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1040 d1b88da0-ebda-0310...
[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   max = [values count];
81   if (max > 0)
82     {
83       [fields setObject: [values objectAtIndex: 0] forKey: @"o"];
84       if (max > 1)
85         [fields setObject: [values objectAtIndex: 1] forKey: @"ou"];
86     }
87   adr = [vCard preferredAdr];
88   if (adr)
89     [fields setObject: [adr value: 3] forKey: @"l"];
90   value = [[vCard uniqueChildWithTag: @"X-AIM"] value: 0];
91   [fields setObject: value forKey: @"screenname"];
92
93   return fields;
94 }
95
96 - (NSMutableDictionary *)extractQuickFieldsFromVCardString:(NSString *)_str {
97   NGVCard *vCard;
98   
99   if ((vCard = [NGVCard parseSingleFromSource: _str]) == nil) {
100     [self errorWithFormat:@"Could not parse content as a vCard."];
101     return nil;
102   }
103   
104   return [self extractQuickFieldsFromVCard: vCard];
105 }
106
107 - (NSMutableDictionary *)extractQuickFieldsFromContent:(NSString *)_content {
108   NSMutableDictionary *fields;
109   NSDictionary *plist;
110   unsigned i;
111   
112   if ([_content length] == 0)
113     return nil;
114   
115   if ([_content hasPrefix:@"BEGIN:VCARD"])
116     return [self extractQuickFieldsFromVCardString:_content];
117   
118   // TODO: we want to support vcard storage in the future?!
119   
120   if ((plist = [_content propertyList]) == nil) {
121     [self logWithFormat:@"ERROR: could not parse property list content!"];
122     return nil;
123   }
124   
125   if (![plist isKindOfClass:[NSDictionary class]]) {
126     [self logWithFormat:@"ERROR: parsed property list is not a dictionary!"];
127     return nil;
128   }
129   
130   fields = [NSMutableDictionary dictionaryWithCapacity:16];
131   
132   /* copy field values to quick record */
133   for (i = 0; fieldNames[i] != nil; i += 2) {
134     NSString *fieldName, *sqlName;
135     id value;
136     
137     fieldName = fieldNames[i];
138     sqlName   = [fieldName lowercaseString]; /* actually pgsql doesn't care */
139     
140     value = [plist objectForKey:fieldName];
141     if ([value isNotNull])
142       [fields setObject:value forKey:sqlName];
143     else
144       [fields setObject:[NSNull null] forKey:sqlName];
145   }
146   
147   return fields;
148 }
149
150 @end /* OCSContactFieldExtractor */