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.FileNotFoundException;
|
---|
14 | import java.io.FileOutputStream;
|
---|
15 | import java.io.FileWriter;
|
---|
16 | import java.io.FilenameFilter;
|
---|
17 | import java.io.IOException;
|
---|
18 | import java.io.InputStream;
|
---|
19 | import java.io.InputStreamReader;
|
---|
20 | import java.io.OutputStream;
|
---|
21 | import java.io.OutputStreamWriter;
|
---|
22 | import java.net.MalformedURLException;
|
---|
23 | import java.net.URL;
|
---|
24 | import java.util.Arrays;
|
---|
25 | import java.util.concurrent.Future;
|
---|
26 | import java.util.Collection;
|
---|
27 | import java.util.LinkedList;
|
---|
28 |
|
---|
29 | import javax.swing.JOptionPane;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.actions.AboutAction;
|
---|
32 | import org.openstreetmap.josm.Main;
|
---|
33 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
34 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
35 | import org.xml.sax.SAXException;
|
---|
36 |
|
---|
37 | public 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 | }
|
---|