Files

212 lines
7.6 KiB
C++
Raw Permalink Normal View History

2021-08-09 00:41:52 +07:00
#ifndef SERVERSMODEL_H
#define SERVERSMODEL_H
#include <QAbstractListModel>
#include "core/controllers/serverController.h"
#include "settings.h"
2021-08-09 00:41:52 +07:00
class ServersModel : public QAbstractListModel
{
Q_OBJECT
public:
2025-10-06 08:06:36 +04:00
struct GatewayStacks
{
QSet<QString> userCountryCodes;
QSet<QString> serviceTypes;
bool isEmpty() const { return userCountryCodes.isEmpty() && serviceTypes.isEmpty(); }
bool operator==(const GatewayStacks &other) const;
QJsonObject toJson() const;
};
enum Roles {
NameRole = Qt::UserRole + 1,
ServerDescriptionRole,
2024-02-19 19:54:15 +05:00
CollapsedServerDescriptionRole,
ExpandedServerDescriptionRole,
HostNameRole,
CredentialsRole,
CredentialsLoginRole,
IsDefaultRole,
IsCurrentlyProcessedRole,
HasWriteAccessRole,
ContainsAmneziaDnsRole,
2024-02-19 19:54:15 +05:00
DefaultContainerRole,
HasInstalledContainers,
IsServerFromTelegramApiRole,
IsServerFromGatewayApiRole,
ApiConfigRole,
IsCountrySelectionAvailableRole,
ApiAvailableCountriesRole,
ApiServerCountryCodeRole,
2025-11-03 10:26:22 +08:00
IsAdVisibleRole,
AdHeaderRole,
AdDescriptionRole,
AdEndpointRole,
IsRenewalAvailableRole,
2024-02-19 19:54:15 +05:00
2026-03-25 14:48:32 +03:00
IsSubscriptionExpiredRole,
IsSubscriptionExpiringSoonRole,
2024-02-19 19:54:15 +05:00
HasAmneziaDns
2021-08-09 00:41:52 +07:00
};
ServersModel(std::shared_ptr<Settings> settings, QObject *parent = nullptr);
2021-08-09 00:41:52 +07:00
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
bool setData(const int index, const QVariant &value, int role = Qt::EditRole);
2021-08-09 00:41:52 +07:00
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QVariant data(const int index, int role = Qt::DisplayRole) const;
2021-08-09 00:41:52 +07:00
void resetModel();
2025-10-06 08:06:36 +04:00
GatewayStacks gatewayStacks() const { return m_gatewayStacks; }
Q_PROPERTY(int defaultIndex READ getDefaultServerIndex WRITE setDefaultServerIndex NOTIFY defaultServerIndexChanged)
Q_PROPERTY(QString defaultServerName READ getDefaultServerName NOTIFY defaultServerNameChanged)
2024-02-19 19:54:15 +05:00
Q_PROPERTY(QString defaultServerDefaultContainerName READ getDefaultServerDefaultContainerName NOTIFY defaultServerDefaultContainerChanged)
Q_PROPERTY(QString defaultServerDescriptionCollapsed READ getDefaultServerDescriptionCollapsed NOTIFY defaultServerDefaultContainerChanged)
Q_PROPERTY(QString defaultServerImagePathCollapsed READ getDefaultServerImagePathCollapsed NOTIFY defaultServerDefaultContainerChanged)
2024-02-19 19:54:15 +05:00
Q_PROPERTY(QString defaultServerDescriptionExpanded READ getDefaultServerDescriptionExpanded NOTIFY defaultServerDefaultContainerChanged)
Q_PROPERTY(bool isDefaultServerDefaultContainerHasSplitTunneling READ isDefaultServerDefaultContainerHasSplitTunneling NOTIFY
defaultServerDefaultContainerChanged)
Q_PROPERTY(bool isDefaultServerFromApi READ isDefaultServerFromApi NOTIFY defaultServerIndexChanged)
2025-10-06 08:06:36 +04:00
Q_PROPERTY(bool hasServersFromGatewayApi READ hasServersFromGatewayApi NOTIFY hasServersFromGatewayApiChanged)
2024-02-19 19:54:15 +05:00
Q_PROPERTY(int processedIndex READ getProcessedServerIndex WRITE setProcessedServerIndex NOTIFY processedServerIndexChanged)
Q_PROPERTY(bool processedServerIsPremium READ processedServerIsPremium NOTIFY processedServerChanged)
2025-11-03 10:26:22 +08:00
Q_PROPERTY(bool isAdVisible READ isAdVisible NOTIFY defaultServerIndexChanged)
Q_PROPERTY(QString adHeader READ adHeader NOTIFY defaultServerIndexChanged)
Q_PROPERTY(QString adDescription READ adDescription NOTIFY defaultServerIndexChanged)
bool processedServerIsPremium() const;
public slots:
void setDefaultServerIndex(const int index);
2023-05-15 13:38:17 +08:00
const int getDefaultServerIndex();
const QString getDefaultServerName();
const QString getDefaultServerDescriptionCollapsed();
const QString getDefaultServerImagePathCollapsed();
const QString getDefaultServerDescriptionExpanded();
2024-02-19 19:54:15 +05:00
const QString getDefaultServerDefaultContainerName();
2023-06-01 11:25:33 +08:00
bool isDefaultServerCurrentlyProcessed();
bool isDefaultServerFromApi();
2024-02-19 19:54:15 +05:00
bool isProcessedServerHasWriteAccess();
bool isDefaultServerHasWriteAccess();
bool hasServerWithWriteAccess();
2025-10-06 08:06:36 +04:00
bool hasServersFromGatewayApi();
2023-05-15 13:38:17 +08:00
const int getServersCount();
2024-02-19 19:54:15 +05:00
void setProcessedServerIndex(const int index);
int getProcessedServerIndex();
2024-02-19 19:54:15 +05:00
const ServerCredentials getProcessedServerCredentials();
const ServerCredentials getServerCredentials(const int index);
2023-06-01 11:25:33 +08:00
void addServer(const QJsonObject &server);
2024-02-19 19:54:15 +05:00
void editServer(const QJsonObject &server, const int serverIndex);
2023-06-01 11:25:33 +08:00
void removeServer();
2025-05-13 12:29:33 +08:00
void removeServer(const int serverIndex);
QJsonObject getServerConfig(const int serverIndex) const;
2024-02-19 19:54:15 +05:00
void reloadDefaultServerContainerConfig();
void updateContainerConfig(const int containerIndex, const QJsonObject config);
void addContainerConfig(const int containerIndex, const QJsonObject config);
void clearCachedProfile(const DockerContainer container);
ErrorCode removeContainer(const QSharedPointer<ServerController> &serverController, const int containerIndex);
ErrorCode removeAllContainers(const QSharedPointer<ServerController> &serverController);
ErrorCode rebootServer(const QSharedPointer<ServerController> &serverController);
2024-02-13 20:20:13 +05:00
void setDefaultContainer(const int serverIndex, const int containerIndex);
QStringList getAllInstalledServicesName(const int serverIndex);
void toggleAmneziaDns(bool enabled);
QPair<QString, QString> getDnsPair(const int serverIndex);
2024-02-09 23:23:26 +05:00
bool isServerFromApiAlreadyExists(const quint16 crc);
bool isServerFromApiAlreadyExists(const QString &userCountryCode, const QString &serviceType, const QString &serviceProtocol);
2026-04-08 11:21:12 +07:00
int indexOfServerWithVpnKey(const QString &vpnKey) const;
2024-02-19 19:54:15 +05:00
QVariant getDefaultServerData(const QString roleString);
QVariant getProcessedServerData(const QString roleString);
bool setProcessedServerData(const QString &roleString, const QVariant &value);
2024-02-19 19:54:15 +05:00
2024-02-21 18:27:27 +07:00
bool isDefaultServerDefaultContainerHasSplitTunneling();
bool isServerFromApi(const int serverIndex);
bool isApiKeyExpired(const int serverIndex);
void removeApiConfig(const int serverIndex);
2025-11-03 10:26:22 +08:00
bool isAdVisible();
QString adHeader();
QString adDescription();
2021-08-09 00:41:52 +07:00
protected:
QHash<int, QByteArray> roleNames() const override;
signals:
2024-02-19 19:54:15 +05:00
void processedServerIndexChanged(const int index);
// emitted when the processed server index or processed server data is changed
void processedServerChanged();
void defaultServerIndexChanged(const int index);
void defaultServerNameChanged();
void defaultServerDescriptionChanged();
2024-02-09 23:23:26 +05:00
void containersUpdated(const QJsonArray &containers);
2024-02-19 19:54:15 +05:00
void defaultServerContainersUpdated(const QJsonArray &containers);
void defaultServerDefaultContainerChanged(const int containerIndex);
void updateApiCountryModel();
void updateApiServicesModel();
2025-10-06 08:06:36 +04:00
void hasServersFromGatewayApiChanged();
void gatewayStacksExpanded();
2021-08-09 00:41:52 +07:00
private:
2023-06-30 18:14:47 +03:00
ServerCredentials serverCredentials(int index) const;
void updateContainersModel();
2024-02-19 19:54:15 +05:00
void updateDefaultServerContainersModel();
QString getServerDescription(const QJsonObject &server, const int index) const;
2024-02-19 19:54:15 +05:00
bool isAmneziaDnsContainerInstalled(const int serverIndex) const;
2023-06-30 18:14:47 +03:00
bool serverHasInstalledContainers(const int serverIndex) const;
QJsonArray m_servers;
std::shared_ptr<Settings> m_settings;
int m_defaultServerIndex;
2024-02-19 19:54:15 +05:00
int m_processedServerIndex;
bool m_isAmneziaDnsEnabled = m_settings->useAmneziaDns();
2025-10-06 08:06:36 +04:00
GatewayStacks m_gatewayStacks;
void recomputeGatewayStacks();
2021-08-09 00:41:52 +07:00
};
#endif // SERVERSMODEL_H