diff --git a/build.gradle b/build.gradle index be2afcc..9347c14 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,9 @@ dependencies { compile 'com.github.spullara.mustache.java:compiler:0.8.18' compile 'org.slf4j:slf4j-api:1.7.21' compile 'ch.qos.logback:logback-classic:1.1.7' + compile 'com.google.code.gson:gson:2.5' + + testCompile 'junit:junit:4.12' } run { diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineRunner.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineRunner.java index 75db6f6..352853f 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineRunner.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineRunner.java @@ -17,6 +17,8 @@ package de.fabianonline.telegram_backup; import de.fabianonline.telegram_backup.CommandLineController; +import de.fabianonline.telegram_backup.Utils; +import de.fabianonline.telegram_backup.Version; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; @@ -55,6 +57,14 @@ public class CommandLineRunner { ((Logger)LoggerFactory.getLogger("com.github.badoualy")).setLevel(Level.TRACE); } + Version v = Utils.getNewestVersion(); + if (v!=null && v.isNewer) { + System.out.println("A newer version is vailable!"); + System.out.println("You are using: " + Config.APP_APPVER); + System.out.println("Available: " + v.version); + System.out.println("Get it here: " + v.url); + System.out.println(); + } if (true || CommandLineOptions.cmd_console) { // Always use the console for now. diff --git a/src/main/java/de/fabianonline/telegram_backup/Utils.java b/src/main/java/de/fabianonline/telegram_backup/Utils.java index 4de408d..04ac672 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Utils.java +++ b/src/main/java/de/fabianonline/telegram_backup/Utils.java @@ -19,8 +19,20 @@ import com.github.badoualy.telegram.tl.exception.RpcErrorException; import java.io.File; import java.util.List; import java.util.Vector; +import com.google.gson.*; +import java.net.URL; +import org.apache.commons.io.IOUtils; +import de.fabianonline.telegram_backup.Version; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class Utils { + public static final int VERSIONS_EQUAL = 0; + public static final int VERSION_1_NEWER = 1; + public static final int VERSION_2_NEWER = 2; + + private static final Logger logger = (Logger)LoggerFactory.getLogger(Utils.class); + static Vector getAccounts() { Vector accounts = new Vector(); File folder = new File(Config.FILE_BASE); @@ -53,4 +65,83 @@ public class Utils { try { Thread.sleep(wait * 60 * 1000); } catch(InterruptedException e2) {} System.out.println(""); } + + static Version getNewestVersion() { + try { + String data_url = "https://api.github.com/repos/fabianonline/telegram_backup/releases"; + logger.debug("Requesting current release info from {}", data_url); + String json = IOUtils.toString(new URL(data_url)); + JsonParser parser = new JsonParser(); + JsonElement root_elm = parser.parse(json); + if (root_elm.isJsonArray()) { + JsonArray root = root_elm.getAsJsonArray(); + JsonObject newest_version = null; + for (JsonElement e : root) if (e.isJsonObject()) { + JsonObject version = e.getAsJsonObject(); + if (version.getAsJsonPrimitive("prerelease").getAsBoolean() == false) { + newest_version = version; + break; + } + } + if (newest_version == null) return null; + String new_v = newest_version.getAsJsonPrimitive("tag_name").getAsString(); + logger.debug("Found current release version {}", new_v); + String cur_v = Config.APP_APPVER; + + int result = compareVersions(cur_v, new_v); + + return new Version(new_v, newest_version.getAsJsonPrimitive("html_url").getAsString(), result == VERSION_2_NEWER); + } + return null; + } catch(Exception e) { + return null; + } + } + + public static int compareVersions(String v1, String v2) { + logger.debug("Comparing versions {} and {}.", v1, v2); + if (v1.equals(v2)) return VERSIONS_EQUAL; + + String[] v1_p = v1.split("-"); + String[] v2_p = v2.split("-"); + + logger.trace("Parts to compare without suffixes: {} and {}.", v1_p[0], v2_p[0]); + + String[] v1_p2 = v1_p[0].split("\\."); + String[] v2_p2 = v2_p[0].split("\\."); + + logger.trace("Length of the parts without suffixes: {} and {}.", v1_p2.length, v2_p2.length); + + int i; + for (i=0; i i_2) { + logger.debug("v1 is newer"); + return VERSION_1_NEWER; + } else if (i_2 > i_1) { + logger.debug("v2 is newer"); + return VERSION_2_NEWER; + } + } + logger.trace("At least one of the versions has run out of parts."); + if (v1_p2.length > v2_p2.length) { + logger.debug("v1 is longer, so it is newer"); + return VERSION_1_NEWER; + } else if (v2_p2.length > v1_p2.length) { + logger.debug("v2 is longer, so it is newer"); + return VERSION_2_NEWER; + } + + if (v1_p.length>1 && v2_p.length==1) { + logger.debug("v1 has a suffix, v2 not - so v2 is newer."); + return VERSION_2_NEWER; + } else if (v1_p.length==1 && v2_p.length>1) { + logger.debug("v1 has no suffix, but v2 has - so v1 is newer."); + return VERSION_1_NEWER; + } + logger.debug("We couldn't find a real difference, so we're assuming the versions are equal-ish."); + return VERSIONS_EQUAL; + } } diff --git a/src/main/java/de/fabianonline/telegram_backup/Version.java b/src/main/java/de/fabianonline/telegram_backup/Version.java new file mode 100644 index 0000000..e325dfa --- /dev/null +++ b/src/main/java/de/fabianonline/telegram_backup/Version.java @@ -0,0 +1,29 @@ +/* Telegram_Backup + * Copyright (C) 2016 Fabian Schlenz + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +package de.fabianonline.telegram_backup; + +public class Version { + public final String version; + public final String url; + public final boolean isNewer; + + public Version(String v, String u, boolean n) { + this.version = v; + this.url = u; + this.isNewer = n; + } +} diff --git a/src/test/java/de/fabianonline/telegram_backup/utils/CompareVersionsTest.java b/src/test/java/de/fabianonline/telegram_backup/utils/CompareVersionsTest.java new file mode 100644 index 0000000..edb4fd8 --- /dev/null +++ b/src/test/java/de/fabianonline/telegram_backup/utils/CompareVersionsTest.java @@ -0,0 +1,15 @@ +import static org.junit.Assert.*; +import org.junit.Test; +import de.fabianonline.telegram_backup.Utils; + +public class CompareVersionsTest { + @Test + public void tests() { + assertEquals(Utils.compareVersions("1.0.3", "1.0.4"), Utils.VERSION_2_NEWER); + assertEquals(Utils.compareVersions("1.0.4", "1.0.3"), Utils.VERSION_1_NEWER); + assertEquals(Utils.compareVersions("1.0.4", "1.0.4.1"), Utils.VERSION_2_NEWER); + assertEquals(Utils.compareVersions("1.0.4", "1.0.4-pre.1"), Utils.VERSION_1_NEWER); + assertEquals(Utils.compareVersions("1.0.4", "1.0.4"), Utils.VERSIONS_EQUAL); + assertEquals(Utils.compareVersions("1.0.4-pre.2", "1.0.4-pre.1"), Utils.VERSIONS_EQUAL); + } +}