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.util;
21  
22  import java.beans.PropertyChangeEvent;
23  import java.beans.PropertyChangeListener;
24  
25  import javax.swing.*;
26  
27  import org.apache.log4j.Logger;
28  import org.xnap.cmdl.CommandHelper;
29  import org.xnap.gui.util.EmacsKeyBindings;
30  import org.xnap.gui.component.*;
31  import org.xnap.io.Library;
32  import org.xnap.io.ThrottledInputStream;
33  import org.xnap.net.NetHelper;
34  import org.xnap.plugin.PluginManager;
35  
36  public class Updater implements PropertyChangeListener
37  {
38  
39      //--- Constant(s) ---
40  
41      //--- Data field(s) ---
42  
43      private static Updater instance = new Updater();
44      private static Logger logger = Logger.getLogger(Updater.class);
45  
46      private Preferences prefs = Preferences.getInstance();
47  
48      // --- Initializer ---
49  
50      //--- Constructor(s) ---
51  
52      private Updater()
53      {
54      }
55  
56      //--- Method(s) ---
57  
58      public static synchronized void start() 
59      {
60  		instance.init();
61      }
62  
63      public static synchronized void stop() 
64      {
65  		PluginManager.getInstance().save();
66  		Preferences.getInstance().write();
67      }
68  
69  	/***
70  	 * Called from {@link org.xnap.gui.XNapFrame} when the swing GUI
71  	 * is up and running.  */
72  	public static synchronized void guiStarted()
73  	{
74  		instance.updateEmacsKeyBindings();
75  		new EmacsCompletionMode(new JTextField());
76  		new ManualCompletionMode(new JTextField());
77  	}
78  
79      public void propertyChange(PropertyChangeEvent e)
80      {
81  		String p = e.getPropertyName();
82  	
83  		logger.debug("Preferences.propertyChange: " + p + " " 
84  					 + e.getOldValue() + " -> " + e.getNewValue());
85  	
86  		if (p.equals("useEmacsKeyBindings")) {
87  			updateEmacsKeyBindings();
88  		}
89  		else if (p.equals("useSocksProxy")
90  				 || p.equals("socksProxyHost")
91  				 || p.equals("socksProxyPort")) {
92  			updateSocksProxy();
93  		}
94  		else if (p.equals("useHttpProxy")
95  				 || p.equals("httpProxyHost")
96  				 || p.equals("httpProxyPort")) {
97  			updateHttpProxy();
98  		}
99  		else if (p.equals("downloadThrottle") 
100 				 || p.equals("throttleDownloads")) {
101 			updateThrottle();
102 		}
103     }
104 
105     private void init()
106     {
107 		prefs.addPropertyChangeListener(this);
108 
109 		updateSocksProxy();
110 		updateHttpProxy();
111 		updateThrottle();
112 
113 		// make sure library is up to date
114 		Library.getInstance();
115 
116 		// make sure everybody is instantiated
117 		//  	DownloadQueue.getInstance();
118 		//  	UploadQueue.getInstance();
119 		//  	SearchManager.getInstance();
120 
121 		PluginManager.getInstance().restore();
122 
123 		// add commands
124 		CommandHelper.init();
125 
126 		// trigger maintenance tasks
127 		int interval = Preferences.getInstance().getUpdaterInterval() * 1000;
128 		Scheduler.run(interval, interval, new RepositoryUpdater());
129     }
130 
131     /***
132      * Loads or unloads the emacs keybindings.
133      */
134     private void updateEmacsKeyBindings()
135     {
136 		if (prefs.getUseEmacsKeyBindings()) {
137 			EmacsKeyBindings.load();
138 		}
139 		else {
140 			EmacsKeyBindings.unload();
141 		}
142     }
143 
144     /***
145      * Updates the http proxy settings.
146      */
147     private void updateHttpProxy()
148     {
149 		NetHelper.disableHttpProxy();
150 
151 		if (prefs.getUseHttpProxy()) {
152 			NetHelper.enableHttpProxy(prefs.getHttpProxyHost(), 
153 									  prefs.getHttpProxyPort());
154 		}
155     }
156 
157     /***
158      * Updates the socks proxy settings.
159      */
160     private void updateSocksProxy()
161     {
162 		NetHelper.disableSocksProxy();
163 
164 		if (prefs.getUseSocksProxy()) {
165 			NetHelper.enableSocksProxy(prefs.getSocksProxyHost(), 
166 									   prefs.getSocksProxyPort());
167 		}
168     }
169 
170     private void updateThrottle()
171     {
172 		boolean throttle = prefs.getThrottleDownloads();
173 		long b = prefs.getDownloadThrottle() * 1024;
174 		ThrottledInputStream.setBandwidth((throttle) ? b : 0);
175     }
176 
177     //--- Inner Class(es) ---
178 
179     private class RepositoryUpdater implements Runnable
180     {
181 
182 		public void run()
183 		{
184 			Library.getInstance().updateLater();
185 		}
186 
187     }
188 
189 }