version checking Fanfix -> Utils
authorNiki Roo <niki@nikiroo.be>
Thu, 7 May 2020 09:37:21 +0000 (11:37 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 7 May 2020 09:37:21 +0000 (11:37 +0200)
Main.java
VersionCheck.java [deleted file]

index 9d9592eefd3356e9d7c1e61b9e0b5142ab7b2376..c0dd9e0db608b4b8c4394821d63c783926150251 100644 (file)
--- a/Main.java
+++ b/Main.java
@@ -28,6 +28,7 @@ import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.fanfix.supported.SupportType;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.Version;
+import be.nikiroo.utils.VersionCheck;
 import be.nikiroo.utils.serial.server.ServerObject;
 
 /**
@@ -397,8 +398,10 @@ public class Main {
                if (exitCode == 0) {
                        switch (action) {
                        case IMPORT:
-                               if (updates != null)
-                                       updates.ok(); // we consider it read
+                               if (updates != null) {
+                                       // we consider it read
+                                       Instance.getInstance().setVersionChecked(); 
+                               }
                                
                                try {
                                        exitCode = imprt(BasicReader.getUrl(urlString), pg);
@@ -409,8 +412,10 @@ public class Main {
                                
                                break;
                        case EXPORT:
-                               if (updates != null)
-                                       updates.ok(); // we consider it read
+                               if (updates != null) {
+                                       // we consider it read
+                                       Instance.getInstance().setVersionChecked(); 
+                               }
                                
                                OutputType exportType = OutputType.valueOfNullOkUC(sourceString, null);
                                if (exportType == null) {
@@ -423,8 +428,10 @@ public class Main {
                                
                                break;
                        case CONVERT:
-                               if (updates != null)
-                                       updates.ok(); // we consider it read
+                               if (updates != null) {
+                                       // we consider it read
+                                       Instance.getInstance().setVersionChecked(); 
+                               }
                                
                                OutputType convertType = OutputType.valueOfAllOkUC(sourceString, null);
                                if (convertType == null) {
@@ -599,8 +606,10 @@ public class Main {
                                exitCode = 0;
                                break;
                        case VERSION:
-                               if (updates != null)
-                                       updates.ok(); // we consider it read
+                               if (updates != null) {
+                                       // we consider it read
+                                       Instance.getInstance().setVersionChecked(); 
+                               }
                                
                                System.out
                                                .println(String.format("Fanfix version %s"
@@ -716,12 +725,17 @@ public class Main {
         * @return the newer version information or NULL if nothing new
         */
        protected VersionCheck checkUpdates(String githubProject) {
-               VersionCheck updates = VersionCheck.check(githubProject);
-               if (updates.isNewVersionAvailable()) {
-                       notifyUpdates(updates);
-                       return updates;
+               try {
+                       VersionCheck updates = VersionCheck.check(githubProject,
+                                       Instance.getInstance().getTrans().getLocale());
+                       if (updates.isNewVersionAvailable()) {
+                               notifyUpdates(updates);
+                               return updates;
+                       }
+               } catch (IOException e) {
+                       // Maybe no internet. Do not report any update.
                }
-               
+
                return null;
        }
 
@@ -730,9 +744,9 @@ public class Main {
         * <p>
         * Will only be called when a version is available.
         * <p>
-        * Note that you can call {@link VersionCheck#ok()} on it if the user has
-        * read the information (by default, it is marked read only on certain other
-        * actions).
+        * Note that you can call {@link Instance#setVersionChecked()} on it if the
+        * user has read the information (by default, it is marked read only on
+        * certain other actions).
         * 
         * @param updates
         *            the new version information
diff --git a/VersionCheck.java b/VersionCheck.java
deleted file mode 100644 (file)
index 8b2a343..0000000
+++ /dev/null
@@ -1,179 +0,0 @@
-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.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.
- * 
- * @author niki
- */
-public class VersionCheck {
-       private static final String base = "https://github.com/${PROJECT}/raw/master/changelog${LANG}.md";
-
-       private Version current;
-       private List<Version> newer;
-       private Map<Version, List<String>> 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<Version> newer,
-                       Map<Version, List<String>> 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<Version> getNewer() {
-               return newer;
-       }
-
-       /**
-        * The list of changes for each available {@link Version} newer than the
-        * current one.
-        * 
-        * @return the list of changes
-        */
-       public Map<Version, List<String>> 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.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(String githubProject) {
-               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}", "");
-
-                               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 {
-                                       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));
-                       }
-               }
-
-               return new VersionCheck(current, newer, changes);
-       }
-}