diff --git a/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy b/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy index e97e2df0..7b4b5f59 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy @@ -47,7 +47,7 @@ public class DownloadManager { destinations.add(it.sender.destination) } - def downloader = new Downloader(this, me, e.target, size, + def downloader = new Downloader(eventBus, this, me, e.target, size, infohash, pieceSize, connector, destinations, incompletes) executor.execute({downloader.download()} as Runnable) 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 dca44fa1..f034751d 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -10,7 +10,10 @@ import java.util.concurrent.Executors import java.util.logging.Level import com.muwire.core.Constants +import com.muwire.core.DownloadedFile +import com.muwire.core.EventBus import com.muwire.core.connection.I2PConnector +import com.muwire.core.files.FileDownloadedEvent import groovy.util.logging.Log import net.i2p.data.Destination @@ -27,6 +30,7 @@ public class Downloader { rv }) + private final EventBus eventBus private final DownloadManager downloadManager private final Persona me private final File file @@ -42,10 +46,13 @@ public class Downloader { private volatile boolean cancelled + private volatile boolean eventFired - public Downloader(DownloadManager downloadManager, Persona me, File file, long length, InfoHash infoHash, + public Downloader(EventBus eventBus, DownloadManager downloadManager, + Persona me, File file, long length, InfoHash infoHash, int pieceSizePow2, I2PConnector connector, Set destinations, File incompletes) { + this.eventBus = eventBus this.me = me this.downloadManager = downloadManager this.file = file @@ -195,6 +202,10 @@ public class Downloader { log.log(Level.WARNING,"Exception while downloading",bad) } finally { currentState = WorkerState.FINISHED + if (downloaded.isComplete() && !eventFired) { + eventFired = true + eventBus.publish(new FileDownloadedEvent(downloadedFile : new DownloadedFile(file, infoHash, Collections.emptySet()))) + } endpoint?.close() } } diff --git a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy index be952689..4747e862 100644 --- a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy @@ -15,6 +15,7 @@ import com.muwire.core.connection.ConnectionEvent import com.muwire.core.connection.DisconnectionEvent import com.muwire.core.download.DownloadStartedEvent import com.muwire.core.download.Downloader +import com.muwire.core.files.FileDownloadedEvent import com.muwire.core.files.FileHashedEvent import com.muwire.core.files.FileLoadedEvent import com.muwire.core.files.FileSharedEvent @@ -100,6 +101,7 @@ class MainFrameModel { core.eventBus.register(TrustEvent.class, this) core.eventBus.register(QueryEvent.class, this) core.eventBus.register(UpdateAvailableEvent.class, this) + core.eventBus.register(FileDownloadedEvent.class, this) timer.schedule({ int retryInterval = application.context.get("muwire-settings").downloadRetryInterval @@ -261,4 +263,13 @@ class MainFrameModel { JOptionPane.showMessageDialog(null, "A new version of MuWire is available from $e.signer. Please update to $e.version") } } + + void onFileDownloadedEvent(FileDownloadedEvent e) { + infoHashes.add(e.downloadedFile.infoHash) + runInsideUIAsync { + shared << e.downloadedFile + JTable table = builder.getVariable("shared-files-table") + table.model.fireTableDataChanged() + } + } } \ No newline at end of file