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
;
11 import java
.util
.Locale
;
14 import be
.nikiroo
.utils
.Version
;
17 * Version checker: can check the current version of the program against a
18 * remote changelog, and list the missed updates and their description.
22 public class VersionCheck
{
23 private static final String base
= "https://github.com/nikiroo/fanfix/raw/master/changelog${LANG}.md";
25 private Version current
;
26 private List
<Version
> newer
;
27 private Map
<Version
, List
<String
>> changes
;
30 * Create a new {@link VersionCheck}.
33 * the current version of the program
35 * the list of available {@link Version}s newer the current one
39 private VersionCheck(Version current
, List
<Version
> newer
,
40 Map
<Version
, List
<String
>> changes
) {
41 this.current
= current
;
43 this.changes
= changes
;
47 * Check if there are more recent {@link Version}s of this program
50 * @return TRUE if there is at least one
52 public boolean isNewVersionAvailable() {
53 return !newer
.isEmpty();
57 * The current {@link Version} of the program.
59 * @return the current {@link Version}
61 public Version
getCurrentVersion() {
66 * The list of available {@link Version}s newer than the current one.
68 * @return the newer {@link Version}s
70 public List
<Version
> getNewer() {
75 * The list of changes for each available {@link Version} newer than the
78 * @return the list of changes
80 public Map
<Version
, List
<String
>> getChanges() {
85 * Ignore the check result.
87 public void ignore() {
92 * Accept the information, and do not check again until the minimum wait
96 Instance
.getInstance().setVersionChecked();
100 * Check if there are available {@link Version}s of this program more recent
101 * than the current one.
103 * @return a {@link VersionCheck}
105 public static VersionCheck
check() {
106 Version current
= Version
.getCurrentVersion();
107 List
<Version
> newer
= new ArrayList
<Version
>();
108 Map
<Version
, List
<String
>> changes
= new HashMap
<Version
, List
<String
>>();
110 if (Instance
.getInstance().isVersionCheckNeeded()) {
112 // Prepare the URLs according to the user's language
113 Locale lang
= Instance
.getInstance().getTrans().getLocale();
114 String fr
= lang
.getLanguage();
115 String BE
= lang
.getCountry().replace(".UTF8", "");
116 String urlFrBE
= base
.replace("${LANG}", "-" + fr
+ "_" + BE
);
117 String urlFr
= base
.replace("${LANG}", "-" + fr
);
118 String urlDefault
= base
.replace("${LANG}", "");
120 InputStream in
= null;
121 for (String url
: new String
[] { urlFrBE
, urlFr
, urlDefault
}) {
123 in
= Instance
.getInstance().getCache().open(new URL(url
), null, false);
125 } catch (IOException e
) {
130 throw new IOException("No changelog found");
133 BufferedReader reader
= new BufferedReader(
134 new InputStreamReader(in
, "UTF-8"));
136 Version version
= new Version();
137 for (String line
= reader
.readLine(); line
!= null; line
= reader
139 if (line
.startsWith("## Version ")) {
140 version
= new Version(line
.substring("## Version "
142 if (version
.isNewerThan(current
)) {
144 changes
.put(version
, new ArrayList
<String
>());
146 version
= new Version();
148 } else if (!version
.isEmpty() && !newer
.isEmpty()
149 && !line
.isEmpty()) {
150 List
<String
> ch
= changes
.get(newer
.get(newer
152 if (!ch
.isEmpty() && !line
.startsWith("- ")) {
153 int i
= ch
.size() - 1;
154 ch
.set(i
, ch
.get(i
) + " " + line
.trim());
156 ch
.add(line
.substring("- ".length()).trim());
163 } catch (IOException e
) {
164 Instance
.getInstance().getTraceHandler()
165 .error(new IOException("Cannot download latest changelist on github.com", e
));
169 return new VersionCheck(current
, newer
, changes
);