From a04b92332f3ce8560bb339c1afb00037adbdd14e Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Thu, 20 Jul 2017 13:16:54 +0000 Subject: [PATCH] gui, lib/config: Add default path for new folders (fixes #2157) GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4192 --- .../syncthing/core/syncthingController.js | 43 ++++++++++++++++--- lib/config/config_test.go | 2 + lib/config/optionsconfiguration.go | 1 + lib/config/testdata/overridenvalues.xml | 13 +++--- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/gui/default/syncthing/core/syncthingController.js b/gui/default/syncthing/core/syncthingController.js index fc5204550..0e314acb4 100755 --- a/gui/default/syncthing/core/syncthingController.js +++ b/gui/default/syncthing/core/syncthingController.js @@ -74,7 +74,8 @@ angular.module('syncthing.core') staggeredCleanInterval: 3600, staggeredVersionsPath: "", externalCommand: "", - autoNormalize: true + autoNormalize: true, + path: "" }; $scope.localStateTotal = { @@ -606,6 +607,21 @@ angular.module('syncthing.core') $scope.neededTotal = data.total; } + function pathJoin(base, name) { + base = expandTilde(base); + if (base[base.length - 1] !== $scope.system.pathSeparator) { + return base + $scope.system.pathSeparator + name; + } + return base + name; + } + + function expandTilde(path) { + if (path && path.trim().charAt(0) === '~') { + return $scope.system.tilde + path.trim().substring(1); + } + return path; + } + $scope.neededPageChanged = function (page) { $scope.neededCurrentPage = page; refreshNeed($scope.neededFolder); @@ -1360,9 +1376,10 @@ angular.module('syncthing.core') $scope.directoryList = []; $scope.$watch('currentFolder.path', function (newvalue) { - if (newvalue && newvalue.trim().charAt(0) === '~') { - $scope.currentFolder.path = $scope.system.tilde + newvalue.trim().substring(1); + if (!newvalue) { + return; } + $scope.currentFolder.path = expandTilde(newvalue); $http.get(urlbase + '/system/browse', { params: { current: newvalue } }).success(function (data) { @@ -1370,6 +1387,20 @@ angular.module('syncthing.core') }).error($scope.emitHTTPError); }); + $scope.$watch('currentFolder.label', function (newvalue) { + if (!$scope.config.options || !$scope.config.options.defaultFolderPath || $scope.editingExisting || !$scope.folderEditor.folderPath.$pristine || !newvalue) { + return; + } + $scope.currentFolder.path = pathJoin($scope.config.options.defaultFolderPath, newvalue); + }); + + $scope.$watch('currentFolder.id', function (newvalue) { + if (!$scope.config.options || !$scope.config.options.defaultFolderPath || !$scope.folderEditor.folderPath.$pristine || !newvalue || $scope.currentFolder.label) { + return; + } + $scope.currentFolder.path = pathJoin($scope.config.options.defaultFolderPath, newvalue); + }); + $scope.loadFormIntoScope = function (form) { console.log('loadFormIntoScope',form.$name); switch (form.$name) { @@ -1394,6 +1425,7 @@ angular.module('syncthing.core') }; $scope.editFolder = function (folderCfg) { + $scope.editingExisting = true; $scope.currentFolder = angular.copy(folderCfg); if ($scope.currentFolder.path.slice(-1) === $scope.system.pathSeparator) { $scope.currentFolder.path = $scope.currentFolder.path.slice(0, -1); @@ -1436,21 +1468,21 @@ angular.module('syncthing.core') } $scope.currentFolder.externalCommand = $scope.currentFolder.externalCommand || ""; - $scope.editingExisting = true; $scope.editFolderModal(); }; $scope.addFolder = function () { $http.get(urlbase + '/svc/random/string?length=10').success(function (data) { + $scope.editingExisting = false; $scope.currentFolder = angular.copy($scope.folderDefaults); $scope.currentFolder.id = (data.random.substr(0, 5) + '-' + data.random.substr(5, 5)).toLowerCase(); - $scope.editingExisting = false; $scope.editFolderModal(); }); }; $scope.addFolderAndShare = function (folder, folderLabel, device) { $scope.dismissFolderRejection(folder, device); + $scope.editingExisting = false; $scope.currentFolder = angular.copy($scope.folderDefaults); $scope.currentFolder.id = folder; $scope.currentFolder.label = folderLabel; @@ -1459,7 +1491,6 @@ angular.module('syncthing.core') }; $scope.currentFolder.selectedDevices[device] = true; - $scope.editingExisting = false; $scope.editFolderModal(); }; diff --git a/lib/config/config_test.go b/lib/config/config_test.go index b48d671ed..5a18e9818 100644 --- a/lib/config/config_test.go +++ b/lib/config/config_test.go @@ -73,6 +73,7 @@ func TestDefaultValues(t *testing.T) { KCPSendWindowSize: 128, KCPUpdateIntervalMs: 25, KCPFastResend: false, + DefaultFolderPath: "~", } cfg := New(device1) @@ -221,6 +222,7 @@ func TestOverriddenValues(t *testing.T) { KCPSendWindowSize: 1280, KCPUpdateIntervalMs: 1000, KCPFastResend: true, + DefaultFolderPath: "/media/syncthing", } os.Unsetenv("STNOUPGRADE") diff --git a/lib/config/optionsconfiguration.go b/lib/config/optionsconfiguration.go index a736d8a5a..24e8b1f74 100644 --- a/lib/config/optionsconfiguration.go +++ b/lib/config/optionsconfiguration.go @@ -140,6 +140,7 @@ type OptionsConfiguration struct { KCPCongestionControl bool `xml:"kcpCongestionControl" json:"kcpCongestionControl" default:"true"` KCPSendWindowSize int `xml:"kcpSendWindowSize" json:"kcpSendWindowSize" default:"128"` KCPReceiveWindowSize int `xml:"kcpReceiveWindowSize" json:"kcpReceiveWindowSize" default:"128"` + DefaultFolderPath string `xml:"defaultFolderPath" json:"defaultFolderPath" default:"~"` DeprecatedUPnPEnabled bool `xml:"upnpEnabled,omitempty" json:"-"` DeprecatedUPnPLeaseM int `xml:"upnpLeaseMinutes,omitempty" json:"-"` diff --git a/lib/config/testdata/overridenvalues.xml b/lib/config/testdata/overridenvalues.xml index 0a34fff10..bda09b9a7 100644 --- a/lib/config/testdata/overridenvalues.xml +++ b/lib/config/testdata/overridenvalues.xml @@ -38,11 +38,12 @@ 10 a.stun.com b.stun.com - true - false - 1280 - 1280 - 1000 - true + true + false + 1280 + 1280 + 1000 + true + /media/syncthing