Added "tou" command
[nnokia.git] / locationquery.cpp
1 #include <QtDebug>
2 #include "locationquery.h"
3
4 LocationQuery::LocationQuery(QObject *parent, QMessageAddress remoteAddr) :
5     QObject(parent)
6 {
7     msgManager = new QMessageManager(this);
8     msgService = new QMessageService(this);
9     posSource = QGeoPositionInfoSource::createDefaultSource(this);
10     qDebug() << posSource;
11     filterId = 0;
12     this->remoteAddr = remoteAddr;
13
14     QObject::connect(posSource, SIGNAL(positionUpdated(const QGeoPositionInfo &)),
15                      this, SLOT(slot_positionUpdated(const QGeoPositionInfo &)));
16     QObject::connect(posSource, SIGNAL(updateTimeout()),
17                      this, SLOT(slot_updateTimeout()));
18     QObject::connect(msgManager, SIGNAL(messageUpdated(const QMessageId&, const QMessageManager::NotificationFilterIdSet&)),
19                      this, SLOT(slot_message_updated(const QMessageId&, const QMessageManager::NotificationFilterIdSet&)));
20
21     qDebug() << "requesting position";
22     posSource->requestUpdate(60000);
23 }
24
25 LocationQuery::~LocationQuery() {
26     qDebug() << "deleting LocationQuery";
27     if (filterId)
28         msgManager->unregisterNotificationFilter(filterId);
29     QObject::disconnect(this, 0, 0, 0);
30     delete msgManager;
31     delete msgService;
32 }
33
34
35 void LocationQuery::slot_positionUpdated(const QGeoPositionInfo & posInfo) {
36     qDebug() << "positioning ok:" << posInfo.coordinate();
37     posSource->stopUpdates();
38
39     qDebug() << "building sms";
40     QMessage response;
41     response.setType(QMessage::Mms);
42     response.setTo(remoteAddr);
43     response.setBody(QString("lat %1 lon %2")
44                      .arg(posInfo.coordinate().latitude())
45                      .arg(posInfo.coordinate().longitude()));
46
47     qDebug() << "sending message";
48     if (!msgService->send(response)) {
49         qDebug() << "send() failed.";
50     }
51     else {
52         qDebug() << "message" << response.id().toString() << "queued";
53         qDebug() << "response status is" << response.status();
54         filterId = msgManager->registerNotificationFilter(
55                             QMessageFilter::byId(response.id()));
56     }
57 }
58
59 void LocationQuery::slot_updateTimeout() {
60     qDebug() << "positioning failed";
61     posSource->stopUpdates();
62
63     qDebug() << "building sms";
64     QMessage response;
65     response.setType(QMessage::Mms);
66     response.setTo(remoteAddr);
67     response.setBody("Positioning failed.");
68
69     qDebug() << "sending message";
70     if (!msgService->send(response)) {
71         qDebug() << "send() failed.";
72     }
73     else {
74         qDebug() << "message" << response.id().toString() << "queued";
75         qDebug() << "response status is" << response.status();
76         filterId = msgManager->registerNotificationFilter(
77                             QMessageFilter::byId(response.id()));
78     }
79 }
80
81
82 void LocationQuery::slot_message_updated(const QMessageId &id, const QMessageManager::NotificationFilterIdSet&) {
83     qDebug() << "Message" << id.toString() << "was updated.";
84     QMessage msg = msgManager->message(id);
85     qDebug() << "msg status is" << msg.status();
86     if (msg.standardFolder() == QMessage::SentFolder) {
87         qDebug() << "Message was sent. deleting.";
88         msgManager->removeMessage(msg.id());
89         qDebug() << "Message deleted.";
90         delete this;
91     }
92 }