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