Search in sources :

Example 1 with ArgonXML

use of com.cburch.logisim.util.ArgonXML in project logisim-evolution by reds-heig.

the class Startup method autoUpdate.

/**
 * Auto-update Logisim-evolution if a new version is available
 *
 * Original idea taken from Jupar:
 * http://masterex.github.io/archive/2011/12/25/jupar.html by Periklis
 * Master_ex Ntanasis <pntanasis@gmail.com>
 *
 * @return true if the code has been updated, and therefore the execution
 *         has to be stopped, false otherwise
 */
public boolean autoUpdate() {
    if (!Main.UPDATE || !networkConnectionAvailable()) {
        // available
        return (false);
    }
    // Get the remote XML file containing the current version
    URL xmlURL;
    try {
        xmlURL = new URL(Main.UPDATE_URL);
    } catch (MalformedURLException e) {
        logger.error("The URL of the XML file for the auto-updater is malformed.\nPlease report this error to the software maintainer\n-- AUTO-UPDATE ABORTED --");
        return (false);
    }
    URLConnection conn;
    try {
        conn = xmlURL.openConnection();
    } catch (IOException e) {
        logger.error("Although an Internet connection should be available, the system couldn't connect to the URL requested by the auto-updater\nIf the error persist, please contact the software maintainer\n-- AUTO-UPDATE ABORTED --");
        return (false);
    }
    InputStream in;
    try {
        in = conn.getInputStream();
    } catch (IOException e) {
        logger.error("Although an Internet connection should be available, the system couldn't retrieve the data requested by the auto-updater.\nIf the error persist, please contact the software maintainer\n-- AUTO-UPDATE ABORTED --");
        return (false);
    }
    ArgonXML logisimData = new ArgonXML(in, "logisim-evolution");
    // Get the appropriate remote version number
    LogisimVersion remoteVersion = LogisimVersion.parse(Main.VERSION.hasTracker() ? logisimData.child("tracked_version").content() : logisimData.child("untracked_version").content());
    // If the remote version is newer, perform the update
    if (remoteVersion.compareTo(Main.VERSION) > 0) {
        int answer = JOptionPane.showConfirmDialog(null, "A new Logisim-evolution version (" + remoteVersion + ") is available!\nWould you like to update?", "Update", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
        if (answer == 1) {
            // annoyed by the message that he finally updates!
            return (false);
        }
        // Obtain the base directory of the jar archive
        CodeSource codeSource = Startup.class.getProtectionDomain().getCodeSource();
        File jarFile = null;
        try {
            jarFile = new File(codeSource.getLocation().toURI().getPath());
        } catch (URISyntaxException e) {
            logger.error("Error in the syntax of the URI for the path of the executed Logisim-evolution JAR file!");
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "An error occurred while updating to the new Logisim-evolution version.\nPlease check the console for log information.", "Update failed", JOptionPane.ERROR_MESSAGE);
            return (false);
        }
        // Get the appropriate remote filename to download
        String remoteJar = Main.VERSION.hasTracker() ? logisimData.child("tracked_file").content() : logisimData.child("untracked_file").content();
        boolean updateOk = downloadInstallUpdatedVersion(remoteJar, jarFile.getAbsolutePath());
        if (updateOk) {
            JOptionPane.showMessageDialog(null, "The new Logisim-evolution version (" + remoteVersion + ") has been correctly installed.\nPlease restart Logisim-evolution for the changes to take effect.", "Update succeeded", JOptionPane.INFORMATION_MESSAGE);
            return (true);
        } else {
            JOptionPane.showMessageDialog(null, "An error occurred while updating to the new Logisim-evolution version.\nPlease check the console for log information.", "Update failed", JOptionPane.ERROR_MESSAGE);
            return (false);
        }
    }
    return (false);
}
Also used : MalformedURLException(java.net.MalformedURLException) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ArgonXML(com.cburch.logisim.util.ArgonXML) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) CodeSource(java.security.CodeSource) LogisimVersion(com.cburch.logisim.LogisimVersion) URL(java.net.URL) URLConnection(java.net.URLConnection) Print(com.cburch.logisim.gui.main.Print) File(java.io.File)

Aggregations

LogisimVersion (com.cburch.logisim.LogisimVersion)1 Print (com.cburch.logisim.gui.main.Print)1 ArgonXML (com.cburch.logisim.util.ArgonXML)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 CodeSource (java.security.CodeSource)1