X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2FVersionCheck.java;fp=src%2Fbe%2Fnikiroo%2Ffanfix%2FVersionCheck.java;h=3359bacbce41b977a6a7ee9daf089a02a8331e61;hb=b42117f163c9c0dd96424d43730fb95fb088b4be;hp=0000000000000000000000000000000000000000;hpb=ba33e96ec7a51c7457d80133b7d202590b8dc005;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/VersionCheck.java b/src/be/nikiroo/fanfix/VersionCheck.java new file mode 100644 index 0000000..3359bac --- /dev/null +++ b/src/be/nikiroo/fanfix/VersionCheck.java @@ -0,0 +1,140 @@ +package be.nikiroo.fanfix; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import be.nikiroo.utils.Version; + +public class VersionCheck { + private static final String url = "https://github.com/nikiroo/fanfix/raw/master/changelog.md"; + + private Version current; + private List newer; + private Map> changes; + + /** + * Create a new {@link VersionCheck}. + * + * @param current + * the current version of the program + * @param newer + * the list of available {@link Version}s newer the current one + * @param changes + * the list of changes + */ + private VersionCheck(Version current, List newer, + Map> changes) { + this.current = current; + this.newer = newer; + this.changes = changes; + } + + /** + * Check if there are more recent {@link Version}s of this program + * available. + * + * @return TRUE if there is at least one + */ + public boolean isNewVersionAvailable() { + return !newer.isEmpty(); + } + + /** + * The current {@link Version} of the program. + * + * @return the current {@link Version} + */ + public Version getCurrentVersion() { + return current; + } + + /** + * The list of available {@link Version}s newer than the current one. + * + * @return the newer {@link Version}s + */ + public List getNewer() { + return newer; + } + + /** + * The list of changes for each available {@link Version} newer than the + * current one. + * + * @return the list of changes + */ + public Map> getChanges() { + return changes; + } + + /** + * Ignore the check result. + */ + public void ignore() { + + } + + /** + * Accept the information, and do not check again until the minimum wait + * time has elapsed. + */ + public void ok() { + Instance.setVersionChecked(); + } + + /** + * Check if there are available {@link Version}s of this program more recent + * than the current one. + * + * @return a {@link VersionCheck} + */ + public static VersionCheck check() { + Version current = Version.getCurrentVersion(); + List newer = new ArrayList(); + Map> changes = new HashMap>(); + + if (Instance.isVersionCheckNeeded()) { + try { + InputStream in = Instance.getCache().openNoCache(new URL(url), + null); + BufferedReader reader = new BufferedReader( + new InputStreamReader(in, "UTF-8")); + try { + for (String line = reader.readLine(); line != null; line = reader + .readLine()) { + if (line.startsWith("## Version ")) { + String v = line.substring("## Version ".length()); + Version version = new Version(v); + if (version.isNewerThan(current)) { + newer.add(version); + changes.put(version, new ArrayList()); + } + } else if (!newer.isEmpty() && !line.isEmpty()) { + Version version = newer.get(newer.size() - 1); + List ch = changes.get(version); + if (!ch.isEmpty() && !line.startsWith("- ")) { + int i = ch.size() - 1; + ch.set(i, ch.get(i) + " " + line.trim()); + } else { + ch.add(line.substring("- ".length()).trim()); + } + } + } + } finally { + reader.close(); + } + } catch (IOException e) { + Instance.syserr(e); + } + } + + return new VersionCheck(current, newer, changes); + } +}