001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.preferences; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005import static org.openstreetmap.josm.tools.Utils.getSystemProperty; 006 007import java.io.File; 008 009import javax.swing.JOptionPane; 010 011import org.openstreetmap.josm.Main; 012import org.openstreetmap.josm.spi.preferences.Config; 013import org.openstreetmap.josm.spi.preferences.IBaseDirectories; 014import org.openstreetmap.josm.tools.Logging; 015 016/** 017 * Class provides base directory locations for JOSM. 018 * @since 13021 019 */ 020public final class JosmBaseDirectories implements IBaseDirectories { 021 022 private JosmBaseDirectories() { 023 // hide constructor 024 } 025 026 private static class InstanceHolder { 027 static final JosmBaseDirectories INSTANCE = new JosmBaseDirectories(); 028 } 029 030 /** 031 * Returns the unique instance. 032 * @return the unique instance 033 */ 034 public static JosmBaseDirectories getInstance() { 035 return InstanceHolder.INSTANCE; 036 } 037 038 /** 039 * Internal storage for the preference directory. 040 */ 041 private File preferencesDir; 042 043 /** 044 * Internal storage for the cache directory. 045 */ 046 private File cacheDir; 047 048 /** 049 * Internal storage for the user data directory. 050 */ 051 private File userdataDir; 052 053 @Override 054 public File getPreferencesDirectory(boolean createIfMissing) { 055 if (preferencesDir == null) { 056 String path = getSystemProperty("josm.pref"); 057 if (path != null) { 058 preferencesDir = new File(path).getAbsoluteFile(); 059 } else { 060 path = getSystemProperty("josm.home"); 061 if (path != null) { 062 preferencesDir = new File(path).getAbsoluteFile(); 063 } else { 064 preferencesDir = Main.platform.getDefaultPrefDirectory(); 065 } 066 } 067 } 068 try { 069 if (createIfMissing && !preferencesDir.exists() && !preferencesDir.mkdirs()) { 070 Logging.warn(tr("Failed to create missing preferences directory: {0}", preferencesDir.getAbsoluteFile())); 071 JOptionPane.showMessageDialog( 072 Main.parent, 073 tr("<html>Failed to create missing preferences directory: {0}</html>", preferencesDir.getAbsoluteFile()), 074 tr("Error"), 075 JOptionPane.ERROR_MESSAGE 076 ); 077 } 078 } catch (SecurityException e) { 079 Logging.log(Logging.LEVEL_ERROR, "Unable to check if preferences dir must be created", e); 080 } 081 return preferencesDir; 082 } 083 084 @Override 085 public File getUserDataDirectory(boolean createIfMissing) { 086 if (userdataDir == null) { 087 String path = getSystemProperty("josm.userdata"); 088 if (path != null) { 089 userdataDir = new File(path).getAbsoluteFile(); 090 } else { 091 path = getSystemProperty("josm.home"); 092 if (path != null) { 093 userdataDir = new File(path).getAbsoluteFile(); 094 } else { 095 userdataDir = Main.platform.getDefaultUserDataDirectory(); 096 } 097 } 098 } 099 try { 100 if (createIfMissing && !userdataDir.exists() && !userdataDir.mkdirs()) { 101 Logging.warn(tr("Failed to create missing user data directory: {0}", userdataDir.getAbsoluteFile())); 102 JOptionPane.showMessageDialog( 103 Main.parent, 104 tr("<html>Failed to create missing user data directory: {0}</html>", userdataDir.getAbsoluteFile()), 105 tr("Error"), 106 JOptionPane.ERROR_MESSAGE 107 ); 108 } 109 } catch (SecurityException e) { 110 Logging.log(Logging.LEVEL_ERROR, "Unable to check if user data dir must be created", e); 111 } 112 return userdataDir; 113 } 114 115 @Override 116 public File getCacheDirectory(boolean createIfMissing) { 117 if (cacheDir == null) { 118 String path = getSystemProperty("josm.cache"); 119 if (path != null) { 120 cacheDir = new File(path).getAbsoluteFile(); 121 } else { 122 path = getSystemProperty("josm.home"); 123 if (path != null) { 124 cacheDir = new File(path, "cache"); 125 } else { 126 path = Config.getPref().get("cache.folder", null); 127 if (path != null) { 128 cacheDir = new File(path).getAbsoluteFile(); 129 } else { 130 cacheDir = Main.platform.getDefaultCacheDirectory(); 131 } 132 } 133 } 134 } 135 try { 136 if (createIfMissing && !cacheDir.exists() && !cacheDir.mkdirs()) { 137 Logging.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile())); 138 JOptionPane.showMessageDialog( 139 Main.parent, 140 tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()), 141 tr("Error"), 142 JOptionPane.ERROR_MESSAGE 143 ); 144 } 145 } catch (SecurityException e) { 146 Logging.log(Logging.LEVEL_ERROR, "Unable to check if cache dir must be created", e); 147 } 148 return cacheDir; 149 } 150}