From 82377aa9df2618de215ebe2c4326f08cf2850328 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 1 Jun 2019 17:44:52 +0100 Subject: [PATCH] hook up cancel button --- .../com/muwire/core/download/Downloader.groovy | 12 +++++++++++- .../com/muwire/gui/MainFrameController.groovy | 11 +++++++++++ .../views/com/muwire/gui/MainFrameView.groovy | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy index b777eddc..c527d614 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -27,6 +27,8 @@ public class Downloader { private Endpoint endpoint private volatile DownloadSession currentSession private volatile DownloadState currentState + private volatile boolean cancelled + private volatile Thread downloadThread public Downloader(File file, long length, InfoHash infoHash, int pieceSizePow2, I2PConnector connector, Destination destination) { this.file = file @@ -48,6 +50,7 @@ public class Downloader { } void download() { + downloadThread = Thread.currentThread() Endpoint endpoint = null try { endpoint = connector.connect(destination) @@ -59,7 +62,9 @@ public class Downloader { currentState = DownloadState.FINISHED } catch (Exception bad) { log.log(Level.WARNING,"Exception while downloading",bad) - if (currentState != DownloadState.FINISHED) + if (cancelled) + currentState = DownloadState.CANCELLED + else if (currentState != DownloadState.FINISHED) currentState = DownloadState.FAILED } finally { endpoint?.close() @@ -79,4 +84,9 @@ public class Downloader { public DownloadState getCurrentState() { currentState } + + public void cancel() { + cancelled = true + downloadThread?.interrupt() + } } diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index 779d8f04..38ce78da 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -54,6 +54,11 @@ class MainFrameController { group.model.results[row] } + private def selectedDownload() { + def selected = builder.getVariable("downloads-table").getSelectedRow() + model.downloads[selected].downloader + } + @ControllerAction void download() { def result = selectedResult() @@ -79,6 +84,12 @@ class MainFrameController { core.eventBus.publish( new TrustEvent(destination : result.sender.destination, level : TrustLevel.DISTRUSTED)) } + @ControllerAction + void cancel() { + def downloader = selectedDownload() + downloader.cancel() + } + void mvcGroupInit(Map args) { application.addPropertyChangeListener("core", {e-> core = e.getNewValue() diff --git a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy index 5f9e9510..0464c9a7 100644 --- a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy @@ -98,7 +98,7 @@ class MainFrameView { } } panel (constraints : BorderLayout.SOUTH) { - button("Cancel", enabled : bind {model.cancelButtonEnabled } ) + button(text: "Cancel", enabled : bind {model.cancelButtonEnabled }, cancelAction ) button("Retry", enabled : bind {model.retryButtonEnabled}) } }