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

Last change on this file since 1750 was 1733, checked in by stoecker, 15 years ago

fixed #2821, #2775 - agpifojpicture position wrong, preferences callable twice

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