1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40
41
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
49
50
51
52 private Updater()
53 {
54 }
55
56
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
114 Library.getInstance();
115
116
117
118
119
120
121 PluginManager.getInstance().restore();
122
123
124 CommandHelper.init();
125
126
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
178
179 private class RepositoryUpdater implements Runnable
180 {
181
182 public void run()
183 {
184 Library.getInstance().updateLater();
185 }
186
187 }
188
189 }