diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 5380566a..42c57a98 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -317,6 +317,7 @@ public class Core { eventBus.register(DirectoryUnsharedEvent.class, fileManager) eventBus.register(UICommentEvent.class, fileManager) eventBus.register(SideCarFileEvent.class, fileManager) + eventBus.register(WatchedDirectoryConfigurationEvent.class, fileManager) log.info("initializing collection manager") collectionManager = new CollectionManager(eventBus, fileManager, home) 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 e2b1b334..269c40df 100644 --- a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy +++ b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy @@ -95,14 +95,16 @@ class DirectoryWatcher { void onWatchedDirectoryConfigurationEvent(WatchedDirectoryConfigurationEvent e) { if (watchService == null) return // still converting - if (!e.autoWatch) { - WatchKey wk = watchedDirectories.remove(e.directory) - wk?.cancel() - } else if (!watchedDirectories.containsKey(e.directory)) { - Path path = e.directory.toPath() - def wk = path.register(watchService, kinds) - watchedDirectories.put(e.directory, wk) - } // else it was already watched + e.toApply.each { + if (!e.autoWatch) { + WatchKey wk = watchedDirectories.remove(it) + wk?.cancel() + } else if (!watchedDirectories.containsKey(it)) { + Path path = it.toPath() + def wk = path.register(watchService, kinds) + watchedDirectories.put(it, wk) + } // else it was already watched + } } private void watch() { 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 0eafb63b..430ea3da 100644 --- a/core/src/main/groovy/com/muwire/core/files/FileManager.groovy +++ b/core/src/main/groovy/com/muwire/core/files/FileManager.groovy @@ -1,5 +1,7 @@ package com.muwire.core.files +import com.muwire.core.files.directories.WatchedDirectoryConfigurationEvent + import java.nio.file.Path import java.util.function.Predicate import java.util.regex.Matcher @@ -366,6 +368,17 @@ class FileManager { } } + void onWatchedDirectoryConfigurationEvent(WatchedDirectoryConfigurationEvent e) { + // just enriches the event with subdirectories. + if (!e.subfolders) { + e.toApply = new File[] {e.directory} + } else { + def cb = new SubDirCallback() + positiveTree.traverse(e.directory, cb) + e.toApply = cb.subDirs.toArray(new File[0]) + } + } + public List getPublishedSince(long timestamp) { synchronized(fileToSharedFile) { fileToSharedFile.values().stream(). @@ -394,4 +407,18 @@ class FileManager { unsharedFiles << value } } + + private static class SubDirCallback implements FileTreeCallback { + final List subDirs = new ArrayList<>() + + @Override + public void onDirectoryEnter(File file) { + subDirs << file + } + + @Override + public void onDirectoryLeave(){} + @Override + public void onFile(File file, SharedFile ignored){} + } } diff --git a/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryConfigurationEvent.groovy b/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryConfigurationEvent.groovy index 3cf34c15..6d941acc 100644 --- a/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryConfigurationEvent.groovy +++ b/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryConfigurationEvent.groovy @@ -3,7 +3,13 @@ package com.muwire.core.files.directories import com.muwire.core.Event class WatchedDirectoryConfigurationEvent extends Event { + /** directory selected by user or by converter */ File directory + + /** actual directories to apply, enriched by FileManager */ + File [] toApply + boolean autoWatch int syncInterval + boolean subfolders } diff --git a/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryManager.groovy b/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryManager.groovy index 92462bdd..3b3c2ac3 100644 --- a/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryManager.groovy +++ b/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryManager.groovy @@ -84,17 +84,19 @@ class WatchedDirectoryManager { newDir.autoWatch = e.autoWatch persist(newDir) } else { - def wd - synchronized (this) { - wd = watchedDirs.get(e.directory) + e.toApply.each { + def wd + synchronized (this) { + wd = watchedDirs.get(it) + } + if (wd == null) { + log.severe("got a configuration event for a non-watched directory ${it}") + return + } + wd.autoWatch = e.autoWatch + wd.syncInterval = e.syncInterval + persist(wd) } - if (wd == null) { - log.severe("got a configuration event for a non-watched directory ${e.directory}") - return - } - wd.autoWatch = e.autoWatch - wd.syncInterval = e.syncInterval - persist(wd) } } diff --git a/gui/griffon-app/controllers/com/muwire/gui/WatchedDirectoryController.groovy b/gui/griffon-app/controllers/com/muwire/gui/WatchedDirectoryController.groovy index 6f893e39..178f281a 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/WatchedDirectoryController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/WatchedDirectoryController.groovy @@ -20,7 +20,8 @@ class WatchedDirectoryController { def event = new WatchedDirectoryConfigurationEvent( directory : model.directory.directory, autoWatch : view.autoWatchCheckbox.model.isSelected(), - syncInterval : Integer.parseInt(view.syncIntervalField.text)) + syncInterval : Integer.parseInt(view.syncIntervalField.text), + subfolders: view.applySubCheckbox.model.isSelected()) model.core.eventBus.publish(event) cancel() } diff --git a/gui/griffon-app/i18n/messages.properties b/gui/griffon-app/i18n/messages.properties index 5fceb467..9d505e40 100644 --- a/gui/griffon-app/i18n/messages.properties +++ b/gui/griffon-app/i18n/messages.properties @@ -556,6 +556,7 @@ WATCHED_DIRECTORY_CONFIGURATION=Watched Folder Configuration WATCHED_DIRECTORY_CONFIGURATION_FOR=Configuration for folder {0} WATCHED_DIRECTORY_AUTO=Auto-watch folder using operating system WATCHED_DIRECTORY_INTERVAL=Folder sync frequency (seconds, 0 means never) +WATCHED_DIRECTORY_APPLY_SUB=Apply to subfolders ## Sign Tool SIGN_TEXT=Sign Text diff --git a/gui/griffon-app/views/com/muwire/gui/WatchedDirectoryView.groovy b/gui/griffon-app/views/com/muwire/gui/WatchedDirectoryView.groovy index 035020d8..d3fd47c4 100644 --- a/gui/griffon-app/views/com/muwire/gui/WatchedDirectoryView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/WatchedDirectoryView.groovy @@ -30,6 +30,7 @@ class WatchedDirectoryView { def autoWatchCheckbox def syncIntervalField + def applySubCheckbox void initUI() { mainFrame = application.windowManager.findWindow("main-frame") @@ -50,8 +51,16 @@ class WatchedDirectoryView { constraints: gbc(gridx: 1, gridy : 1, anchor : GridBagConstraints.LINE_END, insets : [0,10,0,0])) } panel (constraints : BorderLayout.SOUTH) { - button(text : trans("SAVE"), saveAction) - button(text : trans("CANCEL"), cancelAction) + gridLayout(rows: 1, cols: 3) + panel() + panel() { + button(text: trans("SAVE"), saveAction) + button(text: trans("CANCEL"), cancelAction) + } + panel() { + label(text : trans("WATCHED_DIRECTORY_APPLY_SUB")) + applySubCheckbox = checkBox(selected: false) + } } } }