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
93 if(device_->satellites_in_view == 0)
98 double val = (device_->satellites_in_use / static_cast<double>(device_->satellites_in_view)) * 100.0;
108 void Location::setUnit(Location::Unit unit)
113 Location::Unit Location::getUnit()
118 double Location::getUnitMultiplier()
122 return MILE_MULTIPLIER;
125 return KM_MULTIPLIER;
128 void Location::onChanged(LocationGPSDevice *device, gpointer data)
130 if(device && device->fix &&
131 !(device->fix->fields & LOCATION_GPS_DEVICE_NONE_SET))
136 fix.time = device->fix->time;
137 fix.ept = device->fix->ept;
139 if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET)
141 fix.latitude = device->fix->latitude;
142 fix.longitude = device->fix->longitude;
143 fix.eph = device->fix->eph;
146 if(device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET)
148 fix.altitude = device->fix->altitude;
149 fix.epv = device->fix->epv;
152 if(device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET)
154 fix.kmSpeed = device->fix->speed;
155 fix.speed = device->fix->speed * getUnitMultiplier();
156 fix.eps = device->fix->eps;
159 if(device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET)
161 fix.track = device->fix->track;
162 fix.epd = device->fix->epd;
165 if(device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET)
167 fix.climb = device->fix->climb;
168 fix.epc = device->fix->epc;
171 Location* meh = (Location*) data;
172 meh->emit locationChanged(fix);
176 void Location::onError(LocationGPSDControl *control, LocationGPSDControlError error, gpointer data)
180 Error err = SYSTEM_ERROR;
184 case LOCATION_ERROR_USER_REJECTED_DIALOG:
185 err = USER_REJECTED_DIALOG;
187 case LOCATION_ERROR_USER_REJECTED_SETTINGS:
188 err = USER_REJECTED_SETTINGS;
190 case LOCATION_ERROR_BT_GPS_NOT_AVAILABLE:
191 err = GPS_NOT_AVAILABLE;
193 case LOCATION_ERROR_METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
194 err = NOT_ALLOWED_IN_OFFLINE_MODE;
196 case LOCATION_ERROR_SYSTEM:
201 Location* meh = (Location*) data;
202 meh->emit locationError(err);