Wavelength
Privacy-focused, cross-platform, and open-source communication application
Loading...
Searching...
No Matches
wavelength_registry.h
Go to the documentation of this file.
1#ifndef WAVELENGTH_REGISTRY_H
2#define WAVELENGTH_REGISTRY_H
3
4#include <QPointer>
5#include <QWebSocket>
6
15 QString frequency;
19 QString password;
21 QString host_id;
23 bool is_host = false;
25 int host_port = 0;
27 QPointer<QWebSocket> socket = nullptr;
29 bool is_closing = false;
31 QDateTime created_at = QDateTime::currentDateTime();
32};
33
43class WavelengthRegistry final : public QObject {
44 Q_OBJECT
45
46public:
52 static WavelengthRegistry instance;
53 return &instance;
54 }
55
63 bool AddWavelength(const QString &frequency, const WavelengthInfo &info);
64
72 bool RemoveWavelength(const QString &frequency);
73
79 bool HasWavelength(const QString &frequency) const {
80 return wavelengths_.contains(frequency);
81 }
82
88 WavelengthInfo GetWavelengthInfo(const QString &frequency) const;
89
94 QList<QString> GetAllWavelengths() const {
95 return wavelengths_.keys();
96 }
97
105 bool UpdateWavelength(const QString &frequency, const WavelengthInfo &info);
106
113 void SetActiveWavelength(const QString &frequency);
114
119 QString GetActiveWavelength() const {
120 return active_wavelength_;
121 }
122
128 bool IsWavelengthActive(const QString &frequency) const {
129 return active_wavelength_ == frequency;
130 }
131
138 bool AddPendingRegistration(const QString &frequency);
139
145 bool RemovePendingRegistration(const QString &frequency);
146
152 bool IsPendingRegistration(const QString &frequency) const {
153 return pending_registrations_.contains(frequency);
154 }
155
161 QPointer<QWebSocket> GetWavelengthSocket(const QString &frequency) const;
162
168 void SetWavelengthSocket(const QString &frequency, const QPointer<QWebSocket> &socket);
169
176 bool MarkWavelengthClosing(const QString &frequency, bool closing = true);
177
183 bool IsWavelengthClosing(const QString &frequency) const;
184
189 void ClearAllWavelengths();
190
191signals:
196 void wavelengthAdded(QString frequency);
197
202 void wavelengthRemoved(QString frequency);
203
208 void wavelengthUpdated(QString frequency);
209
215 void activeWavelengthChanged(QString previous_frequency, QString new_frequency);
216
217private:
222 explicit WavelengthRegistry(QObject *parent = nullptr) : QObject(parent), active_wavelength_("-1") {
223 }
224
228 ~WavelengthRegistry() override = default;
229
232
235
237 QMap<QString, WavelengthInfo> wavelengths_;
242};
243
244#endif // WAVELENGTH_REGISTRY_H
WavelengthInfo GetWavelengthInfo(const QString &frequency) const
Retrieves the WavelengthInfo for a specific frequency.
Definition wavelength_registry.cpp:37
void wavelengthAdded(QString frequency)
Emitted when a new wavelength is successfully added to the registry.
bool RemoveWavelength(const QString &frequency)
Removes a wavelength from the registry. If the removed wavelength was the active one,...
Definition wavelength_registry.cpp:21
void wavelengthRemoved(QString frequency)
Emitted when a wavelength is successfully removed from the registry.
QString active_wavelength_
The identifier of the currently active wavelength, or "-1" if none is active.
Definition wavelength_registry.h:241
~WavelengthRegistry() override=default
Private destructor.
bool UpdateWavelength(const QString &frequency, const WavelengthInfo &info)
Updates the information for an existing wavelength. Emits wavelengthUpdated signal.
Definition wavelength_registry.cpp:44
void SetActiveWavelength(const QString &frequency)
Sets the specified frequency as the currently active wavelength. If the frequency does not exist in t...
Definition wavelength_registry.cpp:57
void wavelengthUpdated(QString frequency)
Emitted when the information for an existing wavelength is updated.
bool RemovePendingRegistration(const QString &frequency)
Removes a frequency from the pending registration list.
Definition wavelength_registry.cpp:80
WavelengthRegistry(QObject *parent=nullptr)
Private constructor to enforce the singleton pattern. Initializes active_wavelength_ to "-1".
Definition wavelength_registry.h:222
WavelengthRegistry & operator=(const WavelengthRegistry &)=delete
Deleted assignment operator to prevent assignment.
QSet< QString > pending_registrations_
Set storing frequencies for which a registration attempt is currently in progress.
Definition wavelength_registry.h:239
QString GetActiveWavelength() const
Gets the identifier of the currently active wavelength.
Definition wavelength_registry.h:119
QList< QString > GetAllWavelengths() const
Gets a list of all frequency identifiers currently in the registry.
Definition wavelength_registry.h:94
bool IsWavelengthActive(const QString &frequency) const
Checks if the specified frequency is the currently active one.
Definition wavelength_registry.h:128
static WavelengthRegistry * GetInstance()
Gets the singleton instance of the WavelengthRegistry.
Definition wavelength_registry.h:51
QMap< QString, WavelengthInfo > wavelengths_
Map storing WavelengthInfo for each registered frequency. Key is the frequency string.
Definition wavelength_registry.h:237
bool IsWavelengthClosing(const QString &frequency) const
Checks if a wavelength is currently marked as closing.
Definition wavelength_registry.cpp:110
bool HasWavelength(const QString &frequency) const
Checks if a wavelength with the given frequency exists in the registry.
Definition wavelength_registry.h:79
void SetWavelengthSocket(const QString &frequency, const QPointer< QWebSocket > &socket)
Sets or updates the WebSocket connection associated with a specific frequency.
Definition wavelength_registry.cpp:95
void ClearAllWavelengths()
Removes all wavelengths and pending registrations from the registry. Sets the active wavelength to "-...
Definition wavelength_registry.cpp:117
bool AddPendingRegistration(const QString &frequency)
Marks a frequency as having a pending registration attempt. Used to prevent duplicate registration at...
Definition wavelength_registry.cpp:69
void activeWavelengthChanged(QString previous_frequency, QString new_frequency)
Emitted when the active wavelength changes via SetActiveWavelength().
WavelengthRegistry(const WavelengthRegistry &)=delete
Deleted copy constructor to prevent copying.
bool IsPendingRegistration(const QString &frequency) const
Checks if a frequency is currently marked as having a pending registration.
Definition wavelength_registry.h:152
bool MarkWavelengthClosing(const QString &frequency, bool closing=true)
Marks or unmarks a wavelength as being in the process of closing.
Definition wavelength_registry.cpp:101
QPointer< QWebSocket > GetWavelengthSocket(const QString &frequency) const
Retrieves the WebSocket connection associated with a specific frequency.
Definition wavelength_registry.cpp:88
bool AddWavelength(const QString &frequency, const WavelengthInfo &info)
Adds a new wavelength and its information to the registry. Removes any pending registration for the s...
Definition wavelength_registry.cpp:5
Structure holding information about a specific wavelength (frequency).
Definition wavelength_registry.h:13
bool is_host
True if the current user is the host of this wavelength.
Definition wavelength_registry.h:23
QString frequency
The unique identifier (name) of the wavelength.
Definition wavelength_registry.h:15
int host_port
The port number the host is listening on.
Definition wavelength_registry.h:25
QString host_id
The unique client ID of the user hosting the wavelength.
Definition wavelength_registry.h:21
bool is_password_protected
True if the wavelength requires a password to join.
Definition wavelength_registry.h:17
QPointer< QWebSocket > socket
QPointer to the WebSocket connection associated with this wavelength. Automatically nullifies if the ...
Definition wavelength_registry.h:27
QString password
The password for the wavelength (if protected).
Definition wavelength_registry.h:19
bool is_closing
Flag indicating if the wavelength is currently in the process of being closed.
Definition wavelength_registry.h:29
QDateTime created_at
Timestamp when this wavelength information was created or added to the registry locally.
Definition wavelength_registry.h:31