Initial commit.
[jspeed] / src / location.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * jSpeed 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 jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QDebug>
20 #include "location.h"
21
22 namespace
23 {
24     static const double KM_MULTIPLIER = 1.0;
25     static const double MILE_MULTIPLIER = 0.621371192;
26 }
27
28 Location::Unit Location::unit_ = Location::KM;
29
30 Location::Location(QObject* parent): QObject(parent), started_(false),
31 control_(0), device_(0)
32 {
33     g_type_init();
34 }
35
36 Location::~Location()
37 {
38     end();
39 }
40
41 void Location::start()
42 {
43     control_ = location_gpsd_control_get_default();
44     device_ = (LocationGPSDevice*) g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
45
46     g_object_set(G_OBJECT(control_),
47                  "preferred-method", LOCATION_METHOD_USER_SELECTED,
48                  "preferred-interval", LOCATION_INTERVAL_DEFAULT,
49                  NULL);
50
51     g_signal_connect(control_, "error-verbose", G_CALLBACK(onError), this);
52     g_signal_connect(device_, "changed", G_CALLBACK(onChanged), this);
53
54     location_gpsd_control_start(control_);
55
56     started_ = true;
57 }
58
59 void Location::end()
60 {
61     if(!started_)
62     {
63         return;
64     }
65
66     location_gpsd_control_stop(control_);
67
68     g_object_unref(device_);
69     device_ = 0;
70     g_object_unref(control_);
71     control_ = 0;
72
73     started_ = false;
74 }
75
76 void Location::setUnit(Location::Unit unit)
77 {
78     unit_ = unit;
79 }
80
81 Location::Unit Location::getUnit()
82 {
83     return unit_;
84 }
85
86 double Location::getUnitMultiplier()
87 {
88     if(unit_ == MILE)
89     {
90         return MILE_MULTIPLIER;
91     }
92
93     return KM_MULTIPLIER;
94 }
95
96 void Location::onChanged(LocationGPSDevice *device, gpointer data)
97 {
98     if(device && device->fix &&
99        !(device->fix->fields & LOCATION_GPS_DEVICE_NONE_SET))
100     {
101
102         Fix fix;
103         fix.unit = unit_;
104         fix.time = device->fix->time;
105         fix.ept = device->fix->ept;
106
107         if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET)
108         {
109             fix.latitude = device->fix->latitude;
110             fix.longitude = device->fix->longitude;
111             fix.eph = device->fix->eph;
112         }
113
114         if(device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET)
115         {
116             fix.altitude = device->fix->altitude;
117             fix.epv = device->fix->epv;
118         }
119
120         if(device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET)
121         {
122             fix.kmSpeed = device->fix->speed;
123             fix.speed = device->fix->speed * getUnitMultiplier();
124             fix.eps = device->fix->eps;
125         }
126
127         if(device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET)
128         {
129             fix.track = device->fix->track;
130             fix.epd = device->fix->epd;
131         }
132
133         if(device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET)
134         {
135             fix.climb = device->fix->climb;
136             fix.epc = device->fix->epc;
137         }
138
139         Location* meh = (Location*) data;
140         meh->emit locationChanged(fix);
141     }
142 }
143
144 void Location::onError(LocationGPSDControl *control, LocationGPSDControlError error, gpointer data)
145 {
146     Q_UNUSED(control);
147
148     Error err = SYSTEM_ERROR;
149
150     switch(error)
151     {
152     case LOCATION_ERROR_USER_REJECTED_DIALOG:
153         err = USER_REJECTED_DIALOG;
154         break;
155     case LOCATION_ERROR_USER_REJECTED_SETTINGS:
156         err = USER_REJECTED_SETTINGS;
157         break;
158     case LOCATION_ERROR_BT_GPS_NOT_AVAILABLE:
159         err = GPS_NOT_AVAILABLE;
160         break;
161     case LOCATION_ERROR_METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
162         err = NOT_ALLOWED_IN_OFFLINE_MODE;
163         break;
164     case LOCATION_ERROR_SYSTEM:
165         err = SYSTEM_ERROR;
166         break;
167     }
168
169     Location* meh = (Location*) data;
170     meh->emit locationError(err);
171 }
172
173 Location::Fix::Fix()
174 {
175     unit = KM;
176     kmSpeed = 0.0;
177     time = 0.0;
178     ept = 0.0;
179     latitude = 0.0;
180     longitude = 0.0;
181     eph = 0.0;
182     altitude = 0.0;
183     epv = 0.0;
184     track = 0.0;
185     epd = 0.0;
186     speed = 0.0;
187     eps = 0.0;
188     climb = 0.0;
189     epc = 0.0;
190 }