michael@0: /* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "QTMLocationProvider.h" michael@0: #include "nsGeoPosition.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: NS_IMPL_ISUPPORTS(QTMLocationProvider, nsIGeolocationProvider) michael@0: michael@0: QTMLocationProvider::QTMLocationProvider() michael@0: { michael@0: if (QMetaType::type("QGeoPositionInfo") == QMetaType::UnknownType) { michael@0: qRegisterMetaType("QGeoPositionInfo"); michael@0: } michael@0: mLocation = QGeoPositionInfoSource::createDefaultSource(this); michael@0: if (mLocation) michael@0: connect(mLocation, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo))); michael@0: } michael@0: michael@0: QTMLocationProvider::~QTMLocationProvider() michael@0: { michael@0: delete mLocation; michael@0: } michael@0: michael@0: void michael@0: QTMLocationProvider::positionUpdated(const QGeoPositionInfo &geoPosition) michael@0: { michael@0: if (!geoPosition.isValid()) { michael@0: NS_WARNING("Invalida geoposition received"); michael@0: return; michael@0: } michael@0: michael@0: QGeoCoordinate coord = geoPosition.coordinate(); michael@0: double latitude = coord.latitude(); michael@0: double longitude = coord.longitude(); michael@0: double altitude = coord.altitude(); michael@0: double accuracy = geoPosition.attribute(QGeoPositionInfo::HorizontalAccuracy); michael@0: double altitudeAccuracy = geoPosition.attribute(QGeoPositionInfo::VerticalAccuracy); michael@0: double heading = geoPosition.attribute(QGeoPositionInfo::Direction); michael@0: michael@0: bool providesSpeed = geoPosition.hasAttribute(QGeoPositionInfo::GroundSpeed); michael@0: double speed = geoPosition.attribute(QGeoPositionInfo::GroundSpeed); michael@0: michael@0: nsRefPtr p = michael@0: new nsGeoPosition(latitude, longitude, michael@0: altitude, accuracy, michael@0: altitudeAccuracy, heading, michael@0: speed, geoPosition.timestamp().toTime_t()); michael@0: if (mCallback) { michael@0: mCallback->Update(p); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: QTMLocationProvider::Startup() michael@0: { michael@0: if (!mLocation) michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: michael@0: // Not all versions of qt5positioning set default prefered method michael@0: // thus this workaround initializing QGeoPositionSource explicitly michael@0: SetHighAccuracy(false); michael@0: mLocation->startUpdates(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: QTMLocationProvider::Watch(nsIGeolocationUpdate* aCallback) michael@0: { michael@0: mCallback = aCallback; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: QTMLocationProvider::Shutdown() michael@0: { michael@0: if (!mLocation) michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: michael@0: mLocation->stopUpdates(); michael@0: mCallback = nullptr; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: QTMLocationProvider::SetHighAccuracy(bool aHigh) michael@0: { michael@0: if (!mLocation) michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: michael@0: mLocation->setPreferredPositioningMethods(aHigh ? michael@0: QGeoPositionInfoSource::SatellitePositioningMethods : michael@0: QGeoPositionInfoSource::AllPositioningMethods); michael@0: return NS_OK; michael@0: }