swift
Loading...
Searching...
No Matches
textmessage.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QStringBuilder>
7#include <Qt>
8#include <QtGlobal>
9
13#include "misc/pq/constants.h"
15#include "misc/stringutils.h"
16
17using namespace swift::misc::aviation;
18using namespace swift::misc::physical_quantities;
19
20SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::network, CTextMessage)
21
22namespace swift::misc::network
23{
24 CTextMessage::CTextMessage(const QString &message, const CFrequency &frequency, const CCallsign &senderCallsign)
25 : m_senderCallsign(senderCallsign), m_frequency(frequency)
26 {
27 this->setMessage(message); // single place to modify message
28 m_frequency.switchUnit(physical_quantities::CFrequencyUnit::MHz());
29 }
30
31 CTextMessage::CTextMessage(const QString &message, const CCallsign &senderCallsign,
32 const CCallsign &recipientCallsign)
33 : m_senderCallsign(senderCallsign), m_recipientCallsign(recipientCallsign), m_frequency(0, nullptr)
34 {
35 this->setMessage(message); // single place to modify message
36 }
37
39 {
40 if (this->isPrivateMessage())
41 {
42 return m_message % u' ' % m_senderCallsign.toQString(i18n) % u' ' % m_recipientCallsign.toQString(i18n);
43 }
44 return m_message % u' ' % m_frequency.toQString(i18n);
45 }
46
48 {
49 static const QString s("swift relayed: ");
50 return s;
51 }
52
54 {
55 return !m_senderCallsign.isEmpty() && !m_recipientCallsign.isEmpty();
56 }
57
59 {
60 // ignore broadcast messages
61 if (this->isBroadcastMessage())
62 {
63 return false;
64 }
65
66 // normal SUP message
67 return m_senderCallsign.isSupervisorCallsign();
68 }
69
71 {
72 m_wasSent = true;
73 if (!this->hasValidTimestamp())
74 {
75 this->setCurrentUtcTime();
76 }
77 }
78
80 {
81 return m_relayedMessage || this->getMessage().startsWith(CTextMessage::swiftRelayMessage());
82 }
83
85
86 void CTextMessage::makeRelayedMessage(const CCallsign &partnerCallsign)
87 {
88 if (this->getMessage().startsWith(CTextMessage::swiftRelayMessage()))
89 {
90 return;
91 }
92 const QString sender = this->getSenderCallsign().asString();
93 const QString recipient = this->getRecipientCallsign().asString();
95 this->setRecipientCallsign(partnerCallsign);
96 m_recipientCallsign.setTypeHint(CCallsign::Aircraft);
97 const QString newMessage =
98 CTextMessage::swiftRelayMessage() % sender % u" " % recipient % u";" % this->getMessage();
99 m_message = newMessage;
100 }
101
103 {
104 if (!this->isRelayedMessage())
105 {
106 return false;
107 }
108 const int index = m_message.indexOf(';');
109 if (index < CTextMessage::swiftRelayMessage().length())
110 {
111 return false;
112 }
113 if (m_message.length() <= index + 1)
114 {
115 return false;
116 } // no next line
117 const QString senderRecipient = m_message.left(index).remove(CTextMessage::swiftRelayMessage()).trimmed();
118 const QStringList sr = senderRecipient.split(' ');
119 if (sr.size() != 2)
120 {
121 return false;
122 }
123 const QString originalSender = sr.first();
124 const QString originalRecipient = sr.last();
125 this->setSenderCallsign(CCallsign(originalSender)); // sender can be aircraft or ATC
126 this->setRecipientCallsign(CCallsign(originalRecipient)); // recipient can be aircraft or ATC
127 m_message = m_message.mid(index + 1);
128 return true;
129 }
130
131 bool CTextMessage::canBeAppended(const CTextMessage &textMessage) const
132 {
133 if (textMessage.isEmpty())
134 {
135 return false;
136 }
137 if (this->getSenderCallsign() != textMessage.getSenderCallsign())
138 {
139 return false;
140 }
141 if (this->isRadioMessage() && textMessage.isRadioMessage())
142 {
143 return this->getFrequency() == textMessage.getFrequency();
144 }
145 else if (this->isPrivateMessage() && textMessage.isPrivateMessage())
146 {
147 return this->getRecipientCallsign() == textMessage.getRecipientCallsign();
148 }
149 return false;
150 }
151
153 {
154 if (textMessage.isEmpty())
155 {
156 return false;
157 }
158 if (!this->canBeAppended(textMessage))
159 {
160 return false;
161 }
162 m_message += u' ' % textMessage.getMessage();
163 return true;
164 }
165
167 {
168 if (!m_recipientCallsign.isEmpty())
169 {
170 return m_recipientCallsign.asString();
171 }
172 if (m_frequency.isNull())
173 {
174 return {};
175 }
176 return m_frequency.valueRoundedWithUnit(CFrequencyUnit::MHz(), 3);
177 }
178
179 bool CTextMessage::isSendToFrequency(const CFrequency &frequency) const
180 {
181 if (!this->isRadioMessage())
182 {
183 return false;
184 }
185 return m_frequency == frequency;
186 }
187
192
194 {
195 if (!m_recipientCallsign.isEmpty())
196 {
197 return true;
198 }
200 }
201
202 bool CTextMessage::mentionsCallsign(const CCallsign &callsign) const
203 {
204 if (callsign.isEmpty())
205 {
206 return false;
207 }
208 if (m_message.length() < callsign.asString().length())
209 {
210 return false;
211 }
212 return m_message.contains(callsign.asString(), Qt::CaseInsensitive);
213 }
214
216 {
217 if (m_message.isEmpty())
218 {
219 return {};
220 }
221 return asciiOnlyString(simplifyAccents(m_message));
222 }
223
225 {
226 if (m_message.isEmpty())
227 {
228 return {};
229 }
230 return m_message.toHtmlEscaped();
231 }
232
233 void CTextMessage::setMessage(const QString &message) { m_message = message.simplified().trimmed(); }
234
236
238 {
239 if (!this->isPrivateMessage())
240 {
241 return false;
242 }
243 const CCallsign cs = this->getSenderCallsign();
244 return (cs.asString().startsWith("SERVER", Qt::CaseInsensitive));
245 }
246
248 {
249 const CCallsign cs = this->getRecipientCallsign();
250 return cs.isBroadcastCallsign();
251 }
252
254 {
255 const CCallsign cs = this->getRecipientCallsign();
256 return cs.getStringAsSet() == "*S";
257 }
258
259 QString CTextMessage::asString(bool withSender, bool withRecipient, const QString &separator) const
260 {
262 if (withSender)
263 {
264 if (!m_senderCallsign.isEmpty())
265 {
266 if (!s.isEmpty())
267 {
268 s += separator % m_senderCallsign.getStringAsSet();
269 }
270 }
271 }
272
273 if (withRecipient)
274 {
275 if (!m_recipientCallsign.isEmpty())
276 {
277 if (!s.isEmpty())
278 {
279 s += separator % m_recipientCallsign.getStringAsSet();
280 }
281 }
282 else
283 {
285 {
286 if (!s.isEmpty())
287 {
288 s += separator % m_frequency.valueRoundedWithUnit(3, true);
289 }
290 }
291 }
292 } // to
293
294 if (m_message.isEmpty())
295 {
296 return s;
297 }
298 if (!s.isEmpty())
299 {
300 s += separator % m_message;
301 }
302 return s;
303 }
304
305 CStatusMessage CTextMessage::asStatusMessage(bool withSender, bool withRecipient, const QString &separator) const
306 {
307 const QString m = this->asString(withSender, withRecipient, separator);
308 return { this, CStatusMessage::SeverityInfo, m };
309 }
310
312 {
313 return this->asString(true, true, separator);
314 }
315
316 void CTextMessage::toggleSenderRecipient() { std::swap(m_senderCallsign, m_recipientCallsign); }
317
319 {
320 // some first level checks, before really parsing the message
321 if (this->isEmpty())
322 {
323 return false;
324 }
325 if (this->isPrivateMessage())
326 {
327 return false;
328 }
329 if (m_message.length() > 15 || m_message.length() < 10)
330 {
331 return false;
332 } // SELCAL AB-CD -> 12, I allow some more characters as I do not know wheter in real life it exactly matches
333 return this->getSelcalCode().length() == 4;
334 }
335
337 {
338 if (!CSelcal::isValidCode(selcal)) return false;
339 return selcal.toUpper() == this->getSelcalCode();
340 }
341
343 {
344 // http://forums.vatsim.net/viewtopic.php?f=8&t=63467#p458062
345 // When the ATC client sends a selcal, it goes out as a text message
346 // on their primary frequency, formatted like so:
347 // SELCAL AB-CD
348
349 if (this->isEmpty()) return {};
350 if (this->isPrivateMessage()) return {};
351 if (!m_message.startsWith(QLatin1String("SELCAL"), Qt::CaseInsensitive)) return {};
352 if (m_message.length() > 15 || m_message.length() < 10)
353 return {}; // SELCAL AB-CD -> 12, I allow some more characters as I do not know wheter in real life it
354 // exactly matches
355 QString candidate = removeChars(m_message, [](QChar c) { return !c.isLetter(); }); // SELCALABCD
356 if (candidate.length() != 10) return {};
357 return std::move(candidate).right(4).toUpper();
358 }
359
360 CIcons::IconIndex CTextMessage::toIcon() const { return m_senderCallsign.toIcon(); }
361
363
365 {
366 if (index.isMyself())
367 {
368 return QVariant::fromValue(*this);
369 }
371 {
373 }
374
375 const auto i = index.frontCasted<ColumnIndex>();
376 switch (i)
377 {
378 case IndexSenderCallsign: return m_senderCallsign.propertyByIndex(index.copyFrontRemoved());
379 case IndexRecipientCallsign: return m_recipientCallsign.propertyByIndex(index.copyFrontRemoved());
380 case IndexRecipientCallsignOrFrequency: return QVariant::fromValue(this->getRecipientCallsignOrFrequency());
381 case IndexMessage: return QVariant::fromValue(m_message);
382 default: return CValueObject::propertyByIndex(index);
383 }
384 }
385
387 {
388 if (index.isMyself())
389 {
390 (*this) = variant.value<CTextMessage>();
391 return;
392 }
394 {
396 return;
397 }
398
399 const auto i = index.frontCasted<ColumnIndex>();
400 switch (i)
401 {
402 case IndexSenderCallsign: m_senderCallsign.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
403 case IndexRecipientCallsign: m_recipientCallsign.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
404 case IndexMessage: m_message = variant.value<QString>(); break;
405 default: CValueObject::setPropertyByIndex(index, variant); break;
406 }
407 }
408
410 {
412 {
413 return ITimestampBased::comparePropertyByIndex(index, compareValue);
414 }
415 const auto i = index.frontCasted<ColumnIndex>();
416 switch (i)
417 {
418 case IndexSenderCallsign:
419 return m_senderCallsign.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getSenderCallsign());
420 case IndexRecipientCallsign:
421 return m_recipientCallsign.comparePropertyByIndex(index.copyFrontRemoved(),
422 compareValue.getRecipientCallsign());
423 case IndexRecipientCallsignOrFrequency:
424 if (this->isRadioMessage())
425 {
426 return this->getFrequency().compare(compareValue.getFrequency());
427 }
428 return m_recipientCallsign.comparePropertyByIndex(index.copyFrontRemoved(),
429 compareValue.getRecipientCallsign());
430 default: return CValueObject::comparePropertyByIndex(index, *this);
431 }
432 Q_ASSERT_X(false, Q_FUNC_INFO, "No comparison");
433 return 0;
434 }
435} // namespace swift::misc::network
static CFrequencyUnit MHz()
Megahertz.
Definition units.h:380
Value object for icons. An icon is stored in the global icon repository and identified by its index....
Definition icon.h:39
QPixmap toPixmap() const
Corresponding pixmap.
Definition icon.cpp:34
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition icons.h:32
Non-owning reference to a CPropertyIndex with a subset of its features.
Q_REQUIRED_RESULT CPropertyIndexRef copyFrontRemoved() const
Copy with first element removed.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
Streamable status message, e.g.
static constexpr auto SeverityInfo
Status severities.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
int comparePropertyByIndex(CPropertyIndexRef index, const ITimestampBased &compareValue) const
Compare for index.
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled.
void setCurrentUtcTime()
Set the current time as timestamp.
QString getFormattedUtcTimestampHms() const
As hh:mm:ss.
bool hasValidTimestamp() const
Valid timestamp?
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Value object encapsulating information of a callsign.
Definition callsign.h:30
const QString & asString() const
Get callsign (normalized).
Definition callsign.h:96
const QString & getStringAsSet() const
Get callsign.
Definition callsign.h:99
bool isEmpty() const
Is empty?
Definition callsign.h:63
bool isBroadcastCallsign() const
Pseudo callsing for broadcast messages.
Definition callsign.cpp:257
static bool isValidCivilAviationFrequency(const physical_quantities::CFrequency &f)
Valid civil aviation frequency?
Definition comsystem.cpp:98
static bool isValidCode(const QString &code)
Valid SELCAL code?
Definition selcal.cpp:60
int comparePropertyByIndex(CPropertyIndexRef index, const Derived &compareValue) const
Compare for index.
Definition mixinindex.h:203
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition mixinindex.h:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
QString getHtmlEncodedMessage() const
Get HTML escaped message.
int comparePropertyByIndex(CPropertyIndexRef index, const CTextMessage &compareValue) const
Compare for index.
bool isRelayedMessage() const
Is relayed message.
bool relayedMessageToPrivateMessage()
Turn relayed message into private message.
void setMessage(const QString &message)
Set message.
bool isWallopMessage() const
Is this a message send via .wallop.
bool isPrivateMessage() const
Is private message?
bool mentionsCallsign(const aviation::CCallsign &callsign) const
Is the callsign given mentioned in that message.
void setSenderCallsign(const aviation::CCallsign &callsign)
Set callsign (from).
Definition textmessage.h:57
bool isSelcalMessageFor(const QString &selcal) const
Is this a text message encapsulating a SELCAL for given code?
const QString & getMessage() const
Get message.
Definition textmessage.h:78
CIcons::IconIndex toIcon() const
As icon, not implement by all classes.
QString getRecipientCallsignOrFrequency() const
Get recipient or frequency.
QString getSelcalCode() const
Get SELCAL code (if applicable, e.g. ABCD), otherwise "".
bool isBroadcastMessage() const
Is this a broadcast message.
QString asHtmlSummary(const QString &separator="<br>") const
Summary HTML code.
void markAsRelayedMessage()
Mark as relayed message.
bool isEmpty() const
Empty message.
Definition textmessage.h:90
bool canBeAppended(const CTextMessage &textMessage) const
Can another message be appended.
bool isSendToUnicom() const
Send to UNICOM?
QString asString(bool withSender, bool withRecipient, const QString &separator=", ") const
Whole message as formatted string. Used to display message in a console window.
void setRecipientCallsign(const aviation::CCallsign &callsign)
Set callsign (recipient).
Definition textmessage.h:63
bool isServerMessage() const
Initial message of server?
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
bool hasValidRecipient() const
Valid receviver?
QPixmap toPixmap() const
As pixmap, required for most GUI views.
const physical_quantities::CFrequency & getFrequency() const
Get frequency.
Definition textmessage.h:96
bool isSendToFrequency(const physical_quantities::CFrequency &frequency) const
Send to particular frequency?
void makeRelayedMessage(const aviation::CCallsign &partnerCallsign)
Mark as relayed and keep original sender.
CStatusMessage asStatusMessage(bool withSender, bool withRecipient, const QString &separator=", ") const
Whole message as swift::misc::CStatusMessage. Used to display message in logs, or any other situation...
ColumnIndex
Properties by index.
Definition textmessage.h:35
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
void toggleSenderRecipient()
Toggle sender receiver, can be used to ping my own message.
const aviation::CCallsign & getRecipientCallsign() const
Get callsign (to).
Definition textmessage.h:60
static const QString & swiftRelayMessage()
swift relay message marker
void markAsBroadcastMessage()
Mark as broadcast message.
bool isRadioMessage() const
Is radio message?
QString getAsciiOnlyMessage() const
Get ASCII only message.
bool isSelcalMessage() const
Is this a text message encapsulating a SELCAL.
CTextMessage()=default
Default constructor.
bool appendIfPossible(const CTextMessage &textMessage)
Append if possible.
bool isSupervisorMessage() const
Supervisor message?
QString convertToQString(bool i18n=false) const
Cast as QString.
const aviation::CCallsign & getSenderCallsign() const
Get callsign (from).
Definition textmessage.h:54
static CFrequencyUnit MHz()
Megahertz.
Definition units.h:380
static const CFrequency & FrequencyUnicom()
Unicom frequency.
Definition constants.h:29
QString removeChars(const QString &s, F predicate)
Return a string with characters removed that match the given predicate.
Definition stringutils.h:34
SWIFT_MISC_EXPORT QString simplifyAccents(const QString &candidate)
Remove accents / diacritic marks from a string.
QString asciiOnlyString(const QString &string)
String only with ASCII values.
bool isLetter(char32_t ucs4)
T & first()
T & last()
qsizetype size() const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString left(qsizetype n) &&
qsizetype length() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString right(qsizetype n) &&
QString simplified() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toHtmlEscaped() const const
QString toUpper() const const
QString trimmed() const const
CaseInsensitive
QVariant fromValue(T &&value)
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67