Ability to apply folder config changes to subfolders. GitHub issue #120

auto-update
Zlatin Balevsky 2022-02-09 15:00:59 +00:00
parent 2e4733e6c3
commit 508e22d6c4
No known key found for this signature in database
GPG Key ID: A72832072D525E41
8 changed files with 70 additions and 21 deletions

View File

@ -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)

View File

@ -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() {

View File

@ -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<SharedFile> getPublishedSince(long timestamp) {
synchronized(fileToSharedFile) {
fileToSharedFile.values().stream().
@ -394,4 +407,18 @@ class FileManager {
unsharedFiles << value
}
}
private static class SubDirCallback implements FileTreeCallback<SharedFile> {
final List<File> subDirs = new ArrayList<>()
@Override
public void onDirectoryEnter(File file) {
subDirs << file
}
@Override
public void onDirectoryLeave(){}
@Override
public void onFile(File file, SharedFile ignored){}
}
}

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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()
}

View File

@ -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

View File

@ -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)
}
}
}
}