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.opennap.net;
21  
22  import javax.swing.Action;
23  import javax.swing.Icon;
24  
25  import org.xnap.chat.ChatProvider;
26  import org.xnap.cmdl.Command;
27  import org.xnap.peer.Peer;
28  import org.xnap.plugin.opennap.OpenNapPlugin;
29  import org.xnap.plugin.opennap.command.OpenNapMessageCommand;
30  import org.xnap.plugin.opennap.net.msg.MessageHandler;
31  import org.xnap.plugin.opennap.net.msg.MessageListener;
32  import org.xnap.plugin.opennap.net.msg.client.PrivateMessage;
33  import org.xnap.plugin.opennap.net.msg.server.GlobalMessage;
34  import org.xnap.plugin.opennap.net.msg.server.ServerMessage;
35  import org.xnap.plugin.opennap.user.OpenNapUser;
36  import org.xnap.util.Preferences;
37  
38  /***
39   * Manages a list of channels.
40   */
41  public class PrivateOpenNapChannel extends OpenNapAbstractChannel 
42      implements MessageListener
43  {
44  
45      //--- Constant(s) ---
46  
47      /***
48       * Time to wait for global error message.
49       */
50      public static final int ERROR_WAIT = 3 * 1000;
51  
52      //--- Data field(s) ---
53  
54      private static Preferences prefs = Preferences.getInstance();
55  
56      private long lastSent;
57      private OpenNapUser user;
58      private long lastAwayMessageSent = 0;
59  
60      //--- Constructor(s) ---
61  
62      public PrivateOpenNapChannel(OpenNapUser user)
63      {
64  		super(user.getServer(), user.getName());
65  
66  		this.user = user;
67      }
68  
69      //--- Method(s) ---
70  
71      public void close()
72      {
73  		MessageHandler.unsubscribe(GlobalMessage.TYPE, this);
74  		OpenNapPlugin.getMessageHandler().removePrivateChannel(this);
75      }
76  
77      public Action[] getActions()
78      {
79  		return null;
80      }
81  
82  	public Command[] getCommands()
83  	{
84  		return new Command[] { 
85  			new OpenNapMessageCommand(this), 
86  		};
87  	}
88  
89      /***
90       * @see OpenNapPlugin.ICON_16.
91       */
92      public Icon getIcon()
93      {
94  		return OpenNapPlugin.ICON_16;
95      }
96  
97      public Action[] getPeerActions(Peer peer)
98      {
99  		return null;
100     }
101 
102     public Peer[] getPeers()
103     {
104 		return new Peer[] { user, server.getLocalPeer() };
105     }
106 
107     public ChatProvider getProvider()
108     {
109 		return server;
110     }
111 
112     public OpenNapUser getUser()
113     {
114 		return user;
115     }
116 
117 	public boolean isJoined()
118 	{
119 		return true;
120 	}
121 
122     public void join()
123     {
124 		MessageHandler.subscribe(GlobalMessage.TYPE, this);
125 		infoReceived("Talking to " + user.toString());
126     }
127 
128     public void messageReceived(String message)
129     {
130 		messageReceived(user, message);
131 
132 		if (prefs.getSendChatAwayMessage() 
133 			&& (System.currentTimeMillis() - lastAwayMessageSent 
134 				> AWAY_MESSAGE_INTERVAL)) {
135 			PrivateMessage msg = new PrivateMessage
136 				(user.getName(), prefs.getChatAwayMessage());
137 			MessageHandler.send(server, msg);
138 			messageReceived(server.getLocalPeer(), 
139 							prefs.getChatAwayMessage());
140 			lastAwayMessageSent = System.currentTimeMillis();
141 		}
142     }
143 
144     public void sendMessage(String message) 
145     {
146 		PrivateMessage msg = new PrivateMessage(user.getName(), message);
147 		msg.setExceptionListener(this);
148 		MessageHandler.send(server, msg);
149 		lastSent = System.currentTimeMillis();
150 
151 		// self copy
152 		messageReceived(server.getLocalPeer(), message);
153     }   
154 
155     public void messageReceived(ServerMessage msg) 
156     {
157 		if (msg.getServer() != server) {
158 			return;
159 		}
160 
161 		if (msg instanceof GlobalMessage 
162 			&& System.currentTimeMillis() - lastSent < ERROR_WAIT) {	    
163 			StringBuffer sb = new StringBuffer();
164 			sb.append(server.getHost());
165 			sb.append(": ");
166 			sb.append(((GlobalMessage)msg).message);
167 
168 			errorReceived(sb.toString());
169 		}
170     }
171 
172 }
173