-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTNStropheRosterBase.j
191 lines (163 loc) · 5.35 KB
/
TNStropheRosterBase.j
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* TNStropheRosterBase.j
*
* Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import "TNStropheConnection.j"
@import "TNStropheContact.j"
@import "TNStropheGroup.j"
@import "TNStropheJID.j"
@import "TNStropheStanza.j"
/*! @ingroup strophecappuccino
this is an implementation of the functionality shared between real rosters and MUC memberships
*/
@implementation TNStropheRosterBase : CPObject
{
CPArray _contactCache @accessors(getter=contactCache);
CPArray _groupCache @accessors(getter=groupCache);
CPArray _content @accessors(getter=content);
id _delegate @accessors(property=delegate);
TNStropheConnection _connection @accessors(getter=connection);
}
#pragma mark -
#pragma mark Class methods
+ (id)rosterWithConnection:(TNStropheConnection)aConnection
{
return [[TNStropheRosterBase alloc] initWithConnection:aConnection];
}
#pragma mark -
#pragma mark Initialization
/*! initialize a roster with a valid TNStropheConnection
@return initialized instance of TNStropheRosterBase
*/
- (id)initWithConnection:(TNStropheConnection)aConnection
{
if (self = [super init])
{
_connection = aConnection;
_content = [CPArray array];
_contactCache = [CPArray array];
_groupCache = [CPArray array];
}
return self;
}
/*! sent disconnect message to the TNStropheConnection of the roster
*/
- (void)disconnect
{
[_connection disconnect];
}
- (void)clear
{
[_content removeAllObjects];
[_contactCache removeAllObjects];
[_groupCache removeAllObjects];
}
#pragma mark -
#pragma mark Contacts
/*! get an array with all cached contacts
*/
- (void)contacts
{
return _contactCache;
}
/*! add contact to roster cache
@param aContact the contact to add
*/
- (void)cacheContact:(TNStropheContact)aContact
{
if (![_contactCache containsObject:aContact])
[_contactCache addObject:aContact];
}
/*! remove a TNStropheContact from the roster cache
@param aContact the contact to remove
*/
- (void)uncacheContact:(TNStropheContact)aContact
{
[_contactCache removeObject:aContact];
}
/*! performs contactWithFullJID and contactWithBareJID
@param aJID CPString containing the JID
@return TNStropheContact the contact with the given JID
*/
- (TNStropheContact)contactWithJID:(TNStropheJID)aJID
{
return [self contactWithFullJID:aJID] || [self contactWithBareJID:aJID];
}
/*! return a TNStropheContact object according to the given full JID
@param aJID CPString containing the JID
@return TNStropheContact the contact with the given JID
*/
- (TNStropheContact)contactWithFullJID:(TNStropheJID)aJID
{
for (var i = 0; i < [_contactCache count]; i++)
if ([[[_contactCache objectAtIndex:i] JID] fullEquals:aJID])
return [_contactCache objectAtIndex:i];
return nil;
}
/*! return a TNStropheContact object according to the given bare JID
@param aJID CPString containing the JID
@return TNStropheContact the contact with the given JID
*/
- (TNStropheContact)contactWithBareJID:(TNStropheJID)aJID
{
for (var i = 0; i < [_contactCache count]; i++)
if ([[[_contactCache objectAtIndex:i] JID] bareEquals:aJID])
return [_contactCache objectAtIndex:i];
return nil;
}
/*! perform containsFullJID and containsBareJID
@param aJID the JID to search
*/
- (BOOL)containsJID:(TNStropheJID)aJID
{
return ([self contactWithBareJID:aJID] || [self contactWithFullJID:aJID]);
}
/*! check if roster contains a contact with a given full JID
@param aJID the JID to search
@return YES is JID is in roster, NO otherwise
*/
- (BOOL)containsFullJID:(TNStropheJID)aJID
{
return ([self contactWithFullJID:aJID]) ? YES : NO;
}
/*! check if roster contains a contact with a given bare JID
@param aJID the JID to search
@return YES is JID is in roster, NO otherwise
*/
- (BOOL)containsBareJID:(TNStropheJID)aJID
{
return ([self contactWithBareJID:aJID]) ? YES : NO;
}
/*! changes the nickname of the contact with the given JID
@param aName the new nickname
@param aJID the JID of the contact to change the nickname
*/
- (void)changeNickname:(CPString)aName ofContact:(TNStropheContact)aContact
{
[aContact setNickname:aName];
}
/*! changes the nickname of the contact with the given JID
@param aName the new nickname
@param aJID the JID of the contact to change the nickname
*/
- (void)changeNickname:(CPString)aName ofContactWithJID:(TNStropheJID)aJID
{
[self changeNickname:aName ofContact:[self contactWithJID:aJID]];
}
@end