-
Notifications
You must be signed in to change notification settings - Fork 2
/
XMPPvCard.m
165 lines (157 loc) · 5.27 KB
/
XMPPvCard.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// XMPPvCard.m
// Jabber
//
// Created by David Chisnall on 12/11/2007.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import "XMPPvCard.h"
#import <EtoileXML/ETXMLString.h>
#import <EtoileFoundation/EtoileFoundation.h>
@interface XMPPvCardUpdate : ETXMLNullHandler {
}
@end
@implementation XMPPvCardUpdate
- (id) initWithXMLParser: (ETXMLParser*)aParser
key: (id) aKey
{
self = [super initWithXMLParser: aParser
key: aKey];
if (nil == self)
{
return nil;
}
value = @"";
return self;
}
- (void)startElement:(NSString *)aName
attributes:(NSDictionary*)attributes
{
if([aName isEqualToString:@"photo"])
{
[[[ETXMLString alloc] initWithXMLParser:parser
key:aName] startElement:aName
attributes:attributes];
}
else
{
depth++;
}
}
- (void) addphoto:(NSString*)photoHash
{
value = photoHash;
}
@end
@implementation XMPPvCard
- (id) initWithXMLParser: (ETXMLParser*)aParser
key: (id) aKey
{
self = [super initWithXMLParser: aParser
key: aKey];
if (nil == self)
{
return nil;
}
person = [[ABPerson alloc] init];
value = person;
return self;
}
- (void)startElement:(NSString *)aName
attributes:(NSDictionary*)attributes
{
if([aName isEqualToString:@"N"] || [aName isEqualToString:@"vCard"])
{
depth++;
}
else
{
#ifndef DNDEBUG
ETLog(@"Parsing vCard Element: %@", aName);
#endif
[[[ETXMLString alloc] initWithXMLParser:parser
key:aName] startElement:aName
attributes:attributes];
}
}
#define PROPERTY_FROM_XML(property, xml)\
- (void) add ## xml:(NSString*)aString\
{\
if(aString != nil && ![aString isEqualToString:@""])\
{\
[person setValue:aString forProperty:property];\
}\
}
PROPERTY_FROM_XML(kABNicknameProperty, NICKNAME)
PROPERTY_FROM_XML(kABLastNameProperty, FAMILY)
PROPERTY_FROM_XML(kABFirstNameProperty, GIVEN)
#ifdef GNUSTEP
#define MULTI_INIT ABMutableMultiValue * multi = [(ABMutableMultiValue*)[ABMutableMultiValue alloc] initWithType:kABMultiStringProperty]
#else
#define MULTI_INIT ABMutableMultiValue * multi = [[ABMutableMultiValue alloc] init]
#endif
#define MULTI_PROPERTY_FROM_XML(property, label, xml) \
- (void) add ## xml:(NSString*)aString\
{\
if(aString != nil && ![aString isEqualToString:@""])\
{\
MULTI_INIT;\
[multi addValue:aString withLabel:label];\
[person setValue:multi forProperty:property];\
}\
}
MULTI_PROPERTY_FROM_XML(kABEmailProperty, kABEmailHomeLabel, EMAIL)
MULTI_PROPERTY_FROM_XML(kABURLsProperty, kABHomePageLabel, URL)
//FIXME: This should actually be getting <PHOTO><BINVAL>{CDATA}</BINVAL></PHOTO>, not <PHOTO>{CDATA}</PHOTO>
- (void) addPHOTO:(NSString*)aString
{
NSMutableString * photo = [aString mutableCopy];
[photo replaceOccurrencesOfString:@"\n" withString:@"" options:0 range:NSMakeRange(0, [photo length])];
if([photo length] > 9 && [[photo substringToIndex:9] isEqualToString:@"image/png"])
{
[photo deleteCharactersInRange:NSMakeRange(0,9)];
}
else if([photo length] > 10 && [[photo substringToIndex:10] isEqualToString:@"image/jpeg"])
{
[photo deleteCharactersInRange:NSMakeRange(0,10)];
}
[person setImageData:[photo base64DecodedData]];
}
- (void) addFN:(NSString*)aString
{
NSArray * names;
#define GS_GNUSTEP_V GS_API_LATEST
// FIXME: Figure out a way to use GS_API_VERSION() without GNUstep base.
// The issue is that GSVersionMacros.h is LGPL-licensed.
#ifndef GNUSTEP
#define GS_API_VERSION(x, y) 1
#endif
#if GS_API_VERSION(0, 011700)
//Leopard method.
names = [aString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
#else
names = [aString componentsSeparatedByString:@" "];
#endif
switch([names count])
{
case 3:
{
[person setValue:[names objectAtIndex:0] forProperty:kABFirstNameProperty];
[person setValue:[names objectAtIndex:1] forProperty:kABMiddleNameProperty];
[person setValue:[names objectAtIndex:2] forProperty:kABLastNameProperty];
break;
}
case 2:
{
[person setValue:[names objectAtIndex:0] forProperty:kABFirstNameProperty];
[person setValue:[names objectAtIndex:1] forProperty:kABLastNameProperty];
break;
}
case 1:
{
[person setValue:[names objectAtIndex:0] forProperty:kABFirstNameProperty];
break;
}
}
}
@end