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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.java.sip.communicator.impl.muc;
import java.util.*;
import java.util.regex.*;
import net.java.sip.communicator.service.contactsource.*;
import net.java.sip.communicator.service.muc.*;
import net.java.sip.communicator.service.protocol.*;
/**
* The <tt>ServerChatRoomQuery</tt> is a query over the
* <tt>ServerChatRoomContactSourceService</tt>.
*
* @author Hristo Terezov
*/
public class ServerChatRoomQuery
extends AsyncContactQuery<ContactSourceService>
implements ChatRoomProviderWrapperListener
{
/**
* The query string.
*/
private String queryString;
/**
* List with the current results for the query.
*/
private Set<BaseChatRoomSourceContact> contactResults
= new TreeSet<BaseChatRoomSourceContact>();
/**
* MUC service.
*/
private MUCServiceImpl mucService;
/**
* The number of contact query listeners.
*/
private int contactQueryListenersCount = 0;
/**
* The provider associated with the query.
*/
private ChatRoomProviderWrapper provider = null;
/**
* Creates an instance of <tt>ChatRoomQuery</tt> by specifying
* the parent contact source, the query string to match and the maximum
* result contacts to return.
*
* @param contactSource the parent contact source
* @param queryString the query string to match
* @param provider the provider associated with the query
*/
public ServerChatRoomQuery(String queryString,
ServerChatRoomContactSourceService contactSource,
ChatRoomProviderWrapper provider)
{
super(contactSource,
Pattern.compile(queryString, Pattern.CASE_INSENSITIVE
| Pattern.LITERAL), true);
this.queryString = queryString;
mucService = MUCActivator.getMUCService();
this.provider = provider;
}
/**
* Adds listeners for the query
*/
private void initListeners()
{
mucService.addChatRoomProviderWrapperListener(this);
}
@Override
protected void run()
{
if(provider == null)
{
Iterator<ChatRoomProviderWrapper> chatRoomProviders
= mucService.getChatRoomProviders();
while (chatRoomProviders.hasNext())
{
ChatRoomProviderWrapper provider = chatRoomProviders.next();
providerAdded(provider, true);
}
}
else
{
providerAdded(provider, true);
}
if (getStatus() != QUERY_CANCELED)
setStatus(QUERY_COMPLETED);
}
/**
* Handles adding a chat room provider.
* @param provider the provider.
* @param addQueryResult indicates whether we should add the chat room to
* the query results or fire an event without adding it to the results.
*/
private void providerAdded(final ChatRoomProviderWrapper provider,
final boolean addQueryResult)
{
final ProtocolProviderService pps = provider.getProtocolProvider();
List<String> chatRoomNames =
MUCActivator.getMUCService().getExistingChatRooms(provider);
if (chatRoomNames == null)
{
return;
}
// Already create all the BaseChatRoomSourceContact instances since all
// the data is already available.
final Set<BaseChatRoomSourceContact> chatRooms =
new HashSet<BaseChatRoomSourceContact>(chatRoomNames.size());
for (final String name : chatRoomNames)
{
chatRooms.add(new BaseChatRoomSourceContact(name, name, this, pps));
}
addChatRooms(pps, chatRooms, addQueryResult);
}
/**
* Adds found result to the query results.
*
* @param pps the protocol provider associated with the found chat room.
* @param chatRoomName the name of the chat room.
* @param chatRoomID the id of the chat room.
* @param addQueryResult indicates whether we should add the chat room to
* the query results or fire an event without adding it to the results.
*/
private void addChatRoom(ProtocolProviderService pps,
String chatRoomName, String chatRoomID, boolean addQueryResult)
{
if((queryString == null
|| ((chatRoomName.contains(
queryString)
|| chatRoomID.contains(queryString)
))) && isMatching(chatRoomID, pps))
{
BaseChatRoomSourceContact contact
= new BaseChatRoomSourceContact(chatRoomName, chatRoomID, this,
pps);
synchronized (contactResults)
{
contactResults.add(contact);
}
if(addQueryResult)
{
addQueryResult(contact, false);
}
else
{
fireContactReceived(contact, false);
}
}
}
/**
* Adds found results to the query results.
*
* @param pps the protocol provider associated with the found chat room.
* @param chatRooms The set of chat rooms based on
* BaseChatRoomSourceContact. This is the full set and it will be
* filtered according to demands of the queryString.
* @param addQueryResult indicates whether we should add the chat room to
* the query results or fire an event without adding it to the
* results.
*/
private void addChatRooms(final ProtocolProviderService pps,
final Set<BaseChatRoomSourceContact> chatRooms,
final boolean addQueryResult)
{
BaseChatRoomSourceContact room;
Iterator<BaseChatRoomSourceContact> iterator = chatRooms.iterator();
while (iterator.hasNext())
{
room = iterator.next();
// Notice the NOT operator at the start ...
if (!((queryString == null || (room.getChatRoomName().contains(
queryString) || room.getChatRoomID().contains(queryString)))
&& isMatching(room.getChatRoomID(), pps)))
{
iterator.remove();
}
}
synchronized (contactResults)
{
contactResults.addAll(chatRooms);
}
if (addQueryResult)
{
addQueryResults(chatRooms);
}
else
{
// TODO Need something to fire one event for multiple contacts.
for (SourceContact contact : chatRooms)
{
fireContactReceived(contact, false);
}
}
}
@Override
public void chatRoomProviderWrapperAdded(ChatRoomProviderWrapper provider)
{
providerAdded(provider, false);
}
@Override
public void chatRoomProviderWrapperRemoved(ChatRoomProviderWrapper provider)
{
LinkedList<BaseChatRoomSourceContact> tmpContactResults;
synchronized (contactResults)
{
tmpContactResults
= new LinkedList<BaseChatRoomSourceContact>(contactResults);
for(BaseChatRoomSourceContact contact : tmpContactResults)
{
if(contact.getProvider().equals(provider.getProtocolProvider()))
{
contactResults.remove(contact);
fireContactRemoved(contact);
}
}
}
}
/**
* Clears any listener we used.
*/
private void clearListeners()
{
mucService.removeChatRoomProviderWrapperListener(this);
}
/**
* Cancels this <tt>ContactQuery</tt>.
*
* @see ContactQuery#cancel()
*/
public void cancel()
{
clearListeners();
super.cancel();
}
/**
* If query has status changed to cancel, let's clear listeners.
* @param status {@link ContactQuery#QUERY_CANCELED},
* {@link ContactQuery#QUERY_COMPLETED}
*/
public void setStatus(int status)
{
if(status == QUERY_CANCELED)
clearListeners();
super.setStatus(status);
}
@Override
public void addContactQueryListener(ContactQueryListener l)
{
super.addContactQueryListener(l);
contactQueryListenersCount++;
if(contactQueryListenersCount == 1)
{
initListeners();
}
}
@Override
public void removeContactQueryListener(ContactQueryListener l)
{
super.removeContactQueryListener(l);
contactQueryListenersCount--;
if(contactQueryListenersCount == 0)
{
clearListeners();
}
}
/**
* Checks if the contact should be added to results or not.
* @param chatRoomID the chat room id associated with the contact.
* @param pps the provider of the chat room contact.
* @return <tt>true</tt> if the result should be added to the results and
* <tt>false</tt> if not.
*/
public boolean isMatching(String chatRoomID, ProtocolProviderService pps)
{
return (MUCActivator.getMUCService().findChatRoomWrapperFromChatRoomID(
chatRoomID, pps) == null);
}
/**
* Returns the index of the contact in the contact results list.
* @param contact the contact.
* @return the index of the contact in the contact results list.
*/
public int indexOf(BaseChatRoomSourceContact contact)
{
Iterator<BaseChatRoomSourceContact> it = contactResults.iterator();
int i = 0;
while(it.hasNext())
{
if(contact.equals(it.next()))
{
return i;
}
i++;
}
return -1;
}
}
|