]> err.no Git - scalable-opengroupware.org/blob - OGoContentStore/OCSContactFieldExtractor.m
f1f6a56b838baf5b3e6637df3fb68ca580db8eea
[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 #import <Foundation/NSArray.h>
23 #import <Foundation/NSDictionary.h>
24 #import <NGExtensions/NSObject+Logs.h>
25
26 #import <GDLContentStore/GCSFieldExtractor.h>
27 #import <NGCards/NGVCard.h>
28
29 @interface OCSContactFieldExtractor : GCSFieldExtractor
30 @end
31
32 @implementation OCSContactFieldExtractor
33
34 - (NSMutableDictionary *) extractQuickFieldsFromVCard: (NGVCard *) vCard
35 {
36   NSMutableDictionary *fields;
37   NSArray *values;
38   CardElement *adr;
39   NSString *value;
40   unsigned int max;
41
42   fields = [NSMutableDictionary dictionaryWithCapacity: 16];
43
44   value = [vCard fn];
45   if (value)
46     [fields setObject: value forKey: @"cn"];
47   values = [vCard n];
48   if (values)
49     {
50       max = [values count];
51       if (max > 0)
52         {
53           [fields setObject: [values objectAtIndex: 0] forKey: @"sn"];
54           if (max > 1)
55             [fields setObject: [values objectAtIndex: 1]
56                     forKey: @"givenName"];
57         }
58     }
59   value = [vCard preferredTel];
60   if (value)
61     [fields setObject: value forKey: @"telephoneNumber"];
62   value = [vCard preferredEMail];
63   if (value)
64     [fields setObject: value forKey: @"mail"];
65   values = [vCard org];
66   max = [values count];
67   if (max > 0)
68     {
69       [fields setObject: [values objectAtIndex: 0] forKey: @"o"];
70       if (max > 1)
71         [fields setObject: [values objectAtIndex: 1] forKey: @"ou"];
72     }
73   adr = [vCard preferredAdr];
74   if (adr)
75     [fields setObject: [adr value: 3] forKey: @"l"];
76   value = [[vCard uniqueChildWithTag: @"X-AIM"] value: 0];
77   [fields setObject: value forKey: @"screenname"];
78
79   return fields;
80 }
81
82 - (NSMutableDictionary *) extractQuickFieldsFromContent: (NSString *) content
83 {
84   NSMutableDictionary *fields;
85   NGVCard *vCard;
86
87   fields = nil;
88   if ([content length] > 0
89       && [[content uppercaseString] hasPrefix: @"BEGIN:VCARD"])
90     {
91       vCard = [NGVCard parseSingleFromSource: content];
92       if (vCard)
93         fields = [self extractQuickFieldsFromVCard: vCard];
94       else
95         [self errorWithFormat: @"Could not parse content as a vCard."];
96     }
97   else
98     [self errorWithFormat: @"Content is not a vCard"];
99
100   return fields;
101 }
102
103 @end /* OCSContactFieldExtractor */