source: josm/trunk/src/org/openstreetmap/josm/plugins/ReadLocalPluginInformationTask.java@ 3330

Last change on this file since 3330 was 3330, checked in by bastiK, 14 years ago

added icons for plugin list

  • Property svn:eol-style set to native
File size: 9.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.FilenameFilter;
9import java.io.IOException;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.HashMap;
13import java.util.List;
14import java.util.Map;
15
16import org.openstreetmap.josm.gui.PleaseWaitRunnable;
17import org.openstreetmap.josm.gui.progress.ProgressMonitor;
18import org.openstreetmap.josm.io.OsmTransferException;
19import org.openstreetmap.josm.tools.ImageProvider;
20import org.xml.sax.SAXException;
21
22/**
23 * This is an asynchronous task for reading plugin information from the files
24 * in the local plugin repositories.
25 *
26 * It scans the files in the local plugins repository (see {@see Preferences#getPluginsDirectory()}
27 * and extracts plugin information from three kind of files:
28 * <ul>
29 * <li>.jar-files, assuming that they represent plugin jars</li>
30 * <li>.jar.new-files, assuming that these are downloaded but not yet installed plugins</li>
31 * <li>cached lists of available plugins, downloaded for instance from
32 * <a href="http://josm.openstreetmap.de/plugins">http://josm.openstreetmap.de/plugins</a></li>
33 * </ul>
34 *
35 */
36public class ReadLocalPluginInformationTask extends PleaseWaitRunnable {
37 private Map<String, PluginInformation> availablePlugins;
38 private boolean canceled;
39
40 public ReadLocalPluginInformationTask() {
41 super(tr("Reading local plugin information.."), false);
42 availablePlugins = new HashMap<String, PluginInformation>();
43 }
44
45 public ReadLocalPluginInformationTask(ProgressMonitor monitor) {
46 super(tr("Reading local plugin information.."),monitor, false);
47 availablePlugins = new HashMap<String, PluginInformation>();
48 }
49
50 @Override
51 protected void cancel() {
52 canceled = true;
53 }
54
55 @Override
56 protected void finish() {}
57
58 protected void processJarFile(File f, String pluginName) throws PluginException{
59 PluginInformation info = new PluginInformation(
60 f,
61 pluginName
62 );
63 if (!availablePlugins.containsKey(info.getName())) {
64 info.localversion = info.version;
65 availablePlugins.put(info.getName(), info);
66 } else {
67 availablePlugins.get(info.getName()).localversion = info.version;
68 if (info.icon != null) {
69 availablePlugins.get(info.getName()).icon = info.icon;
70 }
71 }
72 }
73
74 protected void scanSiteCacheFiles(ProgressMonitor monitor, File pluginsDirectory) {
75 File[] siteCacheFiles = pluginsDirectory.listFiles(
76 new FilenameFilter() {
77 public boolean accept(File dir, String name) {
78 return name.matches("^([0-9]+-)?site.*\\.txt$");
79 }
80 }
81 );
82 if (siteCacheFiles == null || siteCacheFiles.length == 0)
83 return;
84 monitor.subTask(tr("Processing plugin site cache files..."));
85 monitor.setTicksCount(siteCacheFiles.length);
86 for (File f: siteCacheFiles) {
87 String fname = f.getName();
88 monitor.setCustomText(tr("Processing file ''{0}''", fname));
89 try {
90 processLocalPluginInformationFile(f);
91 } catch(PluginListParseException e) {
92 System.err.println(tr("Warning: Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
93 e.printStackTrace();
94 }
95 monitor.worked(1);
96 }
97 }
98
99 protected void scanIconCacheFiles(ProgressMonitor monitor, File pluginsDirectory) {
100 System.err.println("scanIconCacheFiles");
101 File[] siteCacheFiles = pluginsDirectory.listFiles(
102 new FilenameFilter() {
103 public boolean accept(File dir, String name) {
104 return name.matches("^([0-9]+-)?site.*plugin-icons\\.zip$");
105 }
106 }
107 );
108 if (siteCacheFiles == null || siteCacheFiles.length == 0)
109 return;
110 monitor.subTask(tr("Processing plugin site cache icon files..."));
111 monitor.setTicksCount(siteCacheFiles.length);
112 for (File f: siteCacheFiles) {
113 String fname = f.getName();
114 monitor.setCustomText(tr("Processing file ''{0}''", fname));
115 for (PluginInformation pi : availablePlugins.values()) {
116 if (pi.icon == null && pi.iconPath != null) {
117 pi.icon = ImageProvider.getIfAvailable(null, null, null, pi.name+".jar/"+pi.iconPath, f);
118 }
119 }
120 monitor.worked(1);
121 }
122 }
123
124 protected void scanPluginFiles(ProgressMonitor monitor, File pluginsDirectory) {
125 File[] pluginFiles = pluginsDirectory.listFiles(
126 new FilenameFilter() {
127 public boolean accept(File dir, String name) {
128 return name.endsWith(".jar") || name.endsWith(".jar.new");
129 }
130 }
131 );
132 if (pluginFiles == null || pluginFiles.length == 0)
133 return;
134 monitor.subTask(tr("Processing plugin files..."));
135 monitor.setTicksCount(pluginFiles.length);
136 for (File f: pluginFiles) {
137 String fname = f.getName();
138 monitor.setCustomText(tr("Processing file ''{0}''", fname));
139 try {
140 if (fname.endsWith(".jar")) {
141 String pluginName = fname.substring(0, fname.length() - 4);
142 processJarFile(f, pluginName);
143 } else if (fname.endsWith(".jar.new")) {
144 String pluginName = fname.substring(0, fname.length() - 8);
145 processJarFile(f, pluginName);
146 }
147 } catch(PluginException e){
148 System.err.println(tr("Warning: Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
149 e.printStackTrace();
150 }
151 monitor.worked(1);
152 }
153 }
154
155 protected void scanLocalPluginRepository(ProgressMonitor monitor, File pluginsDirectory) {
156 if (pluginsDirectory == null) return;
157 try {
158 monitor.beginTask("");
159 scanSiteCacheFiles(monitor, pluginsDirectory);
160 scanIconCacheFiles(monitor, pluginsDirectory);
161 scanPluginFiles(monitor, pluginsDirectory);
162 } finally {
163 monitor.setCustomText("");
164 monitor.finishTask();
165 }
166 }
167
168 protected void processLocalPluginInformationFile(File file) throws PluginListParseException{
169 FileInputStream fin = null;
170 try {
171 fin = new FileInputStream(file);
172 List<PluginInformation> pis = new PluginListParser().parse(fin);
173 for (PluginInformation pi : pis) {
174 // we always keep plugin information from a plugin site because it
175 // includes information not available in the plugin jars Manifest, i.e.
176 // the download link or localized descriptions
177 //
178 availablePlugins.put(pi.name, pi);
179 }
180 } catch(IOException e) {
181 throw new PluginListParseException(e);
182 } finally {
183 if (fin != null) {
184 try {
185 fin.close();
186 } catch(IOException e){ /* ignore */}
187 }
188 }
189 }
190
191 protected void analyseInProcessPlugins() {
192 for (PluginProxy proxy : PluginHandler.pluginList) {
193 PluginInformation info = proxy.getPluginInformation();
194 if (canceled)return;
195 if (!availablePlugins.containsKey(info.name)) {
196 availablePlugins.put(info.name, info);
197 } else {
198 availablePlugins.get(info.name).localversion = info.localversion;
199 }
200 }
201 }
202
203 protected void filterOldPlugins() {
204 for (String p : PluginHandler.DEPRECATED_PLUGINS) {
205 if (canceled)return;
206 if (availablePlugins.containsKey(p)) {
207 availablePlugins.remove(p);
208 }
209 }
210 }
211
212 @Override
213 protected void realRun() throws SAXException, IOException, OsmTransferException {
214 Collection<String> pluginLocations = PluginInformation.getPluginLocations();
215 getProgressMonitor().setTicksCount(pluginLocations.size() + 2);
216 if (canceled) return;
217 for (String location : pluginLocations) {
218 scanLocalPluginRepository(
219 getProgressMonitor().createSubTaskMonitor(1, false),
220 new File(location)
221 );
222 getProgressMonitor().worked(1);
223 if (canceled)return;
224 }
225 analyseInProcessPlugins();
226 getProgressMonitor().worked(1);
227 if (canceled)return;
228 filterOldPlugins();
229 getProgressMonitor().worked(1);
230 }
231
232 /**
233 * Replies information about available plugins detected by this task.
234 *
235 * @return information about available plugins detected by this task.
236 */
237 public List<PluginInformation> getAvailablePlugins() {
238 return new ArrayList<PluginInformation>(availablePlugins.values());
239 }
240
241 /**
242 * Replies true if the task was cancelled by the user
243 *
244 * @return true if the task was cancelled by the user
245 */
246 public boolean isCanceled() {
247 return canceled;
248 }
249}
Note: See TracBrowser for help on using the repository browser.