diff options
author | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-09-01 10:52:33 +0200 |
---|---|---|
committer | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-09-01 10:52:33 +0200 |
commit | 05b9f7da53be1b8baf9df913cbd4efc1e2dbb16a (patch) | |
tree | 0cef263662482601bb5d99a968a7a40918a18091 /pdf-over-gui/src/main/java/at/asit | |
parent | 6a01bc630d40e9623a58d6b9da4218389d7a6b1f (diff) | |
download | pdf-over-05b9f7da53be1b8baf9df913cbd4efc1e2dbb16a.tar.gz pdf-over-05b9f7da53be1b8baf9df913cbd4efc1e2dbb16a.tar.bz2 pdf-over-05b9f7da53be1b8baf9df913cbd4efc1e2dbb16a.zip |
refactor update check into its own class, cf. #119
Diffstat (limited to 'pdf-over-gui/src/main/java/at/asit')
-rw-r--r-- | pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/UpdateCheckManager.java | 85 | ||||
-rw-r--r-- | pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java | 51 |
2 files changed, 89 insertions, 47 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 new file mode 100644 index 00000000..d6e650ba --- /dev/null +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/UpdateCheckManager.java @@ -0,0 +1,85 @@ +package at.asit.pdfover.gui.utils; + +import java.awt.Desktop; +import java.net.URI; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.asit.pdfover.commons.Constants; +import at.asit.pdfover.commons.Messages; +import at.asit.pdfover.gui.bku.BKUHelper; +import at.asit.pdfover.gui.controls.Dialog; +import at.asit.pdfover.gui.controls.Dialog.BUTTONS; +import at.asit.pdfover.gui.controls.Dialog.ICON; + +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.GetMethod; +import org.eclipse.swt.SWT; +import org.eclipse.swt.program.Program; +import org.eclipse.swt.widgets.Shell; + +public final class UpdateCheckManager { + static final Logger log = LoggerFactory.getLogger(UpdateCheckManager.class); + private static Thread updateCheckThread = null; + private static boolean needsCheck = false; + + public static void checkNow(Shell shell) { + if (Constants.APP_VERSION == null) + return; + + synchronized (UpdateCheckManager.class) { + if (updateCheckThread != null) + return; + + needsCheck = true; + updateCheckThread = new Thread(() -> { + while (true) { + synchronized (UpdateCheckManager.class) { + if (!needsCheck) { + 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); + } + } + }); + updateCheckThread.start(); + } + } +} diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java index b6ea9c0b..df2a82fa 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java @@ -16,7 +16,6 @@ package at.asit.pdfover.gui.workflow.states; //Imports -import java.awt.Desktop; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; @@ -27,24 +26,19 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.URI; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.methods.GetMethod; + import org.eclipse.swt.SWT; -import org.eclipse.swt.program.Program; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.asit.pdfover.commons.Constants; -import at.asit.pdfover.gui.bku.BKUHelper; import at.asit.pdfover.gui.cliarguments.*; -import at.asit.pdfover.gui.controls.Dialog; import at.asit.pdfover.gui.controls.Dialog.BUTTONS; -import at.asit.pdfover.gui.controls.Dialog.ICON; import at.asit.pdfover.gui.controls.ErrorDialog; import at.asit.pdfover.gui.exceptions.InitializationException; import at.asit.pdfover.commons.Messages; +import at.asit.pdfover.gui.utils.UpdateCheckManager; import at.asit.pdfover.gui.utils.VersionComparator; import at.asit.pdfover.gui.utils.Zipper; import at.asit.pdfover.gui.workflow.GUIProvider; @@ -301,45 +295,8 @@ public class PrepareConfigurationState extends State { } // Check for updates - if (config.getUpdateCheck() && Constants.APP_VERSION != null) { - new Thread(() -> { - 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 - gui.getMainShell().getDisplay().asyncExec(() -> { - Dialog info = new Dialog(gui.getMainShell(), - 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); - } - }).start(); - } + if (config.getUpdateCheck()) + UpdateCheckManager.checkNow(gui.getMainShell()); // Create PDF Signer Status status = stateMachine.status; |