GeolocationClientQt.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "config.h"
  26. #include "GeolocationClientQt.h"
  27. #include "Geolocation.h"
  28. #include "GeolocationController.h"
  29. #include "GeolocationError.h"
  30. #include "GeolocationPermissionClientQt.h"
  31. #include "GeolocationPosition.h"
  32. #include "HTMLFormElement.h"
  33. #include "Page.h"
  34. #include "QWebFrameAdapter.h"
  35. #include "QWebPageAdapter.h"
  36. #include <QtLocation/QGeoPositionInfoSource>
  37. namespace WebCore {
  38. static const char failedToStartServiceErrorMessage[] = "Failed to start Geolocation service";
  39. GeolocationClientQt::GeolocationClientQt(const QWebPageAdapter* page)
  40. : m_webPage(page)
  41. , m_lastPosition(0)
  42. , m_location(0)
  43. {
  44. }
  45. GeolocationClientQt::~GeolocationClientQt()
  46. {
  47. delete m_location;
  48. }
  49. void GeolocationClientQt::geolocationDestroyed()
  50. {
  51. delete this;
  52. }
  53. void GeolocationClientQt::positionUpdated(const QGeoPositionInfo& geoPosition)
  54. {
  55. if (!geoPosition.isValid())
  56. return;
  57. QGeoCoordinate coord = geoPosition.coordinate();
  58. double latitude = coord.latitude();
  59. double longitude = coord.longitude();
  60. bool providesAltitude = (geoPosition.coordinate().type() == QGeoCoordinate::Coordinate3D);
  61. double altitude = coord.altitude();
  62. double accuracy = geoPosition.attribute(QGeoPositionInfo::HorizontalAccuracy);
  63. bool providesAltitudeAccuracy = geoPosition.hasAttribute(QGeoPositionInfo::VerticalAccuracy);
  64. double altitudeAccuracy = geoPosition.attribute(QGeoPositionInfo::VerticalAccuracy);
  65. bool providesHeading = geoPosition.hasAttribute(QGeoPositionInfo::Direction);
  66. double heading = geoPosition.attribute(QGeoPositionInfo::Direction);
  67. bool providesSpeed = geoPosition.hasAttribute(QGeoPositionInfo::GroundSpeed);
  68. double speed = geoPosition.attribute(QGeoPositionInfo::GroundSpeed);
  69. double timeStampInSeconds = geoPosition.timestamp().toMSecsSinceEpoch() / 1000;
  70. m_lastPosition = GeolocationPosition::create(timeStampInSeconds, latitude, longitude, accuracy, providesAltitude, altitude, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed);
  71. WebCore::Page* page = m_webPage->page;
  72. GeolocationController::from(page)->positionChanged(m_lastPosition.get());
  73. }
  74. void GeolocationClientQt::startUpdating()
  75. {
  76. if (!m_location && (m_location = QGeoPositionInfoSource::createDefaultSource(this)))
  77. connect(m_location, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
  78. if (!m_location) {
  79. WebCore::Page* page = m_webPage->page;
  80. RefPtr<WebCore::GeolocationError> error = GeolocationError::create(GeolocationError::PositionUnavailable, failedToStartServiceErrorMessage);
  81. GeolocationController::from(page)->errorOccurred(error.get());
  82. return;
  83. }
  84. m_location->startUpdates();
  85. }
  86. void GeolocationClientQt::stopUpdating()
  87. {
  88. if (m_location)
  89. m_location->stopUpdates();
  90. }
  91. void GeolocationClientQt::setEnableHighAccuracy(bool)
  92. {
  93. // qtmobility 1.0 supports only GPS as of now so high accuracy is enabled by default
  94. }
  95. void GeolocationClientQt::requestPermission(Geolocation* geolocation)
  96. {
  97. ASSERT(geolocation);
  98. QWebFrameAdapter* webFrame = QWebFrameAdapter::kit(geolocation->frame());
  99. GeolocationPermissionClientQt::geolocationPermissionClient()->requestGeolocationPermissionForFrame(webFrame, geolocation);
  100. }
  101. void GeolocationClientQt::cancelPermissionRequest(Geolocation* geolocation)
  102. {
  103. ASSERT(geolocation);
  104. QWebFrameAdapter* webFrame = QWebFrameAdapter::kit(geolocation->frame());
  105. GeolocationPermissionClientQt::geolocationPermissionClient()->cancelGeolocationPermissionRequestForFrame(webFrame, geolocation);
  106. }
  107. } // namespace WebCore
  108. #include "moc_GeolocationClientQt.cpp"