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.user;
21  
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  import javax.swing.Action;
26  
27  import org.xnap.XNap;
28  import org.xnap.peer.AbstractHotlistItem;
29  import org.xnap.peer.HotlistManager;
30  import org.xnap.plugin.opennap.action.OpenNapBrowseAction;
31  import org.xnap.plugin.opennap.action.OpenNapChatAction;
32  import org.xnap.plugin.opennap.action.OpenNapEditGlobalUserAction;
33  import org.xnap.plugin.opennap.action.OpenNapLookupAction;
34  import org.xnap.plugin.opennap.action.OpenNapRemoveFromHotlistAction;
35  import org.xnap.plugin.opennap.net.OpenNapServer;
36  import org.xnap.plugin.opennap.net.msg.MessageHandler;
37  import org.xnap.plugin.opennap.net.msg.client.AddHotlistEntryMessage;
38  import org.xnap.plugin.opennap.net.msg.client.RemoveHotlistEntryMessage;
39  import org.xnap.plugin.opennap.net.msg.client.WhoisRequestMessage;
40  
41  public class OpenNapGlobalUser extends AbstractHotlistItem {
42  
43      //--- Constant(s) ---
44  
45      //--- Constant(s) ---
46  
47      public static int TRANSFER_DEFAULT = -1;
48      public static int TRANSFER_NEVER = 0;
49      public static int TRANSFER_UNLIMITED = Integer.MAX_VALUE;
50  
51      //--- Data field(s) ---
52  
53      private GlobalOpenNapUserData data;
54      private boolean inHotlist;
55      private List servers = new LinkedList();
56      private int queuedCount = 0;
57      private int downloadCount = 0;
58      private int uploadCount = 0;
59      private String status;
60  	private boolean minimumSharesMessageSent = false;
61  
62      //--- Constructor(s) ---
63  
64      /***
65       *
66       */
67      public OpenNapGlobalUser(String name)
68      {
69  		data = new GlobalOpenNapUserData();
70  
71  		data.name = name;
72  
73  		updateStatus();
74      }
75  
76      /***
77       * @see OpenNapUserManager
78       */
79      public OpenNapGlobalUser(GlobalOpenNapUserData data)
80      {
81  		this.data = data;
82  
83  		setInHotlist(true);
84  
85  		updateStatus();
86      }
87  
88      //--- Method(s) ---
89  
90      public void add(OpenNapServer server)
91      {
92  		synchronized (servers) {
93  			servers.add(server);
94  		}
95  		updateStatus();
96      }
97  
98  	public void downloadStarted()
99  	{
100 		synchronized(this) {
101 			downloadCount++;
102 		}
103 		stateChanged();
104 	}
105 
106 	public void downloadStopped()
107 	{
108 		synchronized(this) {
109 			downloadCount--;
110 		}
111 		stateChanged();
112 	}
113 
114     public Action[] getActions()
115     {
116 		return new Action[] { 
117 			new OpenNapBrowseAction(this),
118 			new OpenNapChatAction(this),
119 			new OpenNapLookupAction(this),
120 			new OpenNapRemoveFromHotlistAction(this), 
121 			new OpenNapEditGlobalUserAction(this),
122 		};
123     }
124 
125     public String getCategory()
126     {
127 		return getData().category;
128     }
129 
130     public String getComment()
131     {
132 		return getData().comment;
133     }
134 
135 	public OpenNapServer getConnectedServer(OpenNapServer preferred)
136 	{
137 		if (preferred != null && preferred.isConnected()) {
138 			return preferred;
139 		}
140 
141 		OpenNapServer[] servers = getServers();
142 		for (int i = 0; i < servers.length; i++) {
143 			if (servers[i].isConnected()) {
144 				return servers[i];
145 			}
146 		}
147 
148 		return null;
149 	}
150 
151     public synchronized int getDownloadCount()
152     {
153 		return downloadCount;
154     }
155 
156     /***
157      * Returns the number of allowed donwload from the peers represented by
158      * this item.
159      */
160     public int getMaxDownloads()
161     {
162 		return getData().maxDownloads;
163     }
164 
165     /***
166      * Returns the number of allowed uploads to the peers represented by this
167      * item.
168      */
169     public int getMaxUploads()
170     {
171 		return getData().maxUploads;
172     }
173     
174     public String getName()
175     {
176 		return getData().name;
177     }
178 
179     public synchronized int getQueuedCount()
180     {
181 		return queuedCount;
182     }
183 
184 	public OpenNapServer[] getServers()
185 	{
186 		synchronized (servers) {
187 			return (OpenNapServer[])servers.toArray(new OpenNapServer[0]);
188 		}
189 	}
190 
191     public String getStatus()
192     {
193 		return status;
194     }
195 
196 	public synchronized int getUploadCount()
197 	{
198 		return uploadCount;
199 	}
200 
201     /***
202      * Returns true if chat messages from the peers represented by this item
203      * are ignored.
204      */
205     public boolean isChatIgnored()
206     {
207 		return getData().ignoreChat;
208     }
209 
210 	public boolean isMinimumSharesMessageSent()
211 	{
212 		return minimumSharesMessageSent;
213 	}
214 
215     /***
216      * Returns true, if the user was added to the {@link HotlistManager}.
217      */
218     public boolean isInHotlist()
219     {
220 		return inHotlist;
221     }
222 
223 	public void lookup()
224 	{
225 		MessageHandler.send(new WhoisRequestMessage(getName()));
226 	}
227 
228 	public synchronized void setMinimumSharesMessageSent(boolean sent)
229 	{
230 		minimumSharesMessageSent = sent;
231 	}
232 
233     /***
234      * Set <code>newValue</code> to true to ignore chat messages from the 
235      * peers represented by this item.
236      */
237     public void setChatIgnored(boolean newValue)
238     {
239 		getData().ignoreChat = newValue;
240     }
241 
242     /***
243      * Sets the category to <code>newValue</code>.
244      */
245     public void setCategory(String newValue)
246     {
247 		getData().category = newValue;
248     }
249 
250     /***
251      * Sets the comment to <code>newValue</code>.
252      */
253     public void setComment(String newValue)
254     {
255 		getData().comment = newValue;
256     }
257 
258     /***
259      * Sets the maximum allowed downloads from the peers represented by this 
260      * item to <code>newValue</code>.
261      */
262     public void setMaxDownloads(int newValue)
263     {
264 		getData().maxDownloads = newValue;
265     }
266 
267     /***
268      * Sets the maximum allowed uploads to the peers represented by this 
269      * item to <code>newValue</code>.
270      */
271     public void setMaxUploads(int newValue)
272     {
273 		getData().maxUploads = newValue;
274     }
275 
276     public synchronized void setQueued(boolean isQueued)
277     {
278 		queuedCount += (isQueued) ? 1 : -1;
279     }
280 
281     public void setStatus(String newValue)
282     {
283 		status = newValue;
284 		stateChanged();
285     }
286 
287     public void remove(OpenNapServer server)
288     {
289 		synchronized (servers) {
290 			servers.add(server);
291 		}
292 		updateStatus();
293     }
294 
295     public void setInHotlist(boolean newValue)
296     {
297 		if (newValue != isInHotlist()) {
298 			inHotlist = newValue;
299 			if (newValue) {
300 				HotlistManager.getInstance().add(this);
301 				MessageHandler.send(new AddHotlistEntryMessage(getName()));
302 			}
303 			else {
304 				HotlistManager.getInstance().remove(this);
305 				MessageHandler.send(new RemoveHotlistEntryMessage(getName()));
306 			}
307 		}
308     }
309 
310     GlobalOpenNapUserData getData()
311     {
312 		return data;
313     }
314 
315     private void updateStatus()
316     {
317 		setStatus((servers.size() > 0) 
318 				  ? XNap.tr("Online") : XNap.tr("Offline"));
319     }
320 
321 	public void uploadStarted()
322 	{
323 		synchronized(this) {
324 			uploadCount++;
325 		}
326 		stateChanged();
327 	}
328 
329 	public void uploadStopped()
330 	{
331 		synchronized(this) {
332 			uploadCount--;
333 		}
334 		stateChanged();
335 	}
336 
337 }