qtmobility/src/location/qgeopositioninfosource_maemo5.cpp
changeset 4 90517678cc4f
child 11 06b8e2af4411
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qgeopositioninfosource_maemo5_p.h"
       
    43 #include "liblocationwrapper_p.h"
       
    44 
       
    45 using namespace std;
       
    46 
       
    47 QTM_BEGIN_NAMESPACE
       
    48 
       
    49 QGeoPositionInfoSourceMaemo::QGeoPositionInfoSourceMaemo(QObject *parent) 
       
    50     : QGeoPositionInfoSource(parent)
       
    51 {
       
    52     // default values
       
    53     availableMethods = SatellitePositioningMethods;
       
    54     
       
    55     timerInterval = DEFAULT_UPDATE_INTERVAL;
       
    56     updateTimer = new QTimer(this);
       
    57     updateTimer->setSingleShot(true);
       
    58     connect(updateTimer, SIGNAL(timeout()), this, SLOT(newPositionUpdate()));
       
    59 
       
    60     requestTimer = new QTimer(this);
       
    61     requestTimer->setSingleShot(true);
       
    62     connect(requestTimer, SIGNAL(timeout()), this, SLOT(requestTimeoutElapsed()));
       
    63 
       
    64     errorOccurred = false;
       
    65     errorSent = false;
       
    66 
       
    67     positionInfoState = QGeoPositionInfoSourceMaemo::Undefined;
       
    68 }
       
    69 
       
    70 int QGeoPositionInfoSourceMaemo::init()
       
    71 {
       
    72     if (LiblocationWrapper::instance()->inited()) {
       
    73         connect(LiblocationWrapper::instance(), SIGNAL(error()), this, SLOT(error()));
       
    74         return INIT_OK;
       
    75     } else {
       
    76         return INIT_FAILED;
       
    77     }
       
    78 }
       
    79 
       
    80 QGeoPositionInfo QGeoPositionInfoSourceMaemo::lastKnownPosition(bool fromSatellitePositioningMethodsOnly) const
       
    81 {
       
    82     return (LiblocationWrapper::instance()->lastKnownPosition(fromSatellitePositioningMethodsOnly));
       
    83 }
       
    84 
       
    85 QGeoPositionInfoSource::PositioningMethods QGeoPositionInfoSourceMaemo::supportedPositioningMethods() const
       
    86 {
       
    87     return availableMethods;
       
    88 }
       
    89 
       
    90 void QGeoPositionInfoSourceMaemo::setUpdateInterval(int msec)
       
    91 {
       
    92     bool updateTimerInterval = false;
       
    93 
       
    94     if (positionInfoState & QGeoPositionInfoSourceMaemo::PowersaveActive)
       
    95         if (positionInfoState & QGeoPositionInfoSourceMaemo::Stopped)
       
    96             updateTimerInterval = true;
       
    97 
       
    98     if (!msec) {
       
    99         timerInterval = MINIMUM_UPDATE_INTERVAL;
       
   100         QGeoPositionInfoSource::setUpdateInterval(0);
       
   101     } else {
       
   102         timerInterval = (msec < MINIMUM_UPDATE_INTERVAL) ? MINIMUM_UPDATE_INTERVAL : msec;
       
   103         QGeoPositionInfoSource::setUpdateInterval(timerInterval);
       
   104     }
       
   105 
       
   106     if (timerInterval >= POWERSAVE_THRESHOLD)
       
   107         positionInfoState |= QGeoPositionInfoSourceMaemo::PowersaveActive;
       
   108     else
       
   109         positionInfoState &= ~QGeoPositionInfoSourceMaemo::PowersaveActive;
       
   110 
       
   111     // If powersave has been active when new update interval has been set,
       
   112     // ensure that timer is started.
       
   113     if(updateTimerInterval)
       
   114         startLocationDaemon();
       
   115 
       
   116     // Ensure that new timer interval is taken into use immediately.
       
   117     activateTimer();
       
   118 }
       
   119 
       
   120 void QGeoPositionInfoSourceMaemo::setPreferredPositioningMethods(PositioningMethods sources)
       
   121 {
       
   122     Q_UNUSED(sources)
       
   123     return;
       
   124 }
       
   125 
       
   126 int QGeoPositionInfoSourceMaemo::minimumUpdateInterval() const
       
   127 {
       
   128     return MINIMUM_UPDATE_INTERVAL;
       
   129 }
       
   130 
       
   131 // public slots:
       
   132 void QGeoPositionInfoSourceMaemo::startUpdates()
       
   133 {
       
   134     startLocationDaemon();
       
   135 
       
   136     // Ensure that powersave is selected, if stopUpdates() has been called,
       
   137     // but selected update interval is still greater than POWERSAVE_THRESHOLD.
       
   138     if (timerInterval >= POWERSAVE_THRESHOLD)
       
   139         positionInfoState |= QGeoPositionInfoSourceMaemo::PowersaveActive;
       
   140 
       
   141     activateTimer();
       
   142 }
       
   143 
       
   144 void QGeoPositionInfoSourceMaemo::stopUpdates()
       
   145 {
       
   146     positionInfoState &= ~QGeoPositionInfoSourceMaemo::PowersaveActive;
       
   147     
       
   148     if (!(positionInfoState & QGeoPositionInfoSourceMaemo::RequestActive)) {
       
   149         updateTimer->stop();
       
   150         if (LiblocationWrapper::instance()->isActive())
       
   151             LiblocationWrapper::instance()->stop();
       
   152     }
       
   153 
       
   154     errorOccurred = false;
       
   155     errorSent = false;
       
   156 
       
   157     positionInfoState &= ~QGeoPositionInfoSourceMaemo::Started;
       
   158     positionInfoState |= QGeoPositionInfoSourceMaemo::Stopped;
       
   159 }
       
   160 
       
   161 void QGeoPositionInfoSourceMaemo::requestUpdate(int timeout)
       
   162 {
       
   163     int timeoutForRequest = 0;
       
   164 
       
   165     if (!timeout) {
       
   166         if (LiblocationWrapper::instance()->isActive())
       
   167             // If GPS is active, assume quick fix.
       
   168             timeoutForRequest = DEFAULT_UPDATE_INTERVAL;
       
   169         else
       
   170             // Otherwise reserve longer time to get a fix.
       
   171             timeoutForRequest = POWERSAVE_POWERON_PERIOD;
       
   172     } else if (timeout < MINIMUM_UPDATE_INTERVAL) {
       
   173         if (positionInfoState & QGeoPositionInfoSourceMaemo::RequestActive)
       
   174             return;
       
   175 
       
   176         emit updateTimeout();
       
   177         return;
       
   178     } else {
       
   179         timeoutForRequest = timeout;
       
   180     }
       
   181 
       
   182     positionInfoState |= QGeoPositionInfoSourceMaemo::RequestActive;
       
   183 
       
   184     if (!(LiblocationWrapper::instance()->isActive()))
       
   185         LiblocationWrapper::instance()->start();
       
   186 
       
   187     activateTimer();
       
   188     requestTimer->start(timeoutForRequest);
       
   189 }
       
   190 
       
   191 void QGeoPositionInfoSourceMaemo::newPositionUpdate()
       
   192 {
       
   193     if (LiblocationWrapper::instance()->fixIsValid()) {
       
   194         errorOccurred = false;
       
   195         errorSent = false;
       
   196 
       
   197         if (positionInfoState & QGeoPositionInfoSourceMaemo::RequestActive) {
       
   198             positionInfoState &= ~QGeoPositionInfoSourceMaemo::RequestActive;
       
   199             requestTimer->stop();
       
   200 
       
   201             if (positionInfoState & QGeoPositionInfoSourceMaemo::Stopped)
       
   202                 if (LiblocationWrapper::instance()->isActive())
       
   203                     LiblocationWrapper::instance()->stop();
       
   204 
       
   205             // Ensure that requested position fix is emitted even though
       
   206             // powersave is active and GPS would normally be off.
       
   207             if ((positionInfoState & QGeoPositionInfoSourceMaemo::PowersaveActive) &&
       
   208                (positionInfoState & QGeoPositionInfoSourceMaemo::Stopped)) {
       
   209                 emit positionUpdated(LiblocationWrapper::instance()->position());
       
   210             }
       
   211         }
       
   212 
       
   213         // Make sure that if update is triggered when waking up, there
       
   214         // is no false position update.
       
   215         if (!((positionInfoState & QGeoPositionInfoSourceMaemo::PowersaveActive) &&
       
   216              (positionInfoState & QGeoPositionInfoSourceMaemo::Stopped)))
       
   217             emit positionUpdated(LiblocationWrapper::instance()->position());
       
   218     } else {
       
   219         // if an error occurs when we are updating periodically and we haven't 
       
   220         // sent an error since the last fix...
       
   221         if (!(positionInfoState & QGeoPositionInfoSourceMaemo::RequestActive) && 
       
   222             errorOccurred && !errorSent) {
       
   223             errorSent = true;
       
   224             // we need to emit the updateTimeout signal
       
   225             emit updateTimeout();
       
   226         }
       
   227     }
       
   228     activateTimer();
       
   229 }
       
   230 
       
   231 void QGeoPositionInfoSourceMaemo::requestTimeoutElapsed()
       
   232 {
       
   233     updateTimer->stop();
       
   234     emit updateTimeout();
       
   235 
       
   236     positionInfoState &= ~QGeoPositionInfoSourceMaemo::RequestActive;
       
   237 
       
   238     if (positionInfoState & QGeoPositionInfoSourceMaemo::Stopped)
       
   239         if (LiblocationWrapper::instance()->isActive())
       
   240             LiblocationWrapper::instance()->stop();   
       
   241 
       
   242     activateTimer();
       
   243 }
       
   244 
       
   245 void QGeoPositionInfoSourceMaemo::error()
       
   246 {
       
   247     errorOccurred = true;
       
   248 }
       
   249 
       
   250 void QGeoPositionInfoSourceMaemo::activateTimer() {
       
   251     if (positionInfoState & QGeoPositionInfoSourceMaemo::RequestActive) {
       
   252         updateTimer->start(MINIMUM_UPDATE_INTERVAL);
       
   253         return;
       
   254     }
       
   255     
       
   256     if (positionInfoState & QGeoPositionInfoSourceMaemo::PowersaveActive) {
       
   257         if (positionInfoState & QGeoPositionInfoSourceMaemo::Started) {
       
   258             // Cannot call stopUpdates() here since we want to keep powersave
       
   259             // active.
       
   260             if (LiblocationWrapper::instance()->isActive())
       
   261                 LiblocationWrapper::instance()->stop();
       
   262             updateTimer->start(timerInterval - POWERSAVE_POWERON_PERIOD);
       
   263             errorOccurred = false;
       
   264             errorSent = false;
       
   265 
       
   266             positionInfoState &= ~QGeoPositionInfoSourceMaemo::Started;
       
   267             positionInfoState |= QGeoPositionInfoSourceMaemo::Stopped;
       
   268         } else if (positionInfoState & QGeoPositionInfoSourceMaemo::Stopped) {
       
   269             startLocationDaemon();
       
   270             updateTimer->start(POWERSAVE_POWERON_PERIOD);
       
   271         }
       
   272         return;
       
   273     }
       
   274 
       
   275     if (positionInfoState & QGeoPositionInfoSourceMaemo::Started)
       
   276         updateTimer->start(timerInterval);
       
   277 }
       
   278 
       
   279 void QGeoPositionInfoSourceMaemo::startLocationDaemon() {
       
   280     if (!(LiblocationWrapper::instance()->isActive()))
       
   281         LiblocationWrapper::instance()->start();
       
   282     positionInfoState |= QGeoPositionInfoSourceMaemo::Started;
       
   283     positionInfoState &= ~QGeoPositionInfoSourceMaemo::Stopped;
       
   284 }
       
   285 
       
   286 #include "moc_qgeopositioninfosource_maemo5_p.cpp"
       
   287 QTM_END_NAMESPACE
       
   288