Added GeoPositionInfo and LiblocationWrapper classes.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 3 Jun 2010 12:56:52 +0000 (15:56 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 3 Jun 2010 12:56:52 +0000 (15:56 +0300)
src/gps/geopositioninfo.cpp [new file with mode: 0644]
src/gps/geopositioninfo.h [new file with mode: 0644]
src/gps/gpscommon.h
src/gps/gpspositionprivate.cpp
src/gps/gpspositionprivate.h
src/gps/liblocationwrapper.cpp [new file with mode: 0644]
src/gps/liblocationwrapper.h [new file with mode: 0644]
src/src.pro

diff --git a/src/gps/geopositioninfo.cpp b/src/gps/geopositioninfo.cpp
new file mode 100644 (file)
index 0000000..96c5544
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Jussi Laitinen - jussi.laitinen@ixonos.com
+
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
+
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
+
+#include <QDateTime>
+
+#include "geopositioninfo.h"
+#include "gpscommon.h"
+
+GeoPositionInfo::GeoPositionInfo()
+{
+}
+
+void GeoPositionInfo::setLatitude(qreal latitude)
+{
+    m_latitude = latitude;
+}
+
+qreal GeoPositionInfo::latitude() const
+{
+    return m_latitude;
+}
+void GeoPositionInfo::setLongitude(qreal longitude)
+{
+    m_longitude = longitude;
+}
+
+qreal GeoPositionInfo::longitude() const
+{
+    return m_longitude;
+}
+
+void GeoPositionInfo::setTimestamp(qreal time)
+{
+    m_timestamp = QDateTime::fromTime_t(time);
+}
+
+QDateTime GeoPositionInfo::timestamp() const
+{
+    return m_timestamp;
+}
+
+bool GeoPositionInfo::isValid() const
+{
+    if ((latitude >= MIN_LATITUDE) && (latitude < MAX_LATITUDE) &&
+        (longitude >= MIN_LONGITUDE) && (longitude < MAX_LONGITUDE))
+
+        return true;
+    else
+        return false;
+}
+
+void GeoPositionInfo::setAttribute(Attribute attribute, qreal value)
+{
+    if (hasAttribute(attribute))
+        m_horizontalAccuracy = value;
+}
+
+qreal GeoPositionInfo::attribute(Attribute attribute)
+{
+    if (attribute == HorizontalAccuracy)
+        return m_horizontalAccuracy;
+    else
+        return GPS_ACCURACY_UNDEFINED;
+}
+
+bool GeoPositionInfo::hasAttribute(Attribute attribute)
+{
+    if (attribute == HorizontalAccuracy)
+        return true;
+    else
+        return false;
+}
diff --git a/src/gps/geopositioninfo.h b/src/gps/geopositioninfo.h
new file mode 100644 (file)
index 0000000..9718f52
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Jussi Laitinen - jussi.laitinen@ixonos.com
+
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
+
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
+
+#ifndef GEOPOSITIONINFO_H
+#define GEOPOSITIONINFO_H
+
+#include <QObject>
+#include <QDateTime>
+
+class GeoPositionInfo
+{
+public:
+    GeoPositionInfo();
+
+    enum Attribute {HorizontalAccuracy};
+
+    void setLatitude(qreal latitude);
+    qreal latitude() const;
+    void setLongitude(qreal longitude);
+    qreal longitude() const;
+    void setTimestamp(qreal time);
+    QDateTime timestamp() const;
+    bool isValid() const;
+    void setAttribute(Attribute attribute, qreal value);
+    qreal attribute(Attribute attribute);
+    bool hasAttribute(Attribute attribute);
+
+private:
+    QDateTime m_timestamp;
+    qreal m_latitude;
+    qreal m_longitude;
+};
+
+#endif // GEOPOSITIONINFO_H
index a7380cc..40b54da 100644 (file)
 #ifndef GPSCOMMON_H
 #define GPSCOMMON_H
 
+#include <QObject>
+
 const int GPS_ACCURACY_UNDEFINED = -1; ///< Value used when accuracy is undefined
 
+const qreal MAX_LATITUDE = 85.05112877980659237802;  ///< Maximum latitude value
+const qreal MIN_LATITUDE = -MAX_LATITUDE; ///< Minimum latitude value
+const qreal MIN_LONGITUDE = -180.0;  ///< Minimum longitude value
+const qreal MAX_LONGITUDE = 180.0;  ///< Maximum longitude value
+
 #endif // GPSCOMMON_H
index e16d34c..e194f56 100644 (file)
@@ -28,6 +28,7 @@
 #include "gpscommon.h"
 #include "gpsposition.h"
 #include "gpspositionprivate.h"
+#include "liblocationwrapper.h"
 
 GPSPositionPrivate::GPSPositionPrivate(QObject *parent)
     : QObject(parent),
@@ -50,7 +51,7 @@ void GPSPositionPrivate::setMode(GPSPosition::Mode mode, const QString &filePath
     }
 
     if (mode == GPSPosition::Default) {
-        m_gpsSource = QGeoPositionInfoSource::createDefaultSource(this);
+        m_gpsSource = new LiblocationWrapper(this);
 
         if (!m_gpsSource) {
             emit m_parent->error(tr("Unable to use GPS"));
@@ -58,18 +59,18 @@ void GPSPositionPrivate::setMode(GPSPosition::Mode mode, const QString &filePath
         }
     }
     else if (mode == GPSPosition::Simulation) {
-        QNmeaPositionInfoSource *nmeaSource = new QNmeaPositionInfoSource(
-                QNmeaPositionInfoSource::SimulationMode, this);
-        QFile *logFile = new QFile(filePath, this);
+//        QNmeaPositionInfoSource *nmeaSource = new QNmeaPositionInfoSource(
+//                QNmeaPositionInfoSource::SimulationMode, this);
+//        QFile *logFile = new QFile(filePath, this);
 
-        nmeaSource->setDevice(logFile);
-        m_gpsSource = nmeaSource;
+//        nmeaSource->setDevice(logFile);
+//        m_gpsSource = nmeaSource;
     }
 
     if (m_gpsSource) {
-        connect(m_gpsSource, SIGNAL(positionUpdated(const QGeoPositionInfo &)),
-                this, SLOT(positionUpdated(const QGeoPositionInfo &)));
-        connect(m_gpsSource, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
+        connect(m_gpsSource, SIGNAL(positionUpdated(const GeoPositionInfo &)),
+                this, SLOT(positionUpdated(const GeoPositionInfo &)));
+        //connect(m_gpsSource, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
 
         m_gpsSource->setUpdateInterval(m_updateInterval);
     }
@@ -104,29 +105,30 @@ bool GPSPositionPrivate::isRunning()
 
 QPointF GPSPositionPrivate::lastPosition()
 {
-    QGeoCoordinate coordinate = m_gpsSource->lastKnownPosition().coordinate();
-    return QPointF(coordinate.longitude(), coordinate.latitude());
+    GeoPositionInfo positionInfo = m_gpsSource->lastKnownPosition();
+
+    return QPointF(positionInfo.longitude(), positionInfo.latitude());
 }
 
 void GPSPositionPrivate::requestLastPosition()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QGeoCoordinate coordinate = m_gpsSource->lastKnownPosition().coordinate();
+    GeoPositionInfo positionInfo = m_gpsSource->lastKnownPosition();
 
-    if (coordinate.isValid()) {
-        emit m_parent->position(QPointF(coordinate.longitude(), coordinate.latitude()),
+    if (positionInfo.isValid()) {
+        emit m_parent->position(QPointF(positionInfo.longitude(), positionInfo.latitude()),
                       accuracy(m_gpsSource->lastKnownPosition()));
     }
 }
 
-void GPSPositionPrivate::positionUpdated(const QGeoPositionInfo &positionInfo)
+void GPSPositionPrivate::positionUpdated(const GeoPositionInfo &positionInfo)
 {
     qDebug() << __PRETTY_FUNCTION__ << positionInfo;
 
     if (positionInfo.coordinate().isValid()) {
-        emit m_parent->position(QPointF(positionInfo.coordinate().longitude(),
-                              positionInfo.coordinate().latitude()),
+        emit m_parent->position(QPointF(positionInfo.longitude(),
+                              positionInfo.latitude()),
                       accuracy(positionInfo));
     }
 }
@@ -148,17 +150,15 @@ void GPSPositionPrivate::setUpdateInterval(int interval)
     }
 }
 
-qreal GPSPositionPrivate::accuracy(const QGeoPositionInfo &positionInfo)
+qreal GPSPositionPrivate::accuracy(const GeoPositionInfo &positionInfo)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QDateTime timestamp;
-
-    if (!positionInfo.dateTime().isValid())
+    if (!positionInfo.timestamp().isValid())
         return GPS_ACCURACY_UNDEFINED;
 
-    if (positionInfo.hasAttribute(QGeoPositionInfo::HorizontalAccuracy))
-        return positionInfo.attribute(QGeoPositionInfo::HorizontalAccuracy);
+    if (positionInfo.hasAttribute(GeoPositionInfo::HorizontalAccuracy))
+        return positionInfo.attribute(GeoPositionInfo::HorizontalAccuracy);
     else
         return GPS_ACCURACY_UNDEFINED;
 }
index c0b0bb5..e63720d 100644 (file)
 
 #include <QPointF>
 
-#include <QGeoPositionInfo>
-#include <QGeoPositionInfoSource>
-#include <QNmeaPositionInfoSource>
+//#include <QGeoPositionInfo>
+//#include <QGeoPositionInfoSource>
+//#include <QNmeaPositionInfoSource>
 
 #include "gpsposition.h"
 
-QTM_USE_NAMESPACE
+//QTM_USE_NAMESPACE
+
+#include "geopositioninfo.h"
+
+class LiblocationWrapper;
 
 /**
 * @brief GPSPositionPrivate class use GPS to receive location information.
@@ -102,7 +106,7 @@ private:
     * @return accuracy value, -1 if undefined. Returns -1 also is timestamp is not valid
     *         (when using network positioning)
     */
-    qreal accuracy(const QGeoPositionInfo &positionInfo);
+    qreal accuracy(const GeoPositionInfo &positionInfo);
 
 private slots:
 
@@ -111,7 +115,7 @@ private slots:
     *
     * @param positionInfo Geo position info.
     */
-    void positionUpdated(const QGeoPositionInfo &positionInfo);
+    void positionUpdated(const GeoPositionInfo &positionInfo);
 
     /**
     * @brief Slot for update timeout.
@@ -124,7 +128,8 @@ private slots:
  * DATA MEMBERS
  ******************************************************************************/
 private:
-    QGeoPositionInfoSource *m_gpsSource;        ///< GPS position info source
+    //QGeoPositionInfoSource *m_gpsSource;        ///< GPS position info source
+    LiblocationWrapper  *m_gpsSource;            ///< GPS position info source
     GPSPosition *m_parent;                      ///< Parent object
     bool m_running;                             ///< GPS is running
     int m_updateInterval;                       ///< GPS update interval
diff --git a/src/gps/liblocationwrapper.cpp b/src/gps/liblocationwrapper.cpp
new file mode 100644 (file)
index 0000000..6918b6f
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Jussi Laitinen - jussi.laitinen@ixonos.com
+
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
+
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
+
+#include "liblocationwrapper.h"
+#include "geopositioninfo.h"
+
+LiblocationWrapper::LiblocationWrapper(QObject *parent)
+    : QObject(parent)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_control = location_gpsd_control_get_default();
+    m_device = (LocationGPSDevice*)g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
+
+    g_object_set(G_OBJECT(m_control),
+            "preferred-method", LOCATION_METHOD_USER_SELECTED,
+            "preferred-interval", LOCATION_INTERVAL_5S,
+            NULL);
+
+    g_signal_connect(G_OBJECT(m_device), "changed", G_CALLBACK(&changed),
+                     static_cast<void*>(this));
+}
+
+LiblocationWrapper::~LiblocationWrapper()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_device)
+        g_object_unref(m_device);
+    if (m_control)
+        g_object_unref(m_control);
+}
+
+LiblocationWrapper::changed(LocationGPSDevice *device, gpointer data)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_device) {
+        if (m_device->fix) {
+
+        GeoPositionInfo positionInfo;
+
+            if (m_device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET) {
+                qDebug() <<  __PRETTY_FUNCTION__ << m_device->fix->time;
+
+                positionInfo.setTimestamp(m_device->fix->time);
+                positionInfo.setAttribute(GeoPositionInfo::HorizontalAccuracy, m_device->fix->eph);
+            }
+
+            if (m_device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET) {
+                qDebug() <<  __PRETTY_FUNCTION__ << m_device->fix->latitude << m_device->fix->longitude;
+
+                positionInfo.setLatitude(m_device->fix->latitude);
+                positionInfo.setLongitude(m_device->fix->longitude);
+            }
+        }
+
+        setLastKnownPosition(positionInfo);
+        emit locationChanged(positionInfo);
+    }
+}
+
+GeoPositionInfo LiblocationWrapper::lastKnownPosition() const
+{
+    return m_lastKnownPosition;
+}
+
+void LiblocationWrapper::setLastKnownPosition(const GeoPositionInfo &positionInfo)
+{
+    m_lastKnownPosition = positionInfo;
+}
+
+void LiblocationWrapper::setUpdateInterval(int updateInterval)
+{
+
+}
+
+void LiblocationWrapper::startUpdates()
+{
+    if (!isRunning())
+        location_gpsd_control_start(m_control);
+}
+
+void LiblocationWrapper::stopUpdates()
+{
+    if (isRunning())
+        location_gpsd_control_stop(m_control);
+
+}
+
+bool LiblocationWrapper::isRunning()
+{
+    return m_isRunning();
+}
diff --git a/src/gps/liblocationwrapper.h b/src/gps/liblocationwrapper.h
new file mode 100644 (file)
index 0000000..6a0f30e
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Jussi Laitinen - jussi.laitinen@ixonos.com
+
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
+
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
+
+#ifndef LIBLOCATIONWRAPPER_H
+#define LIBLOCATIONWRAPPER_H
+
+#include <QObject>
+#include "geopositioninfo.h"
+
+extern "C"
+{
+    #include <location/location-gps-device.h>
+    #include <location/location-gpsd-control.h>
+}
+
+class LiblocationWrapper : public QObject
+{
+    Q_OBJECT
+
+public:
+    LiblocationWrapper(QObject *parent = 0);
+    ~LiblocationWrapper();
+
+    GeoPositionInfo lastKnownPosition() const;
+    void setLastKnownPosition(const GeoPositionInfo &positionInfo);
+    void setUpdateInterval(int updateInterval);
+    void startUpdates();
+    void stopUpdates();
+    bool isRunning();
+
+private:
+    static void changed(LocationGPSDevice *device, gpointer data);
+
+signals:
+    void locationChanged(const GeoPositionInfo &positionInfo);
+
+private:
+    LocationGPSDControl *m_control;
+    LocationGPSDevice *m_device;
+
+    GeoPositionInfo m_lastKnownPosition;
+    bool m_isRunning;
+};
+
+#endif // LIBLOCATIONWRAPPER_H
index ceb28fe..1ce1327 100644 (file)
@@ -46,7 +46,9 @@ SOURCES += main.cpp \
     network/networkaccessmanager.cpp \
     network/networkhandler.cpp \
     network/networkcookiejar.cpp \
-    network/networkreply.cpp
+    network/networkreply.cpp \
+    gps/liblocationwrapper.cpp \
+    gps/geopositioninfo.cpp
 HEADERS += ui/mainwindow.h \
     map/mapengine.h \
     map/mapview.h \
@@ -92,7 +94,9 @@ HEADERS += ui/mainwindow.h \
     network/networkaccessmanager.h \
     network/networkhandler.h \
     network/networkcookiejar.h \
-    network/networkreply.h
+    network/networkreply.h \
+    gps/liblocationwrapper.h \
+    gps/geopositioninfo.h
 QT += network \
     webkit
 DEFINES += QT_NO_DEBUG_OUTPUT
@@ -112,8 +116,11 @@ maemo5 | simulator {
     SOURCES += gps/gpspositionprivate.cpp
     HEADERS += gps/gpspositionprivate.h
     QT += maemo5
-    CONFIG += mobility
-    MOBILITY = location
+#    CONFIG += mobility
+#    MOBILITY = location
+    CONFIG += link_pkgconfig
+    PKGCONFIG += glib-2.0 \
+    liblocation
     message([QJson])
     message(Make sure you have QJson development headers installed)
     message(add: deb http://repository.maemo.org/extras-devel fremantle free non-free)