WebGeolocationProviderQt.cpp [plain text]
#include "config.h"
#include "WebGeolocationProviderQt.h"
#if ENABLE(GEOLOCATION)
#include <QtLocation/QGeoPositionInfoSource>
namespace WebKit {
static inline const WebGeolocationProviderQt* toLocationProvider(const void* clientInfo)
{
return static_cast<const WebGeolocationProviderQt*>(clientInfo);
}
static void locationStartUpdating(WKGeolocationManagerRef geolocationManager, const void* clientInfo)
{
toLocationProvider(clientInfo)->startUpdating();
}
static void locationStopUpdating(WKGeolocationManagerRef geolocationManager, const void* clientInfo)
{
toLocationProvider(clientInfo)->stopUpdating();
}
WebGeolocationProviderQt* WebGeolocationProviderQt::create(WKGeolocationManagerRef manager)
{
return new WebGeolocationProviderQt(manager);
}
WKGeolocationProvider* WebGeolocationProviderQt::provider(const WebGeolocationProviderQt* location)
{
static WKGeolocationProvider provider = {
0, location, locationStartUpdating, locationStopUpdating
};
return &provider;
}
WebGeolocationProviderQt::WebGeolocationProviderQt(WKGeolocationManagerRef manager)
: m_manager(manager)
, m_source(0)
{
}
WebGeolocationProviderQt::~WebGeolocationProviderQt()
{
}
void WebGeolocationProviderQt::updateTimeout()
{
WKGeolocationManagerProviderDidFailToDeterminePosition(m_manager);
}
void WebGeolocationProviderQt::positionUpdated(const QGeoPositionInfo& geoPosition)
{
if (!geoPosition.isValid())
return;
QGeoCoordinate coord = geoPosition.coordinate();
double latitude = coord.latitude();
double longitude = coord.longitude();
double accuracy = geoPosition.attribute(QGeoPositionInfo::HorizontalAccuracy);
double timeStampInSeconds = geoPosition.timestamp().toMSecsSinceEpoch() / 1000;
m_lastPosition.adopt(WKGeolocationPositionCreate(timeStampInSeconds, latitude, longitude, accuracy));
WKGeolocationManagerProviderDidChangePosition(m_manager, m_lastPosition.get());
}
void WebGeolocationProviderQt::startUpdating() const
{
if (!m_source) {
if (!(m_source = QGeoPositionInfoSource::createDefaultSource(const_cast<WebGeolocationProviderQt*>(this)))) {
WKGeolocationManagerSetProvider(m_manager, 0);
WKGeolocationManagerProviderDidFailToDeterminePosition(m_manager);
return;
}
connect(m_source, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
connect(m_source, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
}
m_source->startUpdates();
}
void WebGeolocationProviderQt::stopUpdating() const
{
if (m_source)
m_source->stopUpdates();
}
}
#include "moc_WebGeolocationProviderQt.cpp"
#endif // ENABLE(GEOLOCATION)