diff options
author | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-09-01 12:50:50 +0200 |
---|---|---|
committer | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-09-01 12:50:50 +0200 |
commit | 8e553385ab268c216a99b03d15783c563ce748dc (patch) | |
tree | 2a0691bc868acd806b338da1efba22c55a8e7f5d /pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils | |
parent | 05b9f7da53be1b8baf9df913cbd4efc1e2dbb16a (diff) | |
download | pdf-over-8e553385ab268c216a99b03d15783c563ce748dc.tar.gz pdf-over-8e553385ab268c216a99b03d15783c563ce748dc.tar.bz2 pdf-over-8e553385ab268c216a99b03d15783c563ce748dc.zip |
add update check status to about config panel, cf. #119
Diffstat (limited to 'pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils')
-rw-r--r-- | pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/UpdateCheckManager.java | 113 |
1 files changed, 76 insertions, 37 deletions
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/UpdateCheckManager.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/UpdateCheckManager.java index d6e650ba..92104176 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/UpdateCheckManager.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/UpdateCheckManager.java @@ -2,6 +2,8 @@ package at.asit.pdfover.gui.utils; import java.awt.Desktop; import java.net.URI; +import java.util.ArrayList; +import java.util.function.Consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,6 +26,72 @@ public final class UpdateCheckManager { private static Thread updateCheckThread = null; private static boolean needsCheck = false; + public enum Status { NOT_CHECKED, CHECKING, OUTDATED, UP_TO_DATE, FAILED }; + private static Status currentStatus = Status.NOT_CHECKED; + public static Status getCurrentStatus() { + synchronized (UpdateCheckManager.class) { + return currentStatus; + } + } + + private static ArrayList<Consumer<Status>> statusCallbacks = new ArrayList<>(); + public static void registerStatusCallback(Consumer<Status> f) { + synchronized (UpdateCheckManager.class) { + statusCallbacks.add(f); + f.accept(currentStatus); + } + } + + private static void setStatus(Status status) { + synchronized(UpdateCheckManager.class) { + currentStatus = status; + for (Consumer<Status> f : statusCallbacks) + f.accept(status); + } + } + + private static String latestVersionNotified = null; + private static Status runCheck(Shell shell) { + HttpClient client = (HttpClient) BKUHelper.getHttpClient(); + GetMethod method = new GetMethod(Constants.CURRENT_RELEASE_URL); + try { + client.executeMethod(method); + final String version = method.getResponseBodyAsString().trim(); + if (!VersionComparator.before(Constants.APP_VERSION, version)) + return Status.UP_TO_DATE; + + if ((latestVersionNotified == null) || VersionComparator.before(latestVersionNotified, version)) { + latestVersionNotified = version; + // invoke GUI message in main thread + shell.getDisplay().asyncExec(() -> { + Dialog info = new Dialog(shell, + Messages.getString("version_check.UpdateTitle"), + String.format(Messages.getString("version_check.UpdateText"), version), + BUTTONS.OK_CANCEL, ICON.INFORMATION); + + if (info.open() == SWT.OK) + { + if (Desktop.isDesktopSupported()) { + try { + Desktop.getDesktop().browse(new URI(Constants.UPDATE_URL)); + } catch (Exception e) { + log.error("Error opening update location ", e); + } + } else { + log.info("SWT Desktop is not supported on this platform"); + Program.launch(Constants.UPDATE_URL); + } + } + }); + } + + return Status.OUTDATED; + } catch (Exception e) { + log.error("Error downloading update information: ", e); + return Status.FAILED; + } + } + public static void checkNow(Shell shell) { if (Constants.APP_VERSION == null) return; @@ -34,48 +102,19 @@ public final class UpdateCheckManager { needsCheck = true; updateCheckThread = new Thread(() -> { + synchronized(UpdateCheckManager.class) { + setStatus(Status.CHECKING); + } while (true) { + Status status = runCheck(shell); synchronized (UpdateCheckManager.class) { - if (!needsCheck) { + if (!UpdateCheckManager.needsCheck) { + setStatus(status); UpdateCheckManager.updateCheckThread = null; return; } - needsCheck = false; - } - HttpClient client = (HttpClient) BKUHelper.getHttpClient(); - GetMethod method = new GetMethod(Constants.CURRENT_RELEASE_URL); - try { - client.executeMethod(method); - final String version = method.getResponseBodyAsString().trim(); - if (!VersionComparator.before(Constants.APP_VERSION, version)) - return; - - // wait 500ms before invoke the GUI message, because GUI had to be started from - // main thread - try { Thread.sleep(500); } catch (InterruptedException e1) { } - // invoke GUI message in main thread - shell.getDisplay().asyncExec(() -> { - Dialog info = new Dialog(shell, - Messages.getString("version_check.UpdateTitle"), - String.format(Messages.getString("version_check.UpdateText"), version), - BUTTONS.OK_CANCEL, ICON.INFORMATION); - - if (info.open() == SWT.OK) - { - if (Desktop.isDesktopSupported()) { - try { - Desktop.getDesktop().browse(new URI(Constants.UPDATE_URL)); - } catch (Exception e) { - log.error("Error opening update location ", e); - } - } else { - log.info("SWT Desktop is not supported on this platform"); - Program.launch(Constants.UPDATE_URL); - } - } - }); - } catch (Exception e) { - log.error("Error downloading update information: ", e); + UpdateCheckManager.needsCheck = false; + setStatus(Status.CHECKING); } } }); |