From 6742584e124b94e010d2baf245f6b7efca8718a9 Mon Sep 17 00:00:00 2001
From: Tata <tarek@ring0.de>
Date: Tue, 24 Sep 2019 09:56:40 +0200
Subject: [PATCH] gluon-config-mode-geo-location: allow to set location
 independently from sharing it (#1537)

also properly delete location data when a user disables
the location option after re-entering config-mode.
---
 .../gluon-config-mode-geo-location/i18n/de.po |  3 ++
 .../gluon-config-mode-geo-location/i18n/fr.po |  3 ++
 .../i18n/gluon-config-mode-geo-location.pot   |  3 ++
 .../config-mode/wizard/0400-geo-location.lua  | 39 +++++++++++++------
 4 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/package/gluon-config-mode-geo-location/i18n/de.po b/package/gluon-config-mode-geo-location/i18n/de.po
index 0ff3d4339..d844548e8 100644
--- a/package/gluon-config-mode-geo-location/i18n/de.po
+++ b/package/gluon-config-mode-geo-location/i18n/de.po
@@ -26,6 +26,9 @@ msgstr "Breitengrad"
 msgid "Longitude"
 msgstr "Längengrad"
 
+msgid "Set node position"
+msgstr "Knotenposition setzen"
+
 msgid "Advertise node position"
 msgstr "Knotenposition veröffentlichen"
 
diff --git a/package/gluon-config-mode-geo-location/i18n/fr.po b/package/gluon-config-mode-geo-location/i18n/fr.po
index 5a725cb68..5010a7a27 100644
--- a/package/gluon-config-mode-geo-location/i18n/fr.po
+++ b/package/gluon-config-mode-geo-location/i18n/fr.po
@@ -24,6 +24,9 @@ msgstr "Latitude"
 msgid "Longitude"
 msgstr "Longitude"
 
+msgid "Set node position"
+msgstr ""
+
 msgid "Advertise node position"
 msgstr ""
 
diff --git a/package/gluon-config-mode-geo-location/i18n/gluon-config-mode-geo-location.pot b/package/gluon-config-mode-geo-location/i18n/gluon-config-mode-geo-location.pot
index 27ab905d6..3dfa782aa 100644
--- a/package/gluon-config-mode-geo-location/i18n/gluon-config-mode-geo-location.pot
+++ b/package/gluon-config-mode-geo-location/i18n/gluon-config-mode-geo-location.pot
@@ -15,6 +15,9 @@ msgstr ""
 msgid "Longitude"
 msgstr ""
 
+msgid "Set node position"
+msgstr ""
+
 msgid "Advertise node position"
 msgstr ""
 
diff --git a/package/gluon-config-mode-geo-location/luasrc/lib/gluon/config-mode/wizard/0400-geo-location.lua b/package/gluon-config-mode-geo-location/luasrc/lib/gluon/config-mode/wizard/0400-geo-location.lua
index e2d0bdb70..04ebf63aa 100644
--- a/package/gluon-config-mode-geo-location/luasrc/lib/gluon/config-mode/wizard/0400-geo-location.lua
+++ b/package/gluon-config-mode-geo-location/luasrc/lib/gluon/config-mode/wizard/0400-geo-location.lua
@@ -32,26 +32,41 @@ return function(form, uci)
 
 	local o
 
-	local share_location = s:option(Flag, "location", pkg_i18n.translate("Advertise node position"))
-	share_location.default = uci:get_bool("gluon-node-info", location, "share_location")
-	function share_location:write(data)
-		uci:set("gluon-node-info", location, "share_location", data)
+	local own_latitude = uci:get("gluon-node-info", location, "latitude")
+	local own_longitude = uci:get("gluon-node-info", location, "longitude")
+
+	local set_location = s:option(Flag, "location", pkg_i18n.translate("Set node position"))
+	set_location.default = own_latitude or own_longitude
+
+	-- Delete already saved coordinates
+	function set_location:write(data)
+		if not data then
+			uci:delete("gluon-node-info", location, "latitude")
+			uci:delete("gluon-node-info", location, "longitude")
+			uci:set("gluon-node-info", location, "share_location", false)
+		end
 
-		-- The config mode does not have a nicer place to put this at the moment...
-		if not show_altitude then
+		if not show_altitude or not data then
 			uci:delete("gluon-node-info", location, "altitude")
 		end
 	end
 
+	local share_location = s:option(Flag, "share_location", pkg_i18n.translate("Advertise node position"))
+	share_location.default = uci:get_bool("gluon-node-info", location, "share_location")
+	share_location:depends(set_location, true)
+	function share_location:write(data)
+		uci:set("gluon-node-info", location, "share_location", data)
+	end
+
 	local map
 	if osm then
 		map = s:option(osm.MapValue, "map", osm.options())
-		map:depends(share_location, true)
+		map:depends(set_location, true)
 	end
 
 	o = s:option(Value, "latitude", pkg_i18n.translate("Latitude"), pkg_i18n.translatef("e.g. %s", "53.873621"))
-	o.default = uci:get("gluon-node-info", location, "latitude")
-	o:depends(share_location, true)
+	o.default = own_latitude
+	o:depends(set_location, true)
 	o.datatype = "float"
 	function o:write(data)
 		uci:set("gluon-node-info", location, "latitude", data)
@@ -61,8 +76,8 @@ return function(form, uci)
 	end
 
 	o = s:option(Value, "longitude", pkg_i18n.translate("Longitude"), pkg_i18n.translatef("e.g. %s", "10.689901"))
-	o.default = uci:get("gluon-node-info", location, "longitude")
-	o:depends(share_location, true)
+	o.default = own_longitude
+	o:depends(set_location, true)
 	o.datatype = "float"
 	function o:write(data)
 		uci:set("gluon-node-info", location, "longitude", data)
@@ -77,7 +92,7 @@ return function(form, uci)
 			pkg_i18n.translatef("e.g. %s", "11.51")
 		)
 		o.default = uci:get("gluon-node-info", location, "altitude")
-		o:depends(share_location, true)
+		o:depends(set_location, true)
 		o.datatype = "float"
 		o.optional = true
 		function o:write(data)
-- 
GitLab