|
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/. */ |
|
5 |
|
6 #include "QTMLocationProvider.h" |
|
7 #include "nsGeoPosition.h" |
|
8 |
|
9 using namespace mozilla; |
|
10 |
|
11 NS_IMPL_ISUPPORTS(QTMLocationProvider, nsIGeolocationProvider) |
|
12 |
|
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 } |
|
22 |
|
23 QTMLocationProvider::~QTMLocationProvider() |
|
24 { |
|
25 delete mLocation; |
|
26 } |
|
27 |
|
28 void |
|
29 QTMLocationProvider::positionUpdated(const QGeoPositionInfo &geoPosition) |
|
30 { |
|
31 if (!geoPosition.isValid()) { |
|
32 NS_WARNING("Invalida geoposition received"); |
|
33 return; |
|
34 } |
|
35 |
|
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); |
|
43 |
|
44 bool providesSpeed = geoPosition.hasAttribute(QGeoPositionInfo::GroundSpeed); |
|
45 double speed = geoPosition.attribute(QGeoPositionInfo::GroundSpeed); |
|
46 |
|
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 } |
|
56 |
|
57 NS_IMETHODIMP |
|
58 QTMLocationProvider::Startup() |
|
59 { |
|
60 if (!mLocation) |
|
61 return NS_ERROR_NOT_IMPLEMENTED; |
|
62 |
|
63 // Not all versions of qt5positioning set default prefered method |
|
64 // thus this workaround initializing QGeoPositionSource explicitly |
|
65 SetHighAccuracy(false); |
|
66 mLocation->startUpdates(); |
|
67 |
|
68 return NS_OK; |
|
69 } |
|
70 |
|
71 NS_IMETHODIMP |
|
72 QTMLocationProvider::Watch(nsIGeolocationUpdate* aCallback) |
|
73 { |
|
74 mCallback = aCallback; |
|
75 |
|
76 return NS_OK; |
|
77 } |
|
78 |
|
79 NS_IMETHODIMP |
|
80 QTMLocationProvider::Shutdown() |
|
81 { |
|
82 if (!mLocation) |
|
83 return NS_ERROR_NOT_IMPLEMENTED; |
|
84 |
|
85 mLocation->stopUpdates(); |
|
86 mCallback = nullptr; |
|
87 |
|
88 return NS_OK; |
|
89 } |
|
90 |
|
91 NS_IMETHODIMP |
|
92 QTMLocationProvider::SetHighAccuracy(bool aHigh) |
|
93 { |
|
94 if (!mLocation) |
|
95 return NS_ERROR_NOT_IMPLEMENTED; |
|
96 |
|
97 mLocation->setPreferredPositioningMethods(aHigh ? |
|
98 QGeoPositionInfoSource::SatellitePositioningMethods : |
|
99 QGeoPositionInfoSource::AllPositioningMethods); |
|
100 return NS_OK; |
|
101 } |