/
TNPermissionUserFetcher.j
251 lines (204 loc) · 7.2 KB
/
TNPermissionUserFetcher.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* TNPermissionUserFetcher.j
*
* Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@import <Foundation/Foundation.j>
@import <StropheCappuccino/TNStropheStanza.j>
@import <StropheCappuccino/TNStropheContact.j>
@import <TNKit/TNTableViewLazyDataSource.j>
@class TNPermissionsCenter
var TNArchipelTypeXMPPServerUsers = @"archipel:xmppserver:users",
TNArchipelTypeXMPPServerUsersList = @"list",
TNArchipelTypeXMPPServerUsersFilter = @"filter",
TNArchipelTypeXMPPServerUsersNumber = @"number";
/*! @ingroup permissionsmodule
Shared user fetcher using the lazy loading
*/
@implementation TNPermissionUserFetcher: CPObject
{
BOOL _displaysOnlyHumans @accessors(getter=isDisplayingOnlyHumans, setter=setDisplaysOnlyHumans:);
id _delegate @accessors(property=delegate);
TNStropheContact _entity @accessors(property=entity);
TNTableViewLazyDataSource _dataSource @accessors(getter=dataSource);
int _maxLoadedPage;
}
#pragma mark -
#pragma mark Initialization
/*! Instaciate the class
*/
- (id)init
{
if (self = [super init])
{
_maxLoadedPage = 0;
_displaysOnlyHumans = YES;
}
return self;
}
#pragma mark -
#pragma mark Getters / Setters
/*! Set the target datasource, and set self ad datasource delegate
@pathForResource aDataSource the TNTableViewLazyDataSource to use
*/
- (void)setDataSource:(TNTableViewLazyDataSource)aDataSource
{
_dataSource = aDataSource;
[_dataSource setDelegate:self];
}
#pragma mark -
#pragma mark Utilities
/*! Reset informations
*/
- (void)reset
{
_maxLoadedPage = 0;
[_dataSource setTotalCount:-1];
[_dataSource setCurrentlyLoading:NO];
if ([_delegate respondsToSelector:@selector(userFetcher:isLoading:)])
[_delegate userFetcher:self isLoading:NO];
}
#pragma mark -
#pragma mark XMPP Controls
/*! Ask the agent for the total number of accounts
*/
- (void)getNumberOfXMPPUsers
{
[self getNumberOfXMPPUsers:nil];
}
/*! Ask the agent for the total number of accounts
@param aCallback eventual callback to call when user number has been fetched
*/
- (void)getNumberOfXMPPUsers:(SEL)aCallback
{
var stanza = [TNStropheStanza iqWithType:@"get"];
[stanza addChildWithName:@"query" andAttributes:{"xmlns": TNArchipelTypeXMPPServerUsers}];
[stanza addChildWithName:@"archipel" andAttributes:{
"action": TNArchipelTypeXMPPServerUsersNumber,
"humans_only": _displaysOnlyHumans ? "true" : "false"}];
[_entity sendStanza:stanza andRegisterSelector:@selector(_didGetNumberOfXMPPUsers:callback:) ofObject:self userInfo:aCallback];
}
/*! @ignore
*/
- (BOOL)_didGetNumberOfXMPPUsers:(TNStropheStanza)aStanza callback:(SEL)aCallback
{
if ([aStanza type] == @"result")
{
var total = [[aStanza firstChildWithName:@"users"] valueForAttribute:@"total"];
[_dataSource setTotalCount:total];
if (aCallback)
[self performSelector:aCallback];
}
else
{
[_delegate handleIqErrorFromStanza:aStanza];
}
}
/*! compute the answer containing the total number of users
@param aStanza TNStropheStanza containing the answer
*/
- (void)getXMPPUsers
{
if (![[TNPermissionsCenter defaultCenter] hasPermission:@"xmppserver_users_list" forEntity:_entity])
{
[_delegate userFetcherClean];
return;
}
if ([_dataSource totalCount] == -1)
{
[self getNumberOfXMPPUsers:@selector(getXMPPUsers)];
return;
}
var stanza = [TNStropheStanza iqWithType:@"get"];
[stanza addChildWithName:@"query" andAttributes:{"xmlns": TNArchipelTypeXMPPServerUsers}];
[stanza addChildWithName:@"archipel" andAttributes:{
"action": TNArchipelTypeXMPPServerUsersList,
"page": _maxLoadedPage,
"humans_only": _displaysOnlyHumans ? "true" : "false"}];
[_dataSource setCurrentlyLoading:YES];
if ([_delegate respondsToSelector:@selector(userFetcher:isLoading:)])
[_delegate userFetcher:self isLoading:YES];
[_entity sendStanza:stanza andRegisterSelector:@selector(_didGetXMPPUsers:) ofObject:self];
}
/*! Ask for entities that macthes the given filter
@param aFilter the filter to match
*/
- (void)getXMPPFilteredUsers:(CPString)aFilter
{
if (![[TNPermissionsCenter defaultCenter] hasPermission:@"xmppserver_users_list" forEntity:_entity])
{
[_delegate userFetcherClean];
return;
}
var stanza = [TNStropheStanza iqWithType:@"get"];
[stanza addChildWithName:@"query" andAttributes:{"xmlns": TNArchipelTypeXMPPServerUsers}];
[stanza addChildWithName:@"archipel" andAttributes:{
"action": TNArchipelTypeXMPPServerUsersFilter,
"filter": aFilter}];
[_dataSource setCurrentlyLoading:YES];
if ([_delegate respondsToSelector:@selector(userFetcher:isLoading:)])
[_delegate userFetcher:self isLoading:YES];
[_entity sendStanza:stanza andRegisterSelector:@selector(_didGetXMPPUsers:) ofObject:self];
}
/*! compute the answer containing the users list
@param aStanza TNStropheStanza containing the answer
*/
- (void)_didGetXMPPUsers:(TNStropheStanza)aStanza
{
if ([aStanza type] == @"result")
{
var users = [aStanza childrenWithName:@"user"];
for (var i = 0; i < [users count]; i++)
{
var user = [users objectAtIndex:i],
jid;
try {jid = [TNStropheJID stropheJIDWithString:[user valueForAttribute:@"jid"]]} catch(e){continue};
[_dataSource addObject:jid];
}
[[_dataSource table] reloadData];
}
else
{
[_delegate handleIqErrorFromStanza:aStanza];
}
[_dataSource setCurrentlyLoading:NO];
if ([_delegate respondsToSelector:@selector(userFetcher:isLoading:)])
[_delegate userFetcher:self isLoading:NO];
}
#pragma mark -
#pragma mark Delegate
/*! TNTableViewLazyDataSource delegate
*/
- (void)tableViewDataSourceNeedsLoading:(TNTableViewLazyDataSource)aDataSource
{
_maxLoadedPage++;
[self getXMPPUsers];
}
/*! TNTableViewLazyDataSource delegate
*/
- (void)tableViewDataSource:(TNTableViewLazyDataSource)aDataSource applyFilter:(CPString)aFilter
{
[_delegate userFetcherClean];
if ([aFilter length] >= 3)
[self getXMPPFilteredUsers:aFilter];
}
/*! TNTableViewLazyDataSource delegate
*/
- (void)tableViewDataSource:(TNTableViewLazyDataSource)aDataSource removeFilter:(CPString)aFilter
{
[_delegate userFetcherClean];
[self getXMPPUsers];
}
@end