source: josm/trunk/src/org/openstreetmap/josm/plugins/PluginDownloader.java@ 1728

Last change on this file since 1728 was 1647, checked in by stoecker, 15 years ago

fixed plugin handling a bit

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2/**
3 *
4 */
5package org.openstreetmap.josm.plugins;
6
7import static org.openstreetmap.josm.tools.I18n.tr;
8import static org.openstreetmap.josm.tools.I18n.trn;
9
10import java.io.BufferedReader;
11import java.io.BufferedWriter;
12import java.io.File;
13import java.io.FileNotFoundException;
14import java.io.FileOutputStream;
15import java.io.FileWriter;
16import java.io.FilenameFilter;
17import java.io.IOException;
18import java.io.InputStream;
19import java.io.InputStreamReader;
20import java.io.OutputStream;
21import java.io.OutputStreamWriter;
22import java.net.MalformedURLException;
23import java.net.URL;
24import java.util.Arrays;
25import java.util.concurrent.Future;
26import java.util.Collection;
27import java.util.LinkedList;
28
29import javax.swing.JOptionPane;
30
31import org.openstreetmap.josm.actions.AboutAction;
32import org.openstreetmap.josm.Main;
33import org.openstreetmap.josm.gui.ExtendedDialog;
34import org.openstreetmap.josm.gui.PleaseWaitRunnable;
35import org.xml.sax.SAXException;
36
37public class PluginDownloader {
38
39 private static final class UpdateTask extends PleaseWaitRunnable {
40 private final Collection<PluginInformation> toUpdate;
41 public final Collection<PluginInformation> failed = new LinkedList<PluginInformation>();
42 private String errors = "";
43 private int count = 0;
44 private boolean update;
45
46 private UpdateTask(Collection<PluginInformation> toUpdate, boolean up) {
47 super(up ? tr("Update Plugins") : tr("Download Plugins"));
48 update = up;
49 this.toUpdate = toUpdate;
50 }
51
52 @Override protected void cancel() {
53 finish();
54 }
55
56 @Override protected void finish() {
57 Main.pleaseWaitDlg.setVisible(false);
58 if (errors.length() > 0)
59 JOptionPane.showMessageDialog(Main.parent, tr("There were problems with the following plugins:\n\n {0}",errors));
60 else
61 JOptionPane.showMessageDialog(Main.parent, trn("{0} Plugin successfully downloaded. Please restart JOSM.", "{0} Plugins successfully downloaded. Please restart JOSM.", count, count));
62 }
63
64 @Override protected void realRun() throws SAXException, IOException {
65 File pluginDir = Main.pref.getPluginsDirFile();
66 if (!pluginDir.exists())
67 pluginDir.mkdirs();
68 for (PluginInformation d : toUpdate) {
69 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading Plugin {0}...", d.name));
70 File pluginFile = new File(pluginDir, d.name + ".jar.new");
71 if (download(d, pluginFile))
72 {
73 count++;
74 failed.add(d);
75 }
76 else
77 errors += d.name + "\n";
78 }
79 PluginDownloader.moveUpdatedPlugins();
80 }
81 }
82
83 private final static String[] pluginSites = {"http://josm.openstreetmap.de/plugin"};
84
85 public static Collection<String> getSites() {
86 return Main.pref.getCollection("pluginmanager.sites", Arrays.asList(pluginSites));
87 }
88 public static void setSites(Collection<String> c) {
89 Main.pref.putCollection("pluginmanager.sites", c);
90 }
91
92 public static int downloadDescription() {
93 int count = 0;
94 for (String site : getSites()) {
95 /* TODO: remove old site files (everything except .jar) */
96 try {
97 BufferedReader r = new BufferedReader(new InputStreamReader(new URL(site).openStream(), "utf-8"));
98 new File(Main.pref.getPreferencesDir()+"plugins").mkdir();
99 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
100 new FileOutputStream(new File(Main.pref.getPluginsDirFile(),
101 count + "-site-" + site.replaceAll("[/:\\\\ <>|]", "_") + ".txt")), "utf-8"));
102 for (String line = r.readLine(); line != null; line = r.readLine())
103 out.append(line+"\n");
104 r.close();
105 out.close();
106 count++;
107 } catch (IOException x) {
108 }
109 }
110 return count;
111 }
112
113 private static boolean download(PluginInformation pd, File file) {
114 if(pd.mainversion > AboutAction.getVersionNumber())
115 {
116 int answer = new ExtendedDialog(Main.parent,
117 tr("Skip download"),
118 tr("JOSM version {0} required for plugin {1}.", pd.mainversion, pd.name),
119 new String[] {tr("Download Plugin"), tr("Skip Download")},
120 new String[] {"download.png", "cancel.png"}).getValue();
121 if (answer != 1)
122 return false;
123 }
124
125 try {
126 InputStream in = new URL(pd.downloadlink).openStream();
127 OutputStream out = new FileOutputStream(file);
128 byte[] buffer = new byte[8192];
129 for (int read = in.read(buffer); read != -1; read = in.read(buffer))
130 out.write(buffer, 0, read);
131 out.close();
132 in.close();
133 new PluginInformation(file);
134 return true;
135 } catch (Exception e) {
136 e.printStackTrace();
137 }
138 file.delete(); /* cleanup */
139 return false;
140 }
141
142 public static void update(Collection<PluginInformation> update) {
143 Main.worker.execute(new UpdateTask(update, true));
144 }
145
146 public Collection<PluginInformation> download(Collection<PluginInformation> download) {
147 UpdateTask t = new UpdateTask(download, false);
148 try {
149 Future<UpdateTask> ta = Main.worker.submit(t, t);
150 t = ta.get();
151 return t.failed;
152 }
153 catch(java.lang.InterruptedException e) {}
154 catch(java.util.concurrent.ExecutionException e) {}
155 return download;
156 }
157
158 public static boolean moveUpdatedPlugins() {
159 File pluginDir = Main.pref.getPluginsDirFile();
160 boolean ok = true;
161 if (pluginDir.exists() && pluginDir.isDirectory() && pluginDir.canWrite()) {
162 final File[] files = pluginDir.listFiles(new FilenameFilter() {
163 public boolean accept(File dir, String name) {
164 return name.endsWith(".new");
165 }});
166 for (File updatedPlugin : files) {
167 final String filePath = updatedPlugin.getPath();
168 File plugin = new File(filePath.substring(0, filePath.length() - 4));
169 ok = (plugin.delete() || !plugin.exists()) && updatedPlugin.renameTo(plugin) && ok;
170 }
171 }
172 return ok;
173 }
174}
Note: See TracBrowser for help on using the repository browser.