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 javax.swing.Action;
23  import javax.swing.Icon;
24  
25  import java.awt.event.ActionEvent;
26  import java.io.File;
27  
28  import com.limegroup.gnutella.Downloader;
29  import com.limegroup.gnutella.Endpoint;
30  import com.limegroup.gnutella.InsufficientDataException;
31  import com.limegroup.gnutella.RemoteFileDesc;
32  import com.limegroup.gnutella.RouterService;
33  import com.limegroup.gnutella.downloader.AlreadyDownloadingException;
34  
35  import org.xnap.XNap;
36  import org.xnap.gui.StatusBar;
37  import org.xnap.peer.Peer;
38  import org.xnap.peer.action.AbstractBrowseAction;
39  import org.xnap.peer.action.AbstractChatAction;
40  import org.xnap.plugin.Plugin;
41  import org.xnap.transfer.AbstractDownload;
42  import org.xnap.transfer.action.AbstractDeleteAction;
43  import org.xnap.transfer.action.AbstractResumeAction;
44  
45  /***
46   * Downloads a file.
47   */
48  public class LimeWireDownload extends AbstractDownload {
49  
50      //--- Constant(s) ---
51  
52       //--- Data field(s) ---
53  
54      private Downloader download;
55  
56      //--- Constructor(s) ---
57      
58      public LimeWireDownload(Downloader download) 
59      {
60  		this.download = download;
61      }
62  
63      //--- Method(s) ---
64  
65      public long getBytesTransferred() 
66      {
67  		return download.getAmountRead();
68      }
69  
70      public Action[] getActions()
71      {
72  		return new Action[] {
73  			new ResumeAction(), new DeleteAction(),
74  			new BrowseAction(), new ChatAction(),
75  		};
76      }
77  
78      public File getFile()
79      {
80  		return download.getDownloadFragment();
81      }
82  
83      public String getFilename()
84      {
85  		return download.getFileName();
86      }
87  
88      public long getFilesize()
89      {
90  		return download.getContentLength();
91      }
92  
93      public Peer getPeer()
94      {
95  		String host = download.getAddress();
96  		return (host != null) ? new LimeWireServant(host) : null;
97      }
98  
99      public Plugin getPlugin()
100     {
101 		return LimeWirePlugin.getInstance();
102     }
103 
104     public String getStatus()
105     {
106 		switch (download.getState()) {
107 		case Downloader.QUEUED:
108 			return XNap.tr("Queued");
109 		case Downloader.CONNECTING:
110 			return XNap.tr("Connecting");
111 		case Downloader.DOWNLOADING:
112 			return XNap.tr("Downloading from {0} locations", 
113 						   new Integer(download.getNumberOfAlternateLocations()));
114 		case Downloader.WAITING_FOR_RETRY:
115 			return XNap.tr("Waiting for retry");
116 		case Downloader.COMPLETE:
117 			return XNap.tr("Complete");
118 		case Downloader.ABORTED:
119 			return XNap.tr("Aborted");
120 		case Downloader.GAVE_UP:
121 			return XNap.tr("Gave up");
122 		case Downloader.COULDNT_MOVE_TO_LIBRARY:
123 			return XNap.tr("Could not move file to library");
124 		case Downloader.WAITING_FOR_RESULTS:
125 			return XNap.tr("Waiting for results");
126 		case Downloader.CORRUPT_FILE:
127 			return XNap.tr("Corrupted file");
128 		case Downloader.REMOTE_QUEUED:
129 			return XNap.tr("Remotely queued");
130 		case Downloader.HASHING:
131 			return XNap.tr("Hashing file");
132 		case Downloader.SAVING:
133 			return XNap.tr("Saveing file");
134 		case Downloader.WAITING_FOR_USER:
135 			return XNap.tr("Waiting for user");
136 		case Downloader.WAITING_FOR_CONNECTIONS:
137 			return XNap.tr("Waiting for connections");
138 		default:
139 			return XNap.tr("State {0}", new Integer(download.getState()));
140 		}
141     }
142 
143     public long getTotalBytesTransferred()
144     {
145 		return getBytesTransferred();
146     }
147 
148     public boolean isDone()
149     {
150 		int s = download.getState();
151 		return s == Downloader.COMPLETE || s == Downloader.ABORTED 
152 			|| s == Downloader.COULDNT_MOVE_TO_LIBRARY;
153     }
154 
155     public boolean isRunning()
156     {
157 		int s = download.getState();
158 		return s == Downloader.DOWNLOADING; 
159     }
160 
161 	/***
162 	 *  @see xnap.transfer.Transfer#getAverageRate()
163 	 */
164 	public long getAverageRate() 
165 	{
166 		return (long)(download.getAverageBandwidth() * 1024);
167 	}
168 
169 	/***
170 	 *  @see xnap.transfer.Transfer#getCurrentRate()
171 	 */
172 	public long getCurrentRate() {
173 		download.measureBandwidth();
174 		try {
175 			return (long)(download.getMeasuredBandwidth() * 1024);
176 		}
177 		catch (InsufficientDataException e) {
178 			return -1;
179 		}
180 	}
181 
182 	/***
183 	 *  @see xnap.transfer.Transfer#getIcon()
184 	 */
185 	public Icon getIcon() 
186 	{
187 		return LimeWirePlugin.ICON_16;
188 	}
189 
190 	/***
191 	 *  @see xnap.transfer.Transfer#getQueuePosition()
192 	 */
193 	public int getQueuePosition() 
194 	{
195 		String pos = download.getQueuePosition();
196 		if (pos != null) {
197 			try {
198 				return Integer.parseInt(pos);
199 			}
200 			catch (NumberFormatException e) {
201 			}
202 		}
203 		return -1;
204 	}
205 
206 	/***
207 	 *  @see xnap.transfer.Transfer#getRemainingTime()
208 	 */
209 	public int getRemainingTime() 
210 	{
211 		int time = download.getRemainingStateTime();
212 		return (time == Integer.MAX_VALUE) ? -1 : time;
213 	}
214 
215 	//--- Inner Class(es) ---
216 
217 	public class BrowseAction extends AbstractBrowseAction 
218 	{
219 		public BrowseAction()
220 		{
221 			setEnabled(download.hasBrowseEnabledHost());
222 		}
223 		
224 		public void actionPerformed(ActionEvent event)
225 		{
226 			RemoteFileDesc desc = download.getBrowseEnabledHost();
227 			if (desc != null) {
228 				LimeWirePlugin.getSearchManager().browse(desc);
229 			}										
230 		}
231 	}
232 
233 	public class ChatAction extends AbstractChatAction 
234 	{
235 		public ChatAction()
236 		{
237 			setEnabled(download.hasChatEnabledHost());
238 		}
239 		
240 		public void actionPerformed(ActionEvent event)
241 		{
242 			Endpoint host = download.getChatEnabledHost();
243 			RouterService.createChat(host.getAddress(), host.getPort());
244 		}
245 	}
246 
247 	public class ResumeAction extends AbstractResumeAction 
248 	{
249 		public void actionPerformed(ActionEvent event)
250 		{
251 			try {
252 				download.resume();
253 			}
254 			catch (AlreadyDownloadingException e) {
255 				StatusBar.setText(XNap.tr("Already downloading file!"));
256 			}
257 		}
258 	}
259 
260 	public class DeleteAction extends AbstractDeleteAction 
261 	{
262 		public void actionPerformed(ActionEvent event)
263 		{
264 			download.stop();
265 		}
266 	}
267 
268 }