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.gift.net;
21  
22  import java.awt.event.ActionEvent;
23  import java.util.Enumeration;
24  import java.util.Hashtable;
25  
26  import javax.swing.Action;
27  import javax.swing.Icon;
28  
29  import org.xnap.XNap;
30  import org.xnap.gui.action.AbstractDownloadAction;
31  import org.xnap.peer.Peer;
32  import org.xnap.plugin.Plugin;
33  import org.xnap.plugin.gift.GiFTPlugin;
34  import org.xnap.search.AbstractSearchResult;
35  import org.xnap.search.SearchResult;
36  import org.xnap.search.SearchResultContainer;
37  import org.xnap.util.StringHelper;
38  
39  /***
40   * GiFTSearchResult
41   * <pre>Metainfo for mp3:
42   * 		- album
43   * 		- artist
44   * 		- duration
45   * 		- bitrate
46   * 		- frequency
47   * 		- title
48   * 		- tracknumber
49   * 	</pre>
50   *
51   * @author <a href="mailto:tvanlessen@taval.de">Tammo van Lessen</a>
52   * @version CVS $Id: GiFTSearchResult.java,v 1.9 2004/07/20 18:02:28 leist Exp $
53   */
54  public class GiFTSearchResult extends AbstractSearchResult {
55  
56      //--- Constant(s) ---
57  
58  	//~ Instance fields --------------------------------------------------------
59  	private GiFTDaemon daemon;
60  	private String hash;
61  	private Peer user;
62  	private long filesize;
63  	private String filename;
64  	private String mime;
65  	private String node;
66  	private String url;
67  	private int score;
68  	//~ Constructors -----------------------------------------------------------
69  
70  	/***
71  	 * Constructor for GiFTSearchResult.
72  	 *
73  	 * @param size
74  	 * @param user
75  	 * @param filename
76  	 * @param hash
77  	 * @param node
78  	 * @param mime
79  	 * @param url
80  	 * @param score
81  	 * @param meta
82  	 */
83  	public GiFTSearchResult
84  		(GiFTDaemon daemon, long size, Peer user, String filename,
85  		 String hash, String node, String mime, String url, int score,
86  		 Hashtable meta) 
87  	{
88  		super("/");
89  
90  		this.daemon = daemon;
91  
92  		this.filesize = size;
93  		this.filename = (filename != null) ? filename : "";
94  		this.user = user;
95  		this.hash = hash;
96  		this.node = node;
97  		this.mime = mime;
98  		this.url = url;
99  
100 		this.score = score;
101 
102 		Enumeration it = meta.keys();
103 		while (it.hasMoreElements()) {
104 			Object element = it.nextElement();
105 			put(XNap.tr((String)element), meta.get(element));
106 		}
107 
108 		// try to extract network
109 		put(XNap.tr("Network"), StringHelper.firstToken(url, ":"));
110 		
111 		// try to extract checksum
112 		if (hash.startsWith("SHA1:")) {
113 			put(SearchResult.SHA1, hash.substring(5));
114 		}
115 
116 		if (hash.startsWith("MD5:")) {
117 			put(SearchResult.MD5, hash.substring(4));
118 		}
119 
120 		// try to extract meta tags 'bitrate', 'frequency', 'lenght'
121 		try {
122 			// remove "kpbs"
123 			String rate = ((String) meta.get("bitrate"));
124 			rate = rate.substring(0, rate.length() - 4);
125 			put(BITRATE, new Integer(rate));
126 		} catch (Exception e) {
127 		}
128 
129 		//
130 		try {
131 			// remove "s"
132 			String duration = ((String) meta.get("duration"));
133 			duration = duration.substring(0, duration.length() - 1);
134 			put(LENGTH, new Integer(duration));
135 		} catch (Exception e) {
136 		}
137 
138 		//
139 		try {
140 			// remove "Hz"
141 			String freq = ((String) meta.get("frequency"));
142 			freq = freq.substring(0, freq.length() - 2);
143 			put(FREQUENCY, new Integer(freq));
144 		} catch (Exception e) {
145 		}
146 	}
147 
148 	//~ Methods ----------------------------------------------------------------
149 
150 	/***
151 	 * Returns the mime-type.
152 	 *
153 	 * @return String
154 	 */
155 	public String getMimeType() {
156 		return mime;
157 	}
158 
159 	/***
160 	 * Returns the node.
161 	 *
162 	 * @return String
163 	 */
164 	public String getNode() {
165 		return node;
166 	}
167 
168 	/***
169 	 * Returns the url.
170 	 *
171 	 * @return String
172 	 */
173 	public String getUrl() {
174 		return url;
175 	}
176 
177 	/***
178 	 * @see xnap.search.SearchResult#canGroup(xnap.search.SearchResult)
179 	 */
180 	public boolean canGroup(SearchResult result) {
181 		if (result instanceof GiFTSearchResult) {
182 			if (getHash() == null) System.out.println("Baehp!");
183 			if (result == null) System.out.println("Boehp!");
184 			return getHash().equals(((GiFTSearchResult)result).getHash());
185 		}
186 		return false;
187 	}
188 
189 	/***
190 	 * @see xnap.search.SearchResult#createContainer()
191 	 */
192 	public SearchResultContainer createContainer() {
193 		return new GiFTSearchResultContainer(daemon);
194 	}
195 
196 	/***
197 	 * @see xnap.search.SearchResult#getActions()
198 	 */
199 	public Action[] getActions() {
200 		return new Action[] { new DownloadAction() };
201 	}
202 
203 	/***
204 	 * @see xnap.search.SearchResult#getAvailability()
205 	 */
206 	public int getAvailability() {
207 		return score + 1;
208 	}
209 
210 	/***
211 	 * @see xnap.search.SearchResult#getFilename()
212 	 */
213 	public String getFilename() {
214 		return filename;
215 	}
216 
217 	/***
218 	 * @see xnap.search.SearchResult#getFilesize()
219 	 */
220 	public long getFilesize() {
221 		return filesize;
222 	}
223 
224     public Icon getIcon()
225     {
226 	return GiFTPlugin.ICON_16;
227     }
228 
229 	/***
230 	 * @see xnap.search.SearchResult#getPeer()
231 	 */
232 	public Peer getPeer() {
233 		return user;
234 	}
235 
236 	/***
237 	 * @see xnap.search.SearchResult#getPlugin()
238 	 */
239 	public Plugin getPlugin() {
240 		return GiFTPlugin.getInstance();
241 	}
242 
243 	public Object getHash() {
244 		return hash;
245 	}
246 
247 	private class DownloadAction extends AbstractDownloadAction {
248 
249 		/***
250 		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
251 		 */
252 		public void actionPerformed(ActionEvent e) 
253 		{
254 			daemon.getDownloadContainer
255 				(getFilename(), (String)getHash(), getFilesize());
256 			daemon.addDownload(GiFTSearchResult.this);
257 		}
258 
259 	}
260 
261 }