Ignore:
Timestamp:
2014-12-19T15:05:33+01:00 (9 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.

Location:
trunk/src/org/openstreetmap/josm/data
Files:
4 edited

Legend:

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

    r7580 r7834  
    8585    private final Deque<File> deletedLayers = new LinkedList<>();
    8686
    87     private final File autosaveDir = new File(Main.pref.getPreferencesDir() + AUTOSAVE_DIR);
    88     private final File deletedLayersDir = new File(Main.pref.getPreferencesDir() + DELETED_LAYERS_DIR);
     87    private final File autosaveDir = new File(Main.pref.getUserDataDirectory(), AUTOSAVE_DIR);
     88    private final File deletedLayersDir = new File(Main.pref.getUserDataDirectory(), DELETED_LAYERS_DIR);
    8989
    9090    public void schedule() {
  • trunk/src/org/openstreetmap/josm/data/CustomConfigurator.java

    r7082 r7834  
    157157     */
    158158    public static void messageBox(String type, String text) {
    159         if (type==null || type.length()==0) type="plain";
     159        if (type==null || type.isEmpty()) type="plain";
    160160
    161161        switch (type.charAt(0)) {
     
    390390
    391391    private static String getDirectoryByAbbr(String base) {
    392             String dir;
    393             if ("prefs".equals(base) || base.length()==0) {
    394                 dir = Main.pref.getPreferencesDir();
    395             } else if ("cache".equals(base)) {
    396                 dir = Main.pref.getCacheDirectory().getAbsolutePath();
    397             } else if ("plugins".equals(base)) {
    398                 dir = Main.pref.getPluginsDirectory().getAbsolutePath();
    399             } else {
    400                 dir = null;
    401             }
    402             return dir;
     392        String dir;
     393        if ("prefs".equals(base) || base.isEmpty()) {
     394            dir = Main.pref.getPreferencesDirectory().getAbsolutePath();
     395        } else if ("cache".equals(base)) {
     396            dir = Main.pref.getCacheDirectory().getAbsolutePath();
     397        } else if ("plugins".equals(base)) {
     398            dir = Main.pref.getPluginsDirectory().getAbsolutePath();
     399        } else {
     400            dir = null;
     401        }
     402        return dir;
    403403    }
    404404
     
    459459                engine.eval("API={}; API.pref={}; API.fragments={};");
    460460
    461                 engine.eval("homeDir='"+normalizeDirName(Main.pref.getPreferencesDir()) +"';");
     461                engine.eval("homeDir='"+normalizeDirName(Main.pref.getPreferencesDirectory().getAbsolutePath()) +"';");
    462462                engine.eval("josmVersion="+Version.getInstance().getVersion()+";");
    463463                String className = CustomConfigurator.class.getName();
     
    629629            if (locText.length()>0) text=locText;
    630630            String var = elem.getAttribute("var");
    631             if (var.length()==0) var="result";
     631            if (var.isEmpty()) var="result";
    632632
    633633            String input = evalVars(elem.getAttribute("input"));
  • 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()) {
  • trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java

    r7574 r7834  
    154154     */
    155155    public static String getValidatorDir() {
    156         return Main.pref.getPreferencesDir() + "validator/";
     156        return new File(Main.pref.getUserDataDirectory(), "validator").getAbsolutePath();
    157157    }
    158158
     
    174174        ignoredErrors.clear();
    175175        if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
    176             Path path = Paths.get(getValidatorDir() + "ignorederrors");
     176            Path path = Paths.get(getValidatorDir()).resolve("ignorederrors");
    177177            if (Files.exists(path)) {
    178178                try {
     
    196196
    197197    public static void saveIgnoredErrors() {
    198         try (PrintWriter out = new PrintWriter(new OutputStreamWriter(
    199                 new FileOutputStream(getValidatorDir() + "ignorederrors"), StandardCharsets.UTF_8), false)) {
     198        try (PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
     199                new File(getValidatorDir(), "ignorederrors")), StandardCharsets.UTF_8), false)) {
    200200            for (String e : ignoredErrors) {
    201201                out.println(e);
     
    282282     * until most bugs were discovered while keeping the processing time reasonable)
    283283     */
    284     public final void initializeGridDetail() {
     284    public static final void initializeGridDetail() {
    285285        String code = Main.getProjection().toCode();
    286286        if (Arrays.asList(ProjectionPreference.wgs84.allCodes()).contains(code)) {
Note: See TracChangeset for help on using the changeset viewer.