X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=VersionCheck.java;h=0e16c2b5291c4e0a4bb445f8263e78374c3ff7c1;hp=f64159abb97c3fe3236bc272710402a1e32631f7;hb=712ddafb749aada41daab85c36ac12f657b2307e;hpb=371a36bd5f5d0e46038491ecfdd18653fe4d6f61 diff --git a/VersionCheck.java b/VersionCheck.java index f64159a..0e16c2b 100644 --- a/VersionCheck.java +++ b/VersionCheck.java @@ -1,4 +1,4 @@ -package be.nikiroo.fanfix; +package be.nikiroo.utils; import java.io.BufferedReader; import java.io.IOException; @@ -11,8 +11,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import be.nikiroo.utils.Version; - /** * Version checker: can check the current version of the program against a * remote changelog, and list the missed updates and their description. @@ -20,7 +18,8 @@ import be.nikiroo.utils.Version; * @author niki */ public class VersionCheck { - private static final String base = "https://github.com/nikiroo/fanfix/raw/master/changelog${LANG}.md"; + private static final String base = "https://github.com/${PROJECT}/raw/master/changelog${LANG}.md"; + private static Downloader downloader = new Downloader(null); private Version current; private List newer; @@ -81,91 +80,93 @@ public class VersionCheck { 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.getInstance().setVersionChecked(); - } - /** * Check if there are available {@link Version}s of this program more recent * than the current one. * + * @param githubProject + * the GitHub project to check on, for instance "nikiroo/fanfix" + * @param lang + * the current locale, so we can try to get the changelog in the + * correct language (can be NULL, will fetch the default + * changelog) + * * @return a {@link VersionCheck} + * + * @throws IOException + * in case of I/O error */ - public static VersionCheck check() { + public static VersionCheck check(String githubProject, Locale lang) + throws IOException { Version current = Version.getCurrentVersion(); List newer = new ArrayList(); Map> changes = new HashMap>(); - if (Instance.getInstance().isVersionCheckNeeded()) { - try { - // Prepare the URLs according to the user's language - Locale lang = Instance.getInstance().getTrans().getLocale(); - String fr = lang.getLanguage(); - String BE = lang.getCountry().replace(".UTF8", ""); - String urlFrBE = base.replace("${LANG}", "-" + fr + "_" + BE); - String urlFr = base.replace("${LANG}", "-" + fr); - String urlDefault = base.replace("${LANG}", ""); + // Use the right project: + String base = VersionCheck.base.replace("${PROJECT}", githubProject); - InputStream in = null; - for (String url : new String[] { urlFrBE, urlFr, urlDefault }) { - try { - in = Instance.getInstance().getCache().open(new URL(url), null, false); - break; - } catch (IOException e) { - } - } + // Prepare the URLs according to the user's language (we take here + // "-fr_BE" as an example): + String fr = lang == null ? "" : "-" + lang.getLanguage(); + String BE = lang == null ? "" + : "_" + lang.getCountry().replace(".UTF8", ""); + String urlFrBE = base.replace("${LANG}", fr + BE); + String urlFr = base.replace("${LANG}", "-" + fr); + String urlDefault = base.replace("${LANG}", ""); - if (in == null) { - throw new IOException("No changelog found"); - } + InputStream in = null; + for (String url : new String[] { urlFrBE, urlFr, urlDefault }) { + try { + in = downloader.open(new URL(url), false); + break; + } catch (IOException e) { + } + } + + if (in == null) { + throw new IOException("No changelog found"); + } - BufferedReader reader = new BufferedReader( - new InputStreamReader(in, "UTF-8")); - try { - Version version = new Version(); - for (String line = reader.readLine(); line != null; line = reader - .readLine()) { - if (line.startsWith("## Version ")) { - version = new Version(line.substring("## Version " - .length())); - if (version.isNewerThan(current)) { - newer.add(version); - changes.put(version, new ArrayList()); - } else { - version = new Version(); - } - } else if (!version.isEmpty() && !newer.isEmpty() - && !line.isEmpty()) { - List ch = changes.get(newer.get(newer - .size() - 1)); - 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()); - } - } + BufferedReader reader = new BufferedReader( + new InputStreamReader(in, "UTF-8")); + try { + Version version = new Version(); + for (String line = reader.readLine(); line != null; line = reader + .readLine()) { + if (line.startsWith("## Version ")) { + version = new Version( + line.substring("## Version ".length())); + if (version.isNewerThan(current)) { + newer.add(version); + changes.put(version, new ArrayList()); + } else { + version = new Version(); + } + } else if (!version.isEmpty() && !newer.isEmpty() + && !line.isEmpty()) { + List ch = changes.get(newer.get(newer.size() - 1)); + 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.getInstance().getTraceHandler() - .error(new IOException("Cannot download latest changelist on github.com", e)); } + } finally { + reader.close(); } return new VersionCheck(current, newer, changes); } + + @Override + public String toString() { + return String.format( + "Version checker: version [%s], %d releases behind latest version [%s]", // + current, // + newer.size(), // + newer.isEmpty() ? current : newer.get(newer.size() - 1)// + ); + } }