swift
textmessagelist.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 <algorithm>
7 
9 #include "misc/range.h"
10 
11 using namespace swift::misc::physical_quantities;
12 using namespace swift::misc::aviation;
13 
14 SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc::network, CTextMessage, CTextMessageList)
15 
16 namespace swift::misc::network
17 {
18 
19  CTextMessageList::CTextMessageList() {}
20 
21  CTextMessageList::CTextMessageList(const QString &message, const CCallsign &recipientCallsign)
22  {
23  CTextMessage pm(message, recipientCallsign);
24  this->push_back(pm);
25  }
26 
27  CTextMessageList::CTextMessageList(const QString &message, const CCallsign &fromCallsign,
28  const CCallsign &toCallsign)
29  {
30  CTextMessage pm(message, fromCallsign, toCallsign);
31  this->push_back(pm);
32  }
33 
34  CTextMessageList::CTextMessageList(const QString &message, const CFrequency &frequency,
35  const CCallsign &fromCallsign)
36  {
37  CTextMessage pm(message, frequency, fromCallsign);
38  this->push_back(pm);
39  }
40 
41  CTextMessageList::CTextMessageList(const CTextMessage &message) { this->push_back(message); }
42 
43  CTextMessageList::CTextMessageList(const QString &message, const QList<CFrequency> &frequencies,
44  const CCallsign &fromCallsign)
45  {
46  if (frequencies.isEmpty()) return;
47  for (const CFrequency &frequency : frequencies)
48  {
49  CTextMessage pm(message, frequency, fromCallsign);
50  this->push_back(pm);
51  }
52  }
53 
54  CTextMessageList::CTextMessageList(const CSequence<CTextMessage> &other) : CSequence<CTextMessage>(other) {}
55 
57  {
58  return this->findBy(&CTextMessage::isPrivateMessage, true);
59  }
60 
62  {
63  return this->contains(&CTextMessage::isPrivateMessage, true);
64  }
65 
67  {
68  return this->findBy(&CTextMessage::isSupervisorMessage, true);
69  }
70 
72  {
73  return this->findBy(&CTextMessage::isRadioMessage, true);
74  }
75 
77  {
78  return this->findBy(&CTextMessage::isSupervisorMessage, true);
79  }
80 
82 
84  {
85  return this->findBy(&CTextMessage::getFrequency, frequency);
86  }
87 
89  {
90  return this->findBy(&CTextMessage::getSenderCallsign, sender);
91  }
92 
94  {
95  return this->findBy(&CTextMessage::getRecipientCallsign, recipient);
96  }
97 
99  {
100  CTextMessageList result;
101  if (recipient.isEmpty()) { return result; }
102  for (const CTextMessage &m : *this)
103  {
104  if (m.getRecipientCallsign().isEmpty()) { continue; }
105  if (m.getRecipientCallsign() != recipient) { result.push_back(m); }
106  }
107  return result;
108  }
109 
111  {
112  if (this->isEmpty()) { return; }
113  std::for_each(this->begin(), this->end(), [](CTextMessage &tm) { tm.toggleSenderRecipient(); });
114  }
115 
117  {
118  if (this->isEmpty()) { return 0; }
119  int c = 0;
120  for (CTextMessage &m : *this)
121  {
122  // Turn "relay" message into private message
123  if (m.relayedMessageToPrivateMessage()) { c++; }
124  }
125  return c;
126  }
127 
129  {
130  if (this->isEmpty()) { return 0; }
132  const int c = this->size() - r.size();
133  if (c < 1) { return 0; }
134  *this = r;
135  return c;
136  }
137 
139  {
140  if (this->isEmpty()) { return {}; }
141  CTextMessageList copy = *this;
143  return copy;
144  }
145 
147  {
148  if (this->isEmpty()) { return {}; }
149  if (callsign.isEmpty()) { return *this; }
151  for (const CTextMessage &m : *this)
152  {
153  if (m.isPrivateMessage() && m.getSenderCallsign() == callsign) { continue; }
154  r.push_back(m);
155  }
156  return r;
157  }
158 
160  {
161  std::for_each(this->begin(), this->end(), [](CTextMessage &tm) { tm.markAsSent(); });
162  }
163 
165  {
166  if (this->isEmpty()) { return {}; }
167  CTextMessageList copy = *this;
168  copy.markAsSent();
169  return copy;
170  }
171 
173  {
174  if (message.isEmpty()) { return; }
175  for (CTextMessage &tm : *this)
176  {
177  if (tm.appendIfPossible(message)) { return; }
178  }
179  this->push_back(message);
180  }
181 } // namespace swift::misc::network
bool contains(const T &object) const
Return true if there is an element equal to given object. Uses the most efficient implementation avai...
Definition: range.h:109
size_type size() const
Returns number of elements in the sequence.
Definition: sequence.h:273
iterator begin()
Returns iterator at the beginning of the sequence.
Definition: sequence.h:163
CSequence findBy(Predicate p) const
Return a copy containing only those elements for which a given predicate returns true.
Definition: sequence.h:398
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
iterator end()
Returns iterator one past the end of the sequence.
Definition: sequence.h:172
Value object encapsulating information of a callsign.
Definition: callsign.h:30
bool isEmpty() const
Is empty?
Definition: callsign.h:63
Value object encapsulating information of a text message.
Definition: textmessage.h:31
bool relayedMessageToPrivateMessage()
Turn relayed message into private message.
Definition: textmessage.cpp:93
bool isPrivateMessage() const
Is private message?
Definition: textmessage.cpp:53
bool isEmpty() const
Empty message.
Definition: textmessage.h:90
const aviation::CCallsign & getSenderCallsign() const
Get callsign (from)
Definition: textmessage.h:54
const aviation::CCallsign & getRecipientCallsign() const
Get callsign (to)
Definition: textmessage.h:60
void toggleSenderRecipient()
Toggle sender receiver, can be used to ping my own message.
bool isRadioMessage() const
Is radio message?
const physical_quantities::CFrequency & getFrequency() const
Get frequency.
Definition: textmessage.h:96
bool appendIfPossible(const CTextMessage &textMessage)
Append if possible.
bool isSupervisorMessage() const
Supervisor message?
Definition: textmessage.cpp:58
Value object encapsulating a list of text messages.
int removePrivateMessagesFromCallsign(const aviation::CCallsign &callsign)
Remove private messages from callsign.
CTextMessageList withRemovedPrivateMessagesFromCallsign(const aviation::CCallsign &callsign) const
With removed private messages from callsign.
CTextMessageList containsSupervisorMessages() const
Contains supervisor message.
void addConsolidatedTextMessage(const CTextMessage &message)
Add a text message, but append it to an existing message if possible.
int relayedToPrivateMessages()
Turn relayed into normal private messages.
void markAsSent()
Mark all messages as sent.
bool containsPrivateMessages() const
Contains private messages?
CTextMessageList findByNotForRecipient(const swift::misc::aviation::CCallsign &recipient) const
Find by recipient is NOT addressed.
CTextMessageList findBySender(const swift::misc::aviation::CCallsign &sender) const
Find by sender.
CTextMessageList findByFrequency(const physical_quantities::CFrequency &frequency) const
Find by frequency.
bool containsRadioMessages() const
Contains radio messages?
CTextMessageList getPrivateMessages() const
Private messages.
CTextMessageList getRadioMessages() const
Public messages.
CTextMessageList getSupervisorMessages() const
Supervisor messages.
void toggleSenderRecipients()
Toggle all sender <-> recipients.
CTextMessageList withRelayedToPrivateMessages() const
List with relayed messages (if any) as private messages.
CTextMessageList findByRecipient(const swift::misc::aviation::CCallsign &recipient) const
Find by recipient.
CTextMessageList markedAsSent()
Marked as sent.
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63