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.io.File;
23  import java.io.IOException;
24  import java.util.List;
25  import java.util.Locale;
26  
27  import javax.swing.*;
28  
29  import org.apache.log4j.Logger;
30  import org.xnap.XNapFacade;
31  import org.xnap.gui.AbstractPreferencesDialog;
32  import org.xnap.gui.StatusBar;
33  import org.xnap.gui.XNapFrame;
34  import org.xnap.gui.event.AbstractPreferencesDialogListener;
35  import org.xnap.gui.util.IconHelper;
36  import org.xnap.plugin.AbstractPlugin;
37  import org.xnap.search.SearchManager;
38  import org.xnap.util.FileHelper;
39  import org.xnap.util.Preferences;
40  
41  import com.limegroup.gnutella.RouterService;
42  import com.limegroup.gnutella.settings.ApplicationSettings;
43  import com.limegroup.gnutella.settings.ChatSettings;
44  import com.limegroup.gnutella.settings.ConnectionSettings;
45  import com.limegroup.gnutella.settings.DownloadSettings;
46  import com.limegroup.gnutella.settings.SecuritySettings;
47  import com.limegroup.gnutella.settings.SharingSettings;
48  import com.limegroup.gnutella.settings.UltrapeerSettings;
49  import com.limegroup.gnutella.settings.UploadSettings;
50  
51  
52  /***
53   * Provides a gnutella network plugin based on LimeWire.
54   */
55  public class LimeWirePlugin extends AbstractPlugin 
56  {
57  
58      //--- Constant(s) ---
59  
60      public static String ICON_FILENAME = "limewire.png";
61  
62      public static Icon ICON_16 = IconHelper.getIcon(ICON_FILENAME, 16, false);
63      
64      //--- Data Field(s) ---
65  
66      private static Logger logger = Logger.getLogger(LimeWirePlugin.class);
67      private static Preferences prefs = Preferences.getInstance();
68      private static LimeWirePlugin singleton;
69  
70  	private LimeWirePreferences limewirePrefs;
71  	private LimeWirePanel jpLimeWire;
72  	private LimeWireStatusPanel statusPanel;
73  	private PreferencesDialogListener pdl;
74  
75  	private LimeWireSearchManager searchManager;
76  	private LimeWireServantManager servantManager;
77  	private LimeWireChatManager chatManager;
78  	private LimeWireTransferManager transferManager;
79  
80  	private boolean connectStarted = false;
81  
82      //--- Constructor(s) ---
83  
84      public LimeWirePlugin()
85      {
86      }
87  
88      //--- Method(s) ---
89  
90      public static LimeWirePlugin getInstance()
91      {
92  		return singleton;
93      }
94  
95  	public static LimeWirePanel getConnectionPanel()
96  	{
97  		return singleton.jpLimeWire;
98  	}
99  
100 	public static LimeWireChatManager getChatManager()
101 	{
102 		return singleton.chatManager;
103 	}
104 
105 	public static LimeWirePreferences getPreferences()
106 	{
107 		return singleton.limewirePrefs;
108 	}
109 
110 	public static LimeWireSearchManager getSearchManager()
111 	{
112 		return singleton.searchManager;
113 	}
114 
115 	public static LimeWireServantManager getServantManager()
116 	{
117 		return singleton.servantManager;
118 	}
119 
120 	public static LimeWireTransferManager getTransferManager()
121 	{
122 		return singleton.transferManager;
123 	}
124 	
125 	public void connect()
126 	{
127 		if (connectStarted) {
128 			return;
129 		}
130 
131 		connectStarted = true;
132 		RouterService.connect();
133 		if (jpLimeWire != null) {
134 			Runnable runner = new Runnable()
135 				{
136 					public void run() 
137 					{
138 						if (jpLimeWire != null) {
139 							jpLimeWire.setConnected(true);
140 						}
141 					}
142 				};
143 			SwingUtilities.invokeLater(runner);
144 		}
145 	}
146 
147 	public void disconnect()
148 	{
149 		if (!connectStarted) {
150 			return;
151 		}
152 
153 		connectStarted = false;
154 		RouterService.disconnect();
155 		if (jpLimeWire != null) {
156 			Runnable runner = new Runnable()
157 				{
158 					public void run() 
159 					{
160 						if (jpLimeWire != null) {
161 							jpLimeWire.setConnected(false);
162 						}
163 					}
164 				};
165 			SwingUtilities.invokeLater(runner);
166 		}
167 	}
168 
169     /***
170      * Starts the plugin.
171      */
172     public void start() 
173     {
174 		singleton = this;
175 
176   		limewirePrefs = new LimeWirePreferences();
177 
178 		initializeSettings();
179 		
180 		servantManager = new LimeWireServantManager();
181 		chatManager = new LimeWireChatManager();
182 		transferManager = new LimeWireTransferManager();
183 		searchManager = new LimeWireSearchManager();
184 		SearchManager.getInstance().add(searchManager);
185 
186 		LimeWireCallback callback = new LimeWireCallback();
187 		RouterService router = new RouterService(callback);
188 		router.start();
189     }
190 
191     /***
192      * Starts the GUI of the plugin.
193      */
194     public void startGUI()
195     {
196 		jpLimeWire = new LimeWirePanel();
197 		jpLimeWire.setConnected(connectStarted);
198 		XNapFrame.getInstance().insertTab
199 			(getInfo().getName(), IconHelper.getListIcon(ICON_FILENAME), 
200 			 jpLimeWire);
201 		
202 		pdl = new PreferencesDialogListener();
203 		XNapFacade.addPluginPreferencesDialogListener(pdl);
204 
205 		// add status panel
206 		statusPanel = new LimeWireStatusPanel();
207 		StatusBar.getInstance().addComponent(statusPanel);
208 
209 		if (limewirePrefs.getAutoConnect()) {
210 			connect();
211 		}
212     }
213 
214     /***
215 	 * 
216 	 */
217 	private void initializeSettings() 
218 	{
219 		ApplicationSettings.COUNTRY.setValue(Locale.getDefault().getCountry());
220 		ApplicationSettings.INSTALLED.setValue(false);
221 		ApplicationSettings.LANGUAGE.setValue(Locale.getDefault().getLanguage());
222 		ApplicationSettings.LOCALE_VARIANT.setValue(Locale.getDefault().getVariant());
223 
224 		ChatSettings.CHAT_ENABLED.setValue(limewirePrefs.getBoolean("chatEnabled"));
225 		
226 		DownloadSettings.MAX_DOWNLOAD_BYTES_PER_SEC.setValue(prefs.getDownloadThrottle());
227 		DownloadSettings.MAX_SIM_DOWNLOAD.setValue(prefs.getMaxDownloads());
228 
229 		SecuritySettings.COOKIES_FILE.setValue(FileHelper.getHomeDir() + "limewire_cookies");
230 		SecuritySettings.ACCEPT_AUTHENTICATED_CONNECTIONS_ONLY.setValue(limewirePrefs.getAuthenticatedConnectionsOnly());
231 
232 		SharingSettings.ALLOW_BROWSER.setValue(limewirePrefs.getBoolean("allowBrowser"));
233 		SharingSettings.CLEAR_DOWNLOAD.setValue(false);
234 		SharingSettings.CLEAR_UPLOAD.setValue(false);
235 		SharingSettings.FREELOADER_ALLOWED.setValue(prefs.getUseMinimumShares() ? 0 : 100);
236 		SharingSettings.FREELOADER_FILES.setValue(prefs.getMinimumShares());
237 
238 		UploadSettings.SOFT_MAX_UPLOADS.setValue(prefs.getMaxUploads());
239 		UploadSettings.HARD_MAX_UPLOADS.setValue(prefs.getMaxUploads());
240 		UploadSettings.MAX_UPLOAD_BYTES_PER_SEC.setValue(prefs.getUploadThrottle());
241 		UploadSettings.UPLOADS_PER_PERSON.setValue(prefs.getMaxUploadsPerUser());
242 
243 		ConnectionSettings.CONNECT_ON_STARTUP.setValue(limewirePrefs.getAutoConnect());
244 		ConnectionSettings.CONNECTION_SPEED.setValue(prefs.getLinkSpeed());
245 		ConnectionSettings.LOCAL_IS_PRIVATE.setValue(false);
246 		ConnectionSettings.NUM_CONNECTIONS.setValue(5);
247 		ConnectionSettings.FORCED_PORT.setValue(limewirePrefs.getInt("forcedPort"));
248 		ConnectionSettings.PORT.setValue(limewirePrefs.getPort());
249 		ConnectionSettings.FORCE_IP_ADDRESS.setValue(limewirePrefs.getBoolean("forceIPAdress"));
250 		ConnectionSettings.FORCED_IP_ADDRESS_STRING.setValue(limewirePrefs.get("forcedIPAddressString"));
251 		// FIX: s.setExtensions();
252 
253 		UltrapeerSettings.DISABLE_ULTRAPEER_MODE.setValue(limewirePrefs.getDisableUltraPeer());
254 
255 		updateDirectories();
256 	}
257 
258 	/***
259 	 * 
260 	 */
261 	private void updateDirectories() 
262 	{
263 		String[] dirs = prefs.getUploadDirs();
264 		File[] files = new File[dirs.length];
265 		for (int i = 0; i < dirs.length; i++) {
266 			files[i] = new File(dirs[i]);
267 		}
268 		SharingSettings.DIRECTORIES_TO_SHARE.setValue(files);
269 		
270 		// the SettingsManager tries to create a subdirectory 
271 		// "Incompletes" at the parent directory, so we start
272 		// one level below
273 		try {
274 			SharingSettings.setSaveDirectory(new File(prefs.getIncompleteDir() + ".gnutella"));
275 		}
276 		catch (IOException e) {
277 			logger.warn("Could not set download directory", e);
278 		}
279 		SharingSettings.DIRECTORY_FOR_SAVING_FILES.setValue(new File(prefs.getDownloadDir()));
280 	}
281 
282 	/***
283      * Stops the plugin. Disposes all singletons.
284      */
285     public void stop()
286     {
287 		disconnect();
288 		limewirePrefs = null;
289     }
290 
291     /***
292      * Stops the GUI of the plugin.
293      */
294     public void stopGUI()
295     {
296 		pdl.dispose();
297 		XNapFacade.removePluginPreferencesDialogListener(pdl);
298 		pdl = null;
299 
300 		SearchManager.getInstance().remove(searchManager);
301 
302 		chatManager.removeAll();
303 		transferManager.removeAll();
304 
305 		// stop update timer
306 		jpLimeWire.setConnected(false);
307 
308 		// shutdown
309 		RouterService.shutdown();
310 
311 		// remove status panel
312 		StatusBar.getInstance().removeComponent(statusPanel);
313 		
314 		// dispose gui
315 		XNapFrame.getInstance().removeTab(jpLimeWire);
316 		jpLimeWire = null;
317     }
318 
319 	public static void updateStatusPanel()
320 	{
321 		if (singleton.statusPanel != null) {
322 			singleton.statusPanel.updateStatus();
323 		}
324 	}
325 
326     // --- Inner Class(es) ---
327 
328     private class PreferencesDialogListener 
329 		extends AbstractPreferencesDialogListener
330     {
331 
332 		public void addPanels(AbstractPreferencesDialog dialog, List panels)
333 		{
334  			LimeWirePreferencesPanel jpp = new LimeWirePreferencesPanel();
335  			panels.add(jpp);
336  			panels.add(dialog.addPanel(jpp, ICON_FILENAME));
337 		}
338 
339     }
340 
341 }