This repository was archived by the owner on Feb 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathUserDirectories.java
423 lines (403 loc) · 13.1 KB
/
UserDirectories.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package dev.dirs;
import dev.dirs.impl.Linux;
import dev.dirs.impl.Util;
import dev.dirs.impl.Windows;
/** {@code UserDirectories} provides paths of user-facing standard directories, following the conventions of the operating system the library is running on.
*
* <h2>Examples</h2>
* <p>
* All examples on this page are computed with a user named <em>Alice</em>.
* <p>
* Example of {@link UserDirectories#audioDir} value in different operating systems:
* <ul>
* <li><b>Linux/BSD:</b> {@code /home/alice/Music}</li>
* <li><b>macOS:</b> {@code /Users/Alice/Music}</li>
* <li><b>Windows:</b> {@code C:\Users\Alice\Music}</li>
* </ul>
*/
public final class UserDirectories {
/** Returns the path to the user's home directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code $HOME}</td>
* <td>/home/alice</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}</td>
* <td>/Users/Alice</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_Profile}}</td>
* <td>C:\Users\Alice</td>
* </tr>
* </table>
*/
public final String homeDir;
/** Returns the path to the user's audio directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code XDG_MUSIC_DIR}</td>
* <td>/home/alice/Music</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Music</td>
* <td>/Users/Alice/Music</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_Music}}</td>
* <td>C:\Users\Alice\Music</td>
* </tr>
* </table>
*/
public final String audioDir;
/** Returns the path to the user's desktop directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code XDG_DESKTOP_DIR}</td>
* <td>/home/alice/Desktop</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Desktop</td>
* <td>/Users/Alice/Desktop</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_Desktop}}</td>
* <td>C:\Users\Alice\Desktop</td>
* </tr>
* </table>
*/
public final String desktopDir;
/** Returns the path to the user's document directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code XDG_DOCUMENTS_DIR}</td>
* <td>/home/alice/Documents</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Documents</td>
* <td>/Users/Alice/Documents</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_Documents}}</td>
* <td>C:\Users\Alice\Documents</td>
* </tr>
* </table>
*/
public final String documentDir;
/** Returns the path to the user's download directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code XDG_DOWNLOAD_DIR}</td>
* <td>/home/alice/Downloads</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Downloads</td>
* <td>/Users/Alice/Downloads</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_Downloads}}</td>
* <td>C:\Users\Alice\Downloads</td>
* </tr>
* </table>
*/
public final String downloadDir;
/** Returns the path to the user's font directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code $XDG_DATA_HOME}/fonts or {@code $HOME}/.local/share/fonts</td>
* <td>/home/alice/.local/share/fonts</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Library/Fonts</td>
* <td>/Users/Alice/Library/Fonts</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>–</td>
* <td>{@code null}</td>
* </tr>
* </table>
*/
public final String fontDir;
/** Returns the path to the user's picture directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code XDG_PICTURES_DIR}</td>
* <td>/home/alice/Pictures</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Pictures</td>
* <td>/Users/Alice/Pictures</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_Pictures}}</td>
* <td>C:\Users\Alice\Pictures</td>
* </tr>
* </table>
*/
public final String pictureDir;
/** Returns the path to the user's public directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code XDG_PUBLICSHARE_DIR}</td>
* <td>/home/alice/Public</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Public</td>
* <td>/Users/Alice/Public</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_Public}}</td>
* <td>C:\Users\Public</td>
* </tr>
* </table>
*/
public final String publicDir;
/** Returns the path to the user's template directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code XDG_TEMPLATES_DIR}</td>
* <td>/home/alice/Templates</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>–</td>
* <td>{@code null}</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_Templates}}</td>
* <td>C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates</td>
* </tr>
* </table>
*/
public final String templateDir;
/** Returns the path to the user's video directory.
* <br><br>
* <table border="1" cellpadding="1" cellspacing="0">
* <tr>
* <th align="left">Platform</th>
* <th align="left">Value</th>
* <th align="left">Example</th>
* </tr>
* <tr>
* <td>Linux/BSD</td>
* <td>{@code XDG_VIDEOS_DIR}</td>
* <td>/home/alice/Videos</td>
* </tr>
* <tr>
* <td>macOS</td>
* <td>{@code $HOME}/Movies</td>
* <td>/Users/Alice/Movies</td>
* </tr>
* <tr>
* <td>Windows</td>
* <td>{@code {FOLDERID_Videos}}</td>
* <td>C:\Users\Alice\Videos</td>
* </tr>
* </table>
*/
public final String videoDir;
/** Creates a new {@code UserDirectories} instance.
* <p>
* The instance is an immutable snapshot of the state of the system at the time this method is invoked.
* Subsequent changes to the state of the system are not reflected in instances created prior to such a change.
*
* @return A new {@code UserDirectories} instance.
*/
public static UserDirectories get() {
return new UserDirectories();
}
private UserDirectories() {
switch (Constants.operatingSystem) {
case Constants.LIN:
case Constants.BSD:
case Constants.SOLARIS:
case Constants.AIX:
String[] userDirs = Linux.getXDGUserDirs("MUSIC", "DESKTOP", "DOCUMENTS", "DOWNLOAD", "PICTURES", "PUBLICSHARE", "TEMPLATES", "VIDEOS");
homeDir = System.getProperty("user.home");
audioDir = userDirs[0];
desktopDir = userDirs[1];
documentDir = userDirs[2];
downloadDir = userDirs[3];
fontDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
pictureDir = userDirs[4];
publicDir = userDirs[5];
templateDir = userDirs[6];
videoDir = userDirs[7];
break;
case Constants.MAC:
homeDir = System.getProperty("user.home");
audioDir = homeDir + "/Music";
desktopDir = homeDir + "/Desktop";
documentDir = homeDir + "/Documents";
downloadDir = homeDir + "/Downloads";
fontDir = homeDir + "/Library/Fonts";
pictureDir = homeDir + "/Pictures";
publicDir = homeDir + "/Public";
templateDir = null;
videoDir = homeDir + "/Movies";
break;
case Constants.IBMI:
homeDir = System.getProperty("user.home");
audioDir = homeDir + "/Music";
desktopDir = homeDir + "/Desktop";
documentDir = homeDir + "/Documents";
downloadDir = homeDir + "/Downloads";
fontDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
pictureDir = homeDir + "/Pictures";
publicDir = homeDir + "/Public";
templateDir = null;
videoDir = homeDir + "/Movies";
break;
case Constants.WIN:
homeDir = Windows.getProfileDir();
audioDir = Windows.getMusicDir();
fontDir = null;
desktopDir = Windows.getDesktopDir();
documentDir = Windows.getDocumentsDir();
downloadDir = Windows.getDownloadsDir();
pictureDir = Windows.getPicturesDir();
publicDir = Windows.getPublicDir();
templateDir = Windows.getTemplatesDir();
videoDir = Windows.getVideosDir();
break;
default:
throw new UnsupportedOperatingSystemException("User directories are not supported on " + Constants.operatingSystemName);
}
}
@Override
public String toString() {
return "UserDirectories (" + Constants.operatingSystemName + "):\n" +
" homeDir = '" + homeDir + "'\n" +
" audioDir = '" + audioDir + "'\n" +
" fontDir = '" + fontDir + "'\n" +
" desktopDir = '" + desktopDir + "'\n" +
" documentDir = '" + documentDir + "'\n" +
" downloadDir = '" + downloadDir + "'\n" +
" pictureDir = '" + pictureDir + "'\n" +
" publicDir = '" + publicDir + "'\n" +
" templateDir = '" + templateDir + "'\n" +
" videoDir = '" + videoDir + "'\n";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserDirectories that = (UserDirectories) o;
if (homeDir != null ? !homeDir .equals(that.homeDir) : that.homeDir != null)
return false;
if (audioDir != null ? !audioDir .equals(that.audioDir) : that.audioDir != null)
return false;
if (fontDir != null ? !fontDir .equals(that.fontDir) : that.fontDir != null)
return false;
if (desktopDir != null ? !desktopDir .equals(that.desktopDir) : that.desktopDir != null)
return false;
if (documentDir != null ? !documentDir.equals(that.documentDir) : that.documentDir != null)
return false;
if (downloadDir != null ? !downloadDir.equals(that.downloadDir) : that.downloadDir != null)
return false;
if (pictureDir != null ? !pictureDir .equals(that.pictureDir) : that.pictureDir != null)
return false;
if (publicDir != null ? !publicDir .equals(that.publicDir) : that.publicDir != null)
return false;
if (templateDir != null ? !templateDir.equals(that.templateDir) : that.templateDir != null)
return false;
if (videoDir != null ? !videoDir .equals(that.videoDir) : that.videoDir != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = 0;
result = 31 * result + (homeDir != null ? homeDir .hashCode() : 0);
result = 31 * result + (audioDir != null ? audioDir .hashCode() : 0);
result = 31 * result + (fontDir != null ? fontDir .hashCode() : 0);
result = 31 * result + (desktopDir != null ? desktopDir .hashCode() : 0);
result = 31 * result + (documentDir != null ? documentDir.hashCode() : 0);
result = 31 * result + (downloadDir != null ? downloadDir.hashCode() : 0);
result = 31 * result + (pictureDir != null ? pictureDir .hashCode() : 0);
result = 31 * result + (publicDir != null ? publicDir .hashCode() : 0);
result = 31 * result + (templateDir != null ? templateDir.hashCode() : 0);
result = 31 * result + (videoDir != null ? videoDir .hashCode() : 0);
return result;
}
}