From 8c9a5585eb0c998d15cdee18e58e61e69737ef90 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Fri, 31 May 2019 12:27:39 +0100 Subject: [PATCH] show the shared files, controlled by property --- .../com/muwire/core/MuWireSettings.groovy | 4 +++ gui/griffon-app/lifecycle/Ready.groovy | 7 ++++ .../com/muwire/gui/MainFrameModel.groovy | 33 +++++++++++++++++++ .../views/com/muwire/gui/MainFrameView.groovy | 17 +++++++++- 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy b/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy index e26635ab..832688bf 100644 --- a/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy +++ b/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy @@ -8,6 +8,7 @@ class MuWireSettings { boolean allowUntrusted String nickname File downloadLocation + String sharedFiles CrawlerResponse crawlerResponse MuWireSettings() { @@ -21,6 +22,7 @@ class MuWireSettings { nickname = props.getProperty("nickname","MuWireUser") downloadLocation = new File((String)props.getProperty("downloadLocation", System.getProperty("user.home"))) + sharedFiles = props.getProperty("sharedFiles") } void write(OutputStream out) throws IOException { @@ -30,6 +32,8 @@ class MuWireSettings { props.setProperty("crawlerResponse", crawlerResponse.toString()) props.setProperty("nickname", nickname) props.setProperty("downloadLocation", downloadLocation.getAbsolutePath()) + if (sharedFiles != null) + props.setProperty("sharedFiles", sharedFiles) props.store(out, "") } diff --git a/gui/griffon-app/lifecycle/Ready.groovy b/gui/griffon-app/lifecycle/Ready.groovy index b70c823c..59016714 100644 --- a/gui/griffon-app/lifecycle/Ready.groovy +++ b/gui/griffon-app/lifecycle/Ready.groovy @@ -5,6 +5,7 @@ import org.codehaus.griffon.runtime.core.AbstractLifecycleHandler import com.muwire.core.Core import com.muwire.core.MuWireSettings +import com.muwire.core.files.FileSharedEvent import javax.annotation.Nonnull import javax.inject.Inject @@ -88,6 +89,12 @@ class Ready extends AbstractLifecycleHandler { application.getPropertyChangeListeners("core").each { it.propertyChange(new PropertyChangeEvent(this, "core", null, core)) } + + if (props.sharedFiles != null) { + props.sharedFiles.split(",").each { + core.eventBus.publish(new FileSharedEvent(file : new File(it))) + } + } } } diff --git a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy index 64dbc981..83865252 100644 --- a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy @@ -5,10 +5,14 @@ import javax.inject.Inject import javax.swing.JTable import com.muwire.core.Core +import com.muwire.core.InfoHash import com.muwire.core.connection.ConnectionAttemptStatus import com.muwire.core.connection.ConnectionEvent import com.muwire.core.connection.DisconnectionEvent import com.muwire.core.download.DownloadStartedEvent +import com.muwire.core.files.FileHashedEvent +import com.muwire.core.files.FileLoadedEvent +import com.muwire.core.files.FileSharedEvent import com.muwire.core.search.UIResultEvent import griffon.core.GriffonApplication @@ -27,8 +31,11 @@ class MainFrameModel { @Observable def results = [] @Observable def downloads = [] + @Observable def shared = [] @Observable int connections @Observable String me + + private final Set infoHashes = new HashSet<>() volatile Core core @@ -41,6 +48,8 @@ class MainFrameModel { core.eventBus.register(DownloadStartedEvent.class, this) core.eventBus.register(ConnectionEvent.class, this) core.eventBus.register(DisconnectionEvent.class, this) + core.eventBus.register(FileHashedEvent.class, this) + core.eventBus.register(FileLoadedEvent.class, this) }) Timer timer = new Timer("download-pumper", true) timer.schedule({ @@ -75,4 +84,28 @@ class MainFrameModel { connections = core.connectionManager.getConnections().size() } } + + void onFileHashedEvent(FileHashedEvent e) { + if (e.error != null) + return // TODO do something + if (infoHashes.contains(e.sharedFile.infoHash)) + return + infoHashes.add(e.sharedFile.infoHash) + runInsideUIAsync { + shared << e.sharedFile + JTable table = builder.getVariable("shared-files-table") + table.model.fireTableDataChanged() + } + } + + void onFileLoadedEvent(FileLoadedEvent e) { + if (infoHashes.contains(e.loadedFile.infoHash)) + return + infoHashes.add(e.loadedFile.infoHash) + runInsideUIAsync { + shared << e.loadedFile + JTable table = builder.getVariable("shared-files-table") + table.model.fireTableDataChanged() + } + } } \ No newline at end of file diff --git a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy index df3557bf..5360a55b 100644 --- a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy @@ -102,7 +102,22 @@ class MainFrameView { } } panel (constraints: "uploads window"){ - label("card 2") + gridLayout(cols : 1, rows : 2) + panel { + borderLayout() + label(text : "Shared files", constraints: BorderLayout.NORTH) + scrollPane ( constraints : BorderLayout.CENTER) { + table(id : "shared-files-table") { + tableModel(list : model.shared) { + closureColumn(header : "Name", type : String, read : {row -> row.file.getAbsolutePath()}) + closureColumn(header : "Size", type : Long, read : {row -> row.file.length()}) + } + } + } + } + panel { + label("Uploads go here") + } } } panel (border: etchedBorder(), constraints : BorderLayout.SOUTH) {