From ab323db62a48a3f0cf2d0672df86dfbff87732a6 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sun, 20 Oct 2019 18:16:07 +0100 Subject: [PATCH] add ability to choose the incompletes location --- .../groovy/com/muwire/core/MuWireSettings.groovy | 6 ++++++ .../muwire/core/download/DownloadManager.groovy | 16 ++++++++++++---- .../com/muwire/core/download/Downloader.groovy | 2 ++ .../com/muwire/gui/OptionsController.groovy | 16 +++++++++++++++- gui/griffon-app/lifecycle/Ready.groovy | 3 +++ .../models/com/muwire/gui/OptionsModel.groovy | 2 ++ .../views/com/muwire/gui/OptionsView.groovy | 4 ++++ 7 files changed, 44 insertions(+), 5 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy b/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy index 7db120b7..aef4f24f 100644 --- a/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy +++ b/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy @@ -22,6 +22,7 @@ class MuWireSettings { String updateType String nickname File downloadLocation + File incompleteLocation CrawlerResponse crawlerResponse boolean shareDownloadedFiles boolean shareHiddenFiles @@ -50,6 +51,9 @@ class MuWireSettings { nickname = props.getProperty("nickname","MuWireUser") downloadLocation = new File((String)props.getProperty("downloadLocation", System.getProperty("user.home"))) + String incompleteLocationProp = props.getProperty("incompleteLocation") + if (incompleteLocationProp != null) + incompleteLocation = new File(incompleteLocationProp) downloadRetryInterval = Integer.parseInt(props.getProperty("downloadRetryInterval","60")) updateCheckInterval = Integer.parseInt(props.getProperty("updateCheckInterval","24")) autoDownloadUpdate = Boolean.parseBoolean(props.getProperty("autoDownloadUpdate","true")) @@ -91,6 +95,8 @@ class MuWireSettings { props.setProperty("crawlerResponse", crawlerResponse.toString()) props.setProperty("nickname", nickname) props.setProperty("downloadLocation", downloadLocation.getAbsolutePath()) + if (incompleteLocation != null) + props.setProperty("incompleteLocation", incompleteLocation.getAbsolutePath()) props.setProperty("downloadRetryInterval", String.valueOf(downloadRetryInterval)) props.setProperty("updateCheckInterval", String.valueOf(updateCheckInterval)) props.setProperty("autoDownloadUpdate", String.valueOf(autoDownloadUpdate)) diff --git a/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy b/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy index ccc9e93d..a500a9d4 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy @@ -34,7 +34,7 @@ public class DownloadManager { private final MuWireSettings muSettings private final I2PConnector connector private final Executor executor - private final File incompletes, home + private final File home private final Persona me private final Map downloaders = new ConcurrentHashMap<>() @@ -46,12 +46,9 @@ public class DownloadManager { this.meshManager = meshManager this.muSettings = muSettings this.connector = connector - this.incompletes = new File(home,"incompletes") this.home = home this.me = me - incompletes.mkdir() - this.executor = Executors.newCachedThreadPool({ r -> Thread rv = new Thread(r) rv.setName("download-worker") @@ -63,6 +60,11 @@ public class DownloadManager { public void onUIDownloadEvent(UIDownloadEvent e) { + File incompletes = muSettings.incompleteLocation + if (incompletes == null) + incompletes = new File(home, "incompletes") + incompletes.mkdirs() + def size = e.result[0].size def infohash = e.result[0].infohash def pieceSize = e.result[0].pieceSize @@ -126,6 +128,10 @@ public class DownloadManager { boolean sequential = false if (json.sequential != null) sequential = json.sequential + + File incompletes = this.incompletes + if (json.incompletes != null) + incompletes = new File(DataUtil.readi18nString(Base64.decode(json.incompletes))) Pieces pieces = getPieces(infoHash, (long)json.length, json.pieceSizePow2, sequential) @@ -195,6 +201,8 @@ public class DownloadManager { json.sequential = downloader.pieces.ratio == 0f + json.incompletes = Base64.encode(DataUtil.encodei18nString(downloader.incompletes.getAbsolutePath())) + writer.println(JsonOutput.toJson(json)) } } diff --git a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy index aff6b6f7..7e73a7fc 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -48,6 +48,7 @@ public class Downloader { private final I2PConnector connector private final Set destinations private final int nPieces + private final File incompletes private final File piecesFile private final File incompleteFile final int pieceSizePow2 @@ -76,6 +77,7 @@ public class Downloader { this.length = length this.connector = connector this.destinations = destinations + this.incompletes = incompletes this.piecesFile = new File(incompletes, file.getName()+".pieces") this.incompleteFile = new File(incompletes, file.getName()+".part") this.pieceSizePow2 = pieceSizePow2 diff --git a/gui/griffon-app/controllers/com/muwire/gui/OptionsController.groovy b/gui/griffon-app/controllers/com/muwire/gui/OptionsController.groovy index 5b1b5ef7..a06345ad 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/OptionsController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/OptionsController.groovy @@ -93,6 +93,9 @@ class OptionsController { String downloadLocation = model.downloadLocation settings.downloadLocation = new File(downloadLocation) + + String incompleteLocation = model.incompleteLocation + settings.incompleteLocation = new File(incompleteLocation) if (settings.embeddedRouter) { text = view.inBwField.text @@ -174,7 +177,18 @@ class OptionsController { int rv = chooser.showOpenDialog(null) if (rv == JFileChooser.APPROVE_OPTION) model.downloadLocation = chooser.getSelectedFile().getAbsolutePath() - } + } + + @ControllerAction + void incompleteLocation() { + def chooser = new JFileChooser() + chooser.setFileHidingEnabled(false) + chooser.setDialogTitle("Select location for downloaded files") + chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY) + int rv = chooser.showOpenDialog(null) + if (rv == JFileChooser.APPROVE_OPTION) + model.incompleteLocation = chooser.getSelectedFile().getAbsolutePath() + } @ControllerAction void automaticFontAction() { diff --git a/gui/griffon-app/lifecycle/Ready.groovy b/gui/griffon-app/lifecycle/Ready.groovy index 8a36f75d..b069ab9c 100644 --- a/gui/griffon-app/lifecycle/Ready.groovy +++ b/gui/griffon-app/lifecycle/Ready.groovy @@ -45,9 +45,12 @@ class Ready extends AbstractLifecycleHandler { props.load(it) } props = new MuWireSettings(props) + if (props.incompleteLocation == null) + props.incompleteLocation = new File(home, "incompletes") } else { log.info("creating new properties") props = new MuWireSettings() + props.incompleteLocation = new File(home, "incompletes") props.embeddedRouter = Boolean.parseBoolean(System.getProperties().getProperty("embeddedRouter")) props.updateType = System.getProperty("updateType","jar") def nickname diff --git a/gui/griffon-app/models/com/muwire/gui/OptionsModel.groovy b/gui/griffon-app/models/com/muwire/gui/OptionsModel.groovy index 7bf60ee4..23f9274f 100644 --- a/gui/griffon-app/models/com/muwire/gui/OptionsModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/OptionsModel.groovy @@ -15,6 +15,7 @@ class OptionsModel { @Observable boolean shareDownloadedFiles @Observable boolean shareHiddenFiles @Observable String downloadLocation + @Observable String incompleteLocation @Observable boolean searchComments @Observable boolean browseFiles @@ -56,6 +57,7 @@ class OptionsModel { shareDownloadedFiles = settings.shareDownloadedFiles shareHiddenFiles = settings.shareHiddenFiles downloadLocation = settings.downloadLocation.getAbsolutePath() + incompleteLocation = settings.incompleteLocation.getAbsolutePath() searchComments = settings.searchComments browseFiles = settings.browseFiles diff --git a/gui/griffon-app/views/com/muwire/gui/OptionsView.groovy b/gui/griffon-app/views/com/muwire/gui/OptionsView.groovy index ea8fd1a7..da23b83f 100644 --- a/gui/griffon-app/views/com/muwire/gui/OptionsView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/OptionsView.groovy @@ -100,6 +100,10 @@ class OptionsView { label(text : "Save downloaded files to:", constraints: gbc(gridx:0, gridy:7)) button(text : "Choose", constraints : gbc(gridx : 1, gridy:7), downloadLocationAction) label(text : bind {model.downloadLocation}, constraints: gbc(gridx:0, gridy:8, gridwidth:2)) + + label(text : "Store incomplete files in:", constraints: gbc(gridx:0, gridy:9)) + button(text : "Choose", constraints : gbc(gridx : 1, gridy:9), incompleteLocationAction) + label(text : bind {model.incompleteLocation}, constraints: gbc(gridx:0, gridy:10, gridwidth:2)) } i = builder.panel {