0074d284a3c274db71da8f6daac2a19e31fa108d
[googlelatitude] / liblocationmaemo5 / qgeosatelliteinfosource_maemo5.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Qt Mobility Components.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qgeosatelliteinfosource_maemo5_p.h"
43 #include "liblocationwrapper_p.h"
44
45 QTM_BEGIN_NAMESPACE
46
47 QGeoSatelliteInfoSourceMaemo::QGeoSatelliteInfoSourceMaemo(QObject *parent)
48         : QGeoSatelliteInfoSource(parent)
49 {
50     qDebug() << "* QGeoSatelliteInfoSourceMaemo Fremantle Backport";
51     client_id_ = -1;
52     timerInterval = DEFAULT_UPDATE_INTERVAL;
53     updateTimer = new QTimer(this);
54     updateTimer->setSingleShot(true);
55     connect(updateTimer, SIGNAL(timeout()), this, SLOT(satelliteStatus()));
56
57     requestTimer = new QTimer(this);
58     requestTimer->setSingleShot(true);
59     connect(requestTimer, SIGNAL(timeout()), this, SLOT(requestTimeoutElapsed()));
60
61     satelliteInfoState = QGeoSatelliteInfoSourceMaemo::Undefined;
62 }
63
64 int QGeoSatelliteInfoSourceMaemo::init()
65 {
66     if (LiblocationWrapper::instance()->inited())
67         return INIT_OK;
68     else
69         return INIT_FAILED;
70 }
71
72 void QGeoSatelliteInfoSourceMaemo::setUpdateInterval(int msec)
73 {
74     bool updateTimerInterval = false;
75
76     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::PowersaveActive)
77         if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped)
78             updateTimerInterval = true;
79
80     timerInterval = (msec < MINIMUM_UPDATE_INTERVAL) ? MINIMUM_UPDATE_INTERVAL : msec;
81
82     if (timerInterval >= POWERSAVE_THRESHOLD)
83         satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::PowersaveActive;
84     else
85         satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::PowersaveActive;
86
87     // If powersave has been active when new update interval has been set,
88     // ensure that timer is started.
89     if (updateTimerInterval)
90         startLocationDaemon();
91
92     // Ensure that new timer interval is taken into use immediately.
93     activateTimer();
94 }
95
96 void QGeoSatelliteInfoSourceMaemo::startUpdates()
97 {
98     startLocationDaemon();
99
100     // Ensure that powersave is selected, if stopUpdates() has been called,
101     // but selected update interval is still greater than POWERSAVE_THRESHOLD.
102     if (timerInterval >= POWERSAVE_THRESHOLD)
103         satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::PowersaveActive;
104
105     activateTimer();
106 }
107
108 void QGeoSatelliteInfoSourceMaemo::stopUpdates()
109 {
110     satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::PowersaveActive;
111
112     if (!(satelliteInfoState & QGeoSatelliteInfoSourceMaemo::RequestActive)) {
113         updateTimer->stop();
114         if (LiblocationWrapper::instance()->isActive())
115             LiblocationWrapper::instance()->stop();
116     }
117
118     satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::Started;
119     satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::Stopped;
120 }
121
122 void QGeoSatelliteInfoSourceMaemo::requestUpdate(int timeout)
123 {
124     int timeoutForRequest = 0;
125
126     if (!timeout) {
127         if (LiblocationWrapper::instance()->isActive())
128             // If GPS is active, assume quick fix.
129             timeoutForRequest = DEFAULT_UPDATE_INTERVAL;
130         else
131             // Otherwise reserve longer time to get a fix.
132             timeoutForRequest = POWERSAVE_POWERON_PERIOD;
133     } else if (timeout < MINIMUM_UPDATE_INTERVAL) {
134         if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::RequestActive)
135             return;
136
137         emit requestTimeout();
138         return;
139     } else {
140         timeoutForRequest = timeout;
141     }
142
143     satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::RequestActive;
144
145     if (!(LiblocationWrapper::instance()->isActive()))
146         LiblocationWrapper::instance()->start();
147
148     activateTimer();
149     requestTimer->start(timeoutForRequest);
150 }
151
152 void QGeoSatelliteInfoSourceMaemo::satelliteStatus()
153 {
154     QList<QGeoSatelliteInfo> satellitesInView =
155         LiblocationWrapper::instance()->satellitesInView();
156     QList<QGeoSatelliteInfo> satellitesInUse =
157         LiblocationWrapper::instance()->satellitesInUse();
158
159     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::RequestActive) {
160         satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::RequestActive;
161
162         requestTimer->stop();
163
164         if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped) {
165             if (LiblocationWrapper::instance()->isActive()) {
166                 LiblocationWrapper::instance()->stop();
167             }
168         }
169
170         // Ensure that requested satellite info is emitted even though
171         // powersave is active and GPS would normally be off.
172         if ((satelliteInfoState & QGeoSatelliteInfoSourceMaemo::PowersaveActive) &&
173                 (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped)) {
174             if (satellitesInView.length()) {
175                 emit satellitesInViewUpdated(satellitesInView);
176                 emit satellitesInUseUpdated(satellitesInUse);
177             }
178         }
179     }
180
181     // Make sure that if update is triggered when waking up, there
182     // is no false position update.
183     if (!((satelliteInfoState & QGeoSatelliteInfoSourceMaemo::PowersaveActive) &&
184             (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped))) {
185         if (satellitesInView.length()) {
186             emit satellitesInViewUpdated(satellitesInView);
187             emit satellitesInUseUpdated(satellitesInUse);
188         }
189     }
190
191     activateTimer();
192 }
193
194 void QGeoSatelliteInfoSourceMaemo::requestTimeoutElapsed()
195 {
196     updateTimer->stop();
197     emit requestTimeout();
198
199     satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::RequestActive;
200
201     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped)
202         if (LiblocationWrapper::instance()->isActive())
203             LiblocationWrapper::instance()->stop();
204
205     activateTimer();
206 }
207
208 void QGeoSatelliteInfoSourceMaemo::activateTimer()
209 {
210     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::RequestActive) {
211         updateTimer->start(MINIMUM_UPDATE_INTERVAL);
212         return;
213     }
214
215     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::PowersaveActive) {
216         if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Started) {
217             // Cannot call stopUpdates() here since we want to keep powersave
218             // active.
219             if (LiblocationWrapper::instance()->isActive())
220                 LiblocationWrapper::instance()->stop();
221             updateTimer->start(timerInterval - POWERSAVE_POWERON_PERIOD);
222
223             satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::Started;
224             satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::Stopped;
225         } else if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped) {
226             startLocationDaemon();
227             updateTimer->start(POWERSAVE_POWERON_PERIOD);
228         }
229         return;
230     }
231
232     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Started)
233         updateTimer->start(timerInterval);
234 }
235
236 void QGeoSatelliteInfoSourceMaemo::startLocationDaemon()
237 {
238     if (!(LiblocationWrapper::instance()->isActive()))
239         LiblocationWrapper::instance()->start();
240     satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::Started;
241     satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::Stopped;
242 }
243
244 #include "moc_qgeosatelliteinfosource_maemo5_p.cpp"
245 QTM_END_NAMESPACE
246