Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / fanfix / VersionCheck.java
index 62ee814071b734b1fadff3f90d5b5b746b6dae1e..8b2a343f3cef331a2dbc059306586d6c2eaa83b7 100644 (file)
@@ -8,6 +8,7 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
 import be.nikiroo.utils.Version;
@@ -19,7 +20,7 @@ import be.nikiroo.utils.Version;
  * @author niki
  */
 public class VersionCheck {
-       private static final String url = "https://github.com/nikiroo/fanfix/raw/master/changelog.md";
+       private static final String base = "https://github.com/${PROJECT}/raw/master/changelog${LANG}.md";
 
        private Version current;
        private List<Version> newer;
@@ -92,23 +93,51 @@ public class VersionCheck {
         * time has elapsed.
         */
        public void ok() {
-               Instance.setVersionChecked();
+               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"
+        * 
         * @return a {@link VersionCheck}
         */
-       public static VersionCheck check() {
+       public static VersionCheck check(String githubProject) {
                Version current = Version.getCurrentVersion();
                List<Version> newer = new ArrayList<Version>();
                Map<Version, List<String>> changes = new HashMap<Version, List<String>>();
 
-               if (Instance.isVersionCheckNeeded()) {
+               if (Instance.getInstance().isVersionCheckNeeded()) {
                        try {
-                               InputStream in = Instance.getCache().openNoCache(new URL(url));
+                               // 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}", "");
+
+                               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) {
+                                       }
+                               }
+
+                               if (in == null) {
+                                       throw new IOException("No changelog found");
+                               }
+
                                BufferedReader reader = new BufferedReader(
                                                new InputStreamReader(in, "UTF-8"));
                                try {
@@ -140,8 +169,8 @@ public class VersionCheck {
                                        reader.close();
                                }
                        } catch (IOException e) {
-                               Instance.getTraceHandler().error(new IOException(
-                                               "Cannot download latest changelist on github.com", e));
+                               Instance.getInstance().getTraceHandler()
+                                               .error(new IOException("Cannot download latest changelist on github.com", e));
                        }
                }