summaryrefslogtreecommitdiffstats
path: root/src/location/qgeoareamonitor_s60.cpp
blob: 4e6885144b5e135b524055d190f535c3a0db6d43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QDebug>
#include "qgeoareamonitor_s60_p.h"
#include "qgeopositioninfo.h"
#include "qmlbackendmonitorinfo_s60_p.h"
#include "qmlbackendmonitorcreatetriggerao_s60_p.h"

QTM_BEGIN_NAMESPACE

TInt QGeoAreaMonitorS60::refCount = 0;

QGeoAreaMonitorS60* QGeoAreaMonitorS60::NewL(QObject *aParent)
{
    RProcess thisProcess;
    if (!thisProcess.HasCapability(ECapabilityLocation)) {
        qWarning() << "QGeoAreaMonitor::createDefaultMonitor() requires the Symbian Location capability to succeed on the Symbian platform.";
        return 0;
    }

    QGeoAreaMonitorS60 *self = QGeoAreaMonitorS60::NewLC(aParent);
    CleanupStack::Pop();
    if (!self->isValid()) {
        delete self;
        self = NULL;
    }
    return self;
}

//creates an entry and exit trigger based on the aCoordinate and
//aRadius values passed as argument
void QGeoAreaMonitorS60::setMonitoredArea(const QGeoCoordinate & aCoordinate, qreal aRadius)
{
    TCoordinate coord;

    TInt ret1 = QCoordinateToTCoordinate(aCoordinate, coord);

    TInt ret2 = iTriggerCreateAO->getRadius(aRadius);

    if (ret2 == KErrNone)
        QGeoAreaMonitor::setRadius(aRadius);

    if ((ret1 != KErrNone) || (ret2 != KErrNone))
        return;

    //Initialize the trigger and enable the trigger if atleast one slot is  connected to the areaEntered
    //signal
    if ((iTriggerCreateAO->InitializeTrigger(this, EntryTrigger, coord, aRadius)) &&
            (receivers(SIGNAL(areaEntered(const QGeoPositionInfo&))) > 0)) {
        iTriggerCreateAO->SetTriggerState(this, EntryTrigger, true);
        iTriggerAO->NotifyFiredEvent();
    }

    //Initialize the trigger and enable the trigger if atleast one slot is  connected to the areaExited
    //signal
    if ((iTriggerCreateAO->InitializeTrigger(this, ExitTrigger, coord, aRadius)) &&
            (receivers(SIGNAL(areaExited(const QGeoPositionInfo&))) > 0)) {
        iTriggerCreateAO->SetTriggerState(this, ExitTrigger, true);
        iTriggerAO->NotifyFiredEvent();
    }

    //request for the trigger change notification events
    iNotifyTriggerAO->NotifyChangeEvent();
}

//virtual function sets the Center of the monitoring area to coordinate
void QGeoAreaMonitorS60::setCenter(const QGeoCoordinate& coordinate)
{
    if (coordinate.isValid())
        QGeoAreaMonitor::setCenter(coordinate);
    else
        return;

    if (QGeoAreaMonitor::radius() != 0)     //if radius is not initialised
        setMonitoredArea(coordinate, QGeoAreaMonitor::radius());
}

//virtual function sets the radius of the monitorijng area to the radius
void QGeoAreaMonitorS60::setRadius(qreal radius)
{
    setMonitoredArea(QGeoAreaMonitor::center(), radius);
}

//callback from the QMLBackendMonitorAO object for the entry/exit event
void QGeoAreaMonitorS60::handleTriggerEvent(TPositionInfo aPosInfo, enTriggerType aStatus)
{
    QGeoPositionInfo posInfo;

    TPositionInfoToQGeoPositionInfo(aPosInfo, posInfo);

    if (!posInfo.isValid())
        return;

    switch (aStatus) {
        case  EntryTrigger :           //emit areaEntered trigger
            emit areaEntered(posInfo);
            break;
        case ExitTrigger :             //emit areaExited trigger
            emit areaExited(posInfo);
            break;
        case NotifyChangesTrigger:
        case InvalidTrigger:
            break;
    }

}



//destructor cleaning up the resources
QGeoAreaMonitorS60::~QGeoAreaMonitorS60()
{

    if (iTriggerAO || iNotifyTriggerAO || iTriggerCreateAO) {
        QMLBackendMonitorAO::DeleteAO(this);
        iTriggerAO = NULL;
        QMLBackendTriggerChangeAO::DeleteAO();
        iNotifyTriggerAO = NULL;
        delete iTriggerCreateAO;
        iTriggerCreateAO = NULL;
    }
    if (connectedLbt) {
        --refCount;
        if (refCount == 0) {
            lbtServ.Close();
        }
    }
}

QGeoAreaMonitorS60* QGeoAreaMonitorS60::NewLC(QObject* aParent)
{
    QGeoAreaMonitorS60 *self = new(ELeave) QGeoAreaMonitorS60(aParent);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
}

