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(const QString &message, const CCallsign &recipientCallsign)
20  {
21  CTextMessage pm(message, recipientCallsign);
22  this->push_back(pm);
23  }
24 
25  CTextMessageList::CTextMessageList(const QString &message, const CCallsign &fromCallsign,
26  const CCallsign &toCallsign)
27  {
28  CTextMessage pm(message, fromCallsign, toCallsign);
29  this->push_back(pm);
30  }
31 
32  CTextMessageList::CTextMessageList(const QString &message, const CFrequency &frequency,
33  const CCallsign &fromCallsign)
34  {
35  CTextMessage pm(message, frequency, fromCallsign);
36  this->push_back(pm);
37  }
38 
39  CTextMessageList::CTextMessageList(const CTextMessage &message) { this->push_back(message); }
40 
41  CTextMessageList::CTextMessageList(const QString &message, const QList<CFrequency> &frequencies,
42  const CCallsign &fromCallsign)
43  {
44  if (frequencies.isEmpty()) return;
45  for (const CFrequency &frequency : frequencies)
46  {
47  CTextMessage pm(message, frequency, fromCallsign);
48  this->push_back(pm);
49  }
50  }
51 
52  CTextMessageList::CTextMessageList(const CSequence<CTextMessage> &other) : CSequence<CTextMessage>(other) {}
53 
55  {
56  return this->findBy(&CTextMessage::isPrivateMessage, true);
57  }
58 
60  {
61  return this->contains(&CTextMessage::isPrivateMessage, true);
62  }
63 
65  {
66  return this->findBy(&CTextMessage::isSupervisorMessage, true);
67  }
68 
70  {
71  return this->findBy(&CTextMessage::isRadioMessage, true);
72  }
73 
75  {
76  return this->findBy(&CTextMessage::isSupervisorMessage, true);
77  }
78 
80 
82  {
83  return this->findBy(&CTextMessage::getFrequency, frequency);
84  }
85 
87  {
88  return this->findBy(&CTextMessage::getSenderCallsign, sender);
89  }
90 
92  {
93  return this->findBy(&CTextMessage::getRecipientCallsign, recipient);
94  }
95 
97  {
98  CTextMessageList result;
99  if (recipient.isEmpty()) { return result; }
100  for (const CTextMessage &m : *this)
101  {
102  if (m.getRecipientCallsign().isEmpty()) { continue; }
103  if (m.getRecipientCallsign() != recipient) { result.push_back(m); }
104  }
105  return result;
106  }
107 
109  {
110  if (this->isEmpty()) { return; }
111  std::for_each(this->begin(), this->end(), [](CTextMessage &tm) { tm.toggleSenderRecipient(); });
112  }
113 
115  {
116  if (this->isEmpty()) { return 0; }
117  int c = 0;
118  for (CTextMessage &m : *this)
119  {
120  // Turn "relay" message into private message
121  if (m.relayedMessageToPrivateMessage()) { c++; }
122  }
123  return c;
124  }
125 
127  {
128  if (this->isEmpty()) { return 0; }
130  const int c = this->size() - r.size();
131  if (c < 1) { return 0; }
132  *this = r;
133  return c;
134  }
135 
137  {
138  if (this->isEmpty()) { return {}; }
139  CTextMessageList copy = *this;
141  return copy;
142  }
143 
145  {
146  if (this->isEmpty()) { return {}; }
147  if (callsign.isEmpty()) { return *this; }
149  for (const CTextMessage &m : *this)
150  {
151  if (m.isPrivateMessage() && m.getSenderCallsign() == callsign) { continue; }
152  r.push_back(m);
153  }
154  return r;
155  }
156 
158  {
159  std::for_each(this->begin(), this->end(), [](CTextMessage &tm) { tm.markAsSent(); });
160  }
161 
163  {
164  if (this->isEmpty()) { return {}; }
165  CTextMessageList copy = *this;
166  copy.markAsSent();
167  return copy;
168  }
169 
171  {
172  if (message.isEmpty()) { return; }
173  for (CTextMessage &tm : *this)
174  {
175  if (tm.appendIfPossible(message)) { return; }
176  }
177  this->push_back(message);
178  }
179 } // 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.
bool isEmpty() const const
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63