View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.joscar;
21  
22  import javax.swing.Action;
23  
24  import org.apache.log4j.Logger;
25  import org.xnap.XNap;
26  import org.xnap.chat.AbstractChannel;
27  import org.xnap.chat.ChatProvider;
28  import org.xnap.peer.Peer;
29  
30  import JOscarLib.Integration.Event.IncomingMessageEvent;
31  import JOscarLib.Integration.Event.IncomingUrlEvent;
32  import JOscarLib.Integration.Event.IncomingUserEvent;
33  import JOscarLib.Integration.Event.OffgoingUserEvent;
34  import JOscarLib.Integration.Event.OfflineMessageEvent;
35  import JOscarLib.Integration.Event.MessagingListener;
36  import JOscarLib.Integration.Event.StatusListener;
37  
38  
39  public class JOscarChannel extends AbstractChannel 
40  	implements MessagingListener, StatusListener
41  {
42  	//--- Constant(s) ---
43  	
44  	//--- Data field(s) ---
45  
46  	private JOscarPeer peer;
47  
48      private static Logger logger = Logger.getLogger(JOscarChannel.class);
49  	
50  	//--- Constructor(s) ---
51  
52  	/***
53  	 * Constructs a joscar private channel.
54  	 *
55  	 * @param peer the peer the user can talk to
56  	 */
57      public JOscarChannel(JOscarPeer peer)
58  	{
59  		super(peer.getName());
60  		this.peer = peer;
61  		add(peer);
62  		add(JOscarPlugin.getInstance().getLocalPeer());
63  		JOscarPlugin.getInstance().getConnection().addMessagingListener(this);
64  		JOscarPlugin.getInstance().getConnection().addStatusListener(this);
65  	}
66  
67  
68      //--- Method(s) ---
69  
70  	public void close()
71  	{
72  		JOscarPlugin.getInstance().removeChannel(this);
73  	}
74  
75  	/***
76  	 * For now a channel is uniquely identified by the peer it's chatting to.
77  	 */
78  	public JOscarPeer getJOscarPeer()
79  	{
80  		return peer;
81  	}
82  
83  	public Action[] getActions()
84  	{
85  		return null;
86  	}
87  
88  	public Action[] getPeerActions(Peer peer)
89  	{
90  		return null;
91  	}
92  
93  	public ChatProvider getProvider()
94  	{
95  		return null;
96  	}
97  
98  	public boolean isJoined()
99  	{
100 		return true;
101 	}
102 
103     public boolean isLocal(Peer peer)
104 	{
105 		return JOscarPlugin.getInstance().getLocalPeer().equals(peer);
106 	}
107 
108 	public void sendMessage(String message)
109 	{
110 		JOscarPlugin.getInstance().sendMessage(peer.getName(), message);
111 		messageReceived(JOscarPlugin.getInstance().getLocalPeer(),
112 						message);
113 	}
114 
115 	/***
116 	 * Implements the {@link OscarListener} interface.
117 	 */
118 	public void onIncomingMessage(IncomingMessageEvent e)
119 	{
120 		if (peer.getName().equals(e.getSenderID())) {
121 			messageReceived(peer, e.getMessage());
122 		}
123 	}
124 	
125 	/***
126 	 * Implements the {@link OscarListener} interface.
127 	 */
128 	public void onIncomingUrl(IncomingUrlEvent e)
129 	{
130 		if (peer.getName().equals(e.getSenderID())) {
131 			infoReceived(XNap.tr("{0} sent you the URL:", peer.getName())
132 						 + e.getUrl());
133 		}
134 	}
135             
136 	/***
137 	 * Implements the {@link OscarListener} interface.
138 	 */
139 	public void onIncomingUser(IncomingUserEvent e) 
140 	{
141 		if (peer.getName().equals(e.getIncomingUserId())) {
142 			infoReceived(XNap.tr("{0} is {1}.", peer.getName(),
143 								 e.getStatusMode()));
144 		}
145 	}
146             
147 	/***
148 	 * Implements the {@link OscarListener} interface.
149 	 */
150 	public void onOffgoingUser(OffgoingUserEvent e) 
151 	{
152 		if (peer.getName().equals(e.getOffgoingUserId())) {
153 			infoReceived(XNap.tr("{0} is Offline.", peer.getName()));
154 		}
155 	}
156             
157 	/***
158 	 * Implements the {@link OscarListener} interface.
159 	 */
160 	public void onOfflineMessage(OfflineMessageEvent e) 
161 	{
162 		if (peer.getName().equals(e.getSenderUin())) {
163 			infoReceived
164 				(XNap.tr("{0} has gone offline leaving the message {1}.",
165 						 peer.getName(), e.getMessage()));
166 		}
167 	}
168 }
169 
170 
171