From 720b9688b41ae51dad998da79eab578302db51d7 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Fri, 28 Jun 2019 16:08:04 +0100 Subject: [PATCH] Add unsharing of directories --- .../main/groovy/com/muwire/core/Core.groovy | 3 ++ .../core/files/DirectoryUnsharedEvent.groovy | 7 ++++ .../muwire/core/files/DirectoryWatcher.groovy | 9 +++++- .../com/muwire/core/files/FileManager.groovy | 12 +++++++ .../com/muwire/gui/MainFrameController.groovy | 13 ++++++++ .../views/com/muwire/gui/MainFrameView.groovy | 32 +++++++++++++++++++ 6 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 core/src/main/groovy/com/muwire/core/files/DirectoryUnsharedEvent.groovy diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 34a46820..de53ffa9 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -28,6 +28,7 @@ import com.muwire.core.files.FileUnsharedEvent import com.muwire.core.files.HasherService import com.muwire.core.files.PersisterService import com.muwire.core.files.AllFilesLoadedEvent +import com.muwire.core.files.DirectoryUnsharedEvent import com.muwire.core.files.DirectoryWatcher import com.muwire.core.hostcache.CacheClient import com.muwire.core.hostcache.HostCache @@ -193,6 +194,7 @@ public class Core { eventBus.register(FileDownloadedEvent.class, fileManager) eventBus.register(FileUnsharedEvent.class, fileManager) eventBus.register(SearchEvent.class, fileManager) + eventBus.register(DirectoryUnsharedEvent.class, fileManager) log.info("initializing mesh manager") MeshManager meshManager = new MeshManager(fileManager, home, props) @@ -259,6 +261,7 @@ public class Core { directoryWatcher = new DirectoryWatcher(eventBus, fileManager) eventBus.register(FileSharedEvent.class, directoryWatcher) eventBus.register(AllFilesLoadedEvent.class, directoryWatcher) + eventBus.register(DirectoryUnsharedEvent.class, directoryWatcher) log.info("initializing hasher service") hasherService = new HasherService(new FileHasher(), eventBus, fileManager) diff --git a/core/src/main/groovy/com/muwire/core/files/DirectoryUnsharedEvent.groovy b/core/src/main/groovy/com/muwire/core/files/DirectoryUnsharedEvent.groovy new file mode 100644 index 00000000..9c9ea545 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/files/DirectoryUnsharedEvent.groovy @@ -0,0 +1,7 @@ +package com.muwire.core.files + +import com.muwire.core.Event + +class DirectoryUnsharedEvent extends Event { + File directory +} diff --git a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy index a49b4c0a..43165e42 100644 --- a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy +++ b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy @@ -35,6 +35,7 @@ class DirectoryWatcher { private final FileManager fileManager private final Thread watcherThread, publisherThread private final Map waitingFiles = new ConcurrentHashMap<>() + private final Map watchedDirectories = new ConcurrentHashMap<>() private WatchService watchService private volatile boolean shutdown @@ -64,9 +65,15 @@ class DirectoryWatcher { if (!e.file.isDirectory()) return Path path = e.file.getCanonicalFile().toPath() - path.register(watchService, kinds) + WatchKey wk = path.register(watchService, kinds) + watchedDirectories.put(e.file, wk) } + + void onDirectoryUnsharedEvent(DirectoryUnsharedEvent e) { + WatchKey wk = watchedDirectories.remove(e.directory) + wk?.cancel() + } private void watch() { try { diff --git a/core/src/main/groovy/com/muwire/core/files/FileManager.groovy b/core/src/main/groovy/com/muwire/core/files/FileManager.groovy index 424a53f5..81230a96 100644 --- a/core/src/main/groovy/com/muwire/core/files/FileManager.groovy +++ b/core/src/main/groovy/com/muwire/core/files/FileManager.groovy @@ -135,4 +135,16 @@ class FileManager { } rv } + + void onDirectoryUnsharedEvent(DirectoryUnsharedEvent e) { + e.directory.listFiles().each { + if (it.isDirectory()) + eventBus.publish(new DirectoryUnsharedEvent(directory : it)) + else { + SharedFile sf = fileToSharedFile.get(it) + if (sf != null) + eventBus.publish(new FileUnsharedEvent(unsharedFile : sf)) + } + } + } } diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index e140d50a..077b8a6e 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -19,6 +19,7 @@ import com.muwire.core.download.UIDownloadCancelledEvent import com.muwire.core.download.UIDownloadEvent import com.muwire.core.download.UIDownloadPausedEvent import com.muwire.core.download.UIDownloadResumedEvent +import com.muwire.core.files.DirectoryUnsharedEvent import com.muwire.core.search.QueryEvent import com.muwire.core.search.SearchEvent import com.muwire.core.trust.TrustEvent @@ -209,6 +210,18 @@ class MainFrameController { println "unsharing selected files" } + void stopWatchingDirectory() { + String directory = mvcGroup.view.getSelectedWatchedDirectory() + if (directory == null) + return + core.muOptions.watchedDirectories.remove(directory) + saveMuWireSettings() + core.eventBus.publish(new DirectoryUnsharedEvent(directory : new File(directory))) + + model.watched.remove(directory) + builder.getVariable("watched-directories-table").model.fireTableDataChanged() + } + void saveMuWireSettings() { File f = new File(core.home, "MuWire.properties") f.withOutputStream { diff --git a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy index 2131334a..ae79ac39 100644 --- a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy @@ -52,6 +52,7 @@ class MainFrameView { def downloadsTable def lastDownloadSortEvent def lastSharedSortEvent + def lastWatchedSortEvent void initUI() { UISettings settings = application.context.get("ui-settings") @@ -397,6 +398,27 @@ class MainFrameView { } }) + // watched directories table + def watchedTable = builder.getVariable("watched-directories-table") + watchedTable.rowSorter.addRowSorterListener({evt -> lastWatchedSortEvent = evt}) + watchedTable.rowSorter.setSortsOnUpdates(true) + JPopupMenu watchedMenu = new JPopupMenu() + JMenuItem stopWatching = new JMenuItem("Stop sharing") + stopWatching.addActionListener({mvcGroup.controller.stopWatchingDirectory()}) + watchedMenu.add(stopWatching) + watchedTable.addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + if (e.isPopupTrigger()) + showPopupMenu(watchedMenu, e) + } + @Override + public void mousePressed(MouseEvent e) { + if (e.isPopupTrigger()) + showPopupMenu(watchedMenu, e) + } + }) + } private static void showPopupMenu(JPopupMenu menu, MouseEvent event) { @@ -543,4 +565,14 @@ class MainFrameView { model.core.eventBus.publish(new FileSharedEvent(file : f)) } } + + String getSelectedWatchedDirectory() { + def watchedTable = builder.getVariable("watched-directories-table") + int selectedRow = watchedTable.getSelectedRow() + if (selectedRow < 0) + return null + if (lastWatchedSortEvent != null) + selectedRow = watchedTable.rowSorter.convertRowIndexToModel(selectedRow) + model.watched[selectedRow] + } } \ No newline at end of file