Dirk Stöcker schrieb:
> a) Temporary filename is final name+".tmp".
> b) Do the move as is done now. When move fails, leave the tmp file.
> c) On start check if there is a temp file and move it when possible.
>     If not, inform the user.

I've done it over.

I called the new plugins files *.new instead of *.tmp, because latter
sounds as if it were something you could delete after the programm has
quit, which it isn't.

I also cleaned up the use of hardcoded slashes in file paths in some
places using File.separator instead.

I wasn't sure what to write when informing the user that the moving
failed, so if you have any ideas, please change that.

Robin
Index: src/org/openstreetmap/josm/plugins/PluginInformation.java
===================================================================
--- src/org/openstreetmap/josm/plugins/PluginInformation.java   (revision 868)
+++ src/org/openstreetmap/josm/plugins/PluginInformation.java   (working copy)
@@ -195,7 +195,7 @@
         Collection<String> locations = getPluginLocations();
 
                for (String s : locations) {
-                       File pluginFile = new File(s+"/"+pluginName+".jar");
+                       File pluginFile = new File(s, pluginName + ".jar");
                        if (pluginFile.exists()) {
                                PluginInformation info = new 
PluginInformation(pluginFile);
                                return info;
Index: src/org/openstreetmap/josm/plugins/PluginDownloader.java
===================================================================
--- src/org/openstreetmap/josm/plugins/PluginDownloader.java    (revision 868)
+++ src/org/openstreetmap/josm/plugins/PluginDownloader.java    (working copy)
@@ -12,6 +12,7 @@
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileWriter;
+import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -53,14 +54,17 @@
                }
 
                @Override protected void realRun() throws SAXException, 
IOException {
+                       File pluginDir = Main.pref.getPluginsDirFile();
+                       if (!pluginDir.exists())
+                               pluginDir.mkdirs();
                        for (PluginDescription d : toUpdate) {
-                               File tempFile = new 
File(Main.pref.getPreferencesDir()+"temp.jar");
-                               if (download(d.resource, tempFile)) {
-                                       tempFile.renameTo(new 
File(Main.pref.getPreferencesDir()+"plugins/"+d.name+".jar"));
+                               File pluginFile = new File(pluginDir, d.name + 
".jar.new");
+                               if (download(d.resource, pluginFile))
                                        count++;
-                               } else
+                               else
                                        errors += d.name + "\n";
                        }
+                       PluginDownloader.moveUpdatedPlugins();
                }
        }
 
@@ -78,7 +82,9 @@
                                        txt = readWiki(r);
                                r.close();
                                new 
