From c69bd972e9f9b96fe1028f164e2a507d7c0b8f99 Mon Sep 17 00:00:00 2001
From: Adriaan de Groot <groot@kde.org>
Date: Thu, 6 Aug 2020 16:05:58 +0200
Subject: [PATCH] [localeq] Demonstrate "offline" lookups

- we can do GeoIP and GeoNames lookups, **or**
- use Calamares's internal GeoIP lookup and country / city hints.

The online version is much more accurate, but costs more lookups;
in these examples, set it all to "offline" and document what needs
to change (code edit) to use the online version.

It's probably a good beginner job to introduce a bool in localeq.qml
to switch the behaviors.
---
 src/modules/localeq/Map.qml     | 23 +++++++++++++++++++++--
 src/modules/localeq/localeq.qml |  8 ++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/src/modules/localeq/Map.qml b/src/modules/localeq/Map.qml
index 06b4f7a1f..d414825dd 100644
--- a/src/modules/localeq/Map.qml
+++ b/src/modules/localeq/Map.qml
@@ -36,7 +36,12 @@ Column {
     property var cityName: ""
     property var countryName: ""
 
-    function getIp() {
+    /* This is an extra GeoIP lookup, which will find better-accuracy
+     * location data for the user's IP, and then sets the current timezone
+     * and map location. Call it from Component.onCompleted so that
+     * it happens "on time" before the page is shown.
+     */
+    function getIpOnline() {
         var xhr = new XMLHttpRequest
 
         xhr.onreadystatechange = function() {
@@ -59,6 +64,16 @@ Column {
         xhr.send()
     }
 
+    /* This is an "offline" GeoIP lookup -- it just follows what
+     * Calamares itself has figured out with its GeoIP or configuration.
+     * Call it from the **Component** onActivate() -- in localeq.qml --
+     * so it happens as the page is shown.
+     */
+    function getIpOffline() {
+        cityName = config.currentLocation.zone
+        countryName = config.currentLocation.countryCode
+    }
+
     /* 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
@@ -239,7 +254,11 @@ Column {
                     anchors.centerIn: parent
                 }
 
-                Component.onCompleted: getIp();
+                /* If you want an extra (and accurate) GeoIP lookup,
+                 * enable this one and disable the offline lookup in
+                 * onActivate().
+                Component.onCompleted: getIpOnline();
+                 */
             }
         }
 
diff --git a/src/modules/localeq/localeq.qml b/src/modules/localeq/localeq.qml
index 49719db65..1a250f5c1 100644
--- a/src/modules/localeq/localeq.qml
+++ b/src/modules/localeq/localeq.qml
@@ -29,6 +29,14 @@ Page {
     width: 800
     height: 550
 
+    function onActivate() {
+        /* If you want the map to follow Calamares's GeoIP
+         * lookup or configuration, call the update function
+         * here, and disable the one at onCompleted in Map.qml.
+         */
+        if (Network.hasInternet) { image.item.getIpOffline() }
+    }
+
     Loader {
         id: image
         anchors.horizontalCenter: parent.horizontalCenter