Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "QTMLocationProvider.h"
7 #include "nsGeoPosition.h"
9 using namespace mozilla;
11 NS_IMPL_ISUPPORTS(QTMLocationProvider, nsIGeolocationProvider)
13 QTMLocationProvider::QTMLocationProvider()
14 {
15 if (QMetaType::type("QGeoPositionInfo") == QMetaType::UnknownType) {
16 qRegisterMetaType<QGeoPositionInfo>("QGeoPositionInfo");
17 }
18 mLocation = QGeoPositionInfoSource::createDefaultSource(this);
19 if (mLocation)
20 connect(mLocation, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
21 }
23 QTMLocationProvider::~QTMLocationProvider()
24 {
25 delete mLocation;
26 }
28 void
29 QTMLocationProvider::positionUpdated(const QGeoPositionInfo &geoPosition)
30 {
31 if (!geoPosition.isValid()) {
32 NS_WARNING("Invalida geoposition received");
33 return;
34 }
36 QGeoCoordinate coord = geoPosition.coordinate();
37 double latitude = coord.latitude();
38 double longitude = coord.longitude();
39 double altitude = coord.altitude();
40 double accuracy = geoPosition.attribute(QGeoPositionInfo::HorizontalAccuracy);
41 double altitudeAccuracy = geoPosition.attribute(QGeoPositionInfo::VerticalAccuracy);
42 double heading = geoPosition.attribute(QGeoPositionInfo::Direction);
44 bool providesSpeed = geoPosition.hasAttribute(QGeoPositionInfo::GroundSpeed);
45 double speed = geoPosition.attribute(QGeoPositionInfo::GroundSpeed);
47 nsRefPtr<nsGeoPosition> p =
48 new nsGeoPosition(latitude, longitude,
49 altitude, accuracy,
50 altitudeAccuracy, heading,
51 speed, geoPosition.timestamp().toTime_t());
52 if (mCallback) {
53 mCallback->Update(p);
54 }
55 }
57 NS_IMETHODIMP
58 QTMLocationProvider::Startup()
59 {
60 if (!mLocation)
61 return NS_ERROR_NOT_IMPLEMENTED;
63 // Not all versions of qt5positioning set default prefered method
64 // thus this workaround initializing QGeoPositionSource explicitly
65 SetHighAccuracy(false);
66 mLocation->startUpdates();
68 return NS_OK;
69 }
71 NS_IMETHODIMP
72 QTMLocationProvider::Watch(nsIGeolocationUpdate* aCallback)
73 {
74 mCallback = aCallback;
76 return NS_OK;
77 }
79 NS_IMETHODIMP
80 QTMLocationProvider::Shutdown()
81 {
82 if (!mLocation)
83 return NS_ERROR_NOT_IMPLEMENTED;
85 mLocation->stopUpdates();
86 mCallback = nullptr;
88 return NS_OK;
89 }
91 NS_IMETHODIMP
92 QTMLocationProvider::SetHighAccuracy(bool aHigh)
93 {
94 if (!mLocation)
95 return NS_ERROR_NOT_IMPLEMENTED;
97 mLocation->setPreferredPositioningMethods(aHigh ?
98 QGeoPositionInfoSource::SatellitePositioningMethods :
99 QGeoPositionInfoSource::AllPositioningMethods);
100 return NS_OK;
101 }