qtmobility/src/location/qgeosatelliteinfosource_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 "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     client_id_ = -1;
       
    51     timerInterval = DEFAULT_UPDATE_INTERVAL;
       
    52     updateTimer = new QTimer(this);
       
    53     updateTimer->setSingleShot(true);
       
    54     connect(updateTimer, SIGNAL(timeout()), this, SLOT(satelliteStatus()));
       
    55 
       
    56     requestTimer = new QTimer(this);
       
    57     requestTimer->setSingleShot(true);
       
    58     connect(requestTimer, SIGNAL(timeout()), this, SLOT(requestTimeoutElapsed()));
       
    59 
       
    60     satelliteInfoState = QGeoSatelliteInfoSourceMaemo::Undefined;
       
    61 }
       
    62 
       
    63 int QGeoSatelliteInfoSourceMaemo::init()
       
    64 {
       
    65     if (LiblocationWrapper::instance()->inited())
       
    66         return INIT_OK;
       
    67     else
       
    68         return INIT_FAILED;
       
    69 }
       
    70 
       
    71 void QGeoSatelliteInfoSourceMaemo::setUpdateInterval(int msec)
       
    72 {
       
    73     bool updateTimerInterval = false;
       
    74 
       
    75     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::PowersaveActive)
       
    76         if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped)
       
    77             updateTimerInterval = true;
       
    78 
       
    79     timerInterval = (msec < MINIMUM_UPDATE_INTERVAL) ? MINIMUM_UPDATE_INTERVAL : msec;
       
    80 
       
    81     if (timerInterval >= POWERSAVE_THRESHOLD)
       
    82         satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::PowersaveActive;
       
    83     else
       
    84         satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::PowersaveActive;
       
    85 
       
    86     // If powersave has been active when new update interval has been set,
       
    87     // ensure that timer is started.
       
    88     if(updateTimerInterval)
       
    89         startLocationDaemon();
       
    90     
       
    91     // Ensure that new timer interval is taken into use immediately.
       
    92     activateTimer();
       
    93 }
       
    94 
       
    95 void QGeoSatelliteInfoSourceMaemo::startUpdates()
       
    96 {
       
    97     startLocationDaemon();
       
    98 
       
    99     // Ensure that powersave is selected, if stopUpdates() has been called,
       
   100     // but selected update interval is still greater than POWERSAVE_THRESHOLD.
       
   101     if (timerInterval >= POWERSAVE_THRESHOLD)
       
   102         satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::PowersaveActive;
       
   103 
       
   104     activateTimer();
       
   105 }
       
   106 
       
   107 void QGeoSatelliteInfoSourceMaemo::stopUpdates()
       
   108 {
       
   109     satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::PowersaveActive;
       
   110 
       
   111     if (!(satelliteInfoState & QGeoSatelliteInfoSourceMaemo::RequestActive)) {
       
   112         updateTimer->stop();
       
   113         if (LiblocationWrapper::instance()->isActive())
       
   114             LiblocationWrapper::instance()->stop();
       
   115     }
       
   116 
       
   117     satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::Started;
       
   118     satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::Stopped;
       
   119 }
       
   120 
       
   121 void QGeoSatelliteInfoSourceMaemo::requestUpdate(int timeout)
       
   122 {
       
   123     int timeoutForRequest = 0;
       
   124 
       
   125     if (!timeout) {
       
   126         if (LiblocationWrapper::instance()->isActive())
       
   127             // If GPS is active, assume quick fix.
       
   128             timeoutForRequest = DEFAULT_UPDATE_INTERVAL;
       
   129         else
       
   130             // Otherwise reserve longer time to get a fix.
       
   131             timeoutForRequest = POWERSAVE_POWERON_PERIOD;
       
   132     } else if (timeout < MINIMUM_UPDATE_INTERVAL) {
       
   133         if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::RequestActive)
       
   134             return;
       
   135 
       
   136         emit requestTimeout();
       
   137         return;
       
   138     } else {
       
   139         timeoutForRequest = timeout;
       
   140     }
       
   141 
       
   142     satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::RequestActive;
       
   143 
       
   144     if (!(LiblocationWrapper::instance()->isActive()))
       
   145         LiblocationWrapper::instance()->start();
       
   146 
       
   147     activateTimer();
       
   148     requestTimer->start(timeoutForRequest);
       
   149 }
       
   150 
       
   151 void QGeoSatelliteInfoSourceMaemo::satelliteStatus()
       
   152 {
       
   153     QList<QGeoSatelliteInfo> satellitesInView = 
       
   154             LiblocationWrapper::instance()->satellitesInView();
       
   155     QList<QGeoSatelliteInfo> satellitesInUse = 
       
   156             LiblocationWrapper::instance()->satellitesInUse();
       
   157 
       
   158     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::RequestActive) {
       
   159         satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::RequestActive;
       
   160 
       
   161         requestTimer->stop();
       
   162 
       
   163         if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped) {
       
   164             if (LiblocationWrapper::instance()->isActive()) {
       
   165                 LiblocationWrapper::instance()->stop();
       
   166             }
       
   167         }
       
   168 
       
   169         // Ensure that requested satellite info is emitted even though
       
   170         // powersave is active and GPS would normally be off.
       
   171         if ((satelliteInfoState & QGeoSatelliteInfoSourceMaemo::PowersaveActive) &&
       
   172            (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped)) {
       
   173             if (satellitesInView.length()) {
       
   174                 emit satellitesInViewUpdated(satellitesInView);
       
   175                 emit satellitesInUseUpdated(satellitesInUse);
       
   176             }
       
   177         }
       
   178     }
       
   179 
       
   180     // Make sure that if update is triggered when waking up, there
       
   181     // is no false position update.
       
   182     if (!((satelliteInfoState & QGeoSatelliteInfoSourceMaemo::PowersaveActive) &&
       
   183          (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped))) {
       
   184         if (satellitesInView.length()) {
       
   185             emit satellitesInViewUpdated(satellitesInView);
       
   186             emit satellitesInUseUpdated(satellitesInUse);
       
   187         }
       
   188     }
       
   189 
       
   190     activateTimer();
       
   191 }
       
   192 
       
   193 void QGeoSatelliteInfoSourceMaemo::requestTimeoutElapsed()
       
   194 {
       
   195     updateTimer->stop();
       
   196     emit requestTimeout();
       
   197     
       
   198     satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::RequestActive;
       
   199 
       
   200     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped)
       
   201         if (LiblocationWrapper::instance()->isActive())
       
   202             LiblocationWrapper::instance()->stop();
       
   203 
       
   204     activateTimer();
       
   205 }
       
   206 
       
   207 void QGeoSatelliteInfoSourceMaemo::activateTimer() {
       
   208     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::RequestActive) {
       
   209         updateTimer->start(MINIMUM_UPDATE_INTERVAL);
       
   210         return;
       
   211     }
       
   212 
       
   213     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::PowersaveActive) {
       
   214         if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Started) {
       
   215             // Cannot call stopUpdates() here since we want to keep powersave
       
   216             // active.
       
   217             if (LiblocationWrapper::instance()->isActive())
       
   218                 LiblocationWrapper::instance()->stop();
       
   219             updateTimer->start(timerInterval - POWERSAVE_POWERON_PERIOD);
       
   220                 
       
   221             satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::Started;
       
   222             satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::Stopped;
       
   223         } else if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Stopped) {
       
   224             startLocationDaemon();
       
   225             updateTimer->start(POWERSAVE_POWERON_PERIOD);
       
   226         }
       
   227         return;
       
   228     }
       
   229     
       
   230     if (satelliteInfoState & QGeoSatelliteInfoSourceMaemo::Started)
       
   231             updateTimer->start(timerInterval);
       
   232 }
       
   233 
       
   234 void QGeoSatelliteInfoSourceMaemo::startLocationDaemon() {
       
   235     if (!(LiblocationWrapper::instance()->isActive()))
       
   236         LiblocationWrapper::instance()->start();
       
   237     satelliteInfoState |= QGeoSatelliteInfoSourceMaemo::Started;
       
   238     satelliteInfoState &= ~QGeoSatelliteInfoSourceMaemo::Stopped;
       
   239 }
       
   240 
       
   241 #include "moc_qgeosatelliteinfosource_maemo5_p.cpp"
       
   242 QTM_END_NAMESPACE
       
   243