|
| 1 | +package com.ss.editor.task; |
| 2 | + |
| 3 | +import static org.apache.http.impl.client.HttpClients.createMinimal; |
| 4 | +import com.ss.editor.JFXApplication; |
| 5 | +import com.ss.editor.Messages; |
| 6 | +import com.ss.editor.config.Config; |
| 7 | +import javafx.application.HostServices; |
| 8 | +import javafx.application.Platform; |
| 9 | +import javafx.scene.control.Alert; |
| 10 | +import javafx.scene.control.DialogPane; |
| 11 | +import javafx.scene.control.Hyperlink; |
| 12 | +import org.apache.commons.io.IOUtils; |
| 13 | +import org.apache.http.Header; |
| 14 | +import org.apache.http.HttpEntity; |
| 15 | +import org.apache.http.StatusLine; |
| 16 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 17 | +import org.apache.http.client.methods.HttpGet; |
| 18 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 19 | +import rlib.logging.Logger; |
| 20 | +import rlib.logging.LoggerManager; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | + |
| 25 | +/** |
| 26 | + * The task to check new versions of the Editor. |
| 27 | + * |
| 28 | + * @author JavaSaBr |
| 29 | + */ |
| 30 | +public class CheckNewVersionTask implements Runnable { |
| 31 | + |
| 32 | + private static final Logger LOGGER = LoggerManager.getLogger(CheckNewVersionTask.class); |
| 33 | + |
| 34 | + private static final String APP_VERSION_URL = "https://api.bitbucket.org/1.0/repositories/javasabr/jme3-spaceshift-editor/raw/master/app.version"; |
| 35 | + private static final String DOWNLOAD_APP_PATH_URL = "https://api.bitbucket.org/1.0/repositories/javasabr/jme3-spaceshift-editor/raw/master/download.app.path"; |
| 36 | + |
| 37 | + @Override |
| 38 | + public void run() { |
| 39 | + |
| 40 | + try (final CloseableHttpClient httpClient = createMinimal()) { |
| 41 | + |
| 42 | + CloseableHttpResponse response = httpClient.execute(new HttpGet(APP_VERSION_URL)); |
| 43 | + StatusLine statusLine = response.getStatusLine(); |
| 44 | + |
| 45 | + if (statusLine.getStatusCode() != 200) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + HttpEntity entity = response.getEntity(); |
| 50 | + InputStream content = entity.getContent(); |
| 51 | + Header encoding = entity.getContentEncoding(); |
| 52 | + String enc = encoding == null ? "UTF-8" : encoding.getValue(); |
| 53 | + |
| 54 | + final String targetVersion = IOUtils.toString(content, enc).trim(); |
| 55 | + |
| 56 | + if (Config.VERSION.equals(targetVersion)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + response = httpClient.execute(new HttpGet(DOWNLOAD_APP_PATH_URL)); |
| 61 | + statusLine = response.getStatusLine(); |
| 62 | + |
| 63 | + if (statusLine.getStatusCode() != 200) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + entity = response.getEntity(); |
| 68 | + content = entity.getContent(); |
| 69 | + encoding = entity.getContentEncoding(); |
| 70 | + enc = encoding == null ? "UTF-8" : encoding.getValue(); |
| 71 | + |
| 72 | + final String targetLink = IOUtils.toString(content, enc).trim(); |
| 73 | + |
| 74 | + Platform.runLater(() -> { |
| 75 | + |
| 76 | + final JFXApplication jfxApplication = JFXApplication.getInstance(); |
| 77 | + final HostServices hostServices = jfxApplication.getHostServices(); |
| 78 | + |
| 79 | + final Hyperlink hyperlink = new Hyperlink(Messages.CHECK_NEW_VERSION_DIALOG_HYPERLINK + targetLink); |
| 80 | + hyperlink.setOnAction(event -> hostServices.showDocument(targetLink)); |
| 81 | + |
| 82 | + final Alert alert = new Alert(Alert.AlertType.INFORMATION); |
| 83 | + alert.setTitle(Messages.CHECK_NEW_VERSION_DIALOG_TITLE); |
| 84 | + alert.setHeaderText(Messages.CHECK_NEW_VERSION_DIALOG_HEADER_TEXT + targetVersion); |
| 85 | + |
| 86 | + final DialogPane dialogPane = alert.getDialogPane(); |
| 87 | + dialogPane.setContent(hyperlink); |
| 88 | + |
| 89 | + alert.show(); |
| 90 | + }); |
| 91 | + |
| 92 | + } catch (final IOException e) { |
| 93 | + LOGGER.warning(e); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments