~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/geolocation/location_gps.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2009 Petri Damstén <damu@iki.fi>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or
 
5
 *   modify it under the terms of the GNU General Public License as
 
6
 *   published by the Free Software Foundation; either version 2 of
 
7
 *   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
 
12
 *   GNU General Public License for more details.
 
13
 *
 
14
 *   You should have received a copy of the GNU General Public License
 
15
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 */
 
17
 
 
18
#include "location_gps.h"
 
19
#include <kdebug.h>
 
20
 
 
21
Gpsd::Gpsd(gps_data_t* gpsdata)
 
22
    : m_gpsdata(gpsdata)
 
23
    , m_abort(false)
 
24
{
 
25
}
 
26
 
 
27
Gpsd::~Gpsd()
 
28
{
 
29
    m_abort = true;
 
30
    m_condition.wakeOne();
 
31
    wait();
 
32
}
 
33
 
 
34
void Gpsd::update()
 
35
{
 
36
    if (!isRunning()) {
 
37
        start();
 
38
    } else {
 
39
        m_condition.wakeOne();
 
40
    }
 
41
}
 
42
 
 
43
void Gpsd::run()
 
44
{
 
45
#if defined( GPSD_API_MAJOR_VERSION ) && ( GPSD_API_MAJOR_VERSION >= 3 ) && defined( WATCH_ENABLE )
 
46
    gps_stream(m_gpsdata, WATCH_ENABLE, NULL);
 
47
#else
 
48
    gps_query(m_gpsdata, "w+x\n");
 
49
#endif
 
50
 
 
51
    while (!m_abort) {
 
52
        Plasma::DataEngine::Data d;
 
53
 
 
54
#if GPSD_API_MAJOR_VERSION >= 5
 
55
        if (gps_read(m_gpsdata) != -1) {
 
56
#else
 
57
        if (gps_poll(m_gpsdata) != -1) {
 
58
#endif
 
59
            //kDebug() << "poll ok";
 
60
            if (m_gpsdata->online) {
 
61
                //kDebug() << "online";
 
62
                if (m_gpsdata->status != STATUS_NO_FIX) {
 
63
                    //kDebug() << "fix";
 
64
                    d["accuracy"] = 30;
 
65
                    d["latitude"] = QString::number(m_gpsdata->fix.latitude);
 
66
                    d["longitude"] = QString::number(m_gpsdata->fix.longitude);
 
67
                }
 
68
            }
 
69
        }
 
70
 
 
71
        emit dataReady(d);
 
72
 
 
73
        m_condition.wait(&m_mutex);
 
74
    }
 
75
}
 
76
 
 
77
Gps::Gps(QObject* parent, const QVariantList& args)
 
78
    : GeolocationProvider(parent, args),
 
79
      m_gpsd(0)
 
80
#if GPSD_API_MAJOR_VERSION >= 5
 
81
    , m_gpsdata(0)
 
82
#endif
 
83
{
 
84
#if GPSD_API_MAJOR_VERSION >= 5
 
85
    m_gpsdata = new gps_data_t;
 
86
    gps_open("localhost", DEFAULT_GPSD_PORT, m_gpsdata);
 
87
#else
 
88
    gps_data_t* m_gpsdata = gps_open("localhost", DEFAULT_GPSD_PORT);
 
89
#endif
 
90
    if (m_gpsdata) {
 
91
        kDebug() << "gpsd found.";
 
92
        m_gpsd = new Gpsd(m_gpsdata);
 
93
        connect(m_gpsd, SIGNAL(dataReady(const Plasma::DataEngine::Data&)),
 
94
                this, SIGNAL(setData(const Plasma::DataEngine::Data&)));
 
95
    } else {
 
96
        kDebug() << "gpsd not found";
 
97
    }
 
98
 
 
99
    setIsAvailable(m_gpsd);
 
100
}
 
101
 
 
102
Gps::~Gps()
 
103
{
 
104
    delete m_gpsd;
 
105
#if GPSD_API_MAJOR_VERSION >= 5
 
106
    delete m_gpsdata;
 
107
#endif
 
108
}
 
109
 
 
110
void Gps::update()
 
111
{
 
112
    if (m_gpsd) {
 
113
        m_gpsd->update();
 
114
    }
 
115
}
 
116
 
 
117
K_EXPORT_PLASMA_GEOLOCATIONPROVIDER(gps, Gps)
 
118
 
 
119
#include "location_gps.moc"