[13021] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.data.preferences;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.io.File;
|
---|
| 7 |
|
---|
| 8 | import javax.swing.JOptionPane;
|
---|
| 9 |
|
---|
| 10 | import org.openstreetmap.josm.Main;
|
---|
| 11 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
| 12 | import org.openstreetmap.josm.spi.preferences.IBaseDirectories;
|
---|
| 13 | import org.openstreetmap.josm.tools.Logging;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Class provides base directory locations for JOSM.
|
---|
| 17 | * @since 13021
|
---|
| 18 | */
|
---|
| 19 | public final class JosmBaseDirectories implements IBaseDirectories {
|
---|
| 20 |
|
---|
| 21 | private JosmBaseDirectories() {
|
---|
| 22 | // hide constructor
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | private static class InstanceHolder {
|
---|
| 26 | static final JosmBaseDirectories INSTANCE = new JosmBaseDirectories();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public static JosmBaseDirectories getInstance() {
|
---|
| 30 | return InstanceHolder.INSTANCE;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * Internal storage for the preference directory.
|
---|
| 35 | */
|
---|
| 36 | private File preferencesDir;
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Internal storage for the cache directory.
|
---|
| 40 | */
|
---|
| 41 | private File cacheDir;
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Internal storage for the user data directory.
|
---|
| 45 | */
|
---|
| 46 | private File userdataDir;
|
---|
| 47 |
|
---|
| 48 | @Override
|
---|
| 49 | public File getPreferencesDirectory(boolean createIfMissing) {
|
---|
| 50 | if (preferencesDir == null) {
|
---|
| 51 | String path;
|
---|
| 52 | path = System.getProperty("josm.pref");
|
---|
| 53 | if (path != null) {
|
---|
| 54 | preferencesDir = new File(path).getAbsoluteFile();
|
---|
| 55 | } else {
|
---|
| 56 | path = System.getProperty("josm.home");
|
---|
| 57 | if (path != null) {
|
---|
| 58 | preferencesDir = new File(path).getAbsoluteFile();
|
---|
| 59 | } else {
|
---|
| 60 | preferencesDir = Main.platform.getDefaultPrefDirectory();
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | if (createIfMissing && !preferencesDir.exists() && !preferencesDir.mkdirs()) {
|
---|
| 65 | Logging.warn(tr("Failed to create missing preferences directory: {0}", preferencesDir.getAbsoluteFile()));
|
---|
| 66 | JOptionPane.showMessageDialog(
|
---|
| 67 | Main.parent,
|
---|
| 68 | tr("<html>Failed to create missing preferences directory: {0}</html>", preferencesDir.getAbsoluteFile()),
|
---|
| 69 | tr("Error"),
|
---|
| 70 | JOptionPane.ERROR_MESSAGE
|
---|
| 71 | );
|
---|
| 72 | }
|
---|
| 73 | return preferencesDir;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | @Override
|
---|
| 77 | public File getUserDataDirectory(boolean createIfMissing) {
|
---|
| 78 | if (userdataDir == null) {
|
---|
| 79 | String path;
|
---|
| 80 | path = System.getProperty("josm.userdata");
|
---|
| 81 | if (path != null) {
|
---|
| 82 | userdataDir = new File(path).getAbsoluteFile();
|
---|
| 83 | } else {
|
---|
| 84 | path = System.getProperty("josm.home");
|
---|
| 85 | if (path != null) {
|
---|
| 86 | userdataDir = new File(path).getAbsoluteFile();
|
---|
| 87 | } else {
|
---|
| 88 | userdataDir = Main.platform.getDefaultUserDataDirectory();
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | if (createIfMissing && !userdataDir.exists() && !userdataDir.mkdirs()) {
|
---|
| 93 | Logging.warn(tr("Failed to create missing user data directory: {0}", userdataDir.getAbsoluteFile()));
|
---|
| 94 | JOptionPane.showMessageDialog(
|
---|
| 95 | Main.parent,
|
---|
| 96 | tr("<html>Failed to create missing user data directory: {0}</html>", userdataDir.getAbsoluteFile()),
|
---|
| 97 | tr("Error"),
|
---|
| 98 | JOptionPane.ERROR_MESSAGE
|
---|
| 99 | );
|
---|
| 100 | }
|
---|
| 101 | return userdataDir;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | @Override
|
---|
| 105 | public File getCacheDirectory(boolean createIfMissing) {
|
---|
| 106 | if (cacheDir == null) {
|
---|
| 107 | String path = System.getProperty("josm.cache");
|
---|
| 108 | if (path != null) {
|
---|
| 109 | cacheDir = new File(path).getAbsoluteFile();
|
---|
| 110 | } else {
|
---|
| 111 | path = System.getProperty("josm.home");
|
---|
| 112 | if (path != null) {
|
---|
| 113 | cacheDir = new File(path, "cache");
|
---|
| 114 | } else {
|
---|
| 115 | path = Config.getPref().get("cache.folder", null);
|
---|
| 116 | if (path != null) {
|
---|
| 117 | cacheDir = new File(path).getAbsoluteFile();
|
---|
| 118 | } else {
|
---|
| 119 | cacheDir = Main.platform.getDefaultCacheDirectory();
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | if (createIfMissing && !cacheDir.exists() && !cacheDir.mkdirs()) {
|
---|
| 125 | Logging.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile()));
|
---|
| 126 | JOptionPane.showMessageDialog(
|
---|
| 127 | Main.parent,
|
---|
| 128 | tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()),
|
---|
| 129 | tr("Error"),
|
---|
| 130 | JOptionPane.ERROR_MESSAGE
|
---|
| 131 | );
|
---|
| 132 | }
|
---|
| 133 | return cacheDir;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|