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