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 | import static org.openstreetmap.josm.tools.Utils.getSystemProperty;
|
---|
6 |
|
---|
7 | import java.awt.GraphicsEnvironment;
|
---|
8 | import java.io.File;
|
---|
9 |
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
14 | import org.openstreetmap.josm.spi.preferences.IBaseDirectories;
|
---|
15 | import org.openstreetmap.josm.tools.Logging;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Class provides base directory locations for JOSM.
|
---|
19 | * @since 13021
|
---|
20 | */
|
---|
21 | public final class JosmBaseDirectories implements IBaseDirectories {
|
---|
22 |
|
---|
23 | private JosmBaseDirectories() {
|
---|
24 | // hide constructor
|
---|
25 | }
|
---|
26 |
|
---|
27 | private static class InstanceHolder {
|
---|
28 | static final JosmBaseDirectories INSTANCE = new JosmBaseDirectories();
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Returns the unique instance.
|
---|
33 | * @return the unique instance
|
---|
34 | */
|
---|
35 | public static JosmBaseDirectories getInstance() {
|
---|
36 | return InstanceHolder.INSTANCE;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Internal storage for the preference directory.
|
---|
41 | */
|
---|
42 | private File preferencesDir;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Internal storage for the cache directory.
|
---|
46 | */
|
---|
47 | private File cacheDir;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Internal storage for the user data directory.
|
---|
51 | */
|
---|
52 | private File userdataDir;
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public File getPreferencesDirectory(boolean createIfMissing) {
|
---|
56 | if (preferencesDir == null) {
|
---|
57 | String path = getSystemProperty("josm.pref");
|
---|
58 | if (path != null) {
|
---|
59 | preferencesDir = new File(path).getAbsoluteFile();
|
---|
60 | } else {
|
---|
61 | path = getSystemProperty("josm.home");
|
---|
62 | if (path != null) {
|
---|
63 | preferencesDir = new File(path).getAbsoluteFile();
|
---|
64 | } else {
|
---|
65 | preferencesDir = Main.platform.getDefaultPrefDirectory();
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 | try {
|
---|
70 | if (createIfMissing && !preferencesDir.exists() && !preferencesDir.mkdirs()) {
|
---|
71 | Logging.warn(tr("Failed to create missing preferences directory: {0}", preferencesDir.getAbsoluteFile()));
|
---|
72 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
73 | JOptionPane.showMessageDialog(
|
---|
74 | Main.parent,
|
---|
75 | tr("<html>Failed to create missing preferences directory: {0}</html>", preferencesDir.getAbsoluteFile()),
|
---|
76 | tr("Error"),
|
---|
77 | JOptionPane.ERROR_MESSAGE
|
---|
78 | );
|
---|
79 | }
|
---|
80 | }
|
---|
81 | } catch (SecurityException e) {
|
---|
82 | Logging.log(Logging.LEVEL_ERROR, "Unable to check if preferences dir must be created", e);
|
---|
83 | }
|
---|
84 | return preferencesDir;
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public File getUserDataDirectory(boolean createIfMissing) {
|
---|
89 | if (userdataDir == null) {
|
---|
90 | String path = getSystemProperty("josm.userdata");
|
---|
91 | if (path != null) {
|
---|
92 | userdataDir = new File(path).getAbsoluteFile();
|
---|
93 | } else {
|
---|
94 | path = getSystemProperty("josm.home");
|
---|
95 | if (path != null) {
|
---|
96 | userdataDir = new File(path).getAbsoluteFile();
|
---|
97 | } else {
|
---|
98 | userdataDir = Main.platform.getDefaultUserDataDirectory();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 | try {
|
---|
103 | if (createIfMissing && !userdataDir.exists() && !userdataDir.mkdirs()) {
|
---|
104 | Logging.warn(tr("Failed to create missing user data directory: {0}", userdataDir.getAbsoluteFile()));
|
---|
105 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
106 | JOptionPane.showMessageDialog(
|
---|
107 | Main.parent,
|
---|
108 | tr("<html>Failed to create missing user data directory: {0}</html>", userdataDir.getAbsoluteFile()),
|
---|
109 | tr("Error"),
|
---|
110 | JOptionPane.ERROR_MESSAGE
|
---|
111 | );
|
---|
112 | }
|
---|
113 | }
|
---|
114 | } catch (SecurityException e) {
|
---|
115 | Logging.log(Logging.LEVEL_ERROR, "Unable to check if user data dir must be created", e);
|
---|
116 | }
|
---|
117 | return userdataDir;
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public File getCacheDirectory(boolean createIfMissing) {
|
---|
122 | if (cacheDir == null) {
|
---|
123 | String path = getSystemProperty("josm.cache");
|
---|
124 | if (path != null) {
|
---|
125 | cacheDir = new File(path).getAbsoluteFile();
|
---|
126 | } else {
|
---|
127 | path = getSystemProperty("josm.home");
|
---|
128 | if (path != null) {
|
---|
129 | cacheDir = new File(path, "cache");
|
---|
130 | } else {
|
---|
131 | path = Config.getPref().get("cache.folder", null);
|
---|
132 | if (path != null) {
|
---|
133 | cacheDir = new File(path).getAbsoluteFile();
|
---|
134 | } else {
|
---|
135 | cacheDir = Main.platform.getDefaultCacheDirectory();
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | try {
|
---|
141 | if (createIfMissing && !cacheDir.exists() && !cacheDir.mkdirs()) {
|
---|
142 | Logging.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile()));
|
---|
143 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
144 | JOptionPane.showMessageDialog(
|
---|
145 | Main.parent,
|
---|
146 | tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()),
|
---|
147 | tr("Error"),
|
---|
148 | JOptionPane.ERROR_MESSAGE
|
---|
149 | );
|
---|
150 | }
|
---|
151 | }
|
---|
152 | } catch (SecurityException e) {
|
---|
153 | Logging.log(Logging.LEVEL_ERROR, "Unable to check if cache dir must be created", e);
|
---|
154 | }
|
---|
155 | return cacheDir;
|
---|
156 | }
|
---|
157 | }
|
---|