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 const double KM_MULTIPLIER = 1.0;
25 static const double MILE_MULTIPLIER = 0.621371192;
28 Location::Unit Location::unit_ = Location::KM;
30 Location::Location(QObject* parent): QObject(parent), started_(false),
31 control_(0), device_(0)
41 void Location::start()
43 control_ = location_gpsd_control_get_default();
44 device_ = (LocationGPSDevice*) g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
46 g_object_set(G_OBJECT(control_),
47 "preferred-method", LOCATION_METHOD_USER_SELECTED,
48 "preferred-interval", LOCATION_INTERVAL_DEFAULT,
51 g_signal_connect(control_, "error-verbose", G_CALLBACK(onError), this);
52 g_signal_connect(device_, "changed", G_CALLBACK(onChanged), this);
54 location_gpsd_control_start(control_);
66 location_gpsd_control_stop(control_);
68 g_object_unref(device_);
70 g_object_unref(control_);
76 bool Location::hasFix() const
83 return (device_->status == LOCATION_GPS_DEVICE_STATUS_FIX);
86 double Location::getSignalStrength() const
94 if(device_->satellites_in_view == 0)
99 double val = (device_->satellites_in_use / static_cast<double>(device_->satellites_in_view)) * 100.0;
109 void Location::setUnit(Location::Unit unit)
114 Location::Unit Location::getUnit()
119 double Location::getUnitMultiplier()
123 return MILE_MULTIPLIER;
126 return KM_MULTIPLIER;
129 void Location::onChanged(LocationGPSDevice *device, gpointer data)
131 if(device && device->fix &&
132 !(device->fix->fields & LOCATION_GPS_DEVICE_NONE_SET))
137 fix.time = device->fix->time;
138 fix.ept = device->fix->ept;
140 if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET)
142 fix.latitude = device->fix->latitude;
143 fix.longitude = device->fix->longitude;
144 fix.eph = device->fix->eph;
147 if(device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET)
149 fix.altitude = device->fix->altitude;
150 fix.epv = device->fix->epv;
153 if(device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET)
155 fix.kmSpeed = device->fix->speed;
156 fix.speed = device->fix->speed * getUnitMultiplier();
157 fix.eps = device->fix->eps;
160 if(device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET)
162 fix.track = device->fix->track;
163 fix.epd = device->fix->epd;
166 if(device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET)
168 fix.climb = device->fix->climb;
169 fix.epc = device->fix->epc;
172 Location* meh = (Location*) data;
173 meh->emit locationChanged(fix);
177 void Location::onError(LocationGPSDControl *control, LocationGPSDControlError error, gpointer data)
181 Error err = SYSTEM_ERROR;
185 case LOCATION_ERROR_USER_REJECTED_DIALOG:
186 err = USER_REJECTED_DIALOG;
188 case LOCATION_ERROR_USER_REJECTED_SETTINGS:
189 err = USER_REJECTED_SETTINGS;
191 case LOCATION_ERROR_BT_GPS_NOT_AVAILABLE:
192 err = GPS_NOT_AVAILABLE;
194 case LOCATION_ERROR_METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
195 err = NOT_ALLOWED_IN_OFFLINE_MODE;
197 case LOCATION_ERROR_SYSTEM:
202 Location* meh = (Location*) data;
203 meh->emit locationError(err);