Wavelength
Privacy-focused, cross-platform, and open-source communication application
Loading...
Searching...
No Matches
message_service.h
Go to the documentation of this file.
1#ifndef WAVELENGTH_MESSAGE_SERVICE_H
2#define WAVELENGTH_MESSAGE_SERVICE_H
3
4#include <QMap>
5#include <QObject>
6
7class QWebSocket;
8
18class MessageService final : public QObject {
19 Q_OBJECT
20
21public:
27 static MessageService instance;
28 return &instance;
29 }
30
34 MessageService(const MessageService &) = delete;
35
40
47 static bool SendPttRequest(const QString &frequency);
48
55 static bool SendPttRelease(const QString &frequency);
56
64 static bool SendAudioData(const QString &frequency, const QByteArray &audio_data);
65
74 bool SendTextMessage(const QString &message);
75
87 bool SendFile(const QString &file_path, const QString &progress_message_id = QString());
88
100 bool SendFileToServer(const QString &json_message, const QString &frequency, const QString &progress_message_id);
101
102
108 QMap<QString, QString> *GetSentMessageCache() {
109 return &sent_messages_;
110 }
111
116 sent_messages_.clear();
117 }
118
123 [[nodiscard]] QString GetClientId() const {
124 return client_id_;
125 }
126
131 void SetClientId(const QString &clientId) {
132 client_id_ = clientId;
133 }
134
135public slots:
142 void UpdateProgressMessage(const QString &progress_message_id, const QString &message) {
143 if (progress_message_id.isEmpty()) return;
144 emit progressMessageUpdated(progress_message_id, message);
145 }
146
155 void HandleSendJsonViaSocket(const QString &json_message, const QString &frequency,
156 const QString &progress_message_id);
157
158signals:
164 void messageSent(QString frequency, const QString &formatted_message);
165
171 void progressMessageUpdated(const QString &message_id, const QString &message);
172
177 void removeProgressMessage(const QString &message_id);
178
186 void sendJsonViaSocket(const QString &json_message, QString frequency, const QString &progress_message_id);
187
193 void pttGranted(QString frequency);
194
201 void pttDenied(QString frequency, QString reason);
202
209 void pttStartReceiving(QString frequency, QString sender_id);
210
216 void pttStopReceiving(QString frequency);
217
224 void audioDataReceived(QString frequency, const QByteArray &audio_data);
225
232 void remoteAudioAmplitudeUpdate(QString frequency, qreal amplitude);
233
234private:
240 explicit MessageService(QObject *parent = nullptr);
241
245 ~MessageService() override = default;
246
253 static QWebSocket *GetSocketForFrequency(const QString &frequency);
254
256 QMap<QString, QString> sent_messages_;
258 QString client_id_;
259};
260
261#endif // WAVELENGTH_MESSAGE_SERVICE_H
MessageService(const MessageService &)=delete
Deleted copy constructor to prevent copying.
void removeProgressMessage(const QString &message_id)
Emitted after a file transfer is complete (success or failure) to remove the progress message.
void audioDataReceived(QString frequency, const QByteArray &audio_data)
Emitted when raw audio data is received via a binary WebSocket message. Relayed from WavelengthMessag...
void remoteAudioAmplitudeUpdate(QString frequency, qreal amplitude)
Emitted when the server sends an update about the remote audio amplitude (optional)....
static MessageService * GetInstance()
Gets the singleton instance of the MessageService.
Definition message_service.h:26
void HandleSendJsonViaSocket(const QString &json_message, const QString &frequency, const QString &progress_message_id)
Slot connected to the sendJsonViaSocket signal. Performs the actual sending of a JSON message....
Definition message_service.cpp:274
void progressMessageUpdated(const QString &message_id, const QString &message)
Emitted during file processing and sending to update the status display.
~MessageService() override=default
Private destructor.
void ClearSentMessageCache()
Clears the internal cache of sent message contents.
Definition message_service.h:115
QString client_id_
Stores the client ID associated with this service instance.
Definition message_service.h:258
static bool SendPttRequest(const QString &frequency)
Sends a Push-to-Talk (PTT) request message for the specified frequency. Constructs a JSON message of ...
Definition message_service.cpp:14
void UpdateProgressMessage(const QString &progress_message_id, const QString &message)
Slot to update a progress message associated with a file transfer. Emits the progressMessageUpdated s...
Definition message_service.h:142
QMap< QString, QString > * GetSentMessageCache()
Gets a pointer to the internal cache of sent message contents. The cache maps message ID to message c...
Definition message_service.h:108
static bool SendPttRelease(const QString &frequency)
Sends a Push-to-Talk (PTT) release message for the specified frequency. Constructs a JSON message of ...
Definition message_service.cpp:28
void pttDenied(QString frequency, QString reason)
Emitted when the server denies permission to transmit audio (Push-to-Talk). Relayed from WavelengthMe...
void pttStopReceiving(QString frequency)
Emitted when the currently transmitting user stops sending audio. Relayed from WavelengthMessageProce...
static bool SendAudioData(const QString &frequency, const QByteArray &audio_data)
Sends raw audio data as a binary message for the specified frequency. Used for transmitting audio dur...
Definition message_service.cpp:42
void messageSent(QString frequency, const QString &formatted_message)
Emitted immediately after a text message is successfully sent via the socket.
void sendJsonViaSocket(const QString &json_message, QString frequency, const QString &progress_message_id)
Internal signal emitted by the background file processing task when the JSON message is ready to be s...
void pttGranted(QString frequency)
Emitted when the server grants permission to transmit audio (Push-to-Talk). Relayed from WavelengthMe...
QMap< QString, QString > sent_messages_
Cache storing the content of recently sent text messages, mapped by message ID.
Definition message_service.h:256
bool SendFileToServer(const QString &json_message, const QString &frequency, const QString &progress_message_id)
Sends a pre-formatted JSON message (typically containing file data) to the server....
Definition message_service.cpp:240
void pttStartReceiving(QString frequency, QString sender_id)
Emitted when another user starts transmitting audio on the frequency. Relayed from WavelengthMessageP...
bool SendTextMessage(const QString &message)
Sends a text message to the currently active frequency. Retrieves the active frequency and socket fro...
Definition message_service.cpp:50
static QWebSocket * GetSocketForFrequency(const QString &frequency)
Helper function to retrieve the active WebSocket connection for a given frequency....
Definition message_service.cpp:309
QString GetClientId() const
Gets the client ID currently associated with this service instance.
Definition message_service.h:123
void SetClientId(const QString &clientId)
Sets the client ID for this service instance.
Definition message_service.h:131
MessageService & operator=(const MessageService &)=delete
Deleted assignment operator to prevent assignment.
bool SendFile(const QString &file_path, const QString &progress_message_id=QString())
Initiates the process of sending a file to the currently active frequency. Reads the file,...
Definition message_service.cpp:102