File(Main.pref.getPreferencesDir()+"plugins").mkdir();
-                               FileWriter out = new 
FileWriter(Main.pref.getPreferencesDir()+"plugins/"+count+"-site-"+site.replaceAll("[/:\\\\
 <>|]", "_")+".xml");
+                               FileWriter out = new FileWriter(new 
File(Main.pref
+                                       .getPluginsDirFile(), count + "-site-"
+                                       + site.replaceAll("[/:\\\\ <>|]", "_") 
+ ".xml"));
                                out.append(txt);
                                out.close();
                                count++;
@@ -122,7 +128,7 @@
        }
 
        public static boolean downloadPlugin(PluginDescription pd) {
-               File file = new 
File(Main.pref.getPreferencesDir()+"plugins/"+pd.name+".jar");
+               File file = new File(Main.pref.getPluginsDirFile(), pd.name + 
".jar");
                if (!download(pd.resource, file)) {
                        JOptionPane.showMessageDialog(Main.parent, tr("Could 
not download plugin: {0} from {1}", pd.name, pd.resource));
                } else {
@@ -162,4 +168,21 @@
        public static void update(Collection<PluginDescription> update) {
                Main.worker.execute(new UpdateTask(update));
        }
+       
+       public static boolean moveUpdatedPlugins() {
+               File pluginDir = Main.pref.getPluginsDirFile();
+               boolean ok = true;              
+               if (pluginDir.exists() && pluginDir.isDirectory()) {
+                       final File[] files = pluginDir.listFiles(new 
FilenameFilter() {
+                               public boolean accept(File dir, String name) {
+                       return name.endsWith(".new");
+                }});
+                       for (File updatedPlugin : files) {
+                               final String filePath = updatedPlugin.getPath();
+                               File plugin = new File(filePath.substring(0, 
filePath.length() - 4));
+                               ok = plugin.delete() && 
updatedPlugin.renameTo(plugin) && ok;
+                       }
+               }
+               return ok;
+       }
 }
Index: src/org/openstreetmap/josm/plugins/Plugin.java
===================================================================
--- src/org/openstreetmap/josm/plugins/Plugin.java      (revision 868)
+++ src/org/openstreetmap/josm/plugins/Plugin.java      (working copy)
@@ -52,7 +52,7 @@
         * @return The directory for the plugin to store all kind of stuff.
         */
        public final String getPluginDir() {
-               return Main.pref.getPreferencesDir()+"plugins/"+info.name+"/";
+               return new File(Main.pref.getPluginsDirFile(), 
info.name).getPath();
        }
 
        /**
Index: src/org/openstreetmap/josm/gui/MainApplication.java
===================================================================
--- src/org/openstreetmap/josm/gui/MainApplication.java (revision 868)
+++ src/org/openstreetmap/josm/gui/MainApplication.java (working copy)
@@ -21,6 +21,7 @@
 import javax.swing.JOptionPane;
 
 import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.plugins.PluginDownloader;
 import org.openstreetmap.josm.tools.BugReportExceptionHandler;
 /**
  * Main window class application.
@@ -83,7 +84,7 @@
                // get the preferences.
                final File prefDir = new File(Main.pref.getPreferencesDir());
                // check if preferences directory has moved (TODO: Update code. 
Remove this after some time)
-               File oldPrefDir = new 
File(System.getProperty("user.home")+"/.josm");
+               File oldPrefDir = new File(System.getProperty("user.home"), 
".josm");
                if (!prefDir.isDirectory() && oldPrefDir.isDirectory()) {
                        if (oldPrefDir.renameTo(prefDir)) {
                                // do not translate this
@@ -118,6 +119,12 @@
                        Main.pref.save();
                }
 
+               if (!PluginDownloader.moveUpdatedPlugins()) {
+                       JOptionPane.showMessageDialog(null,
+                               tr("Activating the updated plugins failed."),
+                               tr("Plugins"), JOptionPane.ERROR_MESSAGE);
+               }
+               
                // load the early plugins
                Main.loadPlugins(true);
 
Index: src/org/openstreetmap/josm/data/Preferences.java
===================================================================
--- src/org/openstreetmap/josm/data/Preferences.java    (revision 868)
+++ src/org/openstreetmap/josm/data/Preferences.java    (working copy)
@@ -71,10 +71,21 @@
         * Return the location of the user defined preferences file
         */
        public String getPreferencesDir() {
+               final String path = getPreferencesDirFile().getPath();
+               if (path.endsWith(File.separator))
+                       return path;
+               return path + File.separator;
+       }
+
+       public File getPreferencesDirFile() {
                if (System.getenv("APPDATA") != null)
-                       return System.getenv("APPDATA")+"/JOSM/";
-               return System.getProperty("user.home")+"/.josm/";
+                       return new File(System.getenv("APPDATA"), "JOSM");
+               return new File(System.getProperty("user.home"), ".josm");
        }
+       
+       public File getPluginsDirFile() {
+               return new File(getPreferencesDirFile(), "plugins");
+       }
 
        /**
         * @return A list of all existing directories where resources could be 
stored.
@@ -84,19 +95,21 @@
                locations.add(Main.pref.getPreferencesDir());
                String s;
                if ((s = System.getenv("JOSM_RESOURCES")) != null) {
-                       if (!s.endsWith("/") && !s.endsWith("\\"))
-                               s = s + "/";
+                       if (!s.endsWith(File.separator))
+                               s = s + File.separator;
                        locations.add(s);
                }
                if ((s = System.getProperty("josm.resources")) != null) {
-                       if (!s.endsWith("/") && !s.endsWith("\\"))
-                               s = s + "/";
+                       if (!s.endsWith(File.separator))
+                               s = s + File.separator;
                        locations.add(s);
                }
                String appdata = System.getenv("APPDATA");
-               if (System.getenv("ALLUSERSPROFILE") != null && appdata != null 
&& appdata.lastIndexOf("\\") != -1) {
-                       appdata = appdata.substring(appdata.lastIndexOf("\\"));
-                       
locations.add(System.getenv("ALLUSERSPROFILE")+appdata+"/JOSM/");
+               if (System.getenv("ALLUSERSPROFILE") != null && appdata != null
+                       && appdata.lastIndexOf(File.separator) != -1) {
+                       appdata = 
appdata.substring(appdata.lastIndexOf(File.separator));
+                       locations.add(new File(new 
File(System.getenv("ALLUSERSPROFILE"),
+                               appdata), "JOSM").getPath());
                }
                locations.add("/usr/local/share/josm/");
                locations.add("/usr/local/lib/josm/");
_______________________________________________
josm-dev mailing list
josm-dev@openstreetmap.org
http://lists.openstreetmap.org/listinfo/josm-dev

Reply via email to