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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
+
+
+
+
-
+
-
+
-
+
-
+
-
-
-
-
+
+
+
+
-
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
-
+
+
+
+
-
+
-
-
-
+
+
+
|
@implementation XMPPContactManager
- initWithConnection: (XMPPConnection*)connection_
roster: (XMPPRoster*)roster_
{
self = [super init];
@try {
connection = connection_;
[connection addDelegate: self];
roster = roster_;
[roster addDelegate: self];
contacts = [[OFMutableDictionary alloc] init];
delegates = [[XMPPMulticastDelegate alloc] init];
_connection = connection_;
[_connection addDelegate: self];
_roster = roster_;
[_roster addDelegate: self];
_contacts = [[OFMutableDictionary alloc] init];
_delegates = [[XMPPMulticastDelegate alloc] init];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[connection removeDelegate: self];
[roster removeDelegate: self];
[delegates release];
[contacts release];
[_connection removeDelegate: self];
[_roster removeDelegate: self];
[_delegates release];
[_contacts release];
[super dealloc];
}
- (void)addDelegate: (id <XMPPConnectionDelegate>)delegate
{
[delegates addDelegate: delegate];
[_delegates addDelegate: delegate];
}
- (void)removeDelegate: (id <XMPPConnectionDelegate>)delegate
{
[delegates removeDelegate: delegate];
[_delegates removeDelegate: delegate];
}
- (OFDictionary*)contacts
{
OF_GETTER(contacts, YES);
OF_GETTER(_contacts, YES);
}
- (void)rosterWasReceived: (XMPPRoster*)roster_
{
OFEnumerator *contactEnumerator;
XMPPContact *contact;
OFDictionary *rosterItems;
OFEnumerator *rosterItemEnumerator;
OFString *bareJID;
contactEnumerator = [contacts objectEnumerator];
contactEnumerator = [_contacts objectEnumerator];
while ((contact = [contactEnumerator nextObject]) != nil) {
[delegates broadcastSelector: @selector(contactManager:
didRemoveContact:)
withObject: self
withObject: contact];
[_delegates broadcastSelector: @selector(contactManager:
didRemoveContact:)
withObject: self
withObject: contact];
}
[contacts release];
[_contacts release];
contacts = [[OFMutableDictionary alloc] init];
_contacts = [[OFMutableDictionary alloc] init];
rosterItems = [roster_ rosterItems];
rosterItemEnumerator = [rosterItems keyEnumerator];
while ((bareJID = [rosterItemEnumerator nextObject]) != nil) {
contact = [[XMPPContact new] autorelease];
[contact XMPP_setRosterItem:
[rosterItems objectForKey: bareJID]];
[contacts setObject: contact
forKey: bareJID];
[delegates broadcastSelector: @selector(contactManager:
didAddContact:)
withObject: self
withObject: contact];
[_contacts setObject: contact
forKey: bareJID];
[_delegates broadcastSelector: @selector(contactManager:
didAddContact:)
withObject: self
withObject: contact];
}
}
- (void)roster: (XMPPRoster*)roster
didReceiveRosterItem: (XMPPRosterItem*)rosterItem
{
XMPPContact *contact;
OFString *bareJID = [[rosterItem JID] bareJID];
contact = [contacts objectForKey: bareJID];
contact = [_contacts objectForKey: bareJID];
if ([[rosterItem subscription] isEqual: @"remove"]) {
[contacts removeObjectForKey: bareJID];
[_contacts removeObjectForKey: bareJID];
if (contact != nil)
[delegates broadcastSelector: @selector(contactManager:
[_delegates broadcastSelector: @selector(contactManager:
didRemoveContact:)
withObject: self
withObject: contact];
return;
}
if (contact == nil) {
contact = [[XMPPContact new] autorelease];
[contact XMPP_setRosterItem: rosterItem];
[contacts setObject: contact
[_contacts setObject: contact
forKey: bareJID];
[delegates broadcastSelector: @selector(contactManager:
[_delegates broadcastSelector: @selector(contactManager:
didAddContact:)
withObject: self
withObject: contact];
} else {
[delegates broadcastSelector: @selector(contact:
[_delegates broadcastSelector: @selector(contact:
willUpdateWithRosterItem:)
withObject: contact
withObject: rosterItem];
[contact XMPP_setRosterItem: rosterItem];
}
}
- (void)connection: (XMPPConnection*)connection
didReceivePresence: (XMPPPresence*)presence
{
XMPPJID *JID = [presence from];
XMPPContact *contact = [contacts objectForKey: [JID bareJID]];
XMPPContact *contact = [_contacts objectForKey: [JID bareJID]];
if (contact == nil)
return;
// We only care for available and unavailable here, not subscriptions
if ([[presence type] isEqual: @"available"]) {
[contact XMPP_setPresence: presence
resource: [JID resource]];
[delegates broadcastSelector: @selector(contact:
[_delegates broadcastSelector: @selector(contact:
didSendPresence:)
withObject: contact
withObject: presence];
} else if ([[presence type] isEqual: @"unavailable"]) {
[contact XMPP_removePresenceForResource: [JID resource]];
[delegates broadcastSelector: @selector(contact:
didSendPresence:)
withObject: contact
withObject: presence];
[_delegates broadcastSelector: @selector(contact:
didSendPresence:)
withObject: contact
withObject: presence];
}
}
- (void)connection: (XMPPConnection*)connection
didReceiveMessage: (XMPPMessage*)message
{
XMPPJID *JID = [message from];
XMPPContact *contact = [contacts objectForKey: [JID bareJID]];
XMPPContact *contact = [_contacts objectForKey: [JID bareJID]];
if (contact == nil)
return;
[contact XMPP_setLockedOnJID: JID];
[delegates broadcastSelector: @selector(contact:didSendMessage:)
withObject: contact
withObject: message];
[_delegates broadcastSelector: @selector(contact:didSendMessage:)
withObject: contact
withObject: message];
}
@end
|