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.limewire;
21  
22  import java.awt.event.ActionEvent;
23  import java.io.File;
24  
25  import javax.swing.Action;
26  import javax.swing.Icon;
27  
28  import org.xnap.XNap;
29  import org.xnap.peer.Peer;
30  import org.xnap.peer.action.AbstractBrowseAction;
31  import org.xnap.peer.action.AbstractChatAction;
32  import org.xnap.plugin.Plugin;
33  import org.xnap.transfer.AbstractTransfer;
34  import org.xnap.transfer.Upload;
35  import org.xnap.transfer.action.AbstractStopAction;
36  
37  import com.limegroup.gnutella.FileDesc;
38  import com.limegroup.gnutella.InsufficientDataException;
39  import com.limegroup.gnutella.RouterService;
40  import com.limegroup.gnutella.Uploader;
41  
42  /***
43   * Uploads a file.
44   */
45  public class LimeWireUpload extends AbstractTransfer implements Upload {
46  
47      //--- Constant(s) ---
48  
49       //--- Data field(s) ---
50  
51      private Uploader upload;
52  
53      //--- Constructor(s) ---
54      
55      public LimeWireUpload(Uploader upload) 
56      {
57  		this.upload = upload;
58  		
59  		transferStarted();
60      }
61  
62      //--- Method(s) ---
63  
64      public long getBytesTransferred() 
65      {
66  		return upload.amountUploaded();
67      }
68  
69      public Action[] getActions()
70      {
71  		return new Action[] {
72  			new StopAction(), new BrowseAction(), new ChatAction(),
73  		};
74      }
75  
76      public File getFile()
77      {
78  		FileDesc desc = upload.getFileDesc();
79  		return (desc != null) ? desc.getFile() : null;
80      }
81  
82      public String getFilename()
83      {
84  		return upload.getFileName();
85      }
86  
87      public long getFilesize()
88      {
89  		return upload.getFileSize();
90      }
91  
92      public Peer getPeer()
93      {
94  		String host = upload.getHost();
95  		return (host != null) ? new LimeWireServant(host, upload.getGnutellaPort()) : null;
96      }
97  
98      public Plugin getPlugin()
99      {
100 		return LimeWirePlugin.getInstance();
101     }
102 
103     public String getStatus()
104     {
105 		switch (upload.getState()) {
106 		case Uploader.COMPLETE:
107 				return XNap.tr("Complete");
108 		case Uploader.CONNECTING:
109 				return XNap.tr("Connecting");
110 		case Uploader.UPLOADING:
111 			return XNap.tr("Uploading");
112 		}
113 		return null; 
114     }
115 
116     public long getTotalBytesTransferred()
117     {
118 		return getBytesTransferred();
119     }
120 
121     public boolean isDone()
122     {
123 		int s = upload.getState();
124 		return s == Uploader.COMPLETE;
125     }
126 
127     public boolean isRunning()
128     {
129 		int s = upload.getState();
130 		return s == Uploader.UPLOADING; 
131     }
132 
133 	/***
134 	 *  @see xnap.transfer.Transfer#getAverageRate()
135 	 */
136 	public long getAverageRate() 
137 	{
138 		return (long)(upload.getAverageBandwidth() * 1024);
139 	}
140 
141 	/***
142 	 *  @see xnap.transfer.Transfer#getCurrentRate()
143 	 */
144 	public long getCurrentRate() {
145 		upload.measureBandwidth();
146 		try {
147 			return (long)(upload.getMeasuredBandwidth() * 1024);
148 		}
149 		catch (InsufficientDataException e) {
150 			return -1;
151 		}
152 	}
153 
154 	/***
155 	 *  @see xnap.transfer.Transfer#getIcon()
156 	 */
157 	public Icon getIcon() 
158 	{
159 		return LimeWirePlugin.ICON_16;
160 	}
161 
162 	/***
163 	 *  @see xnap.transfer.Transfer#getQueuePosition()
164 	 */
165 	public int getQueuePosition() 
166 	{
167 		return -1;
168 	}
169 
170 	//--- Inner Class(es) ---
171 
172 	public class BrowseAction extends AbstractBrowseAction 
173 	{
174 		public BrowseAction()
175 		{
176 			setEnabled(upload.isBrowseHostEnabled());
177 		}
178 		
179 		public void actionPerformed(ActionEvent event)
180 		{
181 			LimeWirePlugin.getSearchManager().browse
182 				(upload.getHost(), upload.getGnutellaPort(), null, null);
183 		}
184 	}
185 
186 	public class ChatAction extends AbstractChatAction 
187 	{
188 		public ChatAction()
189 		{
190 			setEnabled(upload.isChatEnabled());
191 		}
192 		
193 		public void actionPerformed(ActionEvent event)
194 		{
195 			RouterService.createChat(upload.getHost(), upload.getGnutellaPort());
196 		}
197 	}
198 
199 	public class StopAction extends AbstractStopAction 
200 	{
201 		public void actionPerformed(ActionEvent event)
202 		{
203 			upload.stop();
204 		}
205 	}
206 
207 }