1/*
2 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include "config.h"
22#include "WebGeolocationProviderQt.h"
23
24#if ENABLE(GEOLOCATION) && HAVE(QTLOCATION)
25#include <QtLocation/QGeoPositionInfoSource>
26
27namespace WebKit {
28
29static inline const WebGeolocationProviderQt* toLocationProvider(const void* clientInfo)
30{
31    return static_cast<const WebGeolocationProviderQt*>(clientInfo);
32}
33
34static void locationStartUpdating(WKGeolocationManagerRef geolocationManager, const void* clientInfo)
35{
36    toLocationProvider(clientInfo)->startUpdating();
37}
38
39static void locationStopUpdating(WKGeolocationManagerRef geolocationManager, const void* clientInfo)
40{
41    toLocationProvider(clientInfo)->stopUpdating();
42}
43
44WebGeolocationProviderQt* WebGeolocationProviderQt::create(WKGeolocationManagerRef manager)
45{
46    return new WebGeolocationProviderQt(manager);
47}
48
49WKGeolocationProvider* WebGeolocationProviderQt::provider(const WebGeolocationProviderQt* location)
50{
51    static WKGeolocationProvider provider = {
52        0, // This features the version.
53        location, // This points to the object implementer.
54        locationStartUpdating, // The callbacks are next.
55        locationStopUpdating
56    };
57
58    return &provider;
59}
60
61WebGeolocationProviderQt::WebGeolocationProviderQt(WKGeolocationManagerRef manager)
62    : m_manager(manager)
63    , m_source(0)
64{
65}
66
67WebGeolocationProviderQt::~WebGeolocationProviderQt()
68{
69}
70
71void WebGeolocationProviderQt::updateTimeout()
72{
73    WKGeolocationManagerProviderDidFailToDeterminePosition(m_manager);
74}
75
76void WebGeolocationProviderQt::positionUpdated(const QGeoPositionInfo& geoPosition)
77{
78    if (!geoPosition.isValid())
79        return;
80
81    QGeoCoordinate coord = geoPosition.coordinate();
82    double latitude = coord.latitude();
83    double longitude = coord.longitude();
84    double accuracy = geoPosition.attribute(QGeoPositionInfo::HorizontalAccuracy);
85    double timeStampInSeconds = geoPosition.timestamp().toMSecsSinceEpoch() / 1000;
86
87    m_lastPosition.adopt(WKGeolocationPositionCreate(timeStampInSeconds, latitude, longitude, accuracy));
88
89    WKGeolocationManagerProviderDidChangePosition(m_manager, m_lastPosition.get());
90}
91
92void WebGeolocationProviderQt::startUpdating() const
93{
94    if (!m_source) {
95        if (!(m_source = QGeoPositionInfoSource::createDefaultSource(const_cast<WebGeolocationProviderQt*>(this)))) {
96            // Let the manager known that the provider is not available.
97            WKGeolocationManagerSetProvider(m_manager, 0);
98            // Notify failure at retrieving the position.
99            WKGeolocationManagerProviderDidFailToDeterminePosition(m_manager);
100            return;
101        }
102
103        connect(m_source, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
104        connect(m_source, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
105    }
106
107    m_source->startUpdates();
108}
109
110void WebGeolocationProviderQt::stopUpdating() const
111{
112    if (m_source)
113        m_source->stopUpdates();
114}
115
116} // namespace WebKit
117
118#include "moc_WebGeolocationProviderQt.cpp"
119
120#endif // ENABLE(GEOLOCATION)
121