From 91cc5a2b4225d8a3a4f35dc90bebbd213ee217c9 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 6 Aug 2020 14:47:55 +0200 Subject: [PATCH] [locale] Update the map-QML implementation - Config has suitable strings for displaying TZ information. Use them and automatic bindings. Don't update the strings manually. - Suggest online or offline TZ lookups based on what the distro wants. Edit the QML to pick online lookups (needs access to the geonames service, though). - Drop the variables that point at config and geoip: the Config object has a currentLocation, which is filled in by both the configuration and any GeoIP lookup -- it doesn't have city or country information though. --- src/modules/localeq/Map.qml | 53 ++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/modules/localeq/Map.qml b/src/modules/localeq/Map.qml index 080d7388a..06b4f7a1f 100644 --- a/src/modules/localeq/Map.qml +++ b/src/modules/localeq/Map.qml @@ -29,17 +29,12 @@ import QtPositioning 5.14 Column { width: parent.width - //Needs to come from .conf/geoip - property var configCity: "New York" - property var configCountry: "USA" - property var configTimezone: "America/New York" - property var geoipCity: "" //"Amsterdam" - property var geoipCountry: "" //"Netherlands" - property var geoipTimezone: "" //"Europe/Amsterdam" - // vars that will stay once connected - property var cityName: (geoipCity != "") ? geoipCity : configCity - property var countryName: (geoipCountry != "") ? geoipCountry : configCountry - property var timeZone: (geoipTimezone != "") ? geoipTimezone : configTimezone + // These are used by the map query to initially center the + // map on the user's likely location. They are updated by + // getIp() which does a more accurate GeoIP lookup than + // the default one in Calamares + property var cityName: "" + property var countryName: "" function getIp() { var xhr = new XMLHttpRequest @@ -51,9 +46,10 @@ Column { var ct = responseJSON.city var cy = responseJSON.country - tzText.text = "Timezone: " + tz cityName = ct countryName = cy + + config.setCurrentLocation(tz) } } @@ -63,7 +59,15 @@ Column { xhr.send() } - function getTz() { + /* This is an **accurate** TZ lookup method: it queries an + * online service for the TZ at the given coordinates. It + * requires an internet connection, though, and the distribution + * will need to have an account with geonames to not hit the + * daily query limit. + * + * See below, in MouseArea, for calling the right method. + */ + function getTzOnline() { var xhr = new XMLHttpRequest var latC = map.center.latitude var lonC = map.center.longitude @@ -73,16 +77,29 @@ Column { var responseJSON = JSON.parse(xhr.responseText) var tz2 = responseJSON.timezoneId - tzText.text = "Timezone: " + tz2 config.setCurrentLocation(tz2) } } + console.log("Online lookup", latC, lonC) // Needs to move to localeq.conf, each distribution will need their own account xhr.open("GET", "http://api.geonames.org/timezoneJSON?lat=" + latC + "&lng=" + lonC + "&username=SOME_USERNAME") xhr.send() } + /* This is a quick TZ lookup method: it uses the existing + * Calamares "closest TZ" code, which has lots of caveats. + * + * See below, in MouseArea, for calling the right method. + */ + function getTzOffline() { + var latC = map.center.latitude + var lonC = map.center.longitude + var tz = config.zonesModel.lookup(latC, lonC) + console.log("Offline lookup", latC, lonC) + config.setCurrentLocation(tz.region, tz.zone) + } + Rectangle { width: parent.width height: parent.height / 1.28 @@ -156,9 +173,8 @@ Column { map.center.latitude = coordinate.latitude map.center.longitude = coordinate.longitude - getTz(); - - console.log(coordinate.latitude, coordinate.longitude) + // Pick a TZ lookup method here (quick:offline, accurate:online) + getTzOffline(); } } } @@ -218,8 +234,7 @@ Column { Text { id: tzText - text: tzText.text - //text: qsTr("Timezone: %1").arg(timeZone) + text: qsTr("Timezone: %1").arg(config.currentTimezoneName) color: Kirigami.Theme.textColor anchors.centerIn: parent }