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
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.plugin.phonenumbercontactsource;
import java.util.*;
import java.util.regex.*;
import net.java.sip.communicator.service.contactsource.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.ServerStoredDetails.*;
import net.java.sip.communicator.service.protocol.event.*;
/**
* The <tt>PhoneNumberContactQuery</tt> is a query over the
* <tt>PhoneNumberContactSource</tt>.
*
* @author Yana Stamcheva
* @author Damian Minkov
*/
public class PhoneNumberContactQuery
extends AsyncContactQuery<PhoneNumberContactSource>
implements ContactPresenceStatusListener
{
/**
* The query string.
*/
private String queryString;
/**
* The contact count.
*/
private int contactCount;
/**
* The operations sets we use to listen for the presence that will be used
* for SourceContacts.
*/
private List<OperationSetPersistentPresence> operationSetPersistentPresences
= Collections.synchronizedList(
new LinkedList<OperationSetPersistentPresence>());
/**
* Creates an instance of <tt>PhoneNumberContactQuery</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 contactCount the maximum result contact count
*/
public PhoneNumberContactQuery( PhoneNumberContactSource contactSource,
String queryString,
int contactCount)
{
super(contactSource,
Pattern.compile(queryString, Pattern.CASE_INSENSITIVE
| Pattern.LITERAL), true);
this.queryString = queryString;
this.contactCount = contactCount;
}
/**
* Do all the work in different thread.
*/
@Override
public void run()
{
Iterator<ProtocolProviderService> providers
= PNContactSourceActivator
.getPhoneNumberProviders().iterator();
while (providers.hasNext())
{
if(contactCount > 0 && getQueryResultCount() > contactCount)
break;
ProtocolProviderService provider = providers.next();
OperationSetPersistentPresence persPresOpSet
= provider.getOperationSet(
OperationSetPersistentPresence.class);
// If there's no presence operation set continue to the
// next protocol provider.
if (persPresOpSet == null)
continue;
if(!operationSetPersistentPresences.contains(persPresOpSet))
operationSetPersistentPresences.add(persPresOpSet);
persPresOpSet.addContactPresenceStatusListener(this);
ContactGroup rootGroup
= persPresOpSet.getServerStoredContactListRoot();
addResultContactsForGroup(rootGroup);
Iterator<ContactGroup> subgroups = rootGroup.subgroups();
while (subgroups.hasNext())
{
ContactGroup group = subgroups.next();
addResultContactsForGroup(group);
}
}
if (getStatus() != QUERY_CANCELED)
setStatus(QUERY_COMPLETED);
}
/**
* Adss the result contacts for the given group.
*
* @param group the <tt>ContactGroup</tt> to check for matching contacts
*/
private void addResultContactsForGroup(ContactGroup group)
{
Iterator<Contact> contacts = group.contacts();
while (contacts.hasNext())
{
if(contactCount > 0 && getQueryResultCount() > contactCount)
break;
Contact contact = contacts.next();
addAdditionalNumbers(contact);
}
}
/**
* Returns all additional phone numbers corresponding to the given
* contact.
*
* @param contact the <tt>contact</tt>, which phone details we're
* looking for
* @return a list of all additional phone numbers corresponding to the
* given contact
*/
private void addAdditionalNumbers(Contact contact)
{
OperationSetServerStoredContactInfo infoOpSet
= contact.getProtocolProvider().getOperationSet(
OperationSetServerStoredContactInfo.class);
Iterator<GenericDetail> details;
if(infoOpSet != null)
{
details = infoOpSet.getAllDetailsForContact(contact);
while(details.hasNext())
{
if(contactCount > 0 && getQueryResultCount() > contactCount)
break;
GenericDetail d = details.next();
if(d instanceof PhoneNumberDetail &&
!(d instanceof PagerDetail) &&
!(d instanceof FaxDetail))
{
PhoneNumberDetail pnd = (PhoneNumberDetail)d;
if(pnd.getNumber() != null &&
pnd.getNumber().length() > 0)
{
String localizedType = null;
if(d instanceof WorkPhoneDetail)
{
localizedType =
PNContactSourceActivator.getResources()
.getI18NString("service.gui.WORK_PHONE");
}
else if(d instanceof MobilePhoneDetail)
{
localizedType =
PNContactSourceActivator.getResources()
.getI18NString("service.gui.MOBILE_PHONE");
}
else if(d instanceof VideoDetail)
{
localizedType =
PNContactSourceActivator.getResources().
getI18NString(
"service.gui.VIDEO_PHONE");
}
else
{
localizedType =
PNContactSourceActivator.getResources()
.getI18NString("service.gui.HOME");
}
String contactName = contact.getDisplayName();
String contactAddress = contact.getAddress();
String numberString = pnd.getNumber();
if(queryString == null
|| (queryString != null
&& (numberString.startsWith(
queryString)
|| contactName.startsWith(queryString)
|| contactAddress.startsWith(queryString)
)))
{
ArrayList<ContactDetail> contactDetails
= new ArrayList<ContactDetail>();
String detailDisplayName
= pnd.getNumber() + "(" + localizedType + ")";
ContactDetail detail
= new ContactDetail(pnd.getNumber(),
detailDisplayName);
ArrayList<Class<? extends OperationSet>>
supportedOpSets
= new ArrayList<Class<? extends OperationSet>>();
supportedOpSets
.add(OperationSetBasicTelephony.class);
detail.setSupportedOpSets(supportedOpSets);
contactDetails.add(detail);
PhoneNumberSourceContact numberSourceContact
= new PhoneNumberSourceContact(
this,
getContactSource(),
contact,
contactDetails,
detailDisplayName);
addQueryResult(numberSourceContact);
}
}
}
}
}
}
protected String normalizePhoneNumber(String phoneNumber)
{
return null;
}
@Override
protected boolean phoneNumberMatches(String phoneNumber)
{
return false;
}
/**
* Cancels this <tt>ContactQuery</tt>.
*
* @see ContactQuery#cancel()
*/
public void cancel()
{
clearListeners();
super.cancel();
}
/**
* Clears any listener we used.
*/
private void clearListeners()
{
for(OperationSetPersistentPresence opSetPresence
: operationSetPersistentPresences)
{
opSetPresence.removeContactPresenceStatusListener(this);
}
operationSetPersistentPresences.clear();
}
/**
* Listens for contact status changes and updates it and inform for the
* change.
* @param evt the ContactPresenceStatusChangeEvent describing the status
*/
public void contactPresenceStatusChanged(
ContactPresenceStatusChangeEvent evt)
{
for(SourceContact sc : getQueryResults())
{
if(!(sc instanceof PhoneNumberSourceContact))
continue;
Contact contact = ((PhoneNumberSourceContact)sc).getContact();
if(contact.equals(evt.getSource()))
{
((PhoneNumberSourceContact) sc).setPresenceStatus(
evt.getNewStatus());
fireContactChanged(sc);
}
}
}
/**
* 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);
}
}
|