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 void Location::setUnit(Location::Unit unit)
81 Location::Unit Location::getUnit()
86 double Location::getUnitMultiplier()
90 return MILE_MULTIPLIER;
96 void Location::onChanged(LocationGPSDevice *device, gpointer data)
98 if(device && device->fix &&
99 !(device->fix->fields & LOCATION_GPS_DEVICE_NONE_SET))
104 fix.time = device->fix->time;
105 fix.ept = device->fix->ept;
107 if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET)
109 fix.latitude = device->fix->latitude;
110 fix.longitude = device->fix->longitude;
111 fix.eph = device->fix->eph;
114 if(device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET)
116 fix.altitude = device->fix->altitude;
117 fix.epv = device->fix->epv;
120 if(device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET)
122 fix.kmSpeed = device->fix->speed;
123 fix.speed = device->fix->speed * getUnitMultiplier();
124 fix.eps = device->fix->eps;
127 if(device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET)
129 fix.track = device->fix->track;
130 fix.epd = device->fix->epd;
133 if(device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET)
135 fix.climb = device->fix->climb;
136 fix.epc = device->fix->epc;
139 Location* meh = (Location*) data;
140 meh->emit locationChanged(fix);
144 void Location::onError(LocationGPSDControl *control, LocationGPSDControlError error, gpointer data)
148 Error err = SYSTEM_ERROR;
152 case LOCATION_ERROR_USER_REJECTED_DIALOG:
153 err = USER_REJECTED_DIALOG;
155 case LOCATION_ERROR_USER_REJECTED_SETTINGS:
156 err = USER_REJECTED_SETTINGS;
158 case LOCATION_ERROR_BT_GPS_NOT_AVAILABLE:
159 err = GPS_NOT_AVAILABLE;
161 case LOCATION_ERROR_METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
162 err = NOT_ALLOWED_IN_OFFLINE_MODE;
164 case LOCATION_ERROR_SYSTEM:
169 Location* meh = (Location*) data;
170 meh->emit locationError(err);