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
40using namespace QtMobility;
41
42namespace WebCore {
43
44static const char failedToStartServiceErrorMessage[] = "Failed to start Geolocation service";
45
46GeolocationClientQt::GeolocationClientQt(const QWebPage* page)
47    : m_page(page)
48    , m_lastPosition(0)
49    , m_location(0)
50{
51}
52
53GeolocationClientQt::~GeolocationClientQt()
54{
55    delete m_location;
56}
57
58void GeolocationClientQt::geolocationDestroyed()
59{
60    delete this;
61}
62
63void GeolocationClientQt::positionUpdated(const QGeoPositionInfo &geoPosition)
64{
65    if (!geoPosition.isValid())
66        return;
67
68    QGeoCoordinate coord = geoPosition.coordinate();
69    double latitude = coord.latitude();
70    double longitude = coord.longitude();
71    bool providesAltitude = (geoPosition.coordinate().type() == QGeoCoordinate::Coordinate3D);
72    double altitude = coord.altitude();
73
74    double accuracy = geoPosition.attribute(QGeoPositionInfo::HorizontalAccuracy);
75
76    bool providesAltitudeAccuracy = geoPosition.hasAttribute(QGeoPositionInfo::VerticalAccuracy);
77    double altitudeAccuracy = geoPosition.attribute(QGeoPositionInfo::VerticalAccuracy);
78
79    bool providesHeading =  geoPosition.hasAttribute(QGeoPositionInfo::Direction);
80    double heading = geoPosition.attribute(QGeoPositionInfo::Direction);
81
82    bool providesSpeed = geoPosition.hasAttribute(QGeoPositionInfo::GroundSpeed);
83    double speed = geoPosition.attribute(QGeoPositionInfo::GroundSpeed);
84
85    double timeStampInSeconds = geoPosition.timestamp().toMSecsSinceEpoch() / 1000;
86
87    m_lastPosition = GeolocationPosition::create(timeStampInSeconds, latitude, longitude,
88                                                 accuracy, providesAltitude, altitude,
89                                                 providesAltitudeAccuracy, altitudeAccuracy,
90                                                 providesHeading, heading, providesSpeed, speed);
91
92    WebCore::Page* page = QWebPagePrivate::core(m_page);
93    page->geolocationController()->positionChanged(m_lastPosition.get());
94}
95
96void GeolocationClientQt::startUpdating()
97{
98    if (!m_location && (m_location = QGeoPositionInfoSource::createDefaultSource(this)))
99        connect(m_location, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
100
101    if (!m_location) {
102        WebCore::Page* page = QWebPagePrivate::core(m_page);
103        RefPtr<WebCore::GeolocationError> error = GeolocationError::create(GeolocationError::PositionUnavailable, failedToStartServiceErrorMessage);
104        page->geolocationController()->errorOccurred(error.get());
105        return;
106    }
107
108    m_location->startUpdates();
109}
110
111void GeolocationClientQt::stopUpdating()
112{
113    if (m_location)
114        m_location->stopUpdates();
115}
116
117
118void GeolocationClientQt::setEnableHighAccuracy(bool)
119{
120    // qtmobility 1.0 supports only GPS as of now so high accuracy is enabled by default
121}
122
123void GeolocationClientQt::requestPermission(Geolocation* geolocation)
124{
125    ASSERT(geolocation);
126    QWebFrame* webFrame = QWebFramePrivate::kit(geolocation->frame());
127    GeolocationPermissionClientQt::geolocationPermissionClient()->requestGeolocationPermissionForFrame(webFrame, geolocation);
128}
129
130void GeolocationClientQt::cancelPermissionRequest(Geolocation* geolocation)
131{
132    ASSERT(geolocation);
133    QWebFrame* webFrame = QWebFramePrivate::kit(geolocation->frame());
134    GeolocationPermissionClientQt::geolocationPermissionClient()->cancelGeolocationPermissionRequestForFrame(webFrame, geolocation);
135}
136
137} // namespace WebCore
138
139#include "moc_GeolocationClientQt.cpp"
140