Added zpos attribute to all elements. Changed architecture to allow detailscreen...
[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 bool Location::hasFix() const
77 {
78     if(!started_)
79     {
80         return false;
81     }
82
83     return (device_->status == LOCATION_GPS_DEVICE_STATUS_FIX);
84 }
85
86 double Location::getSignalStrength() const
87 {
88     if(!hasFix())
89     {
90         return 0.0;
91     }
92
93     if(device_->satellites_in_view == 0)
94     {
95         return 0.0;
96     }
97
98     double val = (device_->satellites_in_use / static_cast<double>(device_->satellites_in_view)) * 100.0;
99
100     if(val > 100.0)
101     {
102         val = 100.0;
103     }
104
105     return val;
106 }
107
108 void Location::setUnit(Location::Unit unit)
109 {
110     unit_ = unit;
111 }
112
113 Location::Unit Location::getUnit()
114 {
115     return unit_;
116 }
117
118 double Location::getUnitMultiplier()
119 {
120     if(unit_ == MILE)
121     {
122         return MILE_MULTIPLIER;
123     }
124
125     return KM_MULTIPLIER;
126 }
127
128 void Location::onChanged(LocationGPSDevice *device, gpointer data)
129 {
130     if(device && device->fix &&
131        !(device->fix->fields & LOCATION_GPS_DEVICE_NONE_SET))
132     {
133
134         Fix fix;
135         fix.unit = unit_;
136         fix.time = device->fix->time;
137         fix.ept = device->fix->ept;
138
139         if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET)
140         {
141             fix.latitude = device->fix->latitude;
142             fix.longitude = device->fix->longitude;
143             fix.eph = device->fix->eph;
144         }
145
146         if(device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET)
147         {
148             fix.altitude = device->fix->altitude;
149             fix.epv = device->fix->epv;
150         }
151
152         if(device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET)
153         {
154             fix.kmSpeed = device->fix->speed;
155             fix.speed = device->fix->speed * getUnitMultiplier();
156             fix.eps = device->fix->eps;
157         }
158
159         if(device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET)
160         {
161             fix.track = device->fix->track;
162             fix.epd = device->fix->epd;
163         }
164
165         if(device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET)
166         {
167             fix.climb = device->fix->climb;
168             fix.epc = device->fix->epc;
169         }
170
171         Location* meh = (Location*) data;
172         meh->emit locationChanged(fix);
173     }
174 }
175
176 void Location::onError(LocationGPSDControl *control, LocationGPSDControlError error, gpointer data)
177 {
178     Q_UNUSED(control);
179
180     Error err = SYSTEM_ERROR;
181
182     switch(error)
183     {
184     case LOCATION_ERROR_USER_REJECTED_DIALOG:
185         err = USER_REJECTED_DIALOG;
186         break;
187     case LOCATION_ERROR_USER_REJECTED_SETTINGS:
188         err = USER_REJECTED_SETTINGS;
189         break;
190     case LOCATION_ERROR_BT_GPS_NOT_AVAILABLE:
191         err = GPS_NOT_AVAILABLE;
192         break;
193     case LOCATION_ERROR_METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
194         err = NOT_ALLOWED_IN_OFFLINE_MODE;
195         break;
196     case LOCATION_ERROR_SYSTEM:
197         err = SYSTEM_ERROR;
198         break;
199     }
200
201     Location* meh = (Location*) data;
202     meh->emit locationError(err);
203 }
204
205 Location::Fix::Fix()
206 {
207     unit = KM;
208     kmSpeed = 0.0;
209     time = 0.0;
210     ept = 0.0;
211     latitude = 0.0;
212     longitude = 0.0;
213     eph = 0.0;
214     altitude = 0.0;
215     epv = 0.0;
216     track = 0.0;
217     epd = 0.0;
218     speed = 0.0;
219     eps = 0.0;
220     climb = 0.0;
221     epc = 0.0;
222 }