Bundle: fix memory leak at init/reset
[fanfix.git] / VersionCheck.java
index 8b2a343f3cef331a2dbc059306586d6c2eaa83b7..0e16c2b5291c4e0a4bb445f8263e78374c3ff7c1 100644 (file)
@@ -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.
@@ -21,6 +19,7 @@ import be.nikiroo.utils.Version;
  */
 public class VersionCheck {
        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<Version> newer;
@@ -81,99 +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(String githubProject) {
+       public static VersionCheck check(String githubProject, Locale lang)
+                       throws IOException {
                Version current = Version.getCurrentVersion();
                List<Version> newer = new ArrayList<Version>();
                Map<Version, List<String>> changes = new HashMap<Version, List<String>>();
 
-               if (Instance.getInstance().isVersionCheckNeeded()) {
-                       try {
-                               // Use the right project:
-                               String base = VersionCheck.base.replace("${PROJECT}",
-                                               githubProject);
-                               
-                               // 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().openNoCache(
-                                                               new URL(url), null, null, null, null);
-                                               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<String>());
-                                                       } else {
-                                                               version = new Version();
-                                                       }
-                                               } else if (!version.isEmpty() && !newer.isEmpty()
-                                                               && !line.isEmpty()) {
-                                                       List<String> 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<String>());
+                                       } else {
+                                               version = new Version();
+                                       }
+                               } else if (!version.isEmpty() && !newer.isEmpty()
+                                               && !line.isEmpty()) {
+                                       List<String> 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)//
+               );
+       }
 }