1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Online resources directly used by JOSM.
|
---|
10 | * This does not include websites where user can sometimes be redirected through its web browser,
|
---|
11 | * but only those to we establish a connection.
|
---|
12 | *
|
---|
13 | * @since 7434
|
---|
14 | */
|
---|
15 | public enum OnlineResource {
|
---|
16 |
|
---|
17 | /** The OSM API, used for download, upload, history, etc. */
|
---|
18 | OSM_API(tr("OSM API")),
|
---|
19 | /** The JOSM website, used for startup page, imagery/presets/styles/rules entries, help, etc. */
|
---|
20 | JOSM_WEBSITE(tr("JOSM website")),
|
---|
21 | /** Updates of {@link CachedFile} */
|
---|
22 | CACHE_UPDATES(tr("Cache updates")),
|
---|
23 | /** Various government certificates downloaded on Windows to make https imagery work in some countries */
|
---|
24 | CERTIFICATES(tr("Certificates")),
|
---|
25 | /** Value used to represent all online resources */
|
---|
26 | ALL(tr("All"));
|
---|
27 |
|
---|
28 | private final String locName;
|
---|
29 |
|
---|
30 | OnlineResource(String locName) {
|
---|
31 | this.locName = locName;
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Replies the localized name.
|
---|
36 | * @return the localized name
|
---|
37 | */
|
---|
38 | public final String getLocName() {
|
---|
39 | return locName;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Replies the offline icon.
|
---|
44 | * @return the offline icon
|
---|
45 | * @since 17041
|
---|
46 | */
|
---|
47 | public final String getOfflineIcon() {
|
---|
48 | switch (this) {
|
---|
49 | case OSM_API:
|
---|
50 | return /* ICON() */ "offline_osm_api";
|
---|
51 | case JOSM_WEBSITE:
|
---|
52 | return /* ICON() */ "offline_josm_website";
|
---|
53 | case CACHE_UPDATES:
|
---|
54 | return /* ICON() */ "offline_cache_updates";
|
---|
55 | case CERTIFICATES:
|
---|
56 | return /* ICON() */ "offline_certificates";
|
---|
57 | case ALL:
|
---|
58 | return /* ICON() */ "offline_all";
|
---|
59 | default:
|
---|
60 | return null;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Replies whether the given URL matches this online resource
|
---|
66 | * @param url the URL to check
|
---|
67 | * @return whether the given URL matches this online resource
|
---|
68 | */
|
---|
69 | public final boolean matches(String url) {
|
---|
70 | final String baseUrl;
|
---|
71 | switch (this) {
|
---|
72 | case ALL:
|
---|
73 | return true;
|
---|
74 | case OSM_API:
|
---|
75 | baseUrl = OsmApi.getOsmApi().getServerUrl();
|
---|
76 | break;
|
---|
77 | case JOSM_WEBSITE:
|
---|
78 | baseUrl = Config.getUrls().getJOSMWebsite();
|
---|
79 | break;
|
---|
80 | default:
|
---|
81 | return false;
|
---|
82 | }
|
---|
83 | return url.startsWith(baseUrl.substring(baseUrl.indexOf("://")), url.indexOf("://"));
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Ensures resource is not accessed in offline mode.
|
---|
88 | * @param downloadString The attempted download string
|
---|
89 | * @param ignore ignored
|
---|
90 | * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol
|
---|
91 | * @deprecated use {@link NetworkManager#isOffline(String)}
|
---|
92 | */
|
---|
93 | @Deprecated
|
---|
94 | public final void checkOfflineAccess(String downloadString, String ignore) {
|
---|
95 | if (NetworkManager.isOffline(downloadString)) {
|
---|
96 | throw OfflineAccessException.forResource(downloadString);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|