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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
|
/*
* 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.chatalerter;
import javax.swing.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import net.java.sip.communicator.util.Logger;
import org.jdesktop.jdic.misc.*;
import org.jitsi.service.configuration.*;
import org.jitsi.util.*;
import org.osgi.framework.*;
import java.beans.*;
import java.util.*;
/**
* Chat Alerter plugin.
*
* Sends alerts to the user when new message arrives and the application is not
* in the foreground. On Mac OS X this will bounce the dock icon until the user
* selects the chat windows. On Windows, Gnome and KDE this will flash the
* taskbar button/icon until the user selects the chat window.
*
* @author Damian Minkov
*/
public class ChatAlerterActivator
implements BundleActivator,
ServiceListener,
MessageListener,
ChatRoomMessageListener,
AdHocChatRoomMessageListener,
LocalUserChatRoomPresenceListener,
LocalUserAdHocChatRoomPresenceListener,
PropertyChangeListener,
CallListener
{
/**
* The logger for this class.
*/
private static Logger logger = Logger.getLogger(ChatAlerterActivator.class);
/**
* The BundleContext that we got from the OSGI bus.
*/
private BundleContext bundleContext = null;
/**
* UIService reference.
*/
private UIService uiService;
/**
* Whether we are started.
*/
private boolean started = false;
/**
* Starts this bundle.
* @param bc bundle context.
* @throws Exception
*/
public void start(BundleContext bc) throws Exception
{
this.bundleContext = bc;
ServiceUtils.getService(bundleContext, ConfigurationService.class)
.addPropertyChangeListener(
ConfigurationUtils.ALERTER_ENABLED_PROP, this);
try
{
if(!ConfigurationUtils.isAlerterEnabled())
{
return;
}
// try to load native libs, if it fails don't do anything
if(!OSUtils.IS_MAC)
Alerter.newInstance();
}
catch (Exception exception)
{
if (logger.isInfoEnabled())
logger.info("The Alerter not supported or problem loading it!",
exception);
return;
}
startInternal(bc);
}
/**
* Starts the impl and adds necessary listeners.
* @param bc the current bundle context.
*/
private void startInternal(BundleContext bc)
{
// start listening for newly register or removed protocol providers
bc.addServiceListener(this);
ServiceReference[] protocolProviderRefs;
try
{
protocolProviderRefs = bc.getServiceReferences(
ProtocolProviderService.class.getName(),
null);
}
catch (InvalidSyntaxException ex)
{
// this shouldn't happen since we're providing no parameter string
// but let's log just in case.
logger.error(
"Error while retrieving service refs", ex);
return;
}
// in case we found any
if (protocolProviderRefs != null)
{
if (logger.isDebugEnabled())
logger.debug("Found "
+ protocolProviderRefs.length
+ " already installed providers.");
for (ServiceReference protocolProviderRef : protocolProviderRefs)
{
ProtocolProviderService provider
= (ProtocolProviderService)
bc.getService(protocolProviderRef);
this.handleProviderAdded(provider);
}
}
this.started = true;
}
/**
* Stops bundle.
* @param bc context.
* @throws Exception
*/
public void stop(BundleContext bc) throws Exception
{
stopInternal(bc);
ServiceUtils.getService(bundleContext, ConfigurationService.class)
.removePropertyChangeListener(
ConfigurationUtils.ALERTER_ENABLED_PROP, this);
}
/**
* Stops the impl and removes necessary listeners.
* @param bc the current bundle context.
*/
private void stopInternal(BundleContext bc)
{
// start listening for newly register or removed protocol providers
bc.removeServiceListener(this);
ServiceReference[] protocolProviderRefs;
try
{
protocolProviderRefs = bc.getServiceReferences(
ProtocolProviderService.class.getName(),
null);
}
catch (InvalidSyntaxException ex)
{
// this shouldn't happen since we're providing no parameter string
// but let's log just in case.
logger.error(
"Error while retrieving service refs", ex);
return;
}
// in case we found any
if (protocolProviderRefs != null)
{
for (ServiceReference protocolProviderRef : protocolProviderRefs)
{
ProtocolProviderService provider
= (ProtocolProviderService)
bc.getService(protocolProviderRef);
this.handleProviderRemoved(provider);
}
}
this.started = false;
}
/**
* Used to attach the Alerter plugin to existing or
* just registered protocol provider. Checks if the provider has implementation
* of OperationSetBasicInstantMessaging
*
* @param provider ProtocolProviderService
*/
private void handleProviderAdded(ProtocolProviderService provider)
{
if (logger.isDebugEnabled())
logger.debug("Adding protocol provider " + provider.getProtocolName());
// check whether the provider has a basic im operation set
OperationSetBasicInstantMessaging opSetIm
= provider
.getOperationSet(OperationSetBasicInstantMessaging.class);
if (opSetIm != null)
{
opSetIm.addMessageListener(this);
}
else
{
if (logger.isTraceEnabled())
logger.trace("Service did not have a im op. set.");
}
// check whether the provider has a sms operation set
OperationSetSmsMessaging opSetSms
= provider.getOperationSet(OperationSetSmsMessaging.class);
if (opSetSms != null)
{
opSetSms.addMessageListener(this);
}
else
{
if (logger.isTraceEnabled())
logger.trace("Service did not have a sms op. set.");
}
OperationSetMultiUserChat opSetMultiUChat
= provider.getOperationSet(OperationSetMultiUserChat.class);
if (opSetMultiUChat != null)
{
for (ChatRoom room : opSetMultiUChat.getCurrentlyJoinedChatRooms())
room.addMessageListener(this);
opSetMultiUChat.addPresenceListener(this);
}
else
{
if (logger.isTraceEnabled())
logger.trace("Service did not have a multi im op. set.");
}
OperationSetBasicTelephony<?> basicTelephonyOpSet
= provider.getOperationSet(OperationSetBasicTelephony.class);
if (basicTelephonyOpSet != null)
{
basicTelephonyOpSet.addCallListener(this);
}
}
/**
* Removes the specified provider from the list of currently known providers
* and ignores all the messages exchanged by it
*
* @param provider the ProtocolProviderService that has been unregistered.
*/
private void handleProviderRemoved(ProtocolProviderService provider)
{
OperationSetBasicInstantMessaging opSetIm
= provider.getOperationSet(OperationSetBasicInstantMessaging.class);
if (opSetIm != null)
{
opSetIm.removeMessageListener(this);
}
OperationSetSmsMessaging opSetSms
= provider.getOperationSet(OperationSetSmsMessaging.class);
if (opSetSms != null)
{
opSetSms.removeMessageListener(this);
}
OperationSetMultiUserChat opSetMultiUChat
= provider.getOperationSet(OperationSetMultiUserChat.class);
if (opSetMultiUChat != null)
{
for (ChatRoom room : opSetMultiUChat.getCurrentlyJoinedChatRooms())
room.removeMessageListener(this);
}
OperationSetBasicTelephony<?> basicTelephonyOpSet
= provider.getOperationSet(OperationSetBasicTelephony.class);
if (basicTelephonyOpSet != null)
{
basicTelephonyOpSet.removeCallListener(this);
}
}
/**
* Called to notify interested parties that a change in our presence in
* a chat room has occurred. Changes may include us being kicked, join,
* left.
* @param ev the <tt>LocalUserChatRoomPresenceChangeEvent</tt> instance
* containing the chat room and the type, and reason of the change
*/
public void localUserPresenceChanged(LocalUserChatRoomPresenceChangeEvent ev)
{
ChatRoom chatRoom = ev.getChatRoom();
if(LocalUserChatRoomPresenceChangeEvent.LOCAL_USER_JOINED.equals(
ev.getEventType()))
{
if (!chatRoom.isSystem())
chatRoom.addMessageListener(this);
}
else
{
chatRoom.removeMessageListener(this);
}
}
public void messageReceived(MessageReceivedEvent evt)
{
alertChatWindow();
}
public void messageDelivered(MessageDeliveredEvent evt)
{
// do nothing
}
public void messageDeliveryFailed(MessageDeliveryFailedEvent evt)
{
// do nothing
}
public void messageReceived(ChatRoomMessageReceivedEvent evt)
{
alertChatWindow();
}
public void messageDelivered(ChatRoomMessageDeliveredEvent evt)
{
// do nothing
}
public void messageDeliveryFailed(ChatRoomMessageDeliveryFailedEvent evt)
{
// do nothing
}
/**
* Alerts that a message has been received in
* <code>ExportedWindow.CHAT_WINDOW</code> by using a platform-dependent
* visual clue such as flashing it in the task bar on Windows and Linux.
*/
private void alertChatWindow()
{
alertWindow(ExportedWindow.CHAT_WINDOW);
}
/**
* Alerts the <code>windowID</code> by using a platform-dependent
* visual clue such as flashing it in the task bar on Windows and Linux,
* or the bouncing the dock icon under macosx.
*/
private void alertWindow(WindowID windowID)
{
try
{
ExportedWindow win = getUIService().getExportedWindow(windowID);
if (win == null)
return;
Object winSource = win.getSource();
if (!(winSource instanceof JFrame))
return;
JFrame fr = (JFrame) winSource;
if(OSUtils.IS_MAC)
com.apple.eawt.Application.getApplication()
.requestUserAttention(true);
else
Alerter.newInstance().alert(fr);
}
catch (Throwable ex)
{
logger.error("Cannot alert chat window!", ex);
}
}
/**
* When new protocol provider is registered we check
* does it supports needed Op. Sets and if so add a listener to it
*
* @param serviceEvent ServiceEvent
*/
public void serviceChanged(ServiceEvent serviceEvent)
{
Object sService
= bundleContext.getService(serviceEvent.getServiceReference());
if (logger.isTraceEnabled())
logger.trace("Received a service event for: " +
sService.getClass().getName());
// we don't care if the source service is not a protocol provider
if (!(sService instanceof ProtocolProviderService))
return;
if (logger.isDebugEnabled())
logger.debug("Service is a protocol provider.");
switch (serviceEvent.getType())
{
case ServiceEvent.REGISTERED:
this.handleProviderAdded((ProtocolProviderService)sService);
break;
case ServiceEvent.UNREGISTERING:
this.handleProviderRemoved( (ProtocolProviderService) sService);
break;
}
}
public void messageDelivered(AdHocChatRoomMessageDeliveredEvent evt)
{
// do nothing
}
public void messageDeliveryFailed(
AdHocChatRoomMessageDeliveryFailedEvent evt)
{
// do nothing
}
public void messageReceived(AdHocChatRoomMessageReceivedEvent evt)
{
alertChatWindow();
}
/**
* Called to notify interested parties that a change in our presence in
* an ad-hoc chat room has occurred. Changes may include us being join,
* left.
* @param ev the <tt>LocalUserAdHocChatRoomPresenceChangeEvent</tt>
* instance containing the ad-hoc chat room and the type, and reason of the
* change
*/
public void localUserAdHocPresenceChanged(
LocalUserAdHocChatRoomPresenceChangeEvent ev)
{
AdHocChatRoom adHocChatRoom = ev.getAdHocChatRoom();
if(LocalUserAdHocChatRoomPresenceChangeEvent.LOCAL_USER_JOINED.equals(
ev.getEventType()))
{
adHocChatRoom.addMessageListener(this);
}
else
{
ev.getAdHocChatRoom().removeMessageListener(this);
}
}
/**
* Returns the <tt>UIService</tt> obtained from the bundle
* context.
* @return the <tt>UIService</tt> obtained from the bundle
* context
*/
public UIService getUIService()
{
if(uiService == null)
{
ServiceReference serviceRef = bundleContext
.getServiceReference(UIService.class.getName());
if (serviceRef != null)
uiService = (UIService) bundleContext.getService(serviceRef);
}
return uiService;
}
/**
* Waits for enable/disable property change.
* @param evt the event of change
*/
public void propertyChange(PropertyChangeEvent evt)
{
if(!evt.getPropertyName()
.equals(ConfigurationUtils.ALERTER_ENABLED_PROP))
return;
try
{
if(ConfigurationUtils.isAlerterEnabled() && !started)
{
startInternal(bundleContext);
}
else if(!ConfigurationUtils.isAlerterEnabled() && started)
{
stopInternal(bundleContext);
}
}
catch(Throwable t)
{
logger.error("Error starting/stopping on configuration change");
}
}
/**
* This method is called by a protocol provider whenever an incoming call is
* received.
*
* @param event a CallEvent instance describing the new incoming call
*/
public void incomingCallReceived(CallEvent event)
{
Call call = event.getSourceCall();
/*
* INCOMING_CALL should be dispatched for a Call
* only while there is a CallPeer in the
* INCOMING_CALL state.
*/
Iterator<? extends CallPeer> peerIter = call.getCallPeers();
boolean alert = false;
while (peerIter.hasNext())
{
CallPeer peer = peerIter.next();
if (CallPeerState.INCOMING_CALL.equals(peer.getState()))
{
alert = true;
break;
}
}
if(alert)
alertWindow(ExportedWindow.MAIN_WINDOW);
}
/**
* Not used.
* @param event a CalldEvent instance describing the new outgoing call.
*/
public void outgoingCallCreated(CallEvent event)
{}
/**
* Not used
* @param event the <tt>CallEvent</tt> containing the source call.
*/
public void callEnded(CallEvent event)
{}
}
|