swift
Loading...
Searching...
No Matches
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
11using namespace swift::misc::physical_quantities;
12using namespace swift::misc::aviation;
13
14SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc::network, CTextMessage, CTextMessageList)
15
16namespace 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
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
53
58
63
68
73
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())
100 {
101 return result;
102 }
103 for (const CTextMessage &m : *this)
104 {
106 {
107 continue;
108 }
109 if (m.getRecipientCallsign() != recipient)
110 {
111 result.push_back(m);
112 }
113 }
114 return result;
115 }
116
118 {
119 if (this->isEmpty())
120 {
121 return;
122 }
123 std::ranges::for_each(*this, [](CTextMessage &tm) { tm.toggleSenderRecipient(); });
124 }
125
127 {
128 if (this->isEmpty())
129 {
130 return 0;
131 }
132 int c = 0;
133 for (CTextMessage &m : *this)
134 {
135 // Turn "relay" message into private message
137 {
138 c++;
139 }
140 }
141 return c;
142 }
143
145 {
146 if (this->isEmpty())
147 {
148 return 0;
149 }
151 const int c = this->size() - r.size();
152 if (c < 1)
153 {
154 return 0;
155 }
156 *this = r;
157 return c;
158 }
159
161 {
162 if (this->isEmpty())
163 {
164 return {};
165 }
166 CTextMessageList copy = *this;
168 return copy;
169 }
170
172 {
173 if (this->isEmpty())
174 {
175 return {};
176 }
177 if (callsign.isEmpty())
178 {
179 return *this;
180 }
182 for (const CTextMessage &m : *this)
183 {
184 if (m.isPrivateMessage() && m.getSenderCallsign() == callsign)
185 {
186 continue;
187 }
188 r.push_back(m);
189 }
190 return r;
191 }
192
194 {
195 std::ranges::for_each(*this, [](CTextMessage &tm) { tm.markAsSent(); });
196 }
197
199 {
200 if (this->isEmpty())
201 {
202 return {};
203 }
204 CTextMessageList copy = *this;
205 copy.markAsSent();
206 return copy;
207 }
208
210 {
211 if (message.isEmpty())
212 {
213 return;
214 }
215 for (CTextMessage &tm : *this)
216 {
217 if (tm.appendIfPossible(message))
218 {
219 return;
220 }
221 }
222 this->push_back(message);
223 }
224} // namespace swift::misc::network
CSequence findBy(Predicate p) const
Definition sequence.h:407
void push_back(const CTextMessage &value)
Definition sequence.h:308
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.
bool isPrivateMessage() const
Is private message?
bool isEmpty() const
Empty message.
Definition textmessage.h:90
const physical_quantities::CFrequency & getFrequency() const
Get frequency.
Definition textmessage.h:96
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
bool isRadioMessage() const
Is radio message?
bool appendIfPossible(const CTextMessage &textMessage)
Append if possible.
bool isSupervisorMessage() const
Supervisor message?
const aviation::CCallsign & getSenderCallsign() const
Get callsign (from).
Definition textmessage.h:54
int removePrivateMessagesFromCallsign(const aviation::CCallsign &callsign)
Remove private messages from callsign.
CTextMessageList withRemovedPrivateMessagesFromCallsign(const aviation::CCallsign &callsign) const
With removed private messages from callsign.
CSequence()=default
Default constructor.
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.
CTextMessageList()=default
Default constructor.
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