//second level construction : creating the QMLBackendMonitorAO : for
//monitoring the trigger fired event,QMLBackendMonitorCreateTriggerAO:
//instance object for creating the trigger and QMLBackendTriggerChangeAO :
//for monitoring the changes to the trigger properties
void QGeoAreaMonitorS60::ConstructL()
{
    if (lbtServ.Connect() == KErrNone) {
        CleanupClosePushL(lbtServ);

        connectedLbt = true;
        ++refCount;

        iTriggerAO = QMLBackendMonitorAO::NewL(lbtServ);

        if (!iTriggerAO)
            return;

        iTriggerCreateAO = QMLBackendMonitorCreateTriggerAO::NewL(this, lbtServ);

        if (!iTriggerCreateAO)
            return;

        iNotifyTriggerAO = QMLBackendTriggerChangeAO::NewL(lbtServ);

        CleanupStack::Pop(1);
    } else {
        connectedLbt = false;
    }
}

//constructor initializing the default values
QGeoAreaMonitorS60::QGeoAreaMonitorS60(QObject* aParent) : QGeoAreaMonitor(aParent),
        iTriggerAO(NULL), iNotifyTriggerAO(NULL)
{

}

//convert a symbian-TPositionInfo value to corresponding QT-QGeoPositionInfo
void QGeoAreaMonitorS60::TPositionInfoToQGeoPositionInfo(TPositionInfo& aPosInfo, QGeoPositionInfo& aQInfo)
{
    QGeoCoordinate coord;
    TPosition pos;
    aPosInfo.GetPosition(pos);

    coord.setLatitude(pos.Latitude());
    coord.setLongitude(pos.Longitude());
    coord.setAltitude(pos.Altitude());

    //store the QGeoCoordinate values
    aQInfo.setCoordinate(coord);

    TDateTime datetime = pos.Time().DateTime();
    QDateTime dt(QDate(datetime.Year(), datetime.Month() + 1, datetime.Day() + 1),
                 QTime(datetime.Hour(), datetime.Minute(), datetime.Second(),
                       datetime.MicroSecond() / 1000),
                Qt::UTC);

    //store the time stamp
    aQInfo.setTimestamp(dt);

    //store the horizontal accuracy
    aQInfo.setAttribute(QGeoPositionInfo::HorizontalAccuracy, pos.HorizontalAccuracy());

    //store the vertical accuracy
    aQInfo.setAttribute(QGeoPositionInfo::VerticalAccuracy, pos.VerticalAccuracy());

}


//convert QT-QCoordinate value to Symbian-TCoordinate value
int QGeoAreaMonitorS60::QCoordinateToTCoordinate(const QGeoCoordinate& aQCoord, TCoordinate& aTCoord)
{
    if (!aQCoord.isValid())
        return KErrGeneral;

    QGeoAreaMonitor::setCenter(aQCoord);
    aTCoord.SetCoordinate(aQCoord.latitude(), aQCoord.longitude(), aQCoord.altitude());
    return KErrNone;
}

//Notification called when a sot is connected to the areaEntered or
//areaExited signal
void QGeoAreaMonitorS60::connectNotify(const char* signal)
{
    if ((iTriggerCreateAO->isTriggerInitialized(this, EntryTrigger)) && (QLatin1String(signal) == SIGNAL(areaEntered(QGeoPositionInfo))) && (receivers(SIGNAL(areaEntered(const QGeoPositionInfo&))) <= 1)) {
        iTriggerCreateAO->SetTriggerState(this, EntryTrigger, true);
        iTriggerAO-> NotifyFiredEvent();
    }

    if ((iTriggerCreateAO->isTriggerInitialized(this, ExitTrigger)) && (QLatin1String(signal) == SIGNAL(areaExited(QGeoPositionInfo))) && (receivers(SIGNAL(areaExited(const QGeoPositionInfo&))) <= 1)) {
        iTriggerCreateAO->SetTriggerState(this, ExitTrigger, true);
        iTriggerAO-> NotifyFiredEvent();
    }
}

//Notification called when a sot is disconnected from the areaEntered or
//areaExited signal
void QGeoAreaMonitorS60::disconnectNotify(const char* signal)
{
    // Disable the trigger, if no slot connected to signal
    if ((iTriggerCreateAO->isTriggerInitialized(this, EntryTrigger)) && (QLatin1String(signal) == SIGNAL(areaEntered(QGeoPositionInfo)))  && (receivers(SIGNAL(areaEntered(const QGeoPositionInfo&))) == 0)) {
        // iEnterTrigger->NotifyFiredEvent (FALSE);
        iTriggerCreateAO->SetTriggerState(this, EntryTrigger, FALSE);
    }

    if ((iTriggerCreateAO->isTriggerInitialized(this, ExitTrigger)) && (QLatin1String(signal) == SIGNAL(areaExited(QGeoPositionInfo))) && (receivers(SIGNAL(areaExited(const QGeoPositionInfo&))) == 0)) {
        // iExitTrigger->NotifyFiredEvent (FALSE);
        iTriggerCreateAO->SetTriggerState(this, ExitTrigger, FALSE);
    }
}

QTM_END_NAMESPACE