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
;
16 * Version checker: can check the current version of the program against a
17 * remote changelog, and list the missed updates and their description.
21 public class VersionCheck
{
22 private static final String url
= "https://github.com/nikiroo/fanfix/raw/master/changelog.md";
24 private Version current
;
25 private List
<Version
> newer
;
26 private Map
<Version
, List
<String
>> changes
;
29 * Create a new {@link VersionCheck}.
32 * the current version of the program
34 * the list of available {@link Version}s newer the current one
38 private VersionCheck(Version current
, List
<Version
> newer
,
39 Map
<Version
, List
<String
>> changes
) {
40 this.current
= current
;
42 this.changes
= changes
;
46 * Check if there are more recent {@link Version}s of this program
49 * @return TRUE if there is at least one
51 public boolean isNewVersionAvailable() {
52 return !newer
.isEmpty();
56 * The current {@link Version} of the program.
58 * @return the current {@link Version}
60 public Version
getCurrentVersion() {
65 * The list of available {@link Version}s newer than the current one.
67 * @return the newer {@link Version}s
69 public List
<Version
> getNewer() {
74 * The list of changes for each available {@link Version} newer than the
77 * @return the list of changes
79 public Map
<Version
, List
<String
>> getChanges() {
84 * Ignore the check result.
86 public void ignore() {
91 * Accept the information, and do not check again until the minimum wait
95 Instance
.setVersionChecked();
99 * Check if there are available {@link Version}s of this program more recent
100 * than the current one.
102 * @return a {@link VersionCheck}
104 public static VersionCheck
check() {
105 Version current
= Version
.getCurrentVersion();
106 List
<Version
> newer
= new ArrayList
<Version
>();
107 Map
<Version
, List
<String
>> changes
= new HashMap
<Version
, List
<String
>>();
109 if (Instance
.isVersionCheckNeeded()) {
111 InputStream in
= Instance
.getCache().openNoCache(new URL(url
));
112 BufferedReader reader
= new BufferedReader(
113 new InputStreamReader(in
, "UTF-8"));
115 Version version
= new Version();
116 for (String line
= reader
.readLine(); line
!= null; line
= reader
118 if (line
.startsWith("## Version ")) {
119 version
= new Version(line
.substring("## Version "
121 if (version
.isNewerThan(current
)) {
123 changes
.put(version
, new ArrayList
<String
>());
125 version
= new Version();
127 } else if (!version
.isEmpty() && !newer
.isEmpty()
128 && !line
.isEmpty()) {
129 List
<String
> ch
= changes
.get(newer
.get(newer
131 if (!ch
.isEmpty() && !line
.startsWith("- ")) {
132 int i
= ch
.size() - 1;
133 ch
.set(i
, ch
.get(i
) + " " + line
.trim());
135 ch
.add(line
.substring("- ".length()).trim());
142 } catch (IOException e
) {
143 Instance
.getTraceHandler().error(new IOException(
144 "Cannot download latest changelist on github.com", e
));
148 return new VersionCheck(current
, newer
, changes
);