From 421c0d139ccb223c43c2b785eff1bf501dc90c9a Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Wed, 29 Mar 2023 12:47:29 -0400
Subject: [PATCH] positioning: handle cases where no default source is
 available

In this case we simply don't connect to a null object pointer.

This was causing invalid connections during test setup.

Gitlab: #899
Change-Id: I1c38c9338451f6817444576b95809c19bb28e3ff
---
 src/app/positioning.cpp | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/src/app/positioning.cpp b/src/app/positioning.cpp
index 61e1fbc27..100bc9f68 100644
--- a/src/app/positioning.cpp
+++ b/src/app/positioning.cpp
@@ -27,13 +27,23 @@ Positioning::Positioning(QObject* parent)
     source_ = QGeoPositionInfoSource::createDefaultSource(this);
     timer_ = new QTimer(this);
     connect(timer_, &QTimer::timeout, this, &Positioning::requestPosition);
-    connect(source_, &QGeoPositionInfoSource::errorOccurred, this, &Positioning::slotError);
-    connect(source_, &QGeoPositionInfoSource::positionUpdated, this, &Positioning::positionUpdated);
-    // if location services are activated, positioning will be activated automatically
-    connect(source_,
-            &QGeoPositionInfoSource::supportedPositioningMethodsChanged,
-            this,
-            &Positioning::locationServicesActivated);
+
+    // There are several reasons QGeoPositionInfoSource::createDefaultSource may return
+    // null. For example, if the device has no geolocation providers, or if the device has no
+    // location services activated. This seems to be the case for our QML testing fixture. Ideally,
+    // we would like to listen to system signals to know when location services are activated.
+    if (source_) {
+        connect(source_, &QGeoPositionInfoSource::errorOccurred, this, &Positioning::slotError);
+        connect(source_,
+                &QGeoPositionInfoSource::positionUpdated,
+                this,
+                &Positioning::positionUpdated);
+        // If location services are activated, positioning will be activated automatically.
+        connect(source_,
+                &QGeoPositionInfoSource::supportedPositioningMethodsChanged,
+                this,
+                &Positioning::locationServicesActivated);
+    }
 }
 
 void
-- 
GitLab