Commit | Line | Data |
---|---|---|
b42117f1 NR |
1 | package be.nikiroo.fanfix; |
2 | ||
3 | import java.io.BufferedReader; | |
4 | import java.io.IOException; | |
5 | import java.io.InputStream; | |
6 | import java.io.InputStreamReader; | |
7 | import java.net.URL; | |
8 | import java.util.ArrayList; | |
9 | import java.util.HashMap; | |
10 | import java.util.List; | |
11 | import java.util.Map; | |
12 | ||
13 | import be.nikiroo.utils.Version; | |
14 | ||
15 | public class VersionCheck { | |
16 | private static final String url = "https://github.com/nikiroo/fanfix/raw/master/changelog.md"; | |
17 | ||
18 | private Version current; | |
19 | private List<Version> newer; | |
20 | private Map<Version, List<String>> changes; | |
21 | ||
22 | /** | |
23 | * Create a new {@link VersionCheck}. | |
24 | * | |
25 | * @param current | |
26 | * the current version of the program | |
27 | * @param newer | |
28 | * the list of available {@link Version}s newer the current one | |
29 | * @param changes | |
30 | * the list of changes | |
31 | */ | |
32 | private VersionCheck(Version current, List<Version> newer, | |
33 | Map<Version, List<String>> changes) { | |
34 | this.current = current; | |
35 | this.newer = newer; | |
36 | this.changes = changes; | |
37 | } | |
38 | ||
39 | /** | |
40 | * Check if there are more recent {@link Version}s of this program | |
41 | * available. | |
42 | * | |
43 | * @return TRUE if there is at least one | |
44 | */ | |
45 | public boolean isNewVersionAvailable() { | |
46 | return !newer.isEmpty(); | |
47 | } | |
48 | ||
49 | /** | |
50 | * The current {@link Version} of the program. | |
51 | * | |
52 | * @return the current {@link Version} | |
53 | */ | |
54 | public Version getCurrentVersion() { | |
55 | return current; | |
56 | } | |
57 | ||
58 | /** | |
59 | * The list of available {@link Version}s newer than the current one. | |
60 | * | |
61 | * @return the newer {@link Version}s | |
62 | */ | |
63 | public List<Version> getNewer() { | |
64 | return newer; | |
65 | } | |
66 | ||
67 | /** | |
68 | * The list of changes for each available {@link Version} newer than the | |
69 | * current one. | |
70 | * | |
71 | * @return the list of changes | |
72 | */ | |
73 | public Map<Version, List<String>> getChanges() { | |
74 | return changes; | |
75 | } | |
76 | ||
77 | /** | |
78 | * Ignore the check result. | |
79 | */ | |
80 | public void ignore() { | |
81 | ||
82 | } | |
83 | ||
84 | /** | |
85 | * Accept the information, and do not check again until the minimum wait | |
86 | * time has elapsed. | |
87 | */ | |
88 | public void ok() { | |
89 | Instance.setVersionChecked(); | |
90 | } | |
91 | ||
92 | /** | |
93 | * Check if there are available {@link Version}s of this program more recent | |
94 | * than the current one. | |
95 | * | |
96 | * @return a {@link VersionCheck} | |
97 | */ | |
98 | public static VersionCheck check() { | |
99 | Version current = Version.getCurrentVersion(); | |
100 | List<Version> newer = new ArrayList<Version>(); | |
101 | Map<Version, List<String>> changes = new HashMap<Version, List<String>>(); | |
102 | ||
103 | if (Instance.isVersionCheckNeeded()) { | |
104 | try { | |
105 | InputStream in = Instance.getCache().openNoCache(new URL(url), | |
106 | null); | |
107 | BufferedReader reader = new BufferedReader( | |
108 | new InputStreamReader(in, "UTF-8")); | |
109 | try { | |
110 | for (String line = reader.readLine(); line != null; line = reader | |
111 | .readLine()) { | |
112 | if (line.startsWith("## Version ")) { | |
113 | String v = line.substring("## Version ".length()); | |
114 | Version version = new Version(v); | |
115 | if (version.isNewerThan(current)) { | |
116 | newer.add(version); | |
117 | changes.put(version, new ArrayList<String>()); | |
118 | } | |
119 | } else if (!newer.isEmpty() && !line.isEmpty()) { | |
120 | Version version = newer.get(newer.size() - 1); | |
121 | List<String> ch = changes.get(version); | |
122 | if (!ch.isEmpty() && !line.startsWith("- ")) { | |
123 | int i = ch.size() - 1; | |
124 | ch.set(i, ch.get(i) + " " + line.trim()); | |
125 | } else { | |
126 | ch.add(line.substring("- ".length()).trim()); | |
127 | } | |
128 | } | |
129 | } | |
130 | } finally { | |
131 | reader.close(); | |
132 | } | |
133 | } catch (IOException e) { | |
134 | Instance.syserr(e); | |
135 | } | |
136 | } | |
137 | ||
138 | return new VersionCheck(current, newer, changes); | |
139 | } | |
140 | } |