From e60acb25c369b591958941b30cffafbed7998dac Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 30 Jan 2014 14:08:30 +0100 Subject: [PATCH] Detect textmessage being a SELCAL call --- src/blackmisc/nwtextmessage.cpp | 44 +++++++++++++++++++++++++++++++++ src/blackmisc/nwtextmessage.h | 32 ++++++++++++++++++------ 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/blackmisc/nwtextmessage.cpp b/src/blackmisc/nwtextmessage.cpp index a6786ed02..3936ced1a 100644 --- a/src/blackmisc/nwtextmessage.cpp +++ b/src/blackmisc/nwtextmessage.cpp @@ -3,6 +3,7 @@ #include "blackmisc/pqconstants.h" #include "blackmisc/aviocomsystem.h" #include "blackmisc/avcallsign.h" +#include "blackmisc/avselcal.h" using namespace BlackMisc::Aviation; @@ -179,6 +180,49 @@ namespace BlackMisc qSwap(this->m_senderCallsign, this->m_recipientCallsign); } + /* + * Find out if this is a SELCAL message + */ + bool CTextMessage::isSelcalMessage() const + { + // some first level checks, before really parsing the message + if (this->isEmpty()) return false; + if (this->isPrivateMessage()) return false; + if (this->m_message.length() > 15 || this->m_message.length() < 10) return false; // SELCAL AB-CD -> 12, I allow some more characters as I do not know wheter in real life it exactly matches + return this->getSelcalCode().length() == 4; + } + + /* + * Matching given SELCAL code + */ + bool CTextMessage::isSelcalMessageFor(const QString &selcal) const + { + if (!CSelcal::isValidCode(selcal)) return false; + return selcal.toUpper() == this->getSelcalCode(); + } + + /* + * SELCAL code, 4 letters + */ + QString CTextMessage::getSelcalCode() const + { + // http://forums.vatsim.net/viewtopic.php?f=8&t=63467#p458062 + // When the ATC client sends a selcal, it goes out as a text message + // on their primary frequency, formatted like so: + // SELCAL AB-CD + + const QString invalid; + + // some first level checks, before really parsing the message + if (this->isEmpty()) return invalid; + if (this->isPrivateMessage()) return invalid; + if (this->m_message.length() > 15 || this->m_message.length() < 10) return invalid; // SELCAL AB-CD -> 12, I allow some more characters as I do not know wheter in real life it exactly matches + QString candidate = this->m_message.toUpper().remove(QRegExp("[^A-Z]")); // SELCALABCD + if (candidate.length() != 10) return invalid; + if (!candidate.startsWith("SELCAL")) return invalid; + return candidate.right(4); + } + /* * Equal? */ diff --git a/src/blackmisc/nwtextmessage.h b/src/blackmisc/nwtextmessage.h index dc95fb145..f50656162 100644 --- a/src/blackmisc/nwtextmessage.h +++ b/src/blackmisc/nwtextmessage.h @@ -45,14 +45,6 @@ namespace BlackMisc CTextMessage(const QString &message, const BlackMisc::Aviation::CCallsign &senderCallsign, const BlackMisc::Aviation::CCallsign &recipientCallsign = BlackMisc::Aviation::CCallsign()) : m_message(message), m_received(QDateTime::currentDateTimeUtc()), m_senderCallsign(senderCallsign), m_recipientCallsign(recipientCallsign), m_frequency(-1.0, BlackMisc::PhysicalQuantities::CFrequencyUnit::MHz()) {} - /*! - * \brief QVariant, required for DBus QVariant lists - */ - virtual QVariant toQVariant() const - { - return QVariant::fromValue(*this); - } - /*! * Get callsign (from) */ @@ -179,6 +171,22 @@ namespace BlackMisc */ void toggleSenderRecipient(); + /*! + * \brief Is this a text message encapsulating a SELCAL + * \see http://forums.vatsim.net/viewtopic.php?f=8&t=63467 + */ + bool isSelcalMessage() const; + + /*! + * \brief Is this a text message encapsulating a SELCAL for given code + */ + bool isSelcalMessageFor(const QString &selcal) const; + + /*! + * \brief Get SELCAL code (if applicable, e.g. ABCD), otherwise "" + */ + QString getSelcalCode() const; + /*! * \brief Equal operator == */ @@ -194,6 +202,14 @@ namespace BlackMisc */ virtual uint getValueHash() const; + /*! + * \copydoc CValueObject::toQVariant() + */ + virtual QVariant toQVariant() const + { + return QVariant::fromValue(*this); + } + /*! * \brief Register metadata */