Merge commit 'e6bb1700749980e69b5e913acbfd276f129c24dc'
[fanfix.git] / src / be / nikiroo / fanfix / VersionCheck.java
CommitLineData
b42117f1
NR
1package be.nikiroo.fanfix;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.InputStreamReader;
7import java.net.URL;
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.List;
1163de3d 11import java.util.Locale;
b42117f1
NR
12import java.util.Map;
13
14import be.nikiroo.utils.Version;
15
0efd25e3
NR
16/**
17 * Version checker: can check the current version of the program against a
18 * remote changelog, and list the missed updates and their description.
19 *
20 * @author niki
21 */
b42117f1 22public class VersionCheck {
1163de3d 23 private static final String base = "https://github.com/nikiroo/fanfix/raw/master/changelog${LANG}.md";
b42117f1
NR
24
25 private Version current;
26 private List<Version> newer;
27 private Map<Version, List<String>> changes;
28
29 /**
30 * Create a new {@link VersionCheck}.
31 *
32 * @param current
33 * the current version of the program
34 * @param newer
35 * the list of available {@link Version}s newer the current one
36 * @param changes
37 * the list of changes
38 */
39 private VersionCheck(Version current, List<Version> newer,
40 Map<Version, List<String>> changes) {
41 this.current = current;
42 this.newer = newer;
43 this.changes = changes;
44 }
45
46 /**
47 * Check if there are more recent {@link Version}s of this program
48 * available.
49 *
50 * @return TRUE if there is at least one
51 */
52 public boolean isNewVersionAvailable() {
53 return !newer.isEmpty();
54 }
55
56 /**
57 * The current {@link Version} of the program.
58 *
59 * @return the current {@link Version}
60 */
61 public Version getCurrentVersion() {
62 return current;
63 }
64
65 /**
66 * The list of available {@link Version}s newer than the current one.
67 *
68 * @return the newer {@link Version}s
69 */
70 public List<Version> getNewer() {
71 return newer;
72 }
73
74 /**
75 * The list of changes for each available {@link Version} newer than the
76 * current one.
77 *
78 * @return the list of changes
79 */
80 public Map<Version, List<String>> getChanges() {
81 return changes;
82 }
83
84 /**
85 * Ignore the check result.
86 */
87 public void ignore() {
88
89 }
90
91 /**
92 * Accept the information, and do not check again until the minimum wait
93 * time has elapsed.
94 */
95 public void ok() {
96 Instance.setVersionChecked();
97 }
98
99 /**
100 * Check if there are available {@link Version}s of this program more recent
101 * than the current one.
102 *
103 * @return a {@link VersionCheck}
104 */
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>>();
109
110 if (Instance.isVersionCheckNeeded()) {
111 try {
1163de3d 112 // Prepare the URLs according to the user's language
48f14dc9 113 Locale lang = Instance.getTrans().getLocale();
1163de3d
NR
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}", "");
119
120 InputStream in = null;
121 for (String url : new String[] { urlFrBE, urlFr, urlDefault }) {
122 try {
12443642
NR
123 in = Instance.getCache()
124 .open(new URL(url), null, false);
1163de3d
NR
125 break;
126 } catch (IOException e) {
127 }
128 }
129
130 if (in == null) {
131 throw new IOException("No changelog found");
132 }
133
b42117f1
NR
134 BufferedReader reader = new BufferedReader(
135 new InputStreamReader(in, "UTF-8"));
136 try {
7e191c68 137 Version version = new Version();
b42117f1
NR
138 for (String line = reader.readLine(); line != null; line = reader
139 .readLine()) {
140 if (line.startsWith("## Version ")) {
7e191c68
NR
141 version = new Version(line.substring("## Version "
142 .length()));
b42117f1
NR
143 if (version.isNewerThan(current)) {
144 newer.add(version);
145 changes.put(version, new ArrayList<String>());
7e191c68
NR
146 } else {
147 version = new Version();
b42117f1 148 }
7e191c68
NR
149 } else if (!version.isEmpty() && !newer.isEmpty()
150 && !line.isEmpty()) {
151 List<String> ch = changes.get(newer.get(newer
152 .size() - 1));
b42117f1
NR
153 if (!ch.isEmpty() && !line.startsWith("- ")) {
154 int i = ch.size() - 1;
155 ch.set(i, ch.get(i) + " " + line.trim());
156 } else {
157 ch.add(line.substring("- ".length()).trim());
158 }
159 }
160 }
161 } finally {
162 reader.close();
163 }
164 } catch (IOException e) {
1163de3d
NR
165 Instance.getTraceHandler()
166 .error(new IOException(
167 "Cannot download latest changelist on github.com",
168 e));
b42117f1
NR
169 }
170 }
171
172 return new VersionCheck(current, newer, changes);
173 }
174}