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.util.Properties;
23  
24  import javax.swing.Action;
25  import javax.swing.Icon;
26  
27  import org.xnap.XNap;
28  import org.xnap.peer.Peer;
29  import org.xnap.plugin.Plugin;
30  import org.xnap.search.AbstractSearchResult;
31  import org.xnap.search.SearchResult;
32  import org.xnap.search.SearchResultContainer;
33  
34  import com.limegroup.gnutella.RemoteFileDesc;
35  import com.limegroup.gnutella.URN;
36  import com.limegroup.gnutella.search.HostData;
37  import com.limegroup.gnutella.xml.LimeXMLSchema;
38  import com.limegroup.gnutella.xml.LimeXMLSchemaRepository;
39  import com.limegroup.gnutella.xml.LimeXMLUtils;
40  import com.limegroup.gnutella.xml.SchemaFieldInfo;
41  import com.sun.java.util.collections.Iterator;
42  import com.sun.java.util.collections.Set;
43  
44  /***
45   * This class is the default implementation for search results.
46   */
47  public class LimeWireSearchResult extends AbstractSearchResult {
48  
49      // --- Constants(s) ---
50  
51      //--- Data Field(s) ---
52  
53  	private RemoteFileDesc file;
54  	private HostData data;
55  	private Set locations;
56  
57  	//--- Constructor(s) ---
58  
59  	public LimeWireSearchResult(RemoteFileDesc file, HostData data, 
60  								Set locations)
61      {
62  		this.file = file;
63  		this.data = data;
64  		this.locations = locations;
65  
66  		put(XNap.tr("Client"), file.getVendor());
67  		URN urn = file.getSHA1Urn();
68  		if (urn != null) {
69  			put(SearchResult.URN, urn.httpStringValue());
70  		}
71  		addMetaInfo();
72      }
73  
74      // --- Method(s) ---
75  
76  	private void addMetaInfo()
77  	{
78  		if (file.getXMLDoc() == null) {
79  			return;
80  		}
81  		LimeXMLSchemaRepository rep = LimeXMLSchemaRepository.instance();
82          LimeXMLSchema schema = rep.getSchema(file.getXMLDoc().getSchemaURI());
83  		if(schema!=null) {
84              Iterator fieldIter = schema.getCanonicalizedFields().iterator();
85              while(fieldIter.hasNext()){
86                  SchemaFieldInfo sf = (SchemaFieldInfo)fieldIter.next();
87                  String fieldName = sf.getCanonicalizedFieldName();
88                  String value = file.getXMLDoc().getValue(fieldName);
89                  if(value!=null && !value.equals("") )
90                      put(getDisplayName (fieldName, schema.getSchemaURI()),
91  						value);
92              }
93          }
94  	}
95  	
96  	/***
97  	 * For given schemaURI and rawFieldName returns the Display name. Either
98  	 * by looking up the properties or, by proecessing the rawName
99  	 *  @todo fix properties stuff
100 	 */
101 	private String getDisplayName(String rawName, String schemaURI)
102 	{
103 		//Properties properties = getProperties(schemaURI);
104 		Properties properties = new Properties();
105 		String dispName = properties.getProperty(rawName,"null");
106 		if(dispName.equals("null")) {//no property found...use other method
107 			dispName = LimeXMLUtils.processColName(rawName);
108 			return dispName;
109 		}
110 		dispName = stripPreferredSize(dispName);
111 		return dispName;
112 	}
113 	
114 	//	Utility method to that returns the DisplayName of a field without the
115 	//preferred with (which is usually ^ separated)
116 	private String stripPreferredSize(String rawName){
117 		int i = rawName.indexOf("^");
118 		if(i < 0)
119 			return rawName;
120 		return rawName.substring(0,i);
121 	}
122 	
123     public boolean canGroup(SearchResult result)
124     {
125 		if (result instanceof LimeWireSearchResult) {
126 			return result.getFilesize() == getFilesize();
127 		}
128 
129 		return false;
130     }
131 
132     public SearchResultContainer createContainer()
133     {
134 		return new LimeWireSearchResultContainer();
135     }
136 
137 	public RemoteFileDesc getRemoteDesc()
138 	{
139 		return file;
140 	}
141 
142     public Action[] getActions()
143     {
144 		return new Action[] { new LimeWireDownloadAction(this) };
145     }
146 
147     public int getAvailability()
148     {
149 		return data.getQuality();
150     }
151 
152     public String getFilename()
153     {
154 		return file.getFileName();
155     }
156 
157     public HostData getData()
158     {
159     	return data;
160     }
161     
162     /***
163      * Returns the filesize.
164      */
165     public long getFilesize()
166     {
167 		return file.getSize();
168     }
169 
170 	public Object getHash()
171 	{
172 		return new Long(getFilesize());
173 	}
174 
175     public Icon getIcon()
176     {
177 		return LimeWirePlugin.ICON_16;
178     }
179 
180     /***
181      * Returns the limewire plugin.
182      */
183     public Plugin getPlugin()
184     {
185 		return LimeWirePlugin.getInstance();
186     }
187 
188     /***
189      * Returns the peer that offer this result for download.
190      */
191     public Peer getPeer()
192     {
193 		return new LimeWireServant(file.getHost(), file.getPort());
194     }
195 
196 }