hook up cancel button

pull/4/head
Zlatin Balevsky 2019-06-01 17:44:52 +01:00
parent bd2368e23a
commit 82377aa9df
3 changed files with 23 additions and 2 deletions

View File

@ -27,6 +27,8 @@ public class Downloader {
private Endpoint endpoint private Endpoint endpoint
private volatile DownloadSession currentSession private volatile DownloadSession currentSession
private volatile DownloadState currentState 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) { public Downloader(File file, long length, InfoHash infoHash, int pieceSizePow2, I2PConnector connector, Destination destination) {
this.file = file this.file = file
@ -48,6 +50,7 @@ public class Downloader {
} }
void download() { void download() {
downloadThread = Thread.currentThread()
Endpoint endpoint = null Endpoint endpoint = null
try { try {
endpoint = connector.connect(destination) endpoint = connector.connect(destination)
@ -59,7 +62,9 @@ public class Downloader {
currentState = DownloadState.FINISHED currentState = DownloadState.FINISHED
} catch (Exception bad) { } catch (Exception bad) {
log.log(Level.WARNING,"Exception while downloading",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 currentState = DownloadState.FAILED
} finally { } finally {
endpoint?.close() endpoint?.close()
@ -79,4 +84,9 @@ public class Downloader {
public DownloadState getCurrentState() { public DownloadState getCurrentState() {
currentState currentState
} }
public void cancel() {
cancelled = true
downloadThread?.interrupt()
}
} }

View File

@ -54,6 +54,11 @@ class MainFrameController {
group.model.results[row] group.model.results[row]
} }
private def selectedDownload() {
def selected = builder.getVariable("downloads-table").getSelectedRow()
model.downloads[selected].downloader
}
@ControllerAction @ControllerAction
void download() { void download() {
def result = selectedResult() def result = selectedResult()
@ -79,6 +84,12 @@ class MainFrameController {
core.eventBus.publish( new TrustEvent(destination : result.sender.destination, level : TrustLevel.DISTRUSTED)) core.eventBus.publish( new TrustEvent(destination : result.sender.destination, level : TrustLevel.DISTRUSTED))
} }
@ControllerAction
void cancel() {
def downloader = selectedDownload()
downloader.cancel()
}
void mvcGroupInit(Map<String, String> args) { void mvcGroupInit(Map<String, String> args) {
application.addPropertyChangeListener("core", {e-> application.addPropertyChangeListener("core", {e->
core = e.getNewValue() core = e.getNewValue()

View File

@ -98,7 +98,7 @@ class MainFrameView {
} }
} }
panel (constraints : BorderLayout.SOUTH) { panel (constraints : BorderLayout.SOUTH) {
button("Cancel", enabled : bind {model.cancelButtonEnabled } ) button(text: "Cancel", enabled : bind {model.cancelButtonEnabled }, cancelAction )
button("Retry", enabled : bind {model.retryButtonEnabled}) button("Retry", enabled : bind {model.retryButtonEnabled})
} }
} }