1 package be
.nikiroo
.fanfix
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.io
.InputStreamReader
;
8 import java
.util
.ArrayList
;
9 import java
.util
.HashMap
;
10 import java
.util
.List
;
13 import be
.nikiroo
.utils
.Version
;
15 public class VersionCheck
{
16 private static final String url
= "https://github.com/nikiroo/fanfix/raw/master/changelog.md";
18 private Version current
;
19 private List
<Version
> newer
;
20 private Map
<Version
, List
<String
>> changes
;
23 * Create a new {@link VersionCheck}.
26 * the current version of the program
28 * the list of available {@link Version}s newer the current one
32 private VersionCheck(Version current
, List
<Version
> newer
,
33 Map
<Version
, List
<String
>> changes
) {
34 this.current
= current
;
36 this.changes
= changes
;
40 * Check if there are more recent {@link Version}s of this program
43 * @return TRUE if there is at least one
45 public boolean isNewVersionAvailable() {
46 return !newer
.isEmpty();
50 * The current {@link Version} of the program.
52 * @return the current {@link Version}
54 public Version
getCurrentVersion() {
59 * The list of available {@link Version}s newer than the current one.
61 * @return the newer {@link Version}s
63 public List
<Version
> getNewer() {
68 * The list of changes for each available {@link Version} newer than the
71 * @return the list of changes
73 public Map
<Version
, List
<String
>> getChanges() {
78 * Ignore the check result.
80 public void ignore() {
85 * Accept the information, and do not check again until the minimum wait
89 Instance
.setVersionChecked();
93 * Check if there are available {@link Version}s of this program more recent
94 * than the current one.
96 * @return a {@link VersionCheck}
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
>>();
103 if (Instance
.isVersionCheckNeeded()) {
105 InputStream in
= Instance
.getCache().openNoCache(new URL(url
),
107 BufferedReader reader
= new BufferedReader(
108 new InputStreamReader(in
, "UTF-8"));
110 Version version
= new Version();
111 for (String line
= reader
.readLine(); line
!= null; line
= reader
113 if (line
.startsWith("## Version ")) {
114 version
= new Version(line
.substring("## Version "
116 if (version
.isNewerThan(current
)) {
118 changes
.put(version
, new ArrayList
<String
>());
120 version
= new Version();
122 } else if (!version
.isEmpty() && !newer
.isEmpty()
123 && !line
.isEmpty()) {
124 List
<String
> ch
= changes
.get(newer
.get(newer
126 if (!ch
.isEmpty() && !line
.startsWith("- ")) {
127 int i
= ch
.size() - 1;
128 ch
.set(i
, ch
.get(i
) + " " + line
.trim());
130 ch
.add(line
.substring("- ".length()).trim());
137 } catch (IOException e
) {
142 return new VersionCheck(current
, newer
, changes
);