XMPPPresence.m
3.11 KB
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
#import "XMPPPresence.h"
#import "XMPPJID.h"
#import "NSXMLElement+XMPP.h"
#import <objc/runtime.h>
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
@implementation XMPPPresence
#if DEBUG
+ (void)initialize
{
// We use the object_setClass method below to dynamically change the class from a standard NSXMLElement.
// The size of the two classes is expected to be the same.
//
// If a developer adds instance methods to this class, bad things happen at runtime that are very hard to debug.
// This check is here to aid future developers who may make this mistake.
//
// For Fearless And Experienced Objective-C Developers:
// It may be possible to support adding instance variables to this class if you seriously need it.
// To do so, try realloc'ing self after altering the class, and then initialize your variables.
size_t superSize = class_getInstanceSize([NSXMLElement class]);
size_t ourSize = class_getInstanceSize([XMPPPresence class]);
if (superSize != ourSize)
{
NSLog(@"Adding instance variables to XMPPPresence is not currently supported!");
exit(15);
}
}
#endif
+ (XMPPPresence *)presenceFromElement:(NSXMLElement *)element
{
object_setClass(element, [XMPPPresence class]);
return (XMPPPresence *)element;
}
+ (XMPPPresence *)presence
{
return [[XMPPPresence alloc] init];
}
+ (XMPPPresence *)presenceWithType:(NSString *)type
{
return [[XMPPPresence alloc] initWithType:type to:nil];
}
+ (XMPPPresence *)presenceWithType:(NSString *)type to:(XMPPJID *)to
{
return [[XMPPPresence alloc] initWithType:type to:to];
}
- (id)init
{
self = [super initWithName:@"presence"];
return self;
}
- (id)initWithType:(NSString *)type
{
return [self initWithType:type to:nil];
}
- (id)initWithType:(NSString *)type to:(XMPPJID *)to
{
if ((self = [super initWithName:@"presence"]))
{
if (type)
[self addAttributeWithName:@"type" stringValue:type];
if (to)
[self addAttributeWithName:@"to" stringValue:[to full]];
}
return self;
}
- (id)initWithXMLString:(NSString *)string error:(NSError *__autoreleasing *)error
{
if((self = [super initWithXMLString:string error:error])){
self = [XMPPPresence presenceFromElement:self];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
NSXMLElement *element = [super copyWithZone:zone];
return [XMPPPresence presenceFromElement:element];
}
- (NSString *)type
{
NSString *type = [self attributeStringValueForName:@"type"];
if(type)
return [type lowercaseString];
else
return @"available";
}
- (NSString *)show
{
return [[self elementForName:@"show"] stringValue];
}
- (NSString *)status
{
return [[self elementForName:@"status"] stringValue];
}
- (int)priority
{
return [[[self elementForName:@"priority"] stringValue] intValue];
}
- (int)intShow
{
NSString *show = [self show];
if([show isEqualToString:@"dnd"])
return 0;
if([show isEqualToString:@"xa"])
return 1;
if([show isEqualToString:@"away"])
return 2;
if([show isEqualToString:@"chat"])
return 4;
return 3;
}
- (BOOL)isErrorPresence
{
return [[self type] isEqualToString:@"error"];
}
@end