1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | /**
|
---|
3 | *
|
---|
4 | */
|
---|
5 | package org.openstreetmap.josm.plugins;
|
---|
6 |
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
8 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
9 |
|
---|
10 | import java.io.BufferedReader;
|
---|
11 | import java.io.BufferedWriter;
|
---|
12 | import java.io.File;
|
---|
13 | import java.io.FileOutputStream;
|
---|
14 | import java.io.FilenameFilter;
|
---|
15 | import java.io.IOException;
|
---|
16 | import java.io.InputStream;
|
---|
17 | import java.io.InputStreamReader;
|
---|
18 | import java.io.OutputStream;
|
---|
19 | import java.io.OutputStreamWriter;
|
---|
20 | import java.net.URL;
|
---|
21 | import java.util.Arrays;
|
---|
22 | import java.util.Collection;
|
---|
23 | import java.util.LinkedList;
|
---|
24 |
|
---|
25 | import javax.swing.JOptionPane;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.actions.AboutAction;
|
---|
29 | import org.openstreetmap.josm.data.Version;
|
---|
30 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
31 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
32 | import org.xml.sax.SAXException;
|
---|
33 |
|
---|
34 | public class PluginDownloader {
|
---|
35 |
|
---|
36 | private static final class UpdateTask extends PleaseWaitRunnable {
|
---|
37 | private final Collection<PluginInformation> toUpdate;
|
---|
38 | public final Collection<PluginInformation> failed = new LinkedList<PluginInformation>();
|
---|
39 | private String errors = "";
|
---|
40 | private int count = 0;
|
---|
41 | private boolean restart;
|
---|
42 |
|
---|
43 | private UpdateTask(Collection<PluginInformation> toUpdate, boolean up, boolean restart) {
|
---|
44 | super(up ? tr("Update Plugins") : tr("Download Plugins"));
|
---|
45 | this.toUpdate = toUpdate;
|
---|
46 | this.restart = restart;
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override protected void cancel() {
|
---|
50 | finish();
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override protected void finish() {
|
---|
54 | if (errors.length() > 0) {
|
---|
55 | JOptionPane.showMessageDialog(
|
---|
56 | Main.parent,
|
---|
57 | tr("There were problems with the following plugins:\n\n {0}",errors),
|
---|
58 | tr("Error"),
|
---|
59 | JOptionPane.ERROR_MESSAGE
|
---|
60 | );
|
---|
61 | } else {
|
---|
62 | String txt = trn("{0} Plugin successfully downloaded.", "{0} Plugins successfully downloaded.", count, count);
|
---|
63 | if(restart)
|
---|
64 | txt += "\n"+tr("Please restart JOSM.");
|
---|
65 |
|
---|
66 | JOptionPane.showMessageDialog(
|
---|
67 | Main.parent,
|
---|
68 | txt,
|
---|
69 | tr("Information"),
|
---|
70 | JOptionPane.INFORMATION_MESSAGE
|
---|
71 | );
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override protected void realRun() throws SAXException, IOException {
|
---|
76 | File pluginDir = Main.pref.getPluginsDirFile();
|
---|
77 | if (!pluginDir.exists()) {
|
---|
78 | pluginDir.mkdirs();
|
---|
79 | }
|
---|
80 | progressMonitor.setTicksCount(toUpdate.size());
|
---|
81 | for (PluginInformation d : toUpdate) {
|
---|
82 | progressMonitor.subTask(tr("Downloading Plugin {0}...", d.name));
|
---|
83 | progressMonitor.worked(1);
|
---|
84 | File pluginFile = new File(pluginDir, d.name + ".jar.new");
|
---|
85 | if(download(d, pluginFile)) {
|
---|
86 | count++;
|
---|
87 | } else
|
---|
88 | {
|
---|
89 | errors += d.name + "\n";
|
---|
90 | failed.add(d);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | PluginDownloader.moveUpdatedPlugins();
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | private final static String[] pluginSites = {"http://josm.openstreetmap.de/plugin%<?plugins=>"};
|
---|
98 |
|
---|
99 | public static Collection<String> getSites() {
|
---|
100 | return Main.pref.getCollection("pluginmanager.sites", Arrays.asList(pluginSites));
|
---|
101 | }
|
---|
102 | public static void setSites(Collection<String> c) {
|
---|
103 | Main.pref.putCollection("pluginmanager.sites", c);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public static int downloadDescription() {
|
---|
107 | int count = 0;
|
---|
108 | for (String site : getSites()) {
|
---|
109 | /* TODO: remove old site files (everything except .jar) */
|
---|
110 | try {
|
---|
111 | /* replace %<x> with empty string or x=plugins (separated with comma) */
|
---|
112 | String pl = Main.pref.getCollectionAsString("plugins");
|
---|
113 | if(pl != null && pl.length() != 0)
|
---|
114 | site = site.replaceAll("%<(.*)>", "$1"+pl);
|
---|
115 | else
|
---|
116 | site = site.replaceAll("%<(.*)>", "");
|
---|
117 | BufferedReader r = new BufferedReader(new InputStreamReader(new URL(site).openStream(), "utf-8"));
|
---|
118 | new File(Main.pref.getPreferencesDir()+"plugins").mkdir();
|
---|
119 | BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
|
---|
120 | new FileOutputStream(new File(Main.pref.getPluginsDirFile(),
|
---|
121 | count + "-site-" + site.replaceAll("[/:\\\\ <>|]", "_") + ".txt")), "utf-8"));
|
---|
122 | for (String line = r.readLine(); line != null; line = r.readLine()) {
|
---|
123 | out.append(line+"\n");
|
---|
124 | }
|
---|
125 | r.close();
|
---|
126 | out.close();
|
---|
127 | count++;
|
---|
128 | } catch (IOException x) {
|
---|
129 | }
|
---|
130 | }
|
---|
131 | return count;
|
---|
132 | }
|
---|
133 |
|
---|
134 | private static boolean download(PluginInformation pd, File file) {
|
---|
135 | if(pd.mainversion > Version.getInstance().getVersion())
|
---|
136 | {
|
---|
137 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
138 | Main.parent,
|
---|
139 | tr("Skip download"),
|
---|
140 | new String[] {tr("Download Plugin"), tr("Skip Download")}
|
---|
141 | );
|
---|
142 | dialog.setContent(tr("JOSM version {0} required for plugin {1}.", pd.mainversion, pd.name));
|
---|
143 | dialog.setButtonIcons(new String[] {"download.png", "cancel.png"});
|
---|
144 | dialog.showDialog();
|
---|
145 | int answer = dialog.getValue();
|
---|
146 | if (answer != 1)
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 |
|
---|
150 | try {
|
---|
151 | InputStream in = new URL(pd.downloadlink).openStream();
|
---|
152 | OutputStream out = new FileOutputStream(file);
|
---|
153 | byte[] buffer = new byte[8192];
|
---|
154 | for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
|
---|
155 | out.write(buffer, 0, read);
|
---|
156 | }
|
---|
157 | out.close();
|
---|
158 | in.close();
|
---|
159 | new PluginInformation(file);
|
---|
160 | return true;
|
---|
161 | } catch (Exception e) {
|
---|
162 | e.printStackTrace();
|
---|
163 | }
|
---|
164 | file.delete(); /* cleanup */
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public static void update(Collection<PluginInformation> update, boolean restart) {
|
---|
169 | Main.worker.execute(new UpdateTask(update, true, restart));
|
---|
170 | }
|
---|
171 |
|
---|
172 | public Collection<PluginInformation> download(Collection<PluginInformation> download) {
|
---|
173 | // Execute task in current thread instead of executing it in other thread and waiting for result
|
---|
174 | // Waiting for result is not a good idea because the waiting thread will probably be either EDT
|
---|
175 | // or worker thread. Blocking one of these threads will cause deadlock
|
---|
176 | UpdateTask t = new UpdateTask(download, false, true);
|
---|
177 | t.run();
|
---|
178 | return t.failed;
|
---|
179 | }
|
---|
180 |
|
---|
181 | public static boolean moveUpdatedPlugins() {
|
---|
182 | File pluginDir = Main.pref.getPluginsDirFile();
|
---|
183 | boolean ok = true;
|
---|
184 | if (pluginDir.exists() && pluginDir.isDirectory() && pluginDir.canWrite()) {
|
---|
185 | final File[] files = pluginDir.listFiles(new FilenameFilter() {
|
---|
186 | public boolean accept(File dir, String name) {
|
---|
187 | return name.endsWith(".new");
|
---|
188 | }});
|
---|
189 | for (File updatedPlugin : files) {
|
---|
190 | final String filePath = updatedPlugin.getPath();
|
---|
191 | File plugin = new File(filePath.substring(0, filePath.length() - 4));
|
---|
192 | ok = (plugin.delete() || !plugin.exists()) && updatedPlugin.renameTo(plugin) && ok;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | return ok;
|
---|
196 | }
|
---|
197 | }
|
---|