[Qt] Simplify QT_VERSION_CHECKS for Qt5 by introducing HAVE(QT5)
[WebKit-https.git] / Source / WebKit / qt / WebCoreSupport / GeolocationClientQt.cpp
1 /*
2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "GeolocationClientQt.h"
28
29 #include "Geolocation.h"
30 #include "GeolocationController.h"
31 #include "GeolocationError.h"
32 #include "GeolocationPermissionClientQt.h"
33 #include "GeolocationPosition.h"
34 #include "Page.h"
35 #include "qwebframe.h"
36 #include "qwebframe_p.h"
37 #include "qwebpage.h"
38 #include "qwebpage_p.h"
39
40 #if !HAVE(QT5)
41 #include <QGeoPositionInfoSource>
42 #elif HAVE(QT5)
43 #include <QtLocation/QGeoPositionInfoSource>
44 #endif
45
46 namespace WebCore {
47
48 static const char failedToStartServiceErrorMessage[] = "Failed to start Geolocation service";
49
50 GeolocationClientQt::GeolocationClientQt(const QWebPage* page)
51     : m_page(page)
52     , m_lastPosition(0)
53     , m_location(0)
54 {
55 }
56
57 GeolocationClientQt::~GeolocationClientQt()
58 {
59     delete m_location;
60 }
61
62 void GeolocationClientQt::geolocationDestroyed()
63 {
64     delete this;
65 }
66
67 void GeolocationClientQt::positionUpdated(const QGeoPositionInfo& geoPosition)
68 {
69     if (!geoPosition.isValid())
70         return;
71
72     QGeoCoordinate coord = geoPosition.coordinate();
73     double latitude = coord.latitude();
74     double longitude = coord.longitude();
75     bool providesAltitude = (geoPosition.coordinate().type() == QGeoCoordinate::Coordinate3D);
76     double altitude = coord.altitude();
77
78     double accuracy = geoPosition.attribute(QGeoPositionInfo::HorizontalAccuracy);
79
80     bool providesAltitudeAccuracy = geoPosition.hasAttribute(QGeoPositionInfo::VerticalAccuracy);
81     double altitudeAccuracy = geoPosition.attribute(QGeoPositionInfo::VerticalAccuracy);
82
83     bool providesHeading =  geoPosition.hasAttribute(QGeoPositionInfo::Direction);
84     double heading = geoPosition.attribute(QGeoPositionInfo::Direction);
85
86     bool providesSpeed = geoPosition.hasAttribute(QGeoPositionInfo::GroundSpeed);
87     double speed = geoPosition.attribute(QGeoPositionInfo::GroundSpeed);
88
89     double timeStampInSeconds = geoPosition.timestamp().toMSecsSinceEpoch() / 1000;
90
91     m_lastPosition = GeolocationPosition::create(timeStampInSeconds, latitude, longitude,
92                                                  accuracy, providesAltitude, altitude,
93                                                  providesAltitudeAccuracy, altitudeAccuracy,
94                                                  providesHeading, heading, providesSpeed, speed);
95
96     WebCore::Page* page = QWebPagePrivate::core(m_page);
97     GeolocationController::from(page)->positionChanged(m_lastPosition.get());
98 }
99
100 void GeolocationClientQt::startUpdating()
101 {
102     if (!m_location && (m_location = QGeoPositionInfoSource::createDefaultSource(this)))
103         connect(m_location, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
104
105     if (!m_location) {
106         WebCore::Page* page = QWebPagePrivate::core(m_page);
107         RefPtr<WebCore::GeolocationError> error = GeolocationError::create(GeolocationError::PositionUnavailable, failedToStartServiceErrorMessage);
108         GeolocationController::from(page)->errorOccurred(error.get());
109         return;
110     }
111
112     m_location->startUpdates();
113 }
114
115 void GeolocationClientQt::stopUpdating()
116 {
117     if (m_location)
118         m_location->stopUpdates();
119 }
120
121
122 void GeolocationClientQt::setEnableHighAccuracy(bool)
123 {
124     // qtmobility 1.0 supports only GPS as of now so high accuracy is enabled by default
125 }
126
127 void GeolocationClientQt::requestPermission(Geolocation* geolocation)
128 {
129     ASSERT(geolocation);
130     QWebFrame* webFrame = QWebFramePrivate::kit(geolocation->frame());
131     GeolocationPermissionClientQt::geolocationPermissionClient()->requestGeolocationPermissionForFrame(webFrame, geolocation);
132 }
133
134 void GeolocationClientQt::cancelPermissionRequest(Geolocation* geolocation)
135 {
136     ASSERT(geolocation);
137     QWebFrame* webFrame = QWebFramePrivate::kit(geolocation->frame());
138     GeolocationPermissionClientQt::geolocationPermissionClient()->cancelGeolocationPermissionRequestForFrame(webFrame, geolocation);
139 }
140
141 } // namespace WebCore
142
143 #include "moc_GeolocationClientQt.cpp"