KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SubscriptionRequestPacketListener


1 import org.jivesoftware.smack.PacketListener;
2 import org.jivesoftware.smack.packet.*;
3 import org.jivesoftware.smack.util.StringUtils;
4
5 /** Shows a SubscriptionRequestDialog when a subscript request Presence packet is recieved.
6 * Shows an infomation pane when a subscribed packet is recieved*/

7 public final class SubscriptionRequestPacketListener implements PacketListener{
8     
9     public void processPacket(Packet packet) {
10         Presence p;
11         try{
12             p=(Presence) packet;
13         }
14         catch (ClassCastException JavaDoc cce){
15             return;
16         }
17         if(p.getType()==Presence.Type.SUBSCRIBE){
18             final String JavaDoc from=p.getFrom();
19             javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
20                 public void run(){
21                     (new SubscriptionRequestDialog(from) ).show();
22                 }
23             });
24             return;
25         }
26         if(p.getType()==Presence.Type.SUBSCRIBED){
27             // see if on contact list so we can translate to nick
28
String JavaDoc f=StringUtils.parseBareAddress(p.getFrom().toLowerCase());
29             ContactListItem cli=(ContactListItem) WhisperIM.MainWindow.Contacts.get("jabber"+f);
30             if(cli!=null){
31                 f=cli.toString()+" ("+f+")";
32             }
33             final String JavaDoc ff=f;
34             javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
35                 public void run(){
36                     GUI.showInfo(WhisperIM.MainWindow,"subscribed","been subscribed",ff);
37                 }
38             });
39             return;
40         }
41         if(p.getType()==Presence.Type.UNSUBSCRIBE){
42             Presence np=new Presence(Presence.Type.UNSUBSCRIBED);
43             np.setTo(p.getFrom());
44             np.setFrom(WhisperIM.MainWindow.Conn.getUser());
45             WhisperIM.MainWindow.Conn.sendPacket(np);
46         }
47     }
48 }
49     
Popular Tags