]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Contacts/SOGoContactLDAPEntry.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1033 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / Contacts / SOGoContactLDAPEntry.m
1 /* SOGoContactLDAPEntry.m - this file is part of SOGo
2  *
3  * Copyright (C) 2006 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSDictionary.h>
25 #import <Foundation/NSString.h>
26
27 #import <NGCards/NGVCard.h>
28 #import <NGCards/CardVersitRenderer.h>
29
30 #import <NGLdap/NGLdapEntry.h>
31 #import <NGLdap/NGLdapAttribute.h>
32
33 #import "NGLdapEntry+Contact.h"
34
35 #import "SOGoContactLDAPEntry.h"
36
37 @implementation SOGoContactLDAPEntry
38
39 + (SOGoContactLDAPEntry *) contactEntryWithName: (NSString *) aName
40                                   withLDAPEntry: (NGLdapEntry *) anEntry
41                                     inContainer: (id) aContainer
42 {
43   SOGoContactLDAPEntry *entry;
44
45   entry = [[self alloc] initWithName: aName
46                         withLDAPEntry: anEntry
47                         inContainer: aContainer];
48   [entry autorelease];
49
50   return entry;
51 }
52
53 - (void) dumpEntry: (NGLdapEntry *) anEntry
54 {
55   NSArray *keys;
56   unsigned int count, max;
57
58   keys = [anEntry attributeNames];
59   max = [keys count];
60
61   NSLog (@"dumping entry...");
62   for (count = 0; count < max; count++)
63     NSLog (@"%d: %@ = '%@'", count,
64            [keys objectAtIndex: count],
65            [anEntry singleAttributeWithName: [keys objectAtIndex: count]]);
66   NSLog (@"dumping finished..");
67 }
68
69 - (id) initWithName: (NSString *) aName
70       withLDAPEntry: (NGLdapEntry *) anEntry
71         inContainer: (id) aContainer
72 {
73   if ((self = [super initWithName: aName inContainer: aContainer]))
74     {
75       vcard = nil;
76       [self setLDAPEntry: anEntry];
77     }
78
79   [self dumpEntry: anEntry];
80
81   return self;
82 }
83
84 - (void) dealloc
85 {
86   if (vcard)
87     [vcard release];
88   [super dealloc];
89 }
90
91 - (void) setLDAPEntry: (NGLdapEntry *) anEntry;
92 {
93   ldapEntry = anEntry;
94 }
95
96 - (NSString *) contentAsString
97 {
98   return [[self vCard] versitString];
99 }
100
101 - (void) _setPhonesOfVCard: (NGVCard *) vCard
102 {
103   NSString *info;
104
105   info = [ldapEntry singleAttributeWithName: @"telephoneNumber"];
106   if (info)
107     [vCard addTel: info
108            types: [NSArray arrayWithObjects: @"work", @"voice", @"pref", nil]];
109   info = [ldapEntry singleAttributeWithName: @"homePhone"];
110   if (info)
111     [vCard addTel: info
112            types: [NSArray arrayWithObjects: @"home", @"voice", nil]];
113   info = [ldapEntry singleAttributeWithName: @"fax"];
114   if (info)
115     [vCard addTel: info
116            types: [NSArray arrayWithObjects: @"work", @"fax", nil]];
117   info = [ldapEntry singleAttributeWithName: @"pager"];
118   if (info)
119     [vCard addTel: info
120            types: [NSArray arrayWithObjects: @"pager", nil]];
121   info = [ldapEntry singleAttributeWithName: @"mobile"];
122   if (info)
123     [vCard addTel: info
124            types: [NSArray arrayWithObjects: @"cell", @"voice", nil]];
125
126 // telephoneNumber: work phone
127 // homePhone: home phone
128 // fax: fax phone
129 // pager: page phone
130 // mobile: mobile phone
131   
132 }
133
134 - (NGVCard *) vCard
135 {
136   NSString *info, *surname, *streetAddress, *location;
137   CardElement *element;
138
139   if (!vcard)
140     {
141       vcard = [[NGVCard alloc] initWithUid: [self nameInContainer]];
142       [vcard setVClass: @"PUBLIC"];
143       [vcard setProdID: @"-//OpenGroupware.org//SOGo"];
144       [vcard setProfile: @"vCard"];
145       info = [ldapEntry singleAttributeWithName: @"displayName"];
146       if (!(info && [info length] > 0))
147         info = [ldapEntry singleAttributeWithName: @"cn"];
148       [vcard setFn: info];
149       surname = [ldapEntry singleAttributeWithName: @"sn"];
150       if (!surname)
151         surname = [ldapEntry singleAttributeWithName: @"surname"];
152       [vcard setNWithFamily: surname
153              given: [ldapEntry singleAttributeWithName: @"givenName"]
154              additional: nil
155              prefixes: nil
156              suffixes: nil];
157       info = [ldapEntry singleAttributeWithName: @"title"];
158       if (info)
159         [vcard setTitle: info];
160       info = [ldapEntry singleAttributeWithName: @"mozillaNickname"];
161       if (info)
162         [vcard setNickname: info];
163       info = [ldapEntry singleAttributeWithName: @"xmozillaNickname"];
164       if (info)
165         [vcard setNickname: info];
166       info = [ldapEntry singleAttributeWithName: @"notes"];
167       if (info)
168         [vcard setNote: info];
169       info = [ldapEntry singleAttributeWithName: @"mail"];
170       if (info)
171         [vcard addEmail: info
172                types: [NSArray arrayWithObjects: @"internet", @"pref", nil]];
173       [self _setPhonesOfVCard: vcard];
174       streetAddress = [ldapEntry singleAttributeWithName: @"streetAddress"];
175       location = [ldapEntry singleAttributeWithName: @"l"];
176       element = [CardElement elementWithTag: @"adr"
177                              attributes: nil values: nil];
178       [element setValue: 0 ofAttribute: @"type" to: @"work"];
179       if (streetAddress)
180         [element setValue: 2 to: streetAddress];
181       if (location)
182         [element setValue: 3 to: location];
183       if (streetAddress || location)
184         [vcard addChild: element];
185       info = [ldapEntry singleAttributeWithName: @"calFBURL"];
186       if (info)
187         [vcard addChildWithTag: @"FBURL"
188                types: nil
189                singleValue: info];
190     }
191
192   return vcard;
193 }
194
195 - (NSString *) davEntityTag
196 {
197   return [[ldapEntry attributeWithName: @"modifyTimeStamp"]
198            stringValueAtIndex: 0];
199 }
200
201 - (NSString *) davContentType
202 {
203   return @"text/x-vcard";
204 }
205
206 - (void) save
207 {
208 }
209
210 /* message type */
211
212 - (NSString *) outlookMessageClass
213 {
214   return @"IPM.Contact";
215 }
216
217 @end