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