From e78016ead4ea92cd7107e218c83a16e8798c65ef Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 17 Jun 2019 19:23:04 +0100 Subject: [PATCH] ui panel for managing watched directories --- .../com/muwire/core/MuWireSettings.groovy | 28 +++++++--- .../com/muwire/gui/MainFrameController.groovy | 7 +++ gui/griffon-app/lifecycle/Ready.groovy | 7 +-- .../com/muwire/gui/MainFrameModel.groovy | 8 ++- .../views/com/muwire/gui/MainFrameView.groovy | 53 +++++++++++++++---- 5 files changed, 78 insertions(+), 25 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy b/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy index d1078ceb..76ced3b3 100644 --- a/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy +++ b/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy @@ -1,6 +1,11 @@ package com.muwire.core +import java.util.stream.Collectors + import com.muwire.core.hostcache.CrawlerResponse +import com.muwire.core.util.DataUtil + +import net.i2p.data.Base64 class MuWireSettings { @@ -10,10 +15,9 @@ class MuWireSettings { int updateCheckInterval String nickname File downloadLocation - String sharedFiles CrawlerResponse crawlerResponse boolean shareDownloadedFiles - boolean watchSharedDirectories + Set watchedDirectories MuWireSettings() { this(new Properties()) @@ -26,11 +30,16 @@ class MuWireSettings { nickname = props.getProperty("nickname","MuWireUser") downloadLocation = new File((String)props.getProperty("downloadLocation", System.getProperty("user.home"))) - sharedFiles = props.getProperty("sharedFiles") downloadRetryInterval = Integer.parseInt(props.getProperty("downloadRetryInterval","15")) updateCheckInterval = Integer.parseInt(props.getProperty("updateCheckInterval","36")) shareDownloadedFiles = Boolean.parseBoolean(props.getProperty("shareDownloadedFiles","true")) - watchSharedDirectories = Boolean.parseBoolean(props.getProperty("watchSharedDirectories","true")) + + watchedDirectories = new HashSet<>() + if (props.containsKey("watchedDirectories")) { + String[] encoded = props.getProperty("watchedDirectories").split(",") + encoded.each { watchedDirectories << DataUtil.readi18nString(Base64.decode(it)) } + } + } void write(OutputStream out) throws IOException { @@ -43,9 +52,14 @@ class MuWireSettings { props.setProperty("downloadRetryInterval", String.valueOf(downloadRetryInterval)) props.setProperty("updateCheckInterval", String.valueOf(updateCheckInterval)) props.setProperty("shareDownloadedFiles", String.valueOf(shareDownloadedFiles)) - props.setProperty("watchSharedDirectories", String.valueOf(watchSharedDirectories)) - if (sharedFiles != null) - props.setProperty("sharedFiles", sharedFiles) + + if (!watchedDirectories.isEmpty()) { + String encoded = watchedDirectories.stream(). + map({Base64.encode(DataUtil.encodei18nString(it))}). + collect(Collectors.joining(",")) + props.setProperty("watchedDirectories", encoded) + } + props.store(out, "") } diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index 7102eff1..609e7480 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -194,6 +194,13 @@ class MainFrameController { println "unsharing selected files" } + void saveMuWireSettings() { + File f = new File(core.home, "MuWire.properties") + f.withOutputStream { + core.muOptions.write(it) + } + } + void mvcGroupInit(Map args) { application.addPropertyChangeListener("core", {e-> core = e.getNewValue() diff --git a/gui/griffon-app/lifecycle/Ready.groovy b/gui/griffon-app/lifecycle/Ready.groovy index 7f8419f5..61bdd49b 100644 --- a/gui/griffon-app/lifecycle/Ready.groovy +++ b/gui/griffon-app/lifecycle/Ready.groovy @@ -1,3 +1,4 @@ + import griffon.core.GriffonApplication import griffon.core.env.Metadata import groovy.util.logging.Log @@ -104,12 +105,6 @@ class Ready extends AbstractLifecycleHandler { 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))) - } - } - core.eventBus.publish(new UILoadedEvent()) } } diff --git a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy index e50c3d38..9cc4710f 100644 --- a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy @@ -9,6 +9,7 @@ import javax.swing.JTable import com.muwire.core.Core import com.muwire.core.InfoHash +import com.muwire.core.MuWireSettings import com.muwire.core.Persona import com.muwire.core.connection.ConnectionAttemptStatus import com.muwire.core.connection.ConnectionEvent @@ -53,6 +54,7 @@ class MainFrameModel { def downloads = [] def uploads = [] def shared = [] + def watched = [] def connectionList = [] def searches = new LinkedList() def trusted = [] @@ -133,7 +135,7 @@ class MainFrameModel { core.eventBus.register(FileDownloadedEvent.class, this) timer.schedule({ - int retryInterval = application.context.get("muwire-settings").downloadRetryInterval + int retryInterval = core.muOptions.downloadRetryInterval if (retryInterval > 0) { retryInterval *= 60000 long now = System.currentTimeMillis() @@ -156,6 +158,10 @@ class MainFrameModel { runInsideUIAsync { trusted.addAll(core.trustService.good.values()) distrusted.addAll(core.trustService.bad.values()) + + watched.addAll(core.muOptions.watchedDirectories) + builder.getVariable("watched-directories-table").model.fireTableDataChanged() + watched.each { core.eventBus.publish(new FileSharedEvent(file : new File(it))) } } }) diff --git a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy index 3a5b941d..0e850ffb 100644 --- a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy @@ -139,16 +139,32 @@ class MainFrameView { panel (constraints: "uploads window"){ gridLayout(cols : 1, rows : 2) panel { - borderLayout() - panel (constraints : BorderLayout.NORTH) { - button(text : "Click here to share files", actionPerformed : shareFiles) + gridLayout(cols : 2, rows : 1) + panel { + borderLayout() + panel (constraints : BorderLayout.NORTH) { + button(text : "Add directories to watch", actionPerformed : watchDirectories) + } + scrollPane (constraints : BorderLayout.CENTER) { + table(id : "watched-directories-table", autoCreateRowSorter: true) { + tableModel(list : model.watched) { + closureColumn(header: "Watched Directories", type : String, read : { it }) + } + } + } } - scrollPane ( constraints : BorderLayout.CENTER) { - table(id : "shared-files-table", autoCreateRowSorter: true) { - tableModel(list : model.shared) { - closureColumn(header : "Name", preferredWidth : 550, type : String, read : {row -> row.file.getAbsolutePath()}) - closureColumn(header : "Size", preferredWidth : 50, type : Long, read : {row -> row.file.length() }) - } + panel { + borderLayout() + panel (constraints : BorderLayout.NORTH) { + button(text : "Share files", actionPerformed : shareFiles) + } + scrollPane(constraints : BorderLayout.CENTER) { + table(id : "shared-files-table", autoCreateRowSorter: true) { + tableModel(list : model.shared) { + closureColumn(header : "Name", preferredWidth : 500, type : String, read : {row -> row.file.getAbsolutePath()}) + closureColumn(header : "Size", preferredWidth : 100, type : Long, read : {row -> row.file.length() }) + } + } } } } @@ -466,11 +482,26 @@ class MainFrameView { def shareFiles = { def chooser = new JFileChooser() - chooser.setDialogTitle("Select file or directory to share") - chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES) + chooser.setDialogTitle("Select file to share") + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY) int rv = chooser.showOpenDialog(null) if (rv == JFileChooser.APPROVE_OPTION) { model.core.eventBus.publish(new FileSharedEvent(file : chooser.getSelectedFile())) } } + + def watchDirectories = { + def chooser = new JFileChooser() + chooser.setDialogTitle("Select directory to watch") + chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY) + int rv = chooser.showOpenDialog(null) + if (rv == JFileChooser.APPROVE_OPTION) { + File f = chooser.getSelectedFile() + model.watched << f.getAbsolutePath() + application.context.get("muwire-settings").watchedDirectories << f.getAbsolutePath() + mvcGroup.controller.saveMuWireSettings() + builder.getVariable("watched-directories-table").model.fireTableDataChanged() + model.core.eventBus.publish(new FileSharedEvent(file : f)) + } + } } \ No newline at end of file