Ignore:
Timestamp:
2014-12-19T15:05:33+01:00 (11 years ago)
Author:
Don-vip
Message:

see #10026 - use recommended user data directory on OSX (distinct from preferences directory). Linux and Windows behaviour is unchanged.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r7831 r7834  
    2222import java.util.Collection;
    2323import java.util.Collections;
     24import java.util.HashSet;
    2425import java.util.Iterator;
    2526import java.util.LinkedHashMap;
     
    3031import java.util.Objects;
    3132import java.util.ResourceBundle;
     33import java.util.Set;
    3234import java.util.SortedMap;
    3335import java.util.TreeMap;
     
    8486     * Internal storage for the preference directory.
    8587     * Do not access this variable directly!
    86      * @see #getPreferencesDirFile()
    87      */
    88     private File preferencesDirFile = null;
     88     * @see #getPreferencesDirectory()
     89     */
     90    private File preferencesDir = null;
    8991
    9092    /**
    9193     * Internal storage for the cache directory.
    9294     */
    93     private File cacheDirFile = null;
     95    private File cacheDir = null;
    9496
    9597    /**
     
    530532     * Returns the location of the user defined preferences directory
    531533     * @return The location of the user defined preferences directory
    532      */
     534     * @deprecated use #getPreferencesDirectory() to access preferences directory
     535     * or #getUserDataDirectory to access user data directory
     536     */
     537    @Deprecated
    533538    public String getPreferencesDir() {
    534         final String path = getPreferencesDirFile().getPath();
     539        final String path = getPreferencesDirectory().getPath();
    535540        if (path.endsWith(File.separator))
    536541            return path;
     
    539544
    540545    /**
    541      * Returns the user defined preferences directory
    542      * @return The user defined preferences directory
    543      */
    544     public File getPreferencesDirFile() {
    545         if (preferencesDirFile != null)
    546             return preferencesDirFile;
     546     * Returns the user defined preferences directory, containing the preferences.xml file
     547     * @return The user defined preferences directory, containing the preferences.xml file
     548     * @since 7834
     549     */
     550    public File getPreferencesDirectory() {
     551        if (preferencesDir != null)
     552            return preferencesDir;
    547553        String path;
    548554        path = System.getProperty("josm.home");
    549555        if (path != null) {
    550             preferencesDirFile = new File(path).getAbsoluteFile();
     556            preferencesDir = new File(path).getAbsoluteFile();
    551557        } else {
    552             preferencesDirFile = Main.platform.getDefaultPrefDirectory();
    553         }
    554         return preferencesDirFile;
    555     }
    556 
    557     /**
    558      * Returns the user preferences file
    559      * @return The user preferences file
     558            preferencesDir = Main.platform.getDefaultPrefDirectory();
     559        }
     560        return preferencesDir;
     561    }
     562
     563    /**
     564     * Returns the user data directory, containing autosave, plugins, etc.
     565     * Depending on the OS it may be the same directory as preferences directory.
     566     * @return The user data directory, containing autosave, plugins, etc.
     567     * @since 7834
     568     */
     569    public File getUserDataDirectory() {
     570        return Main.platform.getDefaultUserDataDirectory();
     571    }
     572
     573    /**
     574     * Returns the user preferences file (preferences.xml)
     575     * @return The user preferences file (preferences.xml)
    560576     */
    561577    public File getPreferenceFile() {
    562         return new File(getPreferencesDirFile(), "preferences.xml");
     578        return new File(getPreferencesDirectory(), "preferences.xml");
    563579    }
    564580
     
    568584     */
    569585    public File getPluginsDirectory() {
    570         return new File(getPreferencesDirFile(), "plugins");
     586        return new File(getUserDataDirectory(), "plugins");
    571587    }
    572588
     
    580596     */
    581597    public File getCacheDirectory() {
    582         if (cacheDirFile != null)
    583             return cacheDirFile;
     598        if (cacheDir != null)
     599            return cacheDir;
    584600        String path = System.getProperty("josm.cache");
    585601        if (path != null) {
    586             cacheDirFile = new File(path).getAbsoluteFile();
     602            cacheDir = new File(path).getAbsoluteFile();
    587603        } else {
    588604            path = get("cache.folder", null);
    589605            if (path != null) {
    590                 cacheDirFile = new File(path);
     606                cacheDir = new File(path);
    591607            } else {
    592                 cacheDirFile = Main.platform.getDefaultCacheDirectory();
    593             }
    594         }
    595         if (!cacheDirFile.exists() && !cacheDirFile.mkdirs()) {
    596             Main.warn(tr("Failed to create missing cache directory: {0}", cacheDirFile.getAbsoluteFile()));
     608                cacheDir = Main.platform.getDefaultCacheDirectory();
     609            }
     610        }
     611        if (!cacheDir.exists() && !cacheDir.mkdirs()) {
     612            Main.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile()));
    597613            JOptionPane.showMessageDialog(
    598614                    Main.parent,
    599                     tr("<html>Failed to create missing cache directory: {0}</html>", cacheDirFile.getAbsoluteFile()),
     615                    tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()),
    600616                    tr("Error"),
    601617                    JOptionPane.ERROR_MESSAGE
    602618            );
    603619        }
    604         return cacheDirFile;
    605     }
    606 
    607     /**
    608      * @return A list of all existing directories where resources could be stored.
     620        return cacheDir;
     621    }
     622
     623    private void addPossibleResourceDir(Set<String> locations, String s) {
     624        if (s != null) {
     625            if (!s.endsWith(File.separator)) {
     626                s += File.separator;
     627            }
     628            locations.add(s);
     629        }
     630    }
     631
     632    /**
     633     * Returns a set of all existing directories where resources could be stored.
     634     * @return A set of all existing directories where resources could be stored.
    609635     */
    610636    public Collection<String> getAllPossiblePreferenceDirs() {
    611         LinkedList<String> locations = new LinkedList<>();
    612         locations.add(getPreferencesDir());
    613         String s;
    614         if ((s = System.getenv("JOSM_RESOURCES")) != null) {
    615             if (!s.endsWith(File.separator)) {
    616                 s = s + File.separator;
    617             }
    618             locations.add(s);
    619         }
    620         if ((s = System.getProperty("josm.resources")) != null) {
    621             if (!s.endsWith(File.separator)) {
    622                 s = s + File.separator;
    623             }
    624             locations.add(s);
    625         }
    626         String appdata = System.getenv("APPDATA");
    627         if (System.getenv("ALLUSERSPROFILE") != null && appdata != null
    628                 && appdata.lastIndexOf(File.separator) != -1) {
    629             appdata = appdata.substring(appdata.lastIndexOf(File.separator));
    630             locations.add(new File(new File(System.getenv("ALLUSERSPROFILE"),
    631                     appdata), "JOSM").getPath());
    632         }
    633         locations.add("/usr/local/share/josm/");
    634         locations.add("/usr/local/lib/josm/");
    635         locations.add("/usr/share/josm/");
    636         locations.add("/usr/lib/josm/");
     637        Set<String> locations = new HashSet<>();
     638        addPossibleResourceDir(locations, getPreferencesDirectory().getPath());
     639        addPossibleResourceDir(locations, getUserDataDirectory().getPath());
     640        addPossibleResourceDir(locations, System.getenv("JOSM_RESOURCES"));
     641        addPossibleResourceDir(locations, System.getProperty("josm.resources"));
     642        if (Main.isPlatformWindows()) {
     643            String appdata = System.getenv("APPDATA");
     644            if (System.getenv("ALLUSERSPROFILE") != null && appdata != null
     645                    && appdata.lastIndexOf(File.separator) != -1) {
     646                appdata = appdata.substring(appdata.lastIndexOf(File.separator));
     647                locations.add(new File(new File(System.getenv("ALLUSERSPROFILE"),
     648                        appdata), "JOSM").getPath());
     649            }
     650        } else {
     651            locations.add("/usr/local/share/josm/");
     652            locations.add("/usr/local/lib/josm/");
     653            locations.add("/usr/share/josm/");
     654            locations.add("/usr/lib/josm/");
     655        }
    637656        return locations;
    638657    }
     
    811830    public void init(boolean reset) {
    812831        // get the preferences.
    813         File prefDir = getPreferencesDirFile();
     832        File prefDir = getPreferencesDirectory();
    814833        if (prefDir.exists()) {
    815834            if(!prefDir.isDirectory()) {
Note: See TracChangeset for help on using the changeset viewer.