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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
|
/*
* 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.impl.msghistory;
import net.java.sip.communicator.service.contactsource.*;
import net.java.sip.communicator.service.history.*;
import net.java.sip.communicator.service.history.records.*;
import net.java.sip.communicator.service.muc.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import org.jitsi.util.*;
import java.beans.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
/**
* The source contact service. The will show most recent messages.
*
* @author Damian Minkov
*/
public class MessageSourceService
implements ContactSourceService,
ContactPresenceStatusListener,
ProviderPresenceStatusListener,
LocalUserChatRoomPresenceListener,
MessageListener,
ChatRoomMessageListener,
AdHocChatRoomMessageListener
{
/**
* The logger for this class.
*/
private static Logger logger = Logger
.getLogger(MessageSourceService.class);
/**
* The display name of this contact source.
*/
private final String MESSAGE_HISTORY_NAME;
/**
* The type of the source service, the place to be shown in the ui.
*/
private int sourceServiceType = RECENT_MESSAGES_TYPE;
/**
* Whether to show recent messages in history or in contactlist.
* By default we show it in contactlist.
*/
private static final String IN_HISTORY_PROPERTY
= "net.java.sip.communicator.impl.msghistory.contactsrc.IN_HISTORY";
/**
* Property to control number of recent messages.
*/
private static final String NUMBER_OF_RECENT_MSGS_PROP
= "net.java.sip.communicator.impl.msghistory.contactsrc.MSG_NUMBER";
/**
* Number of messages to show.
*/
private int numberOfMessages = 10;
/**
* The structure to save recent messages list.
*/
private static final String[] STRUCTURE_NAMES
= new String[] { "provider", "contact"};
/**
* The structure.
*/
private static final HistoryRecordStructure recordStructure =
new HistoryRecordStructure(STRUCTURE_NAMES);
/**
* Recent messages history ID.
*/
private static final HistoryID historyID = HistoryID.createFromRawID(
new String[] { "recent_messages"});
/**
* The cache for recent messages.
*/
private History history = null;
/**
* List of recent messages.
*/
private List<MessageSourceContact> recentMessages = null;
/**
* The last query created.
*/
private MessageHistoryContactQuery recentQuery = null;
/**
* Constructs MessageSourceService.
*/
MessageSourceService()
{
if(MessageHistoryActivator.getConfigurationService()
.getBoolean(IN_HISTORY_PROPERTY , false))
{
sourceServiceType = HISTORY_TYPE;
}
MESSAGE_HISTORY_NAME = MessageHistoryActivator.getResources()
.getI18NString("service.gui.RECENT_MESSAGES");
numberOfMessages = MessageHistoryActivator.getConfigurationService()
.getInt(NUMBER_OF_RECENT_MSGS_PROP, numberOfMessages);
}
/**
* Returns the display name of this contact source.
* @return the display name of this contact source
*/
@Override
public String getDisplayName()
{
return MESSAGE_HISTORY_NAME;
}
/**
* Returns default type to indicate that this contact source can be queried
* by default filters.
*
* @return the type of this contact source
*/
@Override
public int getType()
{
return sourceServiceType;
}
/**
* Returns the index of the contact source in the result list.
*
* @return the index of the contact source in the result list
*/
@Override
public int getIndex()
{
return 0;
}
/**
* Creates query for the given <tt>searchString</tt>.
* @param queryString the string to search for
* @return the created query
*/
@Override
public ContactQuery createContactQuery(String queryString)
{
recentQuery =
(MessageHistoryContactQuery)createContactQuery(
queryString, numberOfMessages);
return recentQuery;
}
private synchronized List<MessageSourceContact> getRecentMessages()
{
if(recentMessages == null)
{
// find locally stored list of recent messages
// time, provider, contact
List<MessageSourceContact> cachedRecent
= getRecentMessagesFromHistory();
if(cachedRecent != null)
{
recentMessages = cachedRecent;
Collections.sort(recentMessages);
return recentMessages;
}
recentMessages = new LinkedList<MessageSourceContact>();
// If missing search and construct it and save it
MessageHistoryServiceImpl msgHistoryService =
MessageHistoryActivator.getMessageHistoryService();
Collection<EventObject> res = msgHistoryService
.findRecentMessagesPerContact(numberOfMessages, null, null);
for(EventObject obj : res)
{
recentMessages.add(
new MessageSourceContact(obj, MessageSourceService.this));
}
Collections.sort(recentMessages);
// save it
saveRecentMessagesToHistory();
}
return recentMessages;
}
/**
* Returns the cached recent messages history.
* @return
* @throws IOException
*/
private History getHistory()
throws IOException
{
synchronized(historyID)
{
MessageHistoryServiceImpl msgService
= MessageHistoryActivator.getMessageHistoryService();
HistoryService historyService = msgService.getHistoryService();
// if not existing, return to search for initial load
if (history == null
&& !historyService.isHistoryCreated(historyID))
return null;
if(history == null)
{
if (historyService.isHistoryExisting(historyID))
history = historyService.getHistory(historyID);
else
history = historyService.createHistory(
historyID, recordStructure);
}
return history;
}
}
/**
* Loads recent messages if saved in history.
* @return
*/
private List<MessageSourceContact> getRecentMessagesFromHistory()
{
MessageHistoryServiceImpl msgService
= MessageHistoryActivator.getMessageHistoryService();
// and load it
try
{
History history = getHistory();
if(history == null)
return null;
List<MessageSourceContact> res
= new LinkedList<MessageSourceContact>();
Iterator<HistoryRecord> recs
= history.getReader().findLast(numberOfMessages);
while(recs.hasNext())
{
HistoryRecord hr = recs.next();
String provider = null;
String contact = null;
for (int i = 0; i < hr.getPropertyNames().length; i++)
{
String propName = hr.getPropertyNames()[i];
if (propName.equals(STRUCTURE_NAMES[0]))
provider = hr.getPropertyValues()[i];
else if (propName.equals(STRUCTURE_NAMES[1]))
contact = hr.getPropertyValues()[i];
}
if(provider == null || contact == null)
return res;
for(EventObject ev : msgService.findRecentMessagesPerContact(
numberOfMessages, provider, contact))
{
res.add(new MessageSourceContact(ev, this));
}
}
return res;
}
catch(IOException ex)
{
logger.error("cannot create recent_messages history", ex);
return null;
}
}
/**
* Saves cached list of recent messages in history.
*/
private void saveRecentMessagesToHistory()
{
synchronized(historyID)
{
HistoryService historyService = MessageHistoryActivator
.getMessageHistoryService().getHistoryService();
if (historyService.isHistoryExisting(historyID))
{
// delete it
try
{
historyService.purgeLocallyStoredHistory(historyID);
}
catch(IOException ex)
{
logger.error("Cannot delete recent_messages history", ex);
return;
}
}
// and create it
try
{
history = historyService.createHistory(
historyID, recordStructure);
HistoryWriter writer = history.getWriter();
List<MessageSourceContact> messages = getRecentMessages();
synchronized(messages)
{
for(MessageSourceContact msc : messages)
{
writer.addRecord(
new String[]
{
msc.getProtocolProviderService()
.getAccountID().getAccountUniqueID(),
msc.getContactAddress()
});
}
}
}
catch(IOException ex)
{
logger.error("cannot create recent_messages history", ex);
return;
}
}
}
/**
* Returns the index of the source contact, in the list of recent messages.
* @param messageSourceContact
* @return
*/
int getIndex(MessageSourceContact messageSourceContact)
{
List<MessageSourceContact> messages = getRecentMessages();
synchronized(messages)
{
return messages.indexOf(messageSourceContact);
}
}
/**
* Creates query for the given <tt>searchString</tt>.
* @param queryString the string to search for
* @param contactCount the maximum count of result contacts
* @return the created query
*/
@Override
public ContactQuery createContactQuery(String queryString, int contactCount)
{
if(!StringUtils.isNullOrEmpty(queryString))
return null;
recentQuery = new MessageHistoryContactQuery(numberOfMessages);
return recentQuery;
}
/**
* Updates contact source contacts with status.
* @param evt the ContactPresenceStatusChangeEvent describing the status
*/
@Override
public void contactPresenceStatusChanged(ContactPresenceStatusChangeEvent evt)
{
if(recentQuery == null)
return;
List<MessageSourceContact> messages = getRecentMessages();
synchronized(messages)
{
for(MessageSourceContact msgSC : messages)
{
if(msgSC.getContact() != null
&& msgSC.getContact().equals(evt.getSourceContact()))
{
msgSC.setStatus(evt.getNewStatus());
recentQuery.fireContactChanged(msgSC);
}
}
}
}
@Override
public void providerStatusChanged(ProviderPresenceStatusChangeEvent evt)
{
if(!evt.getNewStatus().isOnline())
return;
// now check for chat rooms as we are connected
MessageHistoryServiceImpl msgHistoryService =
MessageHistoryActivator.getMessageHistoryService();
Collection<EventObject> res = msgHistoryService
.findRecentMessagesPerContact(
numberOfMessages,
evt.getProvider().getAccountID().getAccountUniqueID(),
null);
List<String> recentMessagesForProvider = new LinkedList<String>();
List<MessageSourceContact> messages = getRecentMessages();
synchronized(messages)
{
for(MessageSourceContact msc : messages)
{
if(msc.getProtocolProviderService().equals(evt.getProvider()))
recentMessagesForProvider.add(msc.getContactAddress());
}
List<MessageSourceContact> newContactSources
= new LinkedList<MessageSourceContact>();
for(EventObject obj : res)
{
if(obj instanceof ChatRoomMessageDeliveredEvent
|| obj instanceof ChatRoomMessageReceivedEvent)
{
MessageSourceContact msc
= new MessageSourceContact(obj,
MessageSourceService.this);
if(recentMessagesForProvider
.contains(msc.getContactAddress()))
continue;
messages.add(msc);
newContactSources.add(msc);
}
}
// sort
Collections.sort(messages);
// and now fire events to update ui
if(recentQuery != null)
{
for(MessageSourceContact msc : newContactSources)
{
recentQuery.addQueryResult(msc);
}
}
}
}
@Override
public void providerStatusMessageChanged(PropertyChangeEvent evt)
{}
@Override
public void localUserPresenceChanged(
LocalUserChatRoomPresenceChangeEvent evt)
{
if(recentQuery == null)
return;
MessageSourceContact srcContact = null;
List<MessageSourceContact> messages = getRecentMessages();
synchronized(messages)
{
for(MessageSourceContact msg : messages)
{
if(msg.getRoom() != null
&& msg.getRoom().equals(evt.getChatRoom()))
{
srcContact = msg;
break;
}
}
}
if(srcContact == null)
return;
String eventType = evt.getEventType();
if (LocalUserChatRoomPresenceChangeEvent
.LOCAL_USER_JOINED.equals(eventType))
{
srcContact.setStatus(ChatRoomPresenceStatus.CHAT_ROOM_ONLINE);
recentQuery.fireContactChanged(srcContact);
}
else if ((LocalUserChatRoomPresenceChangeEvent
.LOCAL_USER_LEFT.equals(eventType)
|| LocalUserChatRoomPresenceChangeEvent
.LOCAL_USER_KICKED.equals(eventType)
|| LocalUserChatRoomPresenceChangeEvent
.LOCAL_USER_DROPPED.equals(eventType))
)
{
srcContact.setStatus(ChatRoomPresenceStatus.CHAT_ROOM_OFFLINE);
recentQuery.fireContactChanged(srcContact);
}
}
/**
* Handles new events.
*
* @param obj the event object
* @param provider the provider
* @param id the id of the source of the event
*/
private void handle(EventObject obj,
ProtocolProviderService provider,
String id)
{
// check if provider - contact exist update message content
List<MessageSourceContact> messages = getRecentMessages();
synchronized(messages)
{
for(MessageSourceContact msc : messages)
{
if(msc.getProtocolProviderService().equals(provider)
&& msc.getContactAddress().equals(id))
{
// update
msc.update(obj);
if(recentQuery != null)
recentQuery.fireContactChanged(msc);
return;
}
}
// if missing create source contact
// and update recent messages, trim and sort
MessageSourceContact newSourceContact =
new MessageSourceContact(obj, MessageSourceService.this);
messages.add(newSourceContact);
Collections.sort(messages);
// trim
List<MessageSourceContact> removedItems = null;
if(messages.size() > numberOfMessages)
{
removedItems = new ArrayList<MessageSourceContact>(
messages.subList(numberOfMessages, messages.size()));
messages.removeAll(removedItems);
}
// save
saveRecentMessagesToHistory();
// no query nothing to fire
if(recentQuery == null)
return;
// now fire
if(removedItems != null)
{
for(MessageSourceContact msc : removedItems)
{
recentQuery.fireContactRemoved(msc);
}
}
recentQuery.fireContactReceived(newSourceContact);
}
}
@Override
public void messageReceived(MessageReceivedEvent evt)
{
handle(
evt,
evt.getSourceContact().getProtocolProvider(),
evt.getSourceContact().getAddress());
}
@Override
public void messageDelivered(MessageDeliveredEvent evt)
{
handle(
evt,
evt.getDestinationContact().getProtocolProvider(),
evt.getDestinationContact().getAddress());
}
/**
* Not used.
* @param evt the <tt>MessageFailedEvent</tt> containing the ID of the
*/
@Override
public void messageDeliveryFailed(MessageDeliveryFailedEvent evt)
{}
@Override
public void messageReceived(ChatRoomMessageReceivedEvent evt)
{
// ignore non conversation messages
if(evt.getEventType() !=
ChatRoomMessageReceivedEvent.CONVERSATION_MESSAGE_RECEIVED)
return;
handle(
evt,
evt.getSourceChatRoom().getParentProvider(),
evt.getSourceChatRoom().getIdentifier());
}
@Override
public void messageDelivered(ChatRoomMessageDeliveredEvent evt)
{
handle(
evt,
evt.getSourceChatRoom().getParentProvider(),
evt.getSourceChatRoom().getIdentifier());
}
/**
* Not used.
* @param evt the <tt>ChatroomMessageDeliveryFailedEvent</tt> containing
*/
@Override
public void messageDeliveryFailed(ChatRoomMessageDeliveryFailedEvent evt)
{}
@Override
public void messageReceived(AdHocChatRoomMessageReceivedEvent evt)
{
// TODO
}
@Override
public void messageDelivered(AdHocChatRoomMessageDeliveredEvent evt)
{
// TODO
}
/**
* Not used.
* @param evt the <tt>AdHocChatroomMessageDeliveryFailedEvent</tt>
*/
@Override
public void messageDeliveryFailed(AdHocChatRoomMessageDeliveryFailedEvent evt)
{}
/**
* The contact query implementation.
*/
private class MessageHistoryContactQuery
extends AsyncContactQuery<MessageSourceService>
{
MessageHistoryContactQuery(int contactCount)
{
super(MessageSourceService.this,
Pattern.compile("",
Pattern.CASE_INSENSITIVE | Pattern.LITERAL),
false);
}
@Override
public void run()
{
List<MessageSourceContact> messages = getRecentMessages();
synchronized(messages)
{
for(MessageSourceContact rm : messages)
{
addQueryResult(rm);
}
}
}
/**
* Notifies the <tt>ContactQueryListener</tt>s registered with this
* <tt>ContactQuery</tt> that a new <tt>SourceContact</tt> has been
* received.
*
* @param contact the <tt>SourceContact</tt> which has been received and
* which the registered <tt>ContactQueryListener</tt>s are to be notified
* about
*/
public void fireContactReceived(SourceContact contact)
{
fireContactReceived(contact, false);
}
/**
* Notifies the <tt>ContactQueryListener</tt>s registered with this
* <tt>ContactQuery</tt> that a <tt>SourceContact</tt> has been
* changed.
*
* @param contact the <tt>SourceContact</tt> which has been changed and
* which the registered <tt>ContactQueryListener</tt>s are to be notified
* about
*/
public void fireContactChanged(SourceContact contact)
{
super.fireContactChanged(contact);
}
/**
* Notifies the <tt>ContactQueryListener</tt>s registered with this
* <tt>ContactQuery</tt> that a <tt>SourceContact</tt> has been
* removed.
*
* @param contact the <tt>SourceContact</tt> which has been removed and
* which the registered <tt>ContactQueryListener</tt>s are to be notified
* about
*/
public void fireContactRemoved(SourceContact contact)
{
super.fireContactRemoved(contact);
}
/**
* Adds a specific <tt>SourceContact</tt> to the list of
* <tt>SourceContact</tt>s to be returned by this <tt>ContactQuery</tt> in
* response to {@link #getQueryResults()}.
*
* @param sourceContact the <tt>SourceContact</tt> to be added to the
* <tt>queryResults</tt> of this <tt>ContactQuery</tt>
* @return <tt>true</tt> if the <tt>queryResults</tt> of this
* <tt>ContactQuery</tt> has changed in response to the call
*/
public boolean addQueryResult(SourceContact sourceContact)
{
return super.addQueryResult(sourceContact);
}
}
}
|