Message.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #include "Message.h"
  5. #include <QDebug>
  6. #include <QTimerEvent>
  7. #include <QTimer>
  8. Message::Message(QObject *parent) :
  9. QObject(parent)
  10. {
  11. // QMessageService class provides the interface for requesting messaging service operations
  12. m_service = new QMessageService(this);
  13. // QMessageManager class represents the main interface for storage and
  14. // retrieval of messages, folders and accounts in the system message store
  15. m_manager = new QMessageManager(this);
  16. QObject::connect(m_manager,
  17. SIGNAL(messageAdded(const QMessageId&,const QMessageManager::NotificationFilterIdSet&)),
  18. this, SLOT(messageAdded(const QMessageId&,const QMessageManager::NotificationFilterIdSet&)));
  19. // Register SMS in inbox folder notificationfilter
  20. m_notifFilterSet.insert(m_manager->registerNotificationFilter(QMessageFilter::byStandardFolder(
  21. QMessage::InboxFolder)));
  22. }
  23. Message::~Message()
  24. {
  25. }
  26. void Message::messageAdded(const QMessageId& id,
  27. const QMessageManager::NotificationFilterIdSet& matchingFilterIds)
  28. {
  29. if (matchingFilterIds.contains(m_notifFilterSet))
  30. processIncomingSMS(id);
  31. }
  32. void Message::processIncomingSMS(const QMessageId& id)
  33. {
  34. QMessage message = m_manager->message(id);
  35. // Handle only SMS messages
  36. if (message.type() != QMessage::Sms)
  37. return;
  38. QString txt = message.textContent();
  39. if (txt.length() > 10) {
  40. if (txt.mid(0, 4).contains("REQ:", Qt::CaseSensitive)) {
  41. // Your friend send location request to you
  42. QMessageAddress from = message.from();
  43. if (from.type() == QMessageAddress::Phone) {
  44. emit friendAskLocationSMS(from.addressee());
  45. }
  46. // Remove message from inbox
  47. m_manager->removeMessage(id);
  48. }
  49. }
  50. }
  51. bool Message::sendLocationSMS(QString typeStr, QGeoPositionInfo& position, QString phoneNumber)
  52. {
  53. // Send SMS
  54. if (!position.isValid()) {
  55. return false;
  56. }
  57. QGeoCoordinate coordinate = position.coordinate();
  58. QMessage smsMessage;
  59. smsMessage.setType(QMessage::Sms);
  60. smsMessage.setParentAccountId(QMessageAccount::defaultAccount(QMessage::Sms));
  61. smsMessage.setTo(QMessageAddress(QMessageAddress::Phone, phoneNumber));
  62. QString bodyText;
  63. bodyText += typeStr;
  64. bodyText += QString().setNum(coordinate.latitude(),'g',8);
  65. bodyText += " ";
  66. bodyText += QString().setNum(coordinate.longitude(),'g',8);
  67. smsMessage.setBody(bodyText);
  68. // Send SMS
  69. return m_service->send(smsMessage);
  70. }