2 * This file is part of jSpeed.
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.
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.
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/>.
19 #include <QtCore/QDebug>
24 static double const KM_MULTIPLIER = 1.0;
25 static double const MILE_MULTIPLIER = 0.621371192;
26 static double const METER_MULTIPLIER = 1.0;
27 static double const FEET_MULTIPLIER = 3.2808399;
30 Location::Unit Location::unit_ = Location::KM;
32 Location::Location(QObject* parent): QObject(parent), started_(false),
33 control_(0), device_(0)
43 void Location::start()
45 control_ = location_gpsd_control_get_default();
46 device_ = (LocationGPSDevice*) g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
48 g_object_set(G_OBJECT(control_),
49 "preferred-method", LOCATION_METHOD_USER_SELECTED,
50 "preferred-interval", LOCATION_INTERVAL_DEFAULT,
53 g_signal_connect(control_, "error-verbose", G_CALLBACK(onError), this);
54 g_signal_connect(device_, "changed", G_CALLBACK(onChanged), this);
56 location_gpsd_control_start(control_);
68 location_gpsd_control_stop(control_);
70 g_object_unref(device_);
72 g_object_unref(control_);
78 bool Location::hasFix() const
85 return (device_->status == LOCATION_GPS_DEVICE_STATUS_FIX);
88 double Location::getSignalStrength() const
95 if(device_->satellites_in_view == 0)
100 double val = (device_->satellites_in_use / static_cast<double>(device_->satellites_in_view)) * 100.0;
110 void Location::setUnit(Location::Unit unit)
115 Location::Unit Location::getUnit()
120 double Location::getUnitMultiplier()
124 return MILE_MULTIPLIER;
127 return KM_MULTIPLIER;
130 double Location::getMeterMultiplier()
134 return FEET_MULTIPLIER;
137 return METER_MULTIPLIER;
140 void Location::onChanged(LocationGPSDevice *device, gpointer data)
142 if(device && device->fix &&
143 !(device->fix->fields & LOCATION_GPS_DEVICE_NONE_SET))
148 fix.time = device->fix->time;
149 fix.ept = device->fix->ept;
151 if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET)
153 fix.latitude = device->fix->latitude;
154 fix.longitude = device->fix->longitude;
155 fix.eph = device->fix->eph;
158 if(device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET)
160 fix.altitude = device->fix->altitude * getMeterMultiplier();
161 fix.epv = device->fix->epv;
164 if(device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET)
166 fix.kmSpeed = device->fix->speed;
167 fix.speed = device->fix->speed * getUnitMultiplier();
168 fix.eps = device->fix->eps;
171 if(device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET)
173 fix.track = device->fix->track;
174 fix.epd = device->fix->epd;
177 if(device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET)
179 fix.climb = device->fix->climb;
180 fix.epc = device->fix->epc;
183 Location* meh = (Location*) data;
184 meh->emit locationChanged(fix);
188 void Location::onError(LocationGPSDControl *control, LocationGPSDControlError error, gpointer data)
192 Error err = SYSTEM_ERROR;
196 case LOCATION_ERROR_USER_REJECTED_DIALOG:
197 err = USER_REJECTED_DIALOG;
199 case LOCATION_ERROR_USER_REJECTED_SETTINGS:
200 err = USER_REJECTED_SETTINGS;
202 case LOCATION_ERROR_BT_GPS_NOT_AVAILABLE:
203 err = GPS_NOT_AVAILABLE;
205 case LOCATION_ERROR_METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
206 err = NOT_ALLOWED_IN_OFFLINE_MODE;
208 case LOCATION_ERROR_SYSTEM:
213 Location* meh = (Location*) data;
214 meh->emit locationError(